| 1 | package WWW::Suffit::Server::API::User; | ||
| 2 | use strict; | ||
| 3 | use utf8; | ||
| 4 | |||
| 5 | =encoding utf8 | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | WWW::Suffit::Server::API::User - The Suffit API controller for user management | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | use WWW::Suffit::Server::API::User; | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | The Suffit API controller for user management | ||
| 18 | |||
| 19 | This module uses the following configuration directives: | ||
| 20 | |||
| 21 | =over 8 | ||
| 22 | |||
| 23 | =item JWS_Algorithm | ||
| 24 | |||
| 25 | Allowed JWS signing algorithms: B<HS256>, B<HS384>, B<HS512>, B<RS256>, B<RS384>, B<RS512> | ||
| 26 | |||
| 27 | HS256 HMAC+SHA256 integrity | ||
| 28 | HS384 HMAC+SHA384 integrity | ||
| 29 | HS512 HMAC+SHA512 integrity | ||
| 30 | RS256 RSA+PKCS1-V1_5 + SHA256 signature | ||
| 31 | RS384 RSA+PKCS1-V1_5 + SHA384 signature | ||
| 32 | RS512 RSA+PKCS1-V1_5 + SHA512 signature | ||
| 33 | |||
| 34 | Default: B<HS256> | ||
| 35 | |||
| 36 | =back | ||
| 37 | |||
| 38 | =head1 METHODS | ||
| 39 | |||
| 40 | List of internal methods | ||
| 41 | |||
| 42 | =head2 genkeys | ||
| 43 | |||
| 44 | See L</"POST /api/user/genkeys"> | ||
| 45 | |||
| 46 | =head2 passwd | ||
| 47 | |||
| 48 | See L</"PATCH /api/user/passwd"> | ||
| 49 | |||
| 50 | =head2 token_del | ||
| 51 | |||
| 52 | See L</"DELETE /api/user/token/JTI"> | ||
| 53 | |||
| 54 | =head2 token_get | ||
| 55 | |||
| 56 | See L</"GET /api/user/token"> | ||
| 57 | |||
| 58 | =head2 token_set | ||
| 59 | |||
| 60 | See L</"POST /api/user/token"> | ||
| 61 | |||
| 62 | =head2 user_get | ||
| 63 | |||
| 64 | See L</"GET /api/user"> | ||
| 65 | |||
| 66 | =head2 user_set | ||
| 67 | |||
| 68 | See L</"PUT /api/user"> | ||
| 69 | |||
| 70 | =head1 API METHODS | ||
| 71 | |||
| 72 | List of API methods | ||
| 73 | |||
| 74 | =head2 GET /api/user | ||
| 75 | |||
| 76 | This method returns user data | ||
| 77 | |||
| 78 | # curl -v -H "Authorization: Bearer eyJh...s5aM" \ | ||
| 79 | https://localhost:8695/api/user | ||
| 80 | |||
| 81 | > GET /api/user HTTP/1.1 | ||
| 82 | > Host: localhost:8695 | ||
| 83 | > User-Agent: curl/7.68.0 | ||
| 84 | > Accept: */* | ||
| 85 | > Authorization: Bearer eyJh...s5aM | ||
| 86 | > | ||
| 87 | < HTTP/1.1 200 OK | ||
| 88 | < Content-Length: 653 | ||
| 89 | < Content-Type: application/json;charset=UTF-8 | ||
| 90 | < Date: Wed, 14 Aug 2024 16:33:34 GMT | ||
| 91 | < Server: OWL/1.11 | ||
| 92 | < | ||
| 93 | { | ||
| 94 | "algorithm": "SHA256", | ||
| 95 | "attributes": "", | ||
| 96 | "code": "E0000", | ||
| 97 | "comment": "Test user for internal testing only", | ||
| 98 | "created": 1678741533, | ||
| 99 | "email": "test@owl.localhost", | ||
| 100 | "email_md5": "163e50783979333ebae6fd63b2d96d16", | ||
| 101 | "expires": 0, | ||
| 102 | "flags": 31, | ||
| 103 | "groups": [ | ||
| 104 | "user" | ||
| 105 | ], | ||
| 106 | "name": "Test User", | ||
| 107 | "not_after": 0, | ||
| 108 | "not_before": 1695334721, | ||
| 109 | "public_key": "-----BEGIN RSA PUBLIC KEY-----...", | ||
| 110 | "role": "Test user", | ||
| 111 | "status": true, | ||
| 112 | "uid": 3, | ||
| 113 | "username": "test" | ||
| 114 | } | ||
| 115 | |||
| 116 | =head2 PUT /api/user | ||
| 117 | |||
| 118 | Edit user's data | ||
| 119 | |||
| 120 | # curl -v -H "Authorization: OWL eyJh...04qI" \ | ||
| 121 | -X PUT -d '{ | ||
| 122 | "name": "Test User", | ||
| 123 | "email": "test@owl.localhost", | ||
| 124 | "role": "Test user", | ||
| 125 | "comment": "Test user for internal testing only" | ||
| 126 | }' \ | ||
| 127 | https://owl.localhost:8695/api/user | ||
| 128 | |||
| 129 | > PUT /api/user HTTP/1.1 | ||
| 130 | > Host: owl.localhost:8695 | ||
| 131 | > User-Agent: curl/7.68.0 | ||
| 132 | > Accept: */* | ||
| 133 | > Authorization: OWL eyJh...04qI | ||
| 134 | > Content-Length: 163 | ||
| 135 | > Content-Type: application/x-www-form-urlencoded | ||
| 136 | > | ||
| 137 | < HTTP/1.1 200 OK | ||
| 138 | < Content-Length: 148 | ||
| 139 | < Server: OWL/1.00 | ||
| 140 | < Date: Mon, 15 May 2023 11:25:11 GMT | ||
| 141 | < Content-Type: application/json;charset=UTF-8 | ||
| 142 | < | ||
| 143 | { | ||
| 144 | "comment": "Test user for internal testing only", | ||
| 145 | "email": "test@owl.localhost", | ||
| 146 | "name": "Test User", | ||
| 147 | "role": "Test user", | ||
| 148 | "status": true, | ||
| 149 | "username": "test" | ||
| 150 | } | ||
| 151 | |||
| 152 | =head2 POST /api/user/genkeys | ||
| 153 | |||
| 154 | Issue (generation) RSA keys pair (public and private RSA keys) for user | ||
| 155 | |||
| 156 | # curl -v -X POST -H "Authorization: OWL eyJh...R_0c" \ | ||
| 157 | https://owl.localhost:8695/api/user/genkeys | ||
| 158 | |||
| 159 | > POST /api/user/genkeys HTTP/1.1 | ||
| 160 | > Host: owl.localhost:8695 | ||
| 161 | > User-Agent: curl/7.68.0 | ||
| 162 | > Accept: */* | ||
| 163 | > Authorization: OWL eyJh...R_0c | ||
| 164 | > | ||
| 165 | < HTTP/1.1 200 OK | ||
| 166 | < Date: Fri, 12 May 2023 06:31:21 GMT | ||
| 167 | < Server: OWL/1.00 | ||
| 168 | < Content-Type: application/json;charset=UTF-8 | ||
| 169 | < Content-Length: 1228 | ||
| 170 | < | ||
| 171 | { | ||
| 172 | "error": "", | ||
| 173 | "private_key": "-----BEGIN RSA PRIVATE KEY-----...", | ||
| 174 | "public_key": "-----BEGIN RSA PUBLIC KEY-----", | ||
| 175 | "status": true | ||
| 176 | } | ||
| 177 | |||
| 178 | =head2 PATCH /api/user/passwd | ||
| 179 | |||
| 180 | Change password for user | ||
| 181 | |||
| 182 | # curl -v -H "Authorization: OWL eyJh...Bh7g" \ | ||
| 183 | -X PATCH -d '{ | ||
| 184 | "current": "currentPassword", | ||
| 185 | "password": "newPassword" | ||
| 186 | }' \ | ||
| 187 | https://owl.localhost:8695/api/user/passwd | ||
| 188 | |||
| 189 | > PATCH /api/user/passwd HTTP/1.1 | ||
| 190 | > Host: owl.localhost:8695 | ||
| 191 | > User-Agent: curl/7.68.0 | ||
| 192 | > Accept: */* | ||
| 193 | > Authorization: OWL eyJh...04qI | ||
| 194 | > Content-Length: 64 | ||
| 195 | > Content-Type: application/x-www-form-urlencoded | ||
| 196 | > | ||
| 197 | < HTTP/1.1 200 OK | ||
| 198 | < Server: OWL/1.00 | ||
| 199 | < Content-Type: application/json;charset=UTF-8 | ||
| 200 | < Content-Length: 30 | ||
| 201 | < Date: Mon, 15 May 2023 11:36:50 GMT | ||
| 202 | < | ||
| 203 | { | ||
| 204 | "code": "E0000", | ||
| 205 | "status": true | ||
| 206 | } | ||
| 207 | |||
| 208 | =head2 DELETE /api/user/token/JTI | ||
| 209 | |||
| 210 | Removes specified token from list of tokens for user of current session | ||
| 211 | |||
| 212 | # curl -v -X DELETE -H "Authorization: OWL eyJh...04qI" \ | ||
| 213 | https://owl.localhost:8695/api/user/token/SqpHCfCS2646efd7 | ||
| 214 | |||
| 215 | > DELETE /api/user/token/SqpHCfCS2646efd7 HTTP/1.1 | ||
| 216 | > Host: owl.localhost:8695 | ||
| 217 | > User-Agent: curl/7.68.0 | ||
| 218 | > Accept: */* | ||
| 219 | > Authorization: OWL eyJh...04qI | ||
| 220 | > | ||
| 221 | < HTTP/1.1 200 OK | ||
| 222 | < Server: OWL/1.00 | ||
| 223 | < Content-Type: application/json;charset=UTF-8 | ||
| 224 | < Content-Length: 30 | ||
| 225 | < Date: Mon, 15 May 2023 11:11:35 GMT | ||
| 226 | < | ||
| 227 | { | ||
| 228 | "code": "E0000", | ||
| 229 | "status": true | ||
| 230 | } | ||
| 231 | |||
| 232 | =head2 GET /api/user/token | ||
| 233 | |||
| 234 | Get list of tokens for user of current session | ||
| 235 | |||
| 236 | # curl -v -X GET -H "Authorization: OWL eyJh...04qI" \ | ||
| 237 | https://owl.localhost:8695/api/user/token | ||
| 238 | |||
| 239 | > GET /api/user/token HTTP/1.1 | ||
| 240 | > Host: owl.localhost:8695 | ||
| 241 | > User-Agent: curl/7.68.0 | ||
| 242 | > Accept: */* | ||
| 243 | > Authorization: OWL eyJh...04qI | ||
| 244 | > | ||
| 245 | < HTTP/1.1 200 OK | ||
| 246 | < Server: OWL/1.00 | ||
| 247 | < Date: Mon, 15 May 2023 11:02:33 GMT | ||
| 248 | < Content-Length: 491 | ||
| 249 | < Content-Type: application/json;charset=UTF-8 | ||
| 250 | < | ||
| 251 | [ | ||
| 252 | { | ||
| 253 | "address": "127.0.0.1", | ||
| 254 | "clientid": "b048c047a0f0165ab7630f7aab1cb5aa", | ||
| 255 | "exp": 1684227656, | ||
| 256 | "iat": 1684141256, | ||
| 257 | "id": 88, | ||
| 258 | "jti": "ke2uj4ib059db017", | ||
| 259 | "type": "session", | ||
| 260 | "username": "test", | ||
| 261 | "description": "" | ||
| 262 | } | ||
| 263 | ] | ||
| 264 | |||
| 265 | =head2 POST /api/user/token | ||
| 266 | |||
| 267 | Issues the new API token for user of current session by session or access token | ||
| 268 | |||
| 269 | # curl -v -X POST -H "Authorization: OWL eyJh...aMTc" \ | ||
| 270 | https://owl.localhost:8695/api/user/token | ||
| 271 | |||
| 272 | > POST /api/user/token HTTP/1.1 | ||
| 273 | > Host: owl.localhost:8695 | ||
| 274 | > User-Agent: curl/7.68.0 | ||
| 275 | > Accept: */* | ||
| 276 | > Authorization: OWL eyJh...aMTc | ||
| 277 | > | ||
| 278 | < HTTP/1.1 200 OK | ||
| 279 | < Server: OWL/1.00 | ||
| 280 | < Content-Type: application/json;charset=UTF-8 | ||
| 281 | < Content-Length: 586 | ||
| 282 | < Date: Fri, 12 May 2023 05:42:37 GMT | ||
| 283 | < | ||
| 284 | { | ||
| 285 | "address": "127.0.0.1", | ||
| 286 | "clientid": "f459f12619c961122450ae5883e44a60", | ||
| 287 | "exp": 0, | ||
| 288 | "iat": 1683870157, | ||
| 289 | "id": 87, | ||
| 290 | "jti": "SqpHCfCS2646efd7", | ||
| 291 | "status": true, | ||
| 292 | "token": "eyJh...H8ac", | ||
| 293 | "type": "api", | ||
| 294 | "username": "test", | ||
| 295 | "description": "" | ||
| 296 | } | ||
| 297 | |||
| 298 | =head1 ERROR CODES | ||
| 299 | |||
| 300 | The list of User Suffit API error codes | ||
| 301 | |||
| 302 | API | HTTP | DESCRIPTION | ||
| 303 | -------+-------+------------------------------------------------- | ||
| 304 | E1120 [400] Incorrect username | ||
| 305 | E1121 [400] Incorrect current password | ||
| 306 | E1122 [400] Incorrect new password | ||
| 307 | E1123 [500] Can't generate RSA keys | ||
| 308 | E1124 [500] Can't JWT generate | ||
| 309 | E1125 [500] Can't token store to database | ||
| 310 | E1126 [400] Incorrect JWT jti | ||
| 311 | E1127 [500] Can't delete token from database | ||
| 312 | E1128 [500] Can't edit user data (user_set) | ||
| 313 | E1129 [500] Can't change user password | ||
| 314 | E1130 [500] Can't save RSA keys to database | ||
| 315 | |||
| 316 | B<*> -- this code will be defined later on the interface side | ||
| 317 | |||
| 318 | See also list of common Suffit API error codes in L<WWW::Suffit::API/"ERROR CODES"> | ||
| 319 | |||
| 320 | =head1 HISTORY | ||
| 321 | |||
| 322 | See C<Changes> file | ||
| 323 | |||
| 324 | =head1 TO DO | ||
| 325 | |||
| 326 | See C<TODO> file | ||
| 327 | |||
| 328 | =head1 SEE ALSO | ||
| 329 | |||
| 330 | L<Mojolicious>, L<WWW::Suffit>, L<WWW::Suffit::Server>, L<WWW::Suffit::API> | ||
| 331 | |||
| 332 | =head1 AUTHOR | ||
| 333 | |||
| 334 | Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt> | ||
| 335 | |||
| 336 | =head1 COPYRIGHT | ||
| 337 | |||
| 338 | Copyright (C) 1998-2026 D&D Corporation | ||
| 339 | |||
| 340 | =head1 LICENSE | ||
| 341 | |||
| 342 | This program is distributed under the terms of the Artistic License Version 2.0 | ||
| 343 | |||
| 344 | See the C<LICENSE> file or L<https://opensource.org/license/artistic-2-0> for details | ||
| 345 | |||
| 346 | =cut | ||
| 347 | |||
| 348 | use Mojo::Base 'Mojolicious::Controller'; | ||
| 349 | |||
| 350 | use Mojo::Util qw/ trim /; | ||
| 351 | |||
| 352 | use Acrux::Util qw/ randchars /; | ||
| 353 | |||
| 354 | use WWW::Suffit::Const qw/ USERNAME_REGEXP TOKEN_EXPIRATION JTI_REGEXP /; | ||
| 355 | |||
| 356 | sub user_get { | ||
| 357 | my $self = shift; | ||
| 358 | my $authdb = $self->authdb->clean; | ||
| 359 | |||
| 360 | # Get username from session | ||
| 361 | my $username = $self->stash('username') // ''; # from Auth::is_authorized | ||
| 362 | return $self->reply->json_error(400 => "E1120" => "Incorrect username") | ||
| 363 | unless length($username) && (length($username) <= 64) && $username =~ USERNAME_REGEXP; | ||
| 364 | |||
| 365 | # Get user instance | ||
| 366 | my $user = $authdb->user($username); | ||
| 367 | return $self->reply->json_error($authdb->code, $authdb->error) if $authdb->error; | ||
| 368 | |||
| 369 | # Ok | ||
| 370 | return $self->reply->json_ok({($user->to_hash(1))}); | ||
| 371 | } | ||
| 372 | sub user_set { | ||
| 373 | my $self = shift; | ||
| 374 | my $authdb = $self->authdb->clean; | ||
| 375 | |||
| 376 | # Get username from session | ||
| 377 | my $username = $self->stash('username') // ''; # from Auth::is_authorized | ||
| 378 | return $self->reply->json_error(400 => "E1120" => "Incorrect username") | ||
| 379 | unless length($username) && (length($username) <= 64) && $username =~ USERNAME_REGEXP; | ||
| 380 | |||
| 381 | # Get user instance | ||
| 382 | my $user = $authdb->user($username); | ||
| 383 | return $self->reply->json_error($authdb->code, $authdb->error) if $authdb->error; | ||
| 384 | |||
| 385 | # Get user data from input json | ||
| 386 | my %data = ( | ||
| 387 | username=> $username, | ||
| 388 | comment => $self->req->json('/comment') // '', | ||
| 389 | email => $self->req->json('/email') // '', | ||
| 390 | name => $self->req->json('/name') // '', | ||
| 391 | role => $self->req->json('/role') // '', | ||
| 392 | ); | ||
| 393 | |||
| 394 | # User edit | ||
| 395 | $authdb->user_edit(%data) | ||
| 396 | or return $self->reply->json_error($authdb->code, $authdb->error || "E1128: Can't edit user data"); | ||
| 397 | |||
| 398 | # Render ok | ||
| 399 | return $self->reply->json_ok({ %data }); | ||
| 400 | } | ||
| 401 | sub passwd { | ||
| 402 | my $self = shift; | ||
| 403 | my $authdb = $self->authdb->clean; | ||
| 404 | |||
| 405 | # Get username from session | ||
| 406 | my $username = $self->stash('username') // ''; # from Auth::is_authorized | ||
| 407 | return $self->reply->json_error(400 => "E1120" => "Incorrect username") | ||
| 408 | unless length($username) && (length($username) <= 64) && $username =~ USERNAME_REGEXP; | ||
| 409 | |||
| 410 | # Get data from input json | ||
| 411 | my $cur = trim($self->req->json('/current') // ''); | ||
| 412 | my $pwd = trim($self->req->json('/password') // ''); | ||
| 413 | return $self->reply->json_error(400 => "E1121" => "Incorrect current password") | ||
| 414 | unless length($cur) && (length($cur) <= 255); | ||
| 415 | return $self->reply->json_error(400 => "E1122" => "Incorrect new password") | ||
| 416 | unless length($pwd) && (length($pwd) <= 255); | ||
| 417 | |||
| 418 | # Authentication with old password | ||
| 419 | $authdb->authn( | ||
| 420 | u => $username, | ||
| 421 | p => $cur, | ||
| 422 | a => $self->client_ip($self->app->trustedproxies), | ||
| 423 | k => $self->stash('cachekey'), | ||
| 424 | ) or return $self->reply->json_error(400 => $authdb->error || "E1121: Incorrect current password"); | ||
| 425 | |||
| 426 | # Store data | ||
| 427 | $authdb->user_passwd(username => $username, password => $pwd) | ||
| 428 | or return $self->reply->json_error($authdb->code, $authdb->error || "E1129: Can't change user password"); | ||
| 429 | |||
| 430 | # Render ok | ||
| 431 | return $self->reply->json_ok; | ||
| 432 | } | ||
| 433 | sub genkeys { | ||
| 434 | my $self = shift; | ||
| 435 | my $authdb = $self->authdb->clean; | ||
| 436 | |||
| 437 | # Get username from session | ||
| 438 | my $username = $self->stash('username') // ''; # from Auth::is_authorized | ||
| 439 | return $self->reply->json_error(400 => "E1120" => "Incorrect username") | ||
| 440 | unless length($username) && (length($username) <= 64) && $username =~ USERNAME_REGEXP; | ||
| 441 | |||
| 442 | # Gen RSA keys | ||
| 443 | my %rsadata = $self->gen_rsakeys; | ||
| 444 | return $self->reply->json_error(500 => "E1123" => $rsadata{error}) if $rsadata{error}; | ||
| 445 | $self->log->info(sprintf("Generate new %s bit RSA key pair for user %s", $rsadata{key_size}, $username)); | ||
| 446 | |||
| 447 | # Store data | ||
| 448 | $authdb->user_setkeys(%rsadata, username => $username) | ||
| 449 | or return $self->reply->json_error($authdb->code, $authdb->error || "E1130: Can't save RSA keys to database"); | ||
| 450 | |||
| 451 | # Render ok | ||
| 452 | return $self->reply->json_ok({ %rsadata }); | ||
| 453 | } | ||
| 454 | sub token_get { | ||
| 455 | my $self = shift; | ||
| 456 | my $authdb = $self->authdb->clean; | ||
| 457 | |||
| 458 | # Get username from session | ||
| 459 | my $username = $self->stash('username') // ''; # from Auth::is_authorized | ||
| 460 | return $self->reply->json_error(400 => "E1120" => "Incorrect username") | ||
| 461 | unless length($username) && (length($username) <= 64) && $username =~ USERNAME_REGEXP; | ||
| 462 | |||
| 463 | # Get tokens | ||
| 464 | my @tokens = $authdb->user_tokens($username); | ||
| 465 | return $self->reply->json_error($authdb->code, $authdb->error) if $authdb->error; | ||
| 466 | |||
| 467 | # Render | ||
| 468 | return $self->render(json => [@tokens]); | ||
| 469 | } | ||
| 470 | sub token_set { | ||
| 471 | my $self = shift; | ||
| 472 | my $authdb = $self->authdb->clean; | ||
| 473 | my $clientid = $self->req->json('/clientid') || $self->clientid; | ||
| 474 | my $cachekey = $self->req->json('/cachekey') // $self->app->gen_cachekey; | ||
| 475 | my $description = $self->req->json('/description') // ''; | ||
| 476 | |||
| 477 | # Get username from session | ||
| 478 | my $username = $self->stash('username') // ''; # from Auth::is_authorized | ||
| 479 | return $self->reply->json_error(400 => "E1120" => "Incorrect username") | ||
| 480 | unless length($username) && (length($username) <= 64) && $username =~ USERNAME_REGEXP; | ||
| 481 | |||
| 482 | # Get user instance | ||
| 483 | my $user = $authdb->user($username); | ||
| 484 | return $self->reply->json_error($authdb->code, $authdb->error) if $authdb->error; | ||
| 485 | |||
| 486 | # Generate API token | ||
| 487 | my $jwt = $self->jwt; | ||
| 488 | my $now = time; | ||
| 489 | my $exp = $user->forever ? undef : ($now + TOKEN_EXPIRATION * 365); | ||
| 490 | my $jti = sprintf("%s%s", randchars(8), $self->req->request_id); | ||
| 491 | $jwt->algorithm($self->conf->latest("/jws_algorithm")) if $self->conf->latest("/jws_algorithm"); | ||
| 492 | $jwt->expires($exp); # Set or Flush expires | ||
| 493 | $jwt->iat($now)->jti($jti)->payload({ | ||
| 494 | ver => $self->app->VERSION, | ||
| 495 | typ => 'api', | ||
| 496 | usr => $username, | ||
| 497 | ip => $self->remote_ip($self->app->trustedproxies), | ||
| 498 | cid => $clientid, | ||
| 499 | $cachekey ? (key => $cachekey) : (), | ||
| 500 | }); | ||
| 501 | my $token = $jwt->encode->token; | ||
| 502 | return $self->reply->json_error(500 => $jwt->error || "E1124: Can't JWT generate") unless $token; | ||
| 503 | |||
| 504 | # Store | ||
| 505 | $authdb->token_set( | ||
| 506 | type => 'api', | ||
| 507 | jti => $jti, | ||
| 508 | username => $username, | ||
| 509 | clientid => $clientid, | ||
| 510 | iat => $now, | ||
| 511 | exp => $exp || 0, | ||
| 512 | address => $self->remote_ip($self->app->trustedproxies), | ||
| 513 | description => $description, | ||
| 514 | ) or return $self->reply->json_error($authdb->code, $authdb->error || "E1125: Can't token store to database"); | ||
| 515 | |||
| 516 | # Get actual data by username and jti | ||
| 517 | my %issued = $authdb->token_get($username, $jti); | ||
| 518 | return $self->reply->json_error($authdb->code, $authdb->error) if $authdb->error; | ||
| 519 | $issued{token} = $token; | ||
| 520 | |||
| 521 | # Render ok | ||
| 522 | return $self->reply->json_ok({ %issued }); | ||
| 523 | } | ||
| 524 | sub token_del { | ||
| 525 | my $self = shift; | ||
| 526 | my $authdb = $self->authdb->clean; | ||
| 527 | |||
| 528 | # Get username from session | ||
| 529 | my $username = $self->stash('username') // ''; # from Auth::is_authorized | ||
| 530 | return $self->reply->json_error(400 => "E1120" => "Incorrect username") | ||
| 531 | unless length($username) && (length($username) <= 64) && $username =~ USERNAME_REGEXP; | ||
| 532 | |||
| 533 | # Get jti from URL path | ||
| 534 | my $jti = trim($self->param('jti') // ''); | ||
| 535 | return $self->reply->json_error(400 => "E1126" => "Incorrect JWT jti") | ||
| 536 | unless length($jti) && (length($jti) <= 64) && $jti =~ JTI_REGEXP; | ||
| 537 | |||
| 538 | # Delete from database | ||
| 539 | $authdb->token_del($username, $jti) | ||
| 540 | or return $self->reply->json_error($authdb->code, $authdb->error || "E1127: Can't delete token from database"); | ||
| 541 | |||
| 542 | # Render ok | ||
| 543 | return $self->reply->json_ok; | ||
| 544 | } | ||
| 545 | |||
| 546 | 1; | ||
| 547 | |||
| 548 | __END__ |