| 1 | package WWW::Suffit::Plugin::API; | ||
| 2 | use strict; | ||
| 3 | use utf8; | ||
| 4 | |||
| 5 | =encoding utf8 | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | WWW::Suffit::Plugin::API - The 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', { | ||
| 18 | prefix_path => "/api", | ||
| 19 | prefix_name => "api", | ||
| 20 | }); | ||
| 21 | |||
| 22 | # . . . | ||
| 23 | } | ||
| 24 | |||
| 25 | sub startup { | ||
| 26 | my $self = shift->SUPER::startup( | ||
| 27 | init_api_routes => 'on', | ||
| 28 | api_routes_opts => { | ||
| 29 | prefix_path => "/api", | ||
| 30 | prefix_name => "api", | ||
| 31 | } | ||
| 32 | ); | ||
| 33 | |||
| 34 | # . . . | ||
| 35 | } | ||
| 36 | |||
| 37 | =head1 DESCRIPTION | ||
| 38 | |||
| 39 | The API Suffit plugin | ||
| 40 | |||
| 41 | This plugin requires L<WWW::Suffit::Plugin::AuthDB> plugin | ||
| 42 | |||
| 43 | =head1 OPTIONS | ||
| 44 | |||
| 45 | This plugin supports the following options | ||
| 46 | |||
| 47 | =head2 prefix_name | ||
| 48 | |||
| 49 | prefix_name => "api" | ||
| 50 | |||
| 51 | This option defines prefix of api route name | ||
| 52 | |||
| 53 | Default: 'api' | ||
| 54 | |||
| 55 | =head2 prefix_path | ||
| 56 | |||
| 57 | prefix_path => "/api" | ||
| 58 | |||
| 59 | This option defines prefix of api route | ||
| 60 | |||
| 61 | Default: '/api' | ||
| 62 | |||
| 63 | =head1 HELPERS | ||
| 64 | |||
| 65 | This plugin implements the following helpers | ||
| 66 | |||
| 67 | =head2 api_prefix_name | ||
| 68 | |||
| 69 | my $api_prefix_name = $c->api_prefix_name; | ||
| 70 | |||
| 71 | Returns the api prefix name | ||
| 72 | |||
| 73 | Default: api | ||
| 74 | |||
| 75 | =head2 api_prefix_path | ||
| 76 | |||
| 77 | my $api_prefix_path = $c->api_prefix_path; | ||
| 78 | |||
| 79 | Returns the api prefix path | ||
| 80 | |||
| 81 | Default: /api | ||
| 82 | |||
| 83 | =head1 METHODS | ||
| 84 | |||
| 85 | This plugin inherits all methods from L<Mojolicious::Plugin> and implements the following new ones | ||
| 86 | |||
| 87 | =head2 register | ||
| 88 | |||
| 89 | This method register the plugin and helpers in L<Mojolicious> application | ||
| 90 | |||
| 91 | $plugin->register(Mojolicious->new, { | ||
| 92 | prefix_path => "/api", | ||
| 93 | prefix_name => "api", | ||
| 94 | }); | ||
| 95 | |||
| 96 | Register plugin in L<Mojolicious> application | ||
| 97 | |||
| 98 | =head1 SEE ALSO | ||
| 99 | |||
| 100 | L<Mojolicious>, L<WWW::Suffit::Server>, L<WWW::Suffit::AuthDB>, L<WWW::Suffit::Plugin::AuthDB> | ||
| 101 | |||
| 102 | =head1 AUTHOR | ||
| 103 | |||
| 104 | Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt> | ||
| 105 | |||
| 106 | =head1 COPYRIGHT | ||
| 107 | |||
| 108 | Copyright (C) 1998-2026 D&D Corporation | ||
| 109 | |||
| 110 | =head1 LICENSE | ||
| 111 | |||
| 112 | This program is distributed under the terms of the Artistic License Version 2.0 | ||
| 113 | |||
| 114 | See the C<LICENSE> file or L<https://opensource.org/license/artistic-2-0> for details | ||
| 115 | |||
| 116 | =cut | ||
| 117 | |||
| 118 | use Mojo::Base 'Mojolicious::Plugin'; | ||
| 119 | |||
| 120 | sub register { | ||
| 121 | my ($plugin, $app, $opts) = @_; # $self = $plugin | ||
| 122 | $app->raise("Can't initialize the API plugin: the AuthDB plugin required") | ||
| 123 | unless exists $app->renderer->helpers->{'authdb'}; | ||
| 124 | $opts //= {}; | ||
| 125 | my $pathpfx = $opts->{prefix_path} || '/api'; | ||
| 126 | my $namepfx = $opts->{prefix_name} || $pathpfx; | ||
| 127 | $namepfx =~ s/[^a-z0-9_\-]//g; | ||
| 128 | $namepfx ||= 'api'; | ||
| 129 | |||
| 130 | # Add helpers to quick access to api prefixes | ||
| 131 | $app->helper('api_prefix_name' => sub { "$namepfx" }); | ||
| 132 | $app->helper('api_prefix_path' => sub { "$pathpfx" }); | ||
| 133 | |||
| 134 | # General routes related to the Suffit API | ||
| 135 | my $r = $app->routes->under('/')->to('API#is_connected')->name('__authdb'); | ||
| 136 | |||
| 137 | $r->get(sprintf('%s/status', $pathpfx))->to('API#api')->name(sprintf('%s-status', $namepfx)); | ||
| 138 | $r->get(sprintf('%s/check', $pathpfx))->to('API#check')->name(sprintf('%s-check', $namepfx)); | ||
| 139 | $r->post(sprintf('%s/authorize',$pathpfx))->to('API::Auth#authorize' => { | ||
| 140 | token_type => 'access', | ||
| 141 | skip_authdb_connect => 1, | ||
| 142 | })->name(sprintf('%s-authorize', $namepfx)); | ||
| 143 | |||
| 144 | # API routes with token or cookie authorization | ||
| 145 | my $authorized = $r->under($pathpfx)->to('API::Auth#is_authorized')->name($namepfx); | ||
| 146 | $authorized->get('/')->to('API#api')->name(sprintf('%s-data', $namepfx)); | ||
| 147 | |||
| 148 | # API::V1 | ||
| 149 | $authorized->post('/v1/authn')->to('API::V1#authn')->name(sprintf('%s-v1-authn', $namepfx)); | ||
| 150 | $authorized->post('/v1/authz')->to('API::V1#authz')->name(sprintf('%s-v1-authz', $namepfx)); | ||
| 151 | $authorized->get('/v1/publicKey')->to('API::V1#public_key')->name(sprintf('%s-v1-publickey', $namepfx)); | ||
| 152 | |||
| 153 | # API::NoAPI | ||
| 154 | $authorized->get('/file')->to('API::NoAPI#file_list')->name(sprintf('%s-file-list', $namepfx)); | ||
| 155 | $authorized->get('/file/*filepath')->to('API::NoAPI#file_download')->name(sprintf('%s-file-download', $namepfx)); | ||
| 156 | $authorized->put('/file/*filepath')->to('API::NoAPI#file_upload')->name(sprintf('%s-file-upload', $namepfx)); | ||
| 157 | $authorized->delete('/file/*filepath')->to('API::NoAPI#file_remove')->name(sprintf('%s-file-remove', $namepfx)); | ||
| 158 | } | ||
| 159 | |||
| 160 | 1; | ||
| 161 | |||
| 162 | __END__ |