| 1 | package Yote::YapiServer::Handler; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use JSON::PP; | ||
| 7 | use Time::HiRes qw(time); | ||
| 8 | use Data::Dumper; | ||
| 9 | use Yote::YapiServer::Site; | ||
| 10 | use Yote::YapiServer::Session; | ||
| 11 | use Yote::YapiServer::File; | ||
| 12 | |||
| 13 | # Config vars — set by YapiServer during startup | ||
| 14 | our $max_file_size = 5_000_000; # 5MB default | ||
| 15 | our $webroot_dir = 'www/webroot'; | ||
| 16 | our $debug_mode = 1; | ||
| 17 | |||
| 18 | # Rate limit tracking (in-memory, resets on server restart) | ||
| 19 | our %rate_limits; # { ip => { endpoint => { count => N, window_start => T } } } | ||
| 20 | |||
| 21 | #---------------------------------------------------------------------- | ||
| 22 | # Main request handler | ||
| 23 | #---------------------------------------------------------------------- | ||
| 24 | |||
| 25 | sub handle { | ||
| 26 | my ($class, %args) = @_; | ||
| 27 | |||
| 28 | my $store = $args{store}; | ||
| 29 | my $json_body = $args{body}; | ||
| 30 | my $ip_address = $args{ip_address} // '127.0.0.1'; | ||
| 31 | |||
| 32 | # Parse JSON request | ||
| 33 | my $request; | ||
| 34 | eval { | ||
| 35 | $request = decode_json($json_body); | ||
| 36 | }; | ||
| 37 | if ($@) { | ||
| 38 | return $class->error_response("invalid JSON: $@"); | ||
| 39 | } | ||
| 40 | |||
| 41 | # Extract request fields | ||
| 42 | my $action = $request->{action}; # connect, call, login, createUser, logout | ||
| 43 | my $token = $request->{token}; | ||
| 44 | my $app = $request->{app}; | ||
| 45 | my $target = $request->{target}; # app name or _obj_ID | ||
| 46 | my $method = $request->{method}; | ||
| 47 | my $args_ = $request->{args} // {}; | ||
| 48 | |||
| 49 | # Build context | ||
| 50 | my $context = { | ||
| 51 | ip_address => $ip_address, | ||
| 52 | request => $request, | ||
| 53 | }; | ||
| 54 | |||
| 55 | # Wrap entire request handling so uncaught errors return JSON, not HTTP 500 | ||
| 56 | my ($succ, $res, $extra); | ||
| 57 | my ($root, $session, $user); | ||
| 58 | eval { | ||
| 59 | # Validate token if provided | ||
| 60 | $root = $store->fetch_root // $store->new_obj('Yote::YapiServer::Site'); | ||
| 61 | $root->init; | ||
| 62 | if ($token) { | ||
| 63 | $session = $root->validateToken($token); | ||
| 64 | if ($session) { | ||
| 65 | $user = $session->get_user; | ||
| 66 | $context->{session} = $session; | ||
| 67 | $context->{user} = $user; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | # Create anonymous session if none exists so exposed objects can be tracked | ||
| 72 | if (!$session) { | ||
| 73 | my $new_token = Yote::YapiServer::Session->generate_token(); | ||
| 74 | my $expires = Yote::YapiServer::Session->calculate_expiry(); | ||
| 75 | $session = $store->new_obj('Yote::YapiServer::Session', | ||
| 76 | token => $new_token, | ||
| 77 | expires => $expires, | ||
| 78 | ip_address => $ip_address, | ||
| 79 | ); | ||
| 80 | $session->touch; | ||
| 81 | $store->set_path('sessions', $new_token, $session); | ||
| 82 | $context->{session} = $session; | ||
| 83 | } | ||
| 84 | |||
| 85 | # Handle actions | ||
| 86 | if ($action eq 'connect') { | ||
| 87 | ($succ, $res, $extra) = $class->handle_connect($root, $app, $session, $user); | ||
| 88 | } | ||
| 89 | elsif ($action eq 'login') { | ||
| 90 | ($succ, $res, $extra) = $class->check_rate_limit('login', $ip_address, undef) | ||
| 91 | // $root->login($args_, $context); | ||
| 92 | } | ||
| 93 | elsif ($action eq 'createUser') { | ||
| 94 | ($succ, $res, $extra) = $class->check_rate_limit('createUser', $ip_address, undef) | ||
| 95 | // $root->createUser($args_, $context); | ||
| 96 | } | ||
| 97 | elsif ($action eq 'logout') { | ||
| 98 | ($succ, $res, $extra) = $root->logout($args_, $context); | ||
| 99 | } | ||
| 100 | elsif ($action eq 'call') { | ||
| 101 | ($succ, $res, $extra) = $class->check_rate_limit('default', $ip_address, $session) | ||
| 102 | // $class->handle_call($root, $target, $method, $args_, $session, $user, $store); | ||
| 103 | } | ||
| 104 | else { | ||
| 105 | ($succ, $res) = (0, "unknown action: $action"); | ||
| 106 | } | ||
| 107 | |||
| 108 | $store->save; | ||
| 109 | }; | ||
| 110 | |||
| 111 | if ($@) { | ||
| 112 | chomp(my $error = $@); | ||
| 113 | $class->log_error($context, $error); | ||
| 114 | my $msg = $debug_mode ? $error : "server error"; | ||
| 115 | return $class->error_response($msg); | ||
| 116 | } | ||
| 117 | |||
| 118 | if (!$succ) { | ||
| 119 | return $class->error_response($res); | ||
| 120 | } | ||
| 121 | |||
| 122 | # For login/createUser/logout, serialize the result through serialize_value | ||
| 123 | # (handle_connect and handle_call already do their own serialization) | ||
| 124 | if ($action =~ /^(login|createUser|logout)$/ && defined $res) { | ||
| 125 | my $ctx = { objects => {}, classes => {} }; | ||
| 126 | $res = $class->serialize_value($res, $ctx, $session, $user); | ||
| 127 | $extra //= {}; | ||
| 128 | $extra->{objects} = $ctx->{objects} if %{$ctx->{objects}}; | ||
| 129 | $extra->{classes} = $ctx->{classes} if %{$ctx->{classes}}; | ||
| 130 | } | ||
| 131 | |||
| 132 | # Use session from extra token if login/createUser created a new one | ||
| 133 | if ($extra && $extra->{token}) { | ||
| 134 | my $new_session = $root->validateToken($extra->{token}); | ||
| 135 | $session = $new_session if $new_session; | ||
| 136 | } | ||
| 137 | |||
| 138 | return $class->success_response($res, $extra, $session); | ||
| 139 | } | ||
| 140 | |||
| 141 | #---------------------------------------------------------------------- | ||
| 142 | # Action handlers | ||
| 143 | #---------------------------------------------------------------------- | ||
| 144 | |||
| 145 | sub handle_connect { | ||
| 146 | my ($class, $root, $app_name, $session, $user) = @_; | ||
| 147 | |||
| 148 | return 0, "app name required" unless $app_name; | ||
| 149 | |||
| 150 | my $app = $root->get_app($app_name); | ||
| 151 | return 0, "unknown app: $app_name" unless $app; | ||
| 152 | |||
| 153 | # Expose app to session | ||
| 154 | $session->expose_object($app); | ||
| 155 | |||
| 156 | my $app_id = "_app_$app_name"; | ||
| 157 | my $methods = $app->connect_info_methods($session, $user); | ||
| 158 | my $public_vars = $app->public_vars; | ||
| 159 | |||
| 160 | my $ctx = { objects => {}, classes => {} }; | ||
| 161 | |||
| 162 | # Build app object data from publicVars (serialize values for v/r prefixing) | ||
| 163 | my %serialized_vars; | ||
| 164 | for my $key (keys %$public_vars) { | ||
| 165 | $serialized_vars{$key} = $class->serialize_value($public_vars->{$key}, $ctx, $session, $user); | ||
| 166 | } | ||
| 167 | $ctx->{objects}{$app_id} = { | ||
| 168 | _class => $app->_client_class_name, | ||
| 169 | data => \%serialized_vars, | ||
| 170 | }; | ||
| 171 | |||
| 172 | # Add app methods to apps section | ||
| 173 | my %apps = ( $app_id => $methods ); | ||
| 174 | |||
| 175 | # Serialize user into objects/classes if present | ||
| 176 | if ($user) { | ||
| 177 | $class->serialize_value($user, $ctx, $session, $user); | ||
| 178 | } | ||
| 179 | |||
| 180 | return 1, "r$app_id", { objects => $ctx->{objects}, classes => $ctx->{classes}, apps => \%apps }; | ||
| 181 | } | ||
| 182 | |||
| 183 | sub handle_call { | ||
| 184 | my ($class, $root, $target, $method, $args, $session, $user, $store) = @_; | ||
| 185 | |||
| 186 | return 0, "target required" unless $target; | ||
| 187 | return 0, "method required" unless $method; | ||
| 188 | |||
| 189 | my $target_obj; | ||
| 190 | |||
| 191 | # Determine target object | ||
| 192 | if ($target =~ /^_obj_(\d+)$/) { | ||
| 193 | # Object reference | ||
| 194 | my $obj_id = $1; | ||
| 195 | |||
| 196 | # Security: verify object is in session's exposed set | ||
| 197 | unless ($session && $session->can_access($obj_id)) { | ||
| 198 | return 0, "access denied"; | ||
| 199 | } | ||
| 200 | |||
| 201 | $target_obj = $store->fetch($obj_id); | ||
| 202 | return 0, "object not found" unless $target_obj; | ||
| 203 | } | ||
| 204 | elsif ($target =~ /^_app_(.+)$/) { | ||
| 205 | # App reference via _app_ ID | ||
| 206 | my $app_name = $1; | ||
| 207 | $target_obj = $root->get_app($app_name); | ||
| 208 | return 0, "unknown app: $app_name" unless $target_obj; | ||
| 209 | } | ||
| 210 | else { | ||
| 211 | # App name (legacy) | ||
| 212 | $target_obj = $root->get_app($target); | ||
| 213 | return 0, "unknown app: $target" unless $target_obj; | ||
| 214 | } | ||
| 215 | |||
| 216 | # Look up method definition for files flag | ||
| 217 | my $method_def = {}; | ||
| 218 | if ($target_obj->can('method_defs')) { | ||
| 219 | my $methods = $target_obj->method_defs; | ||
| 220 | $method_def = $methods->{$method} // {}; | ||
| 221 | } | ||
| 222 | |||
| 223 | # Validate arguments with v/r/f prefix dispatch | ||
| 224 | my $opts = { | ||
| 225 | session => $session, | ||
| 226 | store => $store, | ||
| 227 | allow_files => $method_def->{files}, | ||
| 228 | max_file_size => $max_file_size, | ||
| 229 | webroot_dir => $webroot_dir, | ||
| 230 | user => $user, | ||
| 231 | }; | ||
| 232 | my ($args_ok, $validated_args) = $class->validate_args($args, $opts); | ||
| 233 | return 0, $validated_args unless $args_ok; | ||
| 234 | |||
| 235 | # Authorize method call | ||
| 236 | my ($auth_ok, $auth_error); | ||
| 237 | if ($target =~ /^_obj_/) { | ||
| 238 | # Object method - use object-level authorization | ||
| 239 | ($auth_ok, $auth_error) = Yote::YapiServer::App::Base->authorize_object_method( | ||
| 240 | $target_obj, $method, $session, $user | ||
| 241 | ); | ||
| 242 | } else { | ||
| 243 | # App method | ||
| 244 | ($auth_ok, $auth_error) = $target_obj->authorize_method($method, $session, $user); | ||
| 245 | } | ||
| 246 | |||
| 247 | return 0, $auth_error unless $auth_ok; | ||
| 248 | |||
| 249 | # Call the method — methods return (1, $result) or (0, $error) | ||
| 250 | unless ($target_obj->can($method)) { | ||
| 251 | return 0, "method not implemented: $method"; | ||
| 252 | } | ||
| 253 | |||
| 254 | my ($ok, $result) = $target_obj->$method($validated_args, $session); | ||
| 255 | return 0, $result unless $ok; | ||
| 256 | |||
| 257 | # Serialize result with context accumulation | ||
| 258 | my $ctx = { objects => {}, classes => {} }; | ||
| 259 | my $resp = $class->serialize_value($result, $ctx, $session, $user); | ||
| 260 | |||
| 261 | my %extra; | ||
| 262 | $extra{objects} = $ctx->{objects} if %{$ctx->{objects}}; | ||
| 263 | $extra{classes} = $ctx->{classes} if %{$ctx->{classes}}; | ||
| 264 | |||
| 265 | return 1, $resp, \%extra; | ||
| 266 | } | ||
| 267 | |||
| 268 | #---------------------------------------------------------------------- | ||
| 269 | # Argument validation | ||
| 270 | #---------------------------------------------------------------------- | ||
| 271 | |||
| 272 | sub validate_args { | ||
| 273 | my ($class, $args, $opts) = @_; | ||
| 274 | |||
| 275 | # Array — recurse into each element | ||
| 276 | if (ref $args eq 'ARRAY') { | ||
| 277 | my @validated; | ||
| 278 | for my $item (@$args) { | ||
| 279 | my ($ok, $result) = $class->validate_args($item, $opts); | ||
| 280 | return (0, $result) unless $ok; | ||
| 281 | push @validated, $result; | ||
| 282 | } | ||
| 283 | return (1, \@validated); | ||
| 284 | } | ||
| 285 | |||
| 286 | # Hash — recurse into values | ||
| 287 | if (ref $args eq 'HASH') { | ||
| 288 | my %validated; | ||
| 289 | for my $key (keys %$args) { | ||
| 290 | my ($ok, $result) = $class->validate_args($args->{$key}, $opts); | ||
| 291 | return (0, $result) unless $ok; | ||
| 292 | $validated{$key} = $result; | ||
| 293 | } | ||
| 294 | return (1, \%validated); | ||
| 295 | } | ||
| 296 | |||
| 297 | # Leaf value — dispatch on v/r/f prefix | ||
| 298 | return (1, undef) unless defined $args; | ||
| 299 | |||
| 300 | # v prefix — scalar value | ||
| 301 | if ($args =~ /^v(.*)$/s) { | ||
| 302 | return (1, $1); | ||
| 303 | } | ||
| 304 | |||
| 305 | # r prefix — object reference | ||
| 306 | if ($args =~ /^r_obj_(\d+)$/) { | ||
| 307 | my $obj_id = $1; | ||
| 308 | my $session = $opts->{session}; | ||
| 309 | my $store = $opts->{store}; | ||
| 310 | |||
| 311 | unless ($session && $session->can_access($obj_id)) { | ||
| 312 | return (0, "access denied to object: _obj_$obj_id"); | ||
| 313 | } | ||
| 314 | |||
| 315 | my $obj = $store->fetch($obj_id); | ||
| 316 | return (0, "object not found: _obj_$obj_id") unless $obj; | ||
| 317 | |||
| 318 | return (1, $obj); | ||
| 319 | } | ||
| 320 | |||
| 321 | # f prefix — file upload | ||
| 322 | if ($args =~ /^f/) { | ||
| 323 | unless ($opts->{allow_files}) { | ||
| 324 | return (0, "file uploads not allowed for this method"); | ||
| 325 | } | ||
| 326 | return $class->process_file_arg($args, $opts); | ||
| 327 | } | ||
| 328 | |||
| 329 | # No valid prefix | ||
| 330 | return (0, "invalid argument encoding: missing v/r/f prefix"); | ||
| 331 | } | ||
| 332 | |||
| 333 | #---------------------------------------------------------------------- | ||
| 334 | # File upload processing | ||
| 335 | #---------------------------------------------------------------------- | ||
| 336 | |||
| 337 | sub process_file_arg { | ||
| 338 | my ($class, $value, $opts) = @_; | ||
| 339 | |||
| 340 | # Parse: f<original_name>|data:<mime_type>;base64,<encoded_data> | ||
| 341 | unless ($value =~ /^f([^|]*)\|data:([^;]+);base64,(.+)$/s) { | ||
| 342 | return (0, "invalid file format"); | ||
| 343 | } | ||
| 344 | |||
| 345 | my $original_name = $1; | ||
| 346 | my $mime_type = $2; | ||
| 347 | my $encoded_data = $3; | ||
| 348 | |||
| 349 | # Validate MIME type | ||
| 350 | my $ext = _mime_to_ext($mime_type); | ||
| 351 | return (0, "unsupported file type: $mime_type") unless $ext; | ||
| 352 | |||
| 353 | # Decode base64 | ||
| 354 | require MIME::Base64; | ||
| 355 | my $data = MIME::Base64::decode_base64($encoded_data); | ||
| 356 | |||
| 357 | # Check file size | ||
| 358 | my $max_size = $opts->{max_file_size} // 5_000_000; | ||
| 359 | if (length($data) > $max_size) { | ||
| 360 | return (0, "file too large (max " . int($max_size / 1_000_000) . "MB)"); | ||
| 361 | } | ||
| 362 | |||
| 363 | # Hash content for filename | ||
| 364 | require Digest::SHA; | ||
| 365 | my $hash = Digest::SHA::sha256_hex($data); | ||
| 366 | my $prefix = substr($hash, 0, 2); | ||
| 367 | my $filename = "$hash.$ext"; | ||
| 368 | |||
| 369 | # Save to webroot/img/<prefix>/ | ||
| 370 | my $webroot = $opts->{webroot_dir} // 'www/webroot'; | ||
| 371 | my $dir = "$webroot/img/$prefix"; | ||
| 372 | |||
| 373 | require File::Path; | ||
| 374 | File::Path::make_path($dir) unless -d $dir; | ||
| 375 | |||
| 376 | my $file_path = "$dir/$filename"; | ||
| 377 | |||
| 378 | # Content-addressed dedup — only write if not already present | ||
| 379 | unless (-f $file_path) { | ||
| 380 | open my $fh, '>', $file_path or return (0, "failed to save file"); | ||
| 381 | binmode $fh; | ||
| 382 | print $fh $data; | ||
| 383 | close $fh; | ||
| 384 | } | ||
| 385 | |||
| 386 | # Create File object in store | ||
| 387 | my $store = $opts->{store}; | ||
| 388 | my $url = "/img/$prefix/$filename"; | ||
| 389 | |||
| 390 | my $file_obj = $store->new_obj('Yote::YapiServer::File', | ||
| 391 | url => $url, | ||
| 392 | type => $mime_type, | ||
| 393 | size => length($data), | ||
| 394 | original_name => $original_name, | ||
| 395 | file_path => $file_path, | ||
| 396 | ); | ||
| 397 | $file_obj->set_owner($opts->{user}) if $opts->{user}; | ||
| 398 | |||
| 399 | return (1, $file_obj); | ||
| 400 | } | ||
| 401 | |||
| 402 | sub _mime_to_ext { | ||
| 403 | my ($mime) = @_; | ||
| 404 | my %map = ( | ||
| 405 | 'image/jpeg' => 'jpg', | ||
| 406 | 'image/png' => 'png', | ||
| 407 | 'image/gif' => 'gif', | ||
| 408 | 'image/webp' => 'webp', | ||
| 409 | 'image/svg+xml' => 'svg', | ||
| 410 | 'application/pdf' => 'pdf', | ||
| 411 | 'text/plain' => 'txt', | ||
| 412 | 'text/csv' => 'csv', | ||
| 413 | 'application/json' => 'json', | ||
| 414 | ); | ||
| 415 | return $map{$mime}; | ||
| 416 | } | ||
| 417 | |||
| 418 | #---------------------------------------------------------------------- | ||
| 419 | # Serialization | ||
| 420 | #---------------------------------------------------------------------- | ||
| 421 | |||
| 422 | sub serialize_value { | ||
| 423 | my ($class, $value, $ctx, $session, $viewer) = @_; | ||
| 424 | |||
| 425 | return undef unless defined $value; | ||
| 426 | |||
| 427 | # Array | ||
| 428 | if (ref($value) eq 'ARRAY') { | ||
| 429 | return [ map { $class->serialize_value($_, $ctx, $session, $viewer) } @$value ]; | ||
| 430 | } | ||
| 431 | |||
| 432 | # Plain hash (unblessed) | ||
| 433 | if (ref($value) eq 'HASH') { | ||
| 434 | my %result; | ||
| 435 | for my $key (keys %$value) { | ||
| 436 | $result{$key} = $class->serialize_value($value->{$key}, $ctx, $session, $viewer); | ||
| 437 | } | ||
| 438 | return \%result; | ||
| 439 | } | ||
| 440 | |||
| 441 | # Object with to_client_hash (blessed ref) | ||
| 442 | if (ref($value) && $value->can('to_client_hash')) { | ||
| 443 | my $obj_id = "_obj_" . $value->id; | ||
| 444 | |||
| 445 | # Dedup: skip objects already in ctx (or being serialized) | ||
| 446 | unless ($ctx->{objects}{$obj_id}) { | ||
| 447 | # Reserve slot to break circular references (e.g. User->File->User) | ||
| 448 | $ctx->{objects}{$obj_id} = 1; | ||
| 449 | |||
| 450 | # Get the class name | ||
| 451 | my $client_class; | ||
| 452 | if ($value->can('_client_class_name')) { | ||
| 453 | $client_class = $value->_client_class_name; | ||
| 454 | } else { | ||
| 455 | $client_class = ref($value); | ||
| 456 | $client_class =~ s/^Yote::YapiServer::App:://; | ||
| 457 | $client_class =~ s/^Yote::YapiServer:://; | ||
| 458 | } | ||
| 459 | |||
| 460 | # Get data fields from to_client_hash | ||
| 461 | my $data = $value->to_client_hash($session, $viewer); | ||
| 462 | |||
| 463 | # Recursively serialize values within data | ||
| 464 | my %serialized_data; | ||
| 465 | for my $key (keys %$data) { | ||
| 466 | $serialized_data{$key} = $class->serialize_value($data->{$key}, $ctx, $session, $viewer); | ||
| 467 | } | ||
| 468 | |||
| 469 | $ctx->{objects}{$obj_id} = { | ||
| 470 | _class => $client_class, | ||
| 471 | data => \%serialized_data, | ||
| 472 | }; | ||
| 473 | |||
| 474 | # Add methods to classes (once per class) | ||
| 475 | if (!$ctx->{classes}{$client_class} && $value->can('method_defs')) { | ||
| 476 | my $methods = $value->method_defs; | ||
| 477 | $ctx->{classes}{$client_class} = [ sort keys %$methods ]; | ||
| 478 | } | ||
| 479 | |||
| 480 | # Expose object to session | ||
| 481 | $session->expose_object($value) if $session; | ||
| 482 | } | ||
| 483 | |||
| 484 | return "r$obj_id"; | ||
| 485 | } | ||
| 486 | |||
| 487 | # Scalar — prefix with 'v' to distinguish from references | ||
| 488 | return "v$value"; | ||
| 489 | } | ||
| 490 | |||
| 491 | #---------------------------------------------------------------------- | ||
| 492 | # Rate limiting | ||
| 493 | #---------------------------------------------------------------------- | ||
| 494 | |||
| 495 | sub check_rate_limit { | ||
| 496 | my ($class, $endpoint, $ip, $session) = @_; | ||
| 497 | |||
| 498 | my $config = $Yote::YapiServer::Site::RATE_LIMITS{$endpoint} | ||
| 499 | // $Yote::YapiServer::Site::RATE_LIMITS{default}; | ||
| 500 | |||
| 501 | return undef unless $config; | ||
| 502 | |||
| 503 | my $now = time(); | ||
| 504 | my $key; | ||
| 505 | my $limit; | ||
| 506 | |||
| 507 | if ($config->{per_ip}) { | ||
| 508 | $key = "ip:$ip:$endpoint"; | ||
| 509 | $limit = $config->{per_ip}; | ||
| 510 | } | ||
| 511 | elsif ($config->{per_session} && $session) { | ||
| 512 | $key = "session:" . $session->get_token . ":$endpoint"; | ||
| 513 | $limit = $config->{per_session}; | ||
| 514 | } | ||
| 515 | else { | ||
| 516 | return undef; # No applicable limit | ||
| 517 | } | ||
| 518 | |||
| 519 | my $window = $config->{window} // 60; | ||
| 520 | |||
| 521 | # Initialize or reset window | ||
| 522 | $rate_limits{$key} //= { count => 0, window_start => $now }; | ||
| 523 | |||
| 524 | if ($now - $rate_limits{$key}{window_start} > $window) { | ||
| 525 | $rate_limits{$key} = { count => 0, window_start => $now }; | ||
| 526 | } | ||
| 527 | |||
| 528 | # Check limit | ||
| 529 | if ($rate_limits{$key}{count} >= $limit) { | ||
| 530 | return 0, "rate limit exceeded, try again later"; | ||
| 531 | } | ||
| 532 | |||
| 533 | # Increment | ||
| 534 | $rate_limits{$key}{count}++; | ||
| 535 | |||
| 536 | return undef; # OK to proceed | ||
| 537 | } | ||
| 538 | |||
| 539 | #---------------------------------------------------------------------- | ||
| 540 | # Response formatting | ||
| 541 | #---------------------------------------------------------------------- | ||
| 542 | |||
| 543 | sub success_response { | ||
| 544 | my ($class, $resp, $extra, $session) = @_; | ||
| 545 | $extra //= {}; | ||
| 546 | |||
| 547 | my %envelope = ( ok => 1, resp => $resp ); | ||
| 548 | |||
| 549 | # Always include token when there's a session | ||
| 550 | if ($session && $session->can('get_token')) { | ||
| 551 | $envelope{token} = $session->get_token; | ||
| 552 | } | ||
| 553 | |||
| 554 | # Token from extra (login/createUser) overrides session token | ||
| 555 | if ($extra->{token}) { | ||
| 556 | $envelope{token} = $extra->{token}; | ||
| 557 | } | ||
| 558 | |||
| 559 | # Merge extra fields (objects, classes, apps) | ||
| 560 | for my $key (qw(objects classes apps)) { | ||
| 561 | $envelope{$key} = $extra->{$key} if $extra->{$key}; | ||
| 562 | } | ||
| 563 | |||
| 564 | return encode_json(\%envelope); | ||
| 565 | } | ||
| 566 | |||
| 567 | sub error_response { | ||
| 568 | my ($class, $error) = @_; | ||
| 569 | return encode_json({ ok => 0, error => $error }); | ||
| 570 | } | ||
| 571 | |||
| 572 | #---------------------------------------------------------------------- | ||
| 573 | # Logging | ||
| 574 | #---------------------------------------------------------------------- | ||
| 575 | |||
| 576 | sub log_error { | ||
| 577 | my ($class, $context, $error) = @_; | ||
| 578 | my $ip = $context->{ip_address} // 'unknown'; | ||
| 579 | my $action = $context->{request}{action} // 'unknown'; | ||
| 580 | warn "[Yote::YapiServer::Site] ERROR ip=$ip action=$action error=$error\n"; | ||
| 581 | } | ||
| 582 | |||
| 583 | sub log_call { | ||
| 584 | my ($class, $context, $target, $method, $success) = @_; | ||
| 585 | # Implement audit logging here if needed | ||
| 586 | } | ||
| 587 | |||
| 588 | 1; | ||
| 589 | |||
| 590 | __END__ |