| 1 | package WWW::Suffit::Plugin::API::User; | ||
| 2 | use strict; | ||
| 3 | use utf8; | ||
| 4 | |||
| 5 | =encoding utf8 | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | WWW::Suffit::Plugin::API::User - The User API Suffit plugin | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | use Mojo::Base 'WWW::Suffit::Server'; | ||
| 14 | |||
| 15 | sub startup { | ||
| 16 | my $self = shift->SUPER::startup(); | ||
| 17 | $self->plugin('API::User', { | ||
| 18 | prefix_path => "/user", | ||
| 19 | prefix_name => "user", | ||
| 20 | }); | ||
| 21 | |||
| 22 | # . . . | ||
| 23 | } | ||
| 24 | |||
| 25 | sub startup { | ||
| 26 | my $self = shift->SUPER::startup( | ||
| 27 | init_user_routes => 'on', | ||
| 28 | user_routes_opts => { | ||
| 29 | prefix_path => "/user", | ||
| 30 | prefix_name => "user", | ||
| 31 | } | ||
| 32 | ); | ||
| 33 | |||
| 34 | # . . . | ||
| 35 | } | ||
| 36 | |||
| 37 | =head1 DESCRIPTION | ||
| 38 | |||
| 39 | The User API Suffit plugin | ||
| 40 | |||
| 41 | This plugin requires L<WWW::Suffit::Plugin::API> plugin | ||
| 42 | |||
| 43 | =head1 OPTIONS | ||
| 44 | |||
| 45 | This plugin supports the following options | ||
| 46 | |||
| 47 | =head2 prefix_name | ||
| 48 | |||
| 49 | prefix_name => "user" | ||
| 50 | |||
| 51 | This option defines prefix of user api route name | ||
| 52 | |||
| 53 | Default: 'user' | ||
| 54 | |||
| 55 | =head2 prefix_path | ||
| 56 | |||
| 57 | prefix_path => "/user" | ||
| 58 | |||
| 59 | This option defines prefix of user api route | ||
| 60 | |||
| 61 | Default: '/user' | ||
| 62 | |||
| 63 | =head1 METHODS | ||
| 64 | |||
| 65 | This plugin inherits all methods from L<Mojolicious::Plugin> and implements the following new ones | ||
| 66 | |||
| 67 | =head2 register | ||
| 68 | |||
| 69 | This method register the plugin and helpers in L<Mojolicious> application | ||
| 70 | |||
| 71 | $plugin->register(Mojolicious->new, { | ||
| 72 | prefix_path => "/user", | ||
| 73 | prefix_name => "user", | ||
| 74 | }); | ||
| 75 | |||
| 76 | Register plugin in L<Mojolicious> application | ||
| 77 | |||
| 78 | =head1 SEE ALSO | ||
| 79 | |||
| 80 | L<Mojolicious>, L<WWW::Suffit::Server>, L<WWW::Suffit::AuthDB>, L<WWW::Suffit::Plugin::API> | ||
| 81 | |||
| 82 | =head1 AUTHOR | ||
| 83 | |||
| 84 | Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt> | ||
| 85 | |||
| 86 | =head1 COPYRIGHT | ||
| 87 | |||
| 88 | Copyright (C) 1998-2026 D&D Corporation | ||
| 89 | |||
| 90 | =head1 LICENSE | ||
| 91 | |||
| 92 | This program is distributed under the terms of the Artistic License Version 2.0 | ||
| 93 | |||
| 94 | See the C<LICENSE> file or L<https://opensource.org/license/artistic-2-0> for details | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | use Mojo::Base 'Mojolicious::Plugin'; | ||
| 99 | |||
| 100 | use WWW::Suffit::Const qw/ JTI_REGEXP /; | ||
| 101 | |||
| 102 | sub register { | ||
| 103 | my ($plugin, $app, $opts) = @_; # $self = $plugin | ||
| 104 | $app->raise("Can't initialize the API::User plugin: the API plugin required") | ||
| 105 | unless exists $app->renderer->helpers->{'api_prefix_name'}; | ||
| 106 | $opts //= {}; | ||
| 107 | my $pathpfx = $opts->{prefix_path} || '/user'; | ||
| 108 | my $namepfx = $opts->{prefix_name} || 'user'; | ||
| 109 | $namepfx =~ s/[^a-z0-9_\-]//g; | ||
| 110 | $namepfx ||= 'user'; | ||
| 111 | $namepfx = sprintf("%s-%s", $app->api_prefix_name, $namepfx) if $app->api_prefix_name; | ||
| 112 | |||
| 113 | # Get base API route with token/cookie authorization | ||
| 114 | my $r = $app->routes; | ||
| 115 | my $api = $r->lookup($app->api_prefix_name // 'api'); | ||
| 116 | $r->{reverse} = undef; # Force flush Mojolicious routes cache | ||
| 117 | |||
| 118 | # User routes | ||
| 119 | my $u = $api->under($pathpfx)->name($namepfx); # /api/user | ||
| 120 | |||
| 121 | # User routes | ||
| 122 | $u->get('/')->to('API::User#user_get')->name(sprintf('%s-data', $namepfx)); | ||
| 123 | $u->put('/')->to('API::User#user_set')->name(sprintf('%s-set', $namepfx)); | ||
| 124 | $u->patch('/passwd')->to('API::User#passwd')->name(sprintf('%s-passwd', $namepfx)); | ||
| 125 | $u->post('/genkeys')->to('API::User#genkeys')->name(sprintf('%s-genkeys', $namepfx)); | ||
| 126 | $u->get('/token')->to('API::User#token_get')->name(sprintf('%s-token-get', $namepfx)); | ||
| 127 | $u->post('/token')->to('API::User#token_set')->name(sprintf('%s-token-set', $namepfx)); | ||
| 128 | $u->delete('/token/:jti' => [jti => JTI_REGEXP])->to('API::User#token_del')->name(sprintf('%s-token-del', $namepfx)); | ||
| 129 | } | ||
| 130 | |||
| 131 | 1; | ||
| 132 | |||
| 133 | __END__ |