Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 1 | package Kalamar::Plugin::Auth; |
| 2 | use Mojo::Base 'Mojolicious::Plugin'; |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 3 | use File::Basename 'dirname'; |
| 4 | use File::Spec::Functions qw/catdir/; |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 5 | use Mojo::ByteStream 'b'; |
Akron | 66ef3b5 | 2022-11-22 14:25:15 +0100 | [diff] [blame] | 6 | use Mojo::File qw!path!; |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 7 | use Mojo::Util qw!b64_encode encode!; |
Akron | 6b75d12 | 2022-05-12 17:39:05 +0200 | [diff] [blame] | 8 | use Mojo::JSON qw'decode_json encode_json'; |
Akron | 6a228db | 2021-10-14 15:57:00 +0200 | [diff] [blame] | 9 | use Encode 'is_utf8'; |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 10 | |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 11 | # This is a plugin to deal with the Kustvakt OAuth server. |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 12 | # All tokens are stored in the session. Access tokens are short-lived, |
| 13 | # which limits the effects of misuse. |
| 14 | # Refresh tokens are bound to client id and client secret, |
| 15 | # which again limits the effects of misuse. |
| 16 | |
| 17 | # TODO: |
| 18 | # Establish a plugin 'OAuth' that works independent of 'Auth'. |
| 19 | |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 20 | # TODO: |
Akron | c82b1bc | 2018-11-18 18:06:14 +0100 | [diff] [blame] | 21 | # CSRF-protect logout! |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 22 | |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 23 | # TODO: |
| 24 | # Remove the Bearer prefix from auth. |
| 25 | |
| 26 | # In case no expiration time is returned by the server, |
| 27 | # take this time. |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 28 | our $EXPECTED_EXPIRATION_IN = 259200; |
| 29 | |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 30 | # Register the plugin |
| 31 | sub register { |
| 32 | my ($plugin, $app, $param) = @_; |
| 33 | |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 34 | # Load parameter from config file |
| 35 | if (my $config_param = $app->config('Kalamar-Auth')) { |
| 36 | $param = { %$param, %$config_param }; |
| 37 | }; |
| 38 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 39 | if ($param->{jwt}) { |
| 40 | $app->log->error('JWT flow is no longer supported'); |
| 41 | return; |
| 42 | }; |
| 43 | |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 44 | # Load 'notifications' plugin |
| 45 | unless (exists $app->renderer->helpers->{notify}) { |
| 46 | $app->plugin(Notifications => { |
| 47 | HTML => 1 |
| 48 | }); |
| 49 | }; |
| 50 | |
Akron | 8627609 | 2022-05-20 15:36:05 +0200 | [diff] [blame] | 51 | # Set session default timeout |
| 52 | for ($app->sessions) { |
| 53 | $_->default_expiration(60*60*24*3); # Session expires after 3 days of non-use |
| 54 | }; |
| 55 | |
Akron | 66ef3b5 | 2022-11-22 14:25:15 +0100 | [diff] [blame] | 56 | # Get client_id and client_secret from client file |
Akron | 53a171e | 2022-12-05 18:22:58 +0100 | [diff] [blame] | 57 | if ($param->{client_file} || $main::ENV{KALAMAR_CLIENT_FILE}) { |
| 58 | $param->{client_file} ||= $main::ENV{KALAMAR_CLIENT_FILE}; |
Akron | 66ef3b5 | 2022-11-22 14:25:15 +0100 | [diff] [blame] | 59 | my $client_json = decode_json(path($param->{client_file})->slurp); |
| 60 | $param->{client_id} //= $client_json->{client_id}; |
| 61 | $param->{client_secret} //= $client_json->{client_secret}; |
| 62 | }; |
| 63 | |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 64 | # Get the client id and the client_secret as a requirement |
| 65 | unless ($param->{client_id} && $param->{client_secret}) { |
| 66 | $app->log->error('client_id or client_secret not defined'); |
| 67 | }; |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 68 | |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 69 | # Load localize |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 70 | $app->plugin('Localize' => { |
| 71 | dict => { |
Akron | e997bb5 | 2021-06-11 16:44:06 +0200 | [diff] [blame] | 72 | de => { |
| 73 | abort => 'Abbrechen' |
| 74 | }, |
| 75 | -en => { |
| 76 | abort => 'Abort' |
| 77 | }, |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 78 | Auth => { |
| 79 | _ => sub { $_->locale }, |
| 80 | de => { |
Akron | a8efaa9 | 2022-04-09 14:45:43 +0200 | [diff] [blame] | 81 | loginPlease => 'Bitte melden Sie sich an!', |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 82 | loginSuccess => 'Anmeldung erfolgreich', |
| 83 | loginFail => 'Anmeldung fehlgeschlagen', |
| 84 | logoutSuccess => 'Abmeldung erfolgreich', |
| 85 | logoutFail => 'Abmeldung fehlgeschlagen', |
Akron | ff08811 | 2021-06-15 15:26:04 +0200 | [diff] [blame] | 86 | authenticationFail => 'Nicht authentifiziert', |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 87 | csrfFail => 'Fehlerhafter CSRF Token', |
Akron | 2c142ab | 2023-01-30 13:21:57 +0100 | [diff] [blame] | 88 | scopeFail => 'Scope nicht definiert', |
| 89 | clientIDFail => 'Client ID nicht definiert', |
Akron | 6a228db | 2021-10-14 15:57:00 +0200 | [diff] [blame] | 90 | invalidChar => 'Ungültiges Zeichen in Anfrage', |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 91 | openRedirectFail => 'Weiterleitungsfehler', |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 92 | tokenExpired => 'Zugriffstoken abgelaufen', |
| 93 | tokenInvalid => 'Zugriffstoken ungültig', |
| 94 | refreshFail => 'Fehlerhafter Refresh-Token', |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 95 | responseError => 'Unbekannter Autorisierungsfehler', |
Akron | cce055c | 2021-07-02 12:18:03 +0200 | [diff] [blame] | 96 | serverError => 'Unbekannter Serverfehler', |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 97 | revokeFail => 'Der Token kann nicht widerrufen werden', |
| 98 | revokeSuccess => 'Der Token wurde erfolgreich widerrufen', |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 99 | paramError => 'Einige Eingaben sind fehlerhaft', |
Akron | eb39cf3 | 2023-04-03 14:40:48 +0200 | [diff] [blame] | 100 | redirectUri => 'Weiterleitungsadresse', |
Akron | 9f2ad34 | 2022-05-04 16:16:40 +0200 | [diff] [blame] | 101 | pluginSrc => 'Beschreibung des Plugins (*.json-Datei)', |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 102 | homepage => 'Webseite', |
Helge | 9f9d485 | 2024-12-09 16:06:34 +0100 | [diff] [blame] | 103 | homepageReq => '*(Plugins)', |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 104 | desc => 'Kurzbeschreibung', |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 105 | revoke => 'Widerrufen', |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 106 | clientCredentials => 'Client Daten', |
| 107 | clientType => 'Art der Client-Applikation', |
| 108 | clientName => 'Name der Client-Applikation', |
| 109 | clientID => 'ID der Client-Applikation', |
| 110 | clientSecret => 'Client-Secret', |
| 111 | clientRegister => 'Neue Client-Applikation registrieren', |
| 112 | registerSuccess => 'Registrierung erfolgreich', |
| 113 | registerFail => 'Registrierung fehlgeschlagen', |
Akron | b0a7a0f | 2025-02-27 09:51:06 +0100 | [diff] [blame] | 114 | oauthSettings => 'API Tokens', |
Helge | 278fbca | 2022-11-29 18:49:15 +0100 | [diff] [blame] | 115 | #for marketplace settings |
| 116 | marketplace => 'Marktplatz', |
Helge | db720ea | 2023-03-20 09:39:36 +0100 | [diff] [blame] | 117 | plugins => 'Plugins', |
| 118 | instplugins => 'Bereits installierte Plugins', |
| 119 | regby => 'Registriert von', |
| 120 | regdate => 'Registrierungsdatum', |
| 121 | instdate=> 'Installationsdatum', |
| 122 | install => 'Installieren', |
| 123 | installFail => 'Plugin konnte nicht installiert werden', |
Helge | d36478d | 2023-06-08 17:43:01 +0200 | [diff] [blame] | 124 | uninstallFail => 'Plugin konnte nicht deinstalliert werden', |
| 125 | marketplaceFail => { |
| 126 | -long => 'Die Plugins konnten leider nicht angezeigt werden.', |
| 127 | short => 'Erneut versuchen' |
| 128 | }, |
Akron | e997bb5 | 2021-06-11 16:44:06 +0200 | [diff] [blame] | 129 | oauthUnregister => { |
| 130 | -long => 'Möchten sie <span class="client-name"><%= $client_name %></span> wirklich löschen?', |
| 131 | short => 'Löschen' |
| 132 | }, |
Akron | 9f2ad34 | 2022-05-04 16:16:40 +0200 | [diff] [blame] | 133 | oauthHint => 'Die folgende Registrierung (und alle Angaben) für API-Clients folgen der <a href="https://oauth.net/" class="external">OAuth-2.0-Spezifikation</a>.', |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 134 | loginHint => 'Möglicherweise müssen sie sich zunächst einloggen.', |
Akron | e997bb5 | 2021-06-11 16:44:06 +0200 | [diff] [blame] | 135 | oauthIssueToken => { |
| 136 | -long => 'Stelle einen neuen Token für <span class="client-name"><%= $client_name %></span> aus', |
| 137 | short => 'Neuen Token ausstellen' |
| 138 | }, |
Akron | 9ffb4a3 | 2021-06-08 16:11:21 +0200 | [diff] [blame] | 139 | accessToken => 'Access Token', |
Akron | e997bb5 | 2021-06-11 16:44:06 +0200 | [diff] [blame] | 140 | oauthRevokeToken => { |
| 141 | -long => 'Widerrufe einen Token für <span class="client-name"><%= $client_name %></span>', |
| 142 | short => 'Widerrufe' |
| 143 | }, |
Akron | a8efaa9 | 2022-04-09 14:45:43 +0200 | [diff] [blame] | 144 | oauthGrantScope => { |
| 145 | -long => '<span class="client-name"><%= $client_name %></span> möchte Zugriffsrechte', |
| 146 | short => 'Zugriffsrechte erteilen' |
| 147 | }, |
Akron | 408bc7c | 2022-04-28 15:46:43 +0200 | [diff] [blame] | 148 | oauthGrantPublicWarn => 'Achtung - dies ist ein öffentlicher Client!', |
Akron | 9d82690 | 2023-01-25 10:20:52 +0100 | [diff] [blame] | 149 | oauthGrantRedirectWarn => 'Die Weiterleitung findet an eine unbekannte Adresse statt', |
Akron | e997bb5 | 2021-06-11 16:44:06 +0200 | [diff] [blame] | 150 | createdAt => 'Erstellt am <time datetime="<%= stash("date") %>"><%= stash("date") %></date>.', |
Akron | 9f2ad34 | 2022-05-04 16:16:40 +0200 | [diff] [blame] | 151 | expiresIn => 'Läuft in <%= stash("seconds") %> Sekunden ab.', |
| 152 | fileSizeExceeded => 'Dateigröße überschritten' |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 153 | }, |
| 154 | -en => { |
Akron | a8efaa9 | 2022-04-09 14:45:43 +0200 | [diff] [blame] | 155 | loginPlease => 'Please log in!', |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 156 | loginSuccess => 'Login successful', |
| 157 | loginFail => 'Access denied', |
| 158 | logoutSuccess => 'Logout successful', |
| 159 | logoutFail => 'Logout failed', |
Akron | ff08811 | 2021-06-15 15:26:04 +0200 | [diff] [blame] | 160 | authenticationFail => 'Not authenticated', |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 161 | csrfFail => 'Bad CSRF token', |
Akron | 2c142ab | 2023-01-30 13:21:57 +0100 | [diff] [blame] | 162 | scopeFail => 'Scope required', |
| 163 | clientIDFail => 'Client ID required', |
Akron | 6a228db | 2021-10-14 15:57:00 +0200 | [diff] [blame] | 164 | invalidChar => 'Invalid character in request', |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 165 | openRedirectFail => 'Redirect failure', |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 166 | tokenExpired => 'Access token expired', |
| 167 | tokenInvalid => 'Access token invalid', |
| 168 | refreshFail => 'Bad refresh token', |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 169 | responseError => 'Unknown authorization error', |
Akron | cce055c | 2021-07-02 12:18:03 +0200 | [diff] [blame] | 170 | serverError => 'Unknown server error', |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 171 | revokeFail => 'Token can\'t be revoked', |
| 172 | revokeSuccess => 'Token was revoked successfully', |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 173 | paramError => 'Some fields are invalid', |
| 174 | redirectUri => 'Redirect URI', |
Akron | 9f2ad34 | 2022-05-04 16:16:40 +0200 | [diff] [blame] | 175 | pluginSrc => 'Declaration of the plugin (*.json file)', |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 176 | homepage => 'Homepage', |
Helge | 9f9d485 | 2024-12-09 16:06:34 +0100 | [diff] [blame] | 177 | homepageReq =>'*(Plugins)', |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 178 | desc => 'Short description', |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 179 | revoke => 'Revoke', |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 180 | clientCredentials => 'Client Credentials', |
| 181 | clientType => 'Type of the client application', |
| 182 | clientName => 'Name of the client application', |
| 183 | clientID => 'ID of the client application', |
| 184 | clientSecret => 'Client secret', |
| 185 | clientRegister => 'Register new client application', |
| 186 | registerSuccess => 'Registration successful', |
| 187 | registerFail => 'Registration denied', |
Akron | b0a7a0f | 2025-02-27 09:51:06 +0100 | [diff] [blame] | 188 | oauthSettings => 'API tokens', |
Helge | 278fbca | 2022-11-29 18:49:15 +0100 | [diff] [blame] | 189 | #for marketplace settings |
| 190 | marketplace => 'Marketplace', |
Helge | db720ea | 2023-03-20 09:39:36 +0100 | [diff] [blame] | 191 | plugins => 'Plugins', |
| 192 | instplugins => 'Installed Plugins', |
| 193 | regby =>'Registered by', |
Helge | 216a482 | 2024-06-17 12:02:34 +0200 | [diff] [blame] | 194 | regdate =>'Date of registration', |
| 195 | instdate =>'Installation date', |
Helge | db720ea | 2023-03-20 09:39:36 +0100 | [diff] [blame] | 196 | install => 'Install', |
| 197 | uninstall => 'Uninstall', |
| 198 | installFail => 'Plugin could not be installed', |
Helge | d36478d | 2023-06-08 17:43:01 +0200 | [diff] [blame] | 199 | uninstallFail => 'Plugin could not be uninstalled', |
| 200 | uninstallFail => 'Plugin could not be uninstalled', |
| 201 | marketplaceFail => { |
| 202 | -long => 'Plugins could not be displayed.', |
| 203 | short => 'Try again' |
| 204 | }, |
Akron | e997bb5 | 2021-06-11 16:44:06 +0200 | [diff] [blame] | 205 | oauthUnregister => { |
| 206 | -long => 'Do you really want to unregister <span class="client-name"><%= $client_name %></span>?', |
| 207 | short => 'Unregister' |
| 208 | }, |
Akron | 9f2ad34 | 2022-05-04 16:16:40 +0200 | [diff] [blame] | 209 | oauthHint => 'The following registration of API clients follows the <a href="https://oauth.net/" class="external">OAuth 2.0 specification</a>.', |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 210 | loginHint => 'Maybe you need to log in first?', |
Akron | e997bb5 | 2021-06-11 16:44:06 +0200 | [diff] [blame] | 211 | oauthIssueToken => { |
| 212 | -long => 'Issue a new token for <span class="client-name"><%= $client_name %></span>', |
| 213 | short => 'Issue new token' |
| 214 | }, |
Akron | 9ffb4a3 | 2021-06-08 16:11:21 +0200 | [diff] [blame] | 215 | accessToken => 'Access Token', |
Akron | e997bb5 | 2021-06-11 16:44:06 +0200 | [diff] [blame] | 216 | oauthRevokeToken => { |
| 217 | -long => 'Revoke a token for <span class="client-name"><%= $client_name %></span>', |
| 218 | short => 'Revoke' |
| 219 | }, |
Akron | a8efaa9 | 2022-04-09 14:45:43 +0200 | [diff] [blame] | 220 | oauthGrantScope => { |
| 221 | -long => '<span class="client-name"><%= $client_name %></span> wants to have access', |
| 222 | short => 'Grant access' |
| 223 | }, |
Akron | 408bc7c | 2022-04-28 15:46:43 +0200 | [diff] [blame] | 224 | oauthGrantPublicWarn => 'Warning - this is a public client!', |
Akron | 9d82690 | 2023-01-25 10:20:52 +0100 | [diff] [blame] | 225 | oauthGrantRedirectWarn => 'The redirect points to an unknown location', |
Akron | e997bb5 | 2021-06-11 16:44:06 +0200 | [diff] [blame] | 226 | createdAt => 'Created at <time datetime="<%= stash("date") %>"><%= stash("date") %></date>.', |
Akron | 9f2ad34 | 2022-05-04 16:16:40 +0200 | [diff] [blame] | 227 | expiresIn => 'Expires in <%= stash("seconds") %> seconds.', |
| 228 | fileSizeExceeded => 'File size exceeded', |
| 229 | confidentialRequired => 'Plugins need to be confidential', |
| 230 | jsonRequired => 'Plugin declarations need to be json files', |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | } |
| 234 | }); |
| 235 | |
| 236 | |
Akron | a9c8b0e | 2018-11-16 20:20:28 +0100 | [diff] [blame] | 237 | # Add login frame to sidebar |
Akron | 4d17d0f | 2025-03-05 20:58:44 +0100 | [diff] [blame^] | 238 | # $app->content_block( |
| 239 | # sidebar => { |
| 240 | # template => 'partial/auth/login' |
| 241 | # } |
| 242 | # ); |
Akron | a9c8b0e | 2018-11-16 20:20:28 +0100 | [diff] [blame] | 243 | |
| 244 | |
Akron | c82b1bc | 2018-11-18 18:06:14 +0100 | [diff] [blame] | 245 | # Add logout button to header button list |
Uyen-Nhu Tran | a17b08d | 2025-02-18 19:14:55 +0100 | [diff] [blame] | 246 | # $app->content_block( |
| 247 | # headerButtonGroup => { |
| 248 | # template => 'partial/auth/logout' |
| 249 | # } |
| 250 | # ); |
Akron | c82b1bc | 2018-11-18 18:06:14 +0100 | [diff] [blame] | 251 | |
Akron | 27031aa | 2020-04-28 14:57:10 +0200 | [diff] [blame] | 252 | |
| 253 | # Add hook after search |
| 254 | $app->hook( |
| 255 | after_search => sub { |
| 256 | my $c = shift; |
| 257 | |
| 258 | # User is not logged in |
| 259 | if ($c->stash('results')->size == 0 && !$c->auth->token) { |
| 260 | $c->content_for( |
| 261 | 'after_search_results' => |
| 262 | $c->render_to_string( |
| 263 | inline => '<p class="hint"><%= loc "Auth_loginHint" %></p>' |
| 264 | ) |
| 265 | ); |
| 266 | }; |
| 267 | } |
| 268 | ); |
| 269 | |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 270 | # The plugin path |
| 271 | my $path = catdir(dirname(__FILE__), 'Auth'); |
| 272 | |
| 273 | # Append "templates" |
| 274 | push @{$app->renderer->paths}, catdir($path, 'templates'); |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 275 | |
Akron | 4796e00 | 2019-07-05 10:13:15 +0200 | [diff] [blame] | 276 | # Get or set the user token necessary for authorization |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 277 | $app->helper( |
| 278 | 'auth.token' => sub { |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 279 | my ($c, $token, $expires_in) = @_; |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 280 | |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 281 | if ($token) { |
| 282 | # Set auth token |
Akron | 4796e00 | 2019-07-05 10:13:15 +0200 | [diff] [blame] | 283 | $c->stash(auth => $token); |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 284 | $c->session(auth => $token); |
| 285 | $c->session(auth_exp => time + $expires_in); |
| 286 | return 1; |
Akron | 4796e00 | 2019-07-05 10:13:15 +0200 | [diff] [blame] | 287 | }; |
| 288 | |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 289 | # Get token from stash |
| 290 | $token = $c->stash('auth'); |
| 291 | |
| 292 | return $token if $token; |
| 293 | |
| 294 | # Get auth from session |
| 295 | $token = $c->session('auth') or return; |
| 296 | $c->stash(auth => $token); |
| 297 | |
| 298 | # Return stashed value |
| 299 | return $token; |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 300 | } |
| 301 | ); |
| 302 | |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 303 | # Log in to the system |
| 304 | my $r = $app->routes; |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 305 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 306 | my $client_id = $param->{client_id}; |
| 307 | my $client_secret = $param->{client_secret}; |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 308 | |
Akron | e3daaeb | 2023-05-08 09:44:18 +0200 | [diff] [blame] | 309 | my $no_redirect_ua = Mojo::UserAgent->new( |
| 310 | connect_timeout => 30, |
| 311 | inactivity_timeout => 30, |
| 312 | max_redirects => 0 |
| 313 | ); |
| 314 | |
| 315 | $no_redirect_ua->server->app($app); |
| 316 | |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 317 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 318 | # Sets a requested token and returns |
| 319 | # an error, if it didn't work |
| 320 | $app->helper( |
| 321 | 'auth.set_tokens_p' => sub { |
| 322 | my ($c, $json) = @_; |
| 323 | my $promise = Mojo::Promise->new; |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 324 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 325 | # No json object |
| 326 | unless ($json) { |
| 327 | return $promise->reject({ |
| 328 | message => 'Response is no valid JSON object (remote)' |
| 329 | }); |
| 330 | }; |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 331 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 332 | # There is an error here |
| 333 | # Dealing with errors here |
| 334 | if ($json->{error} && ref $json->{error} ne 'ARRAY') { |
| 335 | return $promise->reject( |
| 336 | { |
| 337 | message => $json->{error} . ($json->{error_description} ? ': ' . $json->{error_description} : '') |
Akron | 0f1b93b | 2020-03-17 11:37:19 +0100 | [diff] [blame] | 338 | } |
| 339 | ); |
| 340 | } |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 341 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 342 | # There is an array of errors |
| 343 | elsif (my $error = $json->{errors} // $json->{error}) { |
| 344 | if (ref $error eq 'ARRAY') { |
| 345 | my @errors = (); |
| 346 | foreach (@{$error}) { |
| 347 | if ($_->[1]) { |
| 348 | push @errors, { code => $_->[0], message => $_->[1]} |
Akron | bc94a9c | 2021-04-15 00:07:35 +0200 | [diff] [blame] | 349 | }; |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 350 | }; |
| 351 | return $promise->reject(@errors); |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 352 | }; |
| 353 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 354 | return $promise->reject({message => $error}); |
| 355 | }; |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 356 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 357 | # Everything is fine |
| 358 | my $access_token = $json->{access_token}; |
| 359 | my $token_type = $json->{token_type}; |
| 360 | my $refresh_token = $json->{refresh_token}; |
| 361 | my $expires_in = $json->{"expires_in"} // $EXPECTED_EXPIRATION_IN; |
| 362 | my $auth = $token_type . ' ' . $access_token; |
| 363 | # my $scope = $json->{scope}; |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 364 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 365 | # Set session info |
| 366 | $c->session(auth => $auth); |
| 367 | |
| 368 | # Expiration of the token minus tolerance |
| 369 | $c->session(auth_exp => time + $expires_in - 60); |
| 370 | |
| 371 | # Set session info for refresh token |
| 372 | # This can be stored in the session, as it is useless |
| 373 | # unless the client secret is stolen |
| 374 | $c->session(auth_r => $refresh_token) if $refresh_token; |
| 375 | |
| 376 | # Set stash info |
| 377 | $c->stash(auth => $auth); |
| 378 | |
| 379 | return $promise->resolve; |
| 380 | } |
| 381 | ); |
| 382 | |
| 383 | |
| 384 | # Refresh tokens and return a promise |
| 385 | $app->helper( |
| 386 | 'auth.refresh_p' => sub { |
| 387 | my $c = shift; |
| 388 | my $refresh_token = shift; |
| 389 | |
| 390 | # Get OAuth access token |
| 391 | state $r_url = Mojo::URL->new($c->korap->api)->path('oauth2/token'); |
| 392 | |
| 393 | $c->app->log->debug("Refresh at $r_url"); |
| 394 | |
| 395 | return $c->kalamar_ua->post_p($r_url, {} => form => { |
| 396 | grant_type => 'refresh_token', |
| 397 | client_id => $client_id, |
| 398 | client_secret => $client_secret, |
| 399 | refresh_token => $refresh_token |
| 400 | })->then( |
| 401 | sub { |
| 402 | my $tx = shift; |
| 403 | my $json = $tx->result->json; |
| 404 | |
| 405 | # Response is fine |
| 406 | if ($tx->res->is_success) { |
| 407 | |
| 408 | $c->app->log->info("Refresh was successful"); |
| 409 | |
| 410 | # Set the tokens and return a promise |
| 411 | return $c->auth->set_tokens_p($json); |
| 412 | }; |
| 413 | |
| 414 | # There is a client error - refresh fails |
| 415 | if ($tx->res->is_client_error && $json) { |
| 416 | |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 417 | $c->stash(auth => undef); |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 418 | $c->stash(auth_exp => undef); |
| 419 | delete $c->session->{user}; |
| 420 | delete $c->session->{auth}; |
| 421 | delete $c->session->{auth_r}; |
| 422 | delete $c->session->{auth_exp}; |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 423 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 424 | # Response is 400 |
| 425 | return Mojo::Promise->reject( |
| 426 | $json->{error_description} // $c->loc('Auth_refreshFail') |
| 427 | ); |
| 428 | }; |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 429 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 430 | if ($tx->res->is_server_error) { |
| 431 | return Mojo::Promise->reject( |
| 432 | '600' |
| 433 | ) |
| 434 | }; |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 435 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 436 | $c->notify(error => $c->loc('Auth_responseError')); |
| 437 | return Mojo::Promise->reject; |
| 438 | } |
| 439 | ) |
| 440 | } |
| 441 | ); |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 442 | |
Helge | 278fbca | 2022-11-29 18:49:15 +0100 | [diff] [blame] | 443 | |
Akron | e3daaeb | 2023-05-08 09:44:18 +0200 | [diff] [blame] | 444 | # Issue new token and return a promise |
| 445 | $app->helper( |
| 446 | 'auth.new_token_p' => sub { |
| 447 | my $c = shift; |
| 448 | my %param = @_; |
| 449 | |
| 450 | state $r_url = Mojo::URL->new($c->korap->api)->path('oauth2/authorize'); |
| 451 | |
| 452 | my $client_id = $param{'client_id'}; |
| 453 | |
| 454 | return $c->korap_request($no_redirect_ua, post => $r_url, { } => form => { |
| 455 | response_type => 'code', |
| 456 | client_id => $client_id, |
| 457 | redirect_uri => $param{'redirect_uri'}, |
| 458 | state => $param{'state'}, |
| 459 | scope => $param{'scope'}, |
| 460 | })->then( |
| 461 | sub { |
| 462 | my $tx = shift; |
| 463 | |
| 464 | unless (ref($tx)) { |
| 465 | return Mojo::Promise->reject('Something went wrong'); |
| 466 | }; |
| 467 | |
| 468 | # Check for location header with code in redirects |
| 469 | my ($code, $scope, $loc, $name); |
| 470 | |
| 471 | # Check for location header with code in current tx |
| 472 | # and in redirects. |
| 473 | # The loop should not be relevant though, as for now |
| 474 | # redirects are not allowed. |
| 475 | foreach ($tx, @{$tx->redirects}) { |
| 476 | $loc = $_->res->headers->header('Location'); |
| 477 | |
| 478 | next unless $loc; |
| 479 | |
| 480 | my $url = Mojo::URL->new($loc); |
| 481 | |
| 482 | if ($url->query->param('code')) { |
| 483 | my $q = $url->query; |
| 484 | $code = $q->param('code'); |
| 485 | $scope = $q->param('scope'); |
| 486 | $name = $q->param('name'); |
| 487 | last; |
| 488 | } elsif (my $err = $url->query->param('error_description')) { |
| 489 | return Mojo::Promise->reject($err); |
| 490 | } |
| 491 | }; |
| 492 | |
| 493 | return Mojo::Promise->resolve( |
| 494 | $loc, |
| 495 | $client_id, |
| 496 | $param{'redirect_uri'}, |
| 497 | $code, |
| 498 | $scope, |
| 499 | $name |
| 500 | ) if $loc; |
| 501 | |
| 502 | # Failed redirect, but location set |
| 503 | if ($tx->res->headers->location) { |
| 504 | my $url = Mojo::URL->new($tx->res->headers->location); |
| 505 | if (my $err = $url->query->param('error_description')) { |
| 506 | return Mojo::Promise->reject($err); |
| 507 | }; |
| 508 | } |
| 509 | |
| 510 | $c->stash(redirect_uri => undef); |
| 511 | |
| 512 | # Maybe json |
| 513 | my $json = $tx->res->json; |
| 514 | if ($json && $json->{error_description}) { |
| 515 | return Mojo::Promise->reject($json->{error_description}); |
| 516 | }; |
| 517 | |
| 518 | # No location code |
| 519 | return Mojo::Promise->reject('no location response'); |
| 520 | } |
| 521 | ) |
| 522 | } |
| 523 | ); |
| 524 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 525 | # Get a list of registered clients |
| 526 | $app->helper( |
| 527 | 'auth.client_list_p' => sub { |
| 528 | my $c = shift; |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 529 | |
Helge | 278fbca | 2022-11-29 18:49:15 +0100 | [diff] [blame] | 530 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 531 | # Get list of registered clients |
| 532 | state $r_url = Mojo::URL->new($c->korap->api)->path('oauth2/client/list'); |
Akron | a8efaa9 | 2022-04-09 14:45:43 +0200 | [diff] [blame] | 533 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 534 | # Get the list of all clients |
| 535 | return $c->korap_request(post => $r_url, {} => form => { |
| 536 | super_client_id => $client_id, |
| 537 | super_client_secret => $client_secret, |
Helge | 0543670 | 2024-08-05 17:08:44 +0200 | [diff] [blame] | 538 | filter_by => 'owned_only' |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 539 | })->then( |
| 540 | sub { |
| 541 | my $tx = shift; |
| 542 | my $json = $tx->result->json; |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 543 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 544 | # Response is fine |
| 545 | if ($tx->res->is_success) { |
| 546 | return Mojo::Promise->resolve($json); |
| 547 | }; |
| 548 | |
Helge | 690e94d | 2023-04-19 16:01:24 +0200 | [diff] [blame] | 549 | $c->log->error($tx->res->to_string); |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 550 | |
| 551 | # Failure |
| 552 | $c->notify(error => $c->loc('Auth_responseError')); |
| 553 | return Mojo::Promise->reject($json // 'No response'); |
| 554 | } |
| 555 | ); |
| 556 | } |
| 557 | ); |
| 558 | |
Akron | db1f467 | 2023-01-24 12:05:07 +0100 | [diff] [blame] | 559 | # Get info for registered client |
| 560 | $app->helper( |
| 561 | 'auth.client_info_p' => sub { |
| 562 | my $c = shift; |
Akron | 9d82690 | 2023-01-25 10:20:52 +0100 | [diff] [blame] | 563 | my $req_client_id = shift; |
Akron | db1f467 | 2023-01-24 12:05:07 +0100 | [diff] [blame] | 564 | |
| 565 | # Get list of registered clients |
Akron | 9d82690 | 2023-01-25 10:20:52 +0100 | [diff] [blame] | 566 | my $r_url = Mojo::URL->new($c->korap->api)->path('oauth2/client/')->path($req_client_id); |
Akron | db1f467 | 2023-01-24 12:05:07 +0100 | [diff] [blame] | 567 | |
| 568 | my $form = { |
| 569 | super_client_id => $client_id, |
| 570 | super_client_secret => $client_secret, |
| 571 | }; |
| 572 | |
| 573 | # Get the list of all clients |
| 574 | return $c->korap_request(POST => $r_url, {} => form => $form)->then( |
| 575 | sub { |
| 576 | my $tx = shift; |
| 577 | my $json = $tx->result->json // {}; |
| 578 | |
| 579 | # Response is fine |
| 580 | if ($tx->res->is_success) { |
| 581 | return Mojo::Promise->resolve($json); |
| 582 | }; |
| 583 | |
Helge | 690e94d | 2023-04-19 16:01:24 +0200 | [diff] [blame] | 584 | $c->log->error($tx->res->to_string); |
Akron | db1f467 | 2023-01-24 12:05:07 +0100 | [diff] [blame] | 585 | |
| 586 | # Failure |
| 587 | return Mojo::Promise->reject($json->{error_description} // 'Client unknown'); |
| 588 | } |
| 589 | ); |
| 590 | } |
| 591 | ); |
| 592 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 593 | |
| 594 | # Get a list of registered clients |
| 595 | $app->helper( |
| 596 | 'auth.token_list_p' => sub { |
| 597 | my $c = shift; |
| 598 | my $user_client_id = shift; |
| 599 | |
| 600 | # Revoke the token |
| 601 | state $r_url = Mojo::URL->new($c->korap->api)->path('oauth2/token/list'); |
| 602 | |
| 603 | my $form = { |
| 604 | super_client_id => $client_id, |
| 605 | super_client_secret => $client_secret, |
| 606 | token_type => 'access_token', |
| 607 | }; |
| 608 | |
| 609 | if ($user_client_id) { |
| 610 | $form->{client_id} = $user_client_id; |
| 611 | }; |
| 612 | |
| 613 | # Get the list of all clients |
| 614 | return $c->korap_request(post => $r_url, {} => form => $form)->then( |
| 615 | sub { |
| 616 | my $tx = shift; |
| 617 | my $json = $tx->result->json; |
| 618 | |
| 619 | # Response is fine |
| 620 | if ($tx->res->is_success) { |
| 621 | return Mojo::Promise->resolve($json); |
| 622 | }; |
| 623 | |
Helge | 690e94d | 2023-04-19 16:01:24 +0200 | [diff] [blame] | 624 | $c->log->error($tx->res->to_string); |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 625 | |
| 626 | # Failure |
| 627 | $c->notify(error => $c->loc('Auth_responseError')); |
| 628 | return Mojo::Promise->reject($json // 'No response'); |
| 629 | } |
| 630 | ); |
| 631 | } |
| 632 | ); |
| 633 | |
| 634 | |
| 635 | # Issue a korap request with "oauth"orization |
| 636 | # This will override the core request helper |
| 637 | $app->helper( |
| 638 | korap_request => sub { |
| 639 | my $c = shift; |
Akron | a8f87cc | 2023-02-23 12:21:30 +0100 | [diff] [blame] | 640 | |
| 641 | # Get plugin user agent |
| 642 | my $ua = $c->kalamar_ua; |
| 643 | |
| 644 | # Override if UA is granted |
| 645 | if (ref $_[0] eq 'Mojo::UserAgent') { |
| 646 | $ua = shift; |
| 647 | }; |
| 648 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 649 | my $method = shift; |
| 650 | my $path = shift; |
Akron | a8f87cc | 2023-02-23 12:21:30 +0100 | [diff] [blame] | 651 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 652 | my @param = @_; |
| 653 | |
| 654 | # TODO: |
| 655 | # Check if $tx is not leaked! |
| 656 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 657 | my $url = Mojo::URL->new($path); |
| 658 | my $tx = $ua->build_tx(uc($method), $url->clone, @param); |
| 659 | |
| 660 | # Set X-Forwarded for |
| 661 | $tx->req->headers->header( |
| 662 | 'X-Forwarded-For' => $c->client_ip |
| 663 | ); |
| 664 | |
| 665 | # Emit Hook to alter request |
| 666 | $c->app->plugins->emit_hook( |
| 667 | before_korap_request => ($c, $tx) |
| 668 | ); |
| 669 | |
| 670 | my $h = $tx->req->headers; |
| 671 | |
| 672 | # If the request already has an Authorization |
| 673 | # header, respect it! |
| 674 | if ($h->authorization) { |
| 675 | return $ua->start_p($tx); |
| 676 | }; |
| 677 | |
| 678 | # Get auth token |
| 679 | if (my $auth_token = $c->auth->token) { |
| 680 | |
| 681 | # The token is already expired! |
| 682 | my $exp = $c->session('auth_exp'); |
| 683 | if (defined $exp && $exp < time) { |
| 684 | |
| 685 | # Remove auth ... |
| 686 | $c->stash(auth => undef); |
| 687 | |
| 688 | # And get refresh token from session |
| 689 | if (my $refresh_token = $c->session('auth_r')) { |
| 690 | |
| 691 | $c->app->log->debug("Refresh is required"); |
| 692 | |
| 693 | # Refresh |
| 694 | return $c->auth->refresh_p($refresh_token)->then( |
| 695 | sub { |
| 696 | $c->app->log->debug("Search with refreshed tokens"); |
| 697 | |
| 698 | # Tokens were set - now send the request the first time! |
| 699 | $tx->req->headers->authorization($c->stash('auth')); |
| 700 | return $ua->start_p($tx); |
| 701 | } |
| 702 | ); |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 703 | } |
| 704 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 705 | # The token is expired and no refresh token is |
| 706 | # available - issue an unauthorized request! |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 707 | else { |
| 708 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 709 | $c->stash(auth => undef); |
| 710 | $c->stash(auth_exp => undef); |
| 711 | delete $c->session->{user}; |
| 712 | delete $c->session->{auth}; |
| 713 | delete $c->session->{auth_r}; |
| 714 | delete $c->session->{auth_exp}; |
| 715 | |
| 716 | # Warn on Error! |
| 717 | $c->notify(warn => $c->loc('Auth_tokenExpired')); |
| 718 | return $ua->start_p($tx); |
| 719 | }; |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 720 | } |
| 721 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 722 | # Auth token is fine |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 723 | else { |
| 724 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 725 | # Set auth |
| 726 | $h->authorization($auth_token); |
| 727 | } |
| 728 | } |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 729 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 730 | # No token set |
| 731 | else { |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 732 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 733 | # Return unauthorized request |
| 734 | return $ua->start_p($tx); |
| 735 | }; |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 736 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 737 | # Issue an authorized request and automatically |
| 738 | # refresh the token on expiration! |
| 739 | return $ua->start_p($tx)->then( |
| 740 | sub { |
| 741 | my $tx = shift; |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 742 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 743 | # Response is fine |
Akron | a8f87cc | 2023-02-23 12:21:30 +0100 | [diff] [blame] | 744 | if ($tx->res->is_success || $tx->res->is_redirect) { |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 745 | return Mojo::Promise->resolve($tx); |
| 746 | } |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 747 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 748 | # There is a client error - maybe refresh! |
| 749 | elsif ($tx->res->is_client_error) { |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 750 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 751 | # Check the error |
| 752 | my $json = $tx->res->json('/errors/0/1'); |
| 753 | if ($json && ($json =~ /expired|invalid/)) { |
| 754 | $c->stash(auth => undef); |
| 755 | $c->stash(auth_exp => undef); |
| 756 | delete $c->session->{user}; |
| 757 | delete $c->session->{auth}; |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 758 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 759 | # And get refresh token from session |
| 760 | if (my $refresh_token = $c->session('auth_r')) { |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 761 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 762 | # Refresh |
| 763 | return $c->auth->refresh_p($refresh_token)->then( |
| 764 | sub { |
| 765 | $c->app->log->debug("Search with refreshed tokens"); |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 766 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 767 | my $tx = $ua->build_tx(uc($method), $url->clone, @param); |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 768 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 769 | # Set X-Forwarded for |
| 770 | $tx->req->headers->header( |
| 771 | 'X-Forwarded-For' => $c->client_ip |
| 772 | ); |
| 773 | |
| 774 | # Tokens were set - now send the request the first time! |
| 775 | $tx->req->headers->authorization($c->stash('auth')); |
| 776 | return $ua->start_p($tx); |
| 777 | } |
| 778 | ) |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 779 | }; |
| 780 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 781 | # Reject the invalid token |
| 782 | $c->notify(error => $c->loc('Auth_tokenInvalid')); |
| 783 | return Mojo::Promise->reject; |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 784 | }; |
| 785 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 786 | return Mojo::Promise->resolve($tx); |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 787 | } |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 788 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 789 | # There is a server error - just report |
| 790 | elsif ($tx->res->is_server_error) { |
| 791 | my $err = $tx->res->error; |
| 792 | if ($err) { |
| 793 | return Mojo::Promise->reject($err->{code} . ': ' . $err->{message}); |
| 794 | } |
| 795 | else { |
| 796 | $c->notify(error => $c->loc('Auth_serverError')); |
| 797 | return Mojo::Promise->reject; |
| 798 | }; |
| 799 | }; |
| 800 | |
| 801 | $c->notify(error => $c->loc('Auth_responseError')); |
| 802 | return Mojo::Promise->reject; |
| 803 | } |
| 804 | ); |
| 805 | } |
| 806 | ); |
| 807 | |
| 808 | # Password flow for OAuth |
| 809 | $r->post('/user/login')->to( |
| 810 | cb => sub { |
| 811 | my $c = shift; |
| 812 | |
| 813 | # Validate input |
| 814 | my $v = $c->validation; |
| 815 | $v->required('handle_or_email', 'trim'); |
| 816 | $v->required('pwd', 'trim'); |
| 817 | $v->csrf_protect; |
| 818 | $v->optional('fwd')->closed_redirect; |
| 819 | |
| 820 | my $user = check_decode($v->param('handle_or_email')); |
| 821 | unless ($user) { |
| 822 | $c->notify(error => $c->loc('Auth_invalidChar')); |
| 823 | $c->param(handle_or_email => ''); |
| 824 | return $c->relative_redirect_to('index'); |
| 825 | }; |
| 826 | |
| 827 | my $fwd = $v->param('fwd'); |
| 828 | |
| 829 | # Set flash for redirect |
| 830 | $c->flash(handle_or_email => $user); |
| 831 | |
| 832 | if ($v->has_error || index($user, ':') >= 0) { |
| 833 | if ($v->has_error('fwd')) { |
| 834 | $c->notify(error => $c->loc('Auth_openRedirectFail')); |
| 835 | } |
| 836 | elsif ($v->has_error('csrf_token')) { |
| 837 | $c->notify(error => $c->loc('Auth_csrfFail')); |
| 838 | } |
| 839 | else { |
| 840 | $c->notify(error => $c->loc('Auth_loginFail')); |
| 841 | }; |
| 842 | |
| 843 | return $c->relative_redirect_to($fwd // 'index'); |
| 844 | } |
| 845 | |
| 846 | my $pwd = $v->param('pwd'); |
| 847 | |
| 848 | $c->app->log->debug("Login from user $user"); |
| 849 | |
| 850 | # <specific> |
| 851 | |
| 852 | # Get OAuth access token |
| 853 | my $url = Mojo::URL->new($c->korap->api)->path('oauth2/token'); |
| 854 | |
| 855 | # Korap request for login |
| 856 | $c->korap_request('post', $url, {}, form => { |
| 857 | grant_type => 'password', |
| 858 | username => $user, |
| 859 | password => $pwd, |
| 860 | client_id => $client_id, |
| 861 | client_secret => $client_secret |
| 862 | })->then( |
| 863 | sub { |
| 864 | # Set the tokens and return a promise |
| 865 | return $c->auth->set_tokens_p(shift->result->json) |
| 866 | } |
| 867 | )->then( |
| 868 | sub { |
| 869 | # Set user info |
| 870 | $c->session(user => $user); |
| 871 | $c->stash(user => $user); |
| 872 | |
| 873 | # Notify on success |
| 874 | $c->app->log->debug(qq!Login successful: "$user"!); |
| 875 | $c->notify(success => $c->loc('Auth_loginSuccess')); |
| 876 | } |
| 877 | )->catch( |
| 878 | sub { |
| 879 | |
| 880 | # Notify the user on login failure |
| 881 | unless (@_) { |
| 882 | $c->notify(error => $c->loc('Auth_loginFail')); |
| 883 | } |
| 884 | |
| 885 | # There are known errors |
| 886 | foreach (@_) { |
| 887 | if (ref $_ eq 'HASH') { |
| 888 | my $err = ($_->{code} ? $_->{code} . ': ' : '') . |
| 889 | $_->{message}; |
| 890 | $c->notify(error => $err); |
| 891 | # Log failure |
| 892 | $c->app->log->debug($err); |
| 893 | } |
| 894 | else { |
| 895 | $c->notify(error => $_); |
| 896 | $c->app->log->debug($_); |
| 897 | }; |
| 898 | }; |
| 899 | |
| 900 | $c->app->log->debug(qq!Login fail: "$user"!); |
| 901 | } |
| 902 | )->finally( |
| 903 | sub { |
| 904 | # Redirect to slash |
| 905 | return $c->relative_redirect_to($fwd // 'index'); |
| 906 | } |
| 907 | ) |
| 908 | |
| 909 | # Start IOLoop |
| 910 | ->wait; |
| 911 | |
| 912 | return 1; |
| 913 | } |
| 914 | )->name('login'); |
| 915 | |
| 916 | |
| 917 | # Log out of the session |
| 918 | $r->get('/user/logout')->to( |
| 919 | cb => sub { |
| 920 | my $c = shift; |
| 921 | |
| 922 | # TODO: csrf-protection! |
| 923 | |
| 924 | my $refresh_token = $c->session('auth_r'); |
| 925 | |
| 926 | # Revoke the token |
| 927 | state $url = Mojo::URL->new($c->korap->api)->path('oauth2/revoke'); |
| 928 | |
| 929 | $c->kalamar_ua->post_p($url => {} => form => { |
| 930 | client_id => $client_id, |
| 931 | client_secret => $client_secret, |
| 932 | token => $refresh_token, |
| 933 | token_type => 'refresh_token' |
| 934 | })->then( |
| 935 | sub { |
| 936 | my $tx = shift; |
| 937 | my $json = $tx->result->json; |
| 938 | |
| 939 | my $promise; |
| 940 | |
| 941 | # Response is fine |
| 942 | if ($tx->res->is_success) { |
| 943 | $c->app->log->info("Revocation was successful"); |
| 944 | $c->notify(success => $c->loc('Auth_logoutSuccess')); |
| 945 | |
| 946 | $c->stash(auth => undef); |
| 947 | $c->stash(auth_exp => undef); |
| 948 | $c->flash(handle_or_email => delete $c->session->{user}); |
| 949 | delete $c->session->{auth}; |
| 950 | delete $c->session->{auth_r}; |
| 951 | delete $c->session->{auth_exp}; |
| 952 | return Mojo::Promise->resolve; |
| 953 | }; |
| 954 | |
| 955 | # Token may be invalid |
| 956 | $c->notify('error', $c->loc('Auth_logoutFail')); |
| 957 | |
| 958 | # There is a client error - refresh fails |
| 959 | if ($tx->res->is_client_error && $json) { |
| 960 | |
| 961 | return Mojo::Promise->reject( |
| 962 | $json->{error_description} |
| 963 | ); |
| 964 | }; |
| 965 | |
| 966 | # Resource may not be found (404) |
| 967 | return Mojo::Promise->reject |
| 968 | |
| 969 | } |
| 970 | )->catch( |
| 971 | sub { |
| 972 | my $err = shift; |
| 973 | |
| 974 | # Server may be irresponsible |
| 975 | $c->notify('error', $c->loc('Auth_logoutFail')); |
| 976 | return Mojo::Promise->reject($err); |
| 977 | } |
| 978 | )->finally( |
| 979 | sub { |
| 980 | return $c->redirect_to('index'); |
| 981 | } |
| 982 | )->wait; |
| 983 | } |
| 984 | )->name('logout'); |
| 985 | |
| 986 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 987 | # Add settings |
| 988 | $app->navi->add(settings => ( |
| 989 | $app->loc('Auth_oauthSettings'), 'oauth' |
| 990 | )); |
Helge | 278fbca | 2022-11-29 18:49:15 +0100 | [diff] [blame] | 991 | |
Helge | 4a53a9b | 2023-07-18 18:40:49 +0200 | [diff] [blame] | 992 | # Get configuration for marketplace settings |
| 993 | if ($param->{marketplace}) { |
| 994 | $app->navi->add(settings => ( |
| 995 | $app->loc('Auth_marketplace'), 'marketplace' |
| 996 | )); |
| 997 | } |
Helge | 278fbca | 2022-11-29 18:49:15 +0100 | [diff] [blame] | 998 | |
| 999 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1000 | # Helper: Returns lists of registered plugins (of all users), which are permitted |
| 1001 | $app->helper( |
Helge | db720ea | 2023-03-20 09:39:36 +0100 | [diff] [blame] | 1002 | 'auth.plugin_list_p' => sub { |
Helge | 278fbca | 2022-11-29 18:49:15 +0100 | [diff] [blame] | 1003 | my $c = shift; |
Helge | db720ea | 2023-03-20 09:39:36 +0100 | [diff] [blame] | 1004 | state $l_url = Mojo::URL->new($c->korap->api)->path('plugins'); |
| 1005 | return $c->korap_request(post => $l_url, {} => form => { |
Helge | 278fbca | 2022-11-29 18:49:15 +0100 | [diff] [blame] | 1006 | super_client_id => $client_id, |
| 1007 | super_client_secret => $client_secret, |
| 1008 | #list only permitted plugins |
| 1009 | permitted_only => 'true' |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1010 | })->then( |
| 1011 | sub { |
| 1012 | my $tx = shift; |
| 1013 | my $json = $tx->result->json; |
| 1014 | # Response is fine |
| 1015 | if ($tx->res->is_success) { |
Helge | 278fbca | 2022-11-29 18:49:15 +0100 | [diff] [blame] | 1016 | return Mojo::Promise->resolve($json); |
| 1017 | }; |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1018 | $c->log->error($tx->res->to_string); |
Helge | 278fbca | 2022-11-29 18:49:15 +0100 | [diff] [blame] | 1019 | # Failure |
| 1020 | $c->notify(error => $c->loc('Auth_responseError')); |
| 1021 | return Mojo::Promise->reject($json // 'No response'); |
| 1022 | } |
| 1023 | ); |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1024 | } |
| 1025 | ); |
Helge | 278fbca | 2022-11-29 18:49:15 +0100 | [diff] [blame] | 1026 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1027 | |
| 1028 | #Helper: Returns list of all plugins, which are already installed |
| 1029 | $app->helper( |
| 1030 | 'auth.plugin_listin_p' => sub { |
| 1031 | my $c = shift; |
| 1032 | state $i_url = Mojo::URL->new($c->korap->api)->path('plugins/installed'); |
| 1033 | return $c->korap_request(post => $i_url, {} => form => { |
| 1034 | super_client_id => $client_id, |
| 1035 | super_client_secret => $client_secret, |
| 1036 | })->then( |
| 1037 | sub { |
| 1038 | my $tx = shift; |
| 1039 | my $json = $tx->result->json; |
| 1040 | # Response is fine |
| 1041 | if ($tx->res->is_success) { |
| 1042 | return Mojo::Promise->resolve($json); |
Helge | db720ea | 2023-03-20 09:39:36 +0100 | [diff] [blame] | 1043 | }; |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1044 | |
Helge | db720ea | 2023-03-20 09:39:36 +0100 | [diff] [blame] | 1045 | $c->log->error($tx->res->to_string); |
Akron | a278b31 | 2023-05-08 11:19:13 +0200 | [diff] [blame] | 1046 | |
Helge | db720ea | 2023-03-20 09:39:36 +0100 | [diff] [blame] | 1047 | # Failure |
| 1048 | $c->notify(error => $c->loc('Auth_responseError')); |
| 1049 | return Mojo::Promise->reject($json // 'No response'); |
| 1050 | } |
| 1051 | ); |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1052 | } |
| 1053 | ); |
Helge | db720ea | 2023-03-20 09:39:36 +0100 | [diff] [blame] | 1054 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1055 | # Route to marketplace (for installation and deinstallation of plugins) |
| 1056 | $r->get('/settings/marketplace')->to( |
| 1057 | cb => sub { |
| 1058 | my $c = shift; |
| 1059 | _set_no_cache($c->res->headers); |
| 1060 | unless ($c->auth->token) { |
| 1061 | return $c->render( |
| 1062 | template => 'exception', |
| 1063 | msg => $c->loc('Auth_authenticationFail'), |
| 1064 | status => 401 |
| 1065 | ); |
| 1066 | }; |
Helge | 278fbca | 2022-11-29 18:49:15 +0100 | [diff] [blame] | 1067 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1068 | $c->render_later; |
Helge | db720ea | 2023-03-20 09:39:36 +0100 | [diff] [blame] | 1069 | my $promiselist = $c->auth->plugin_list_p; |
| 1070 | my $promiseinlist = $c->auth->plugin_listin_p; |
Helge | d36478d | 2023-06-08 17:43:01 +0200 | [diff] [blame] | 1071 | my $fl = 0; |
Helge | db720ea | 2023-03-20 09:39:36 +0100 | [diff] [blame] | 1072 | Mojo::Promise->all($promiselist, $promiseinlist)-> then( |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1073 | sub { |
Helge | db720ea | 2023-03-20 09:39:36 +0100 | [diff] [blame] | 1074 | my ($promiselist, $promiseinlist) = @_; |
| 1075 | my $plist = ref($promiselist->[0]) eq 'ARRAY' ? $promiselist->[0] : []; |
| 1076 | my $plinlist = ref($promiseinlist->[0]) eq 'ARRAY' ? $promiseinlist->[0] : []; |
| 1077 | my $clean_pllist = $plist; |
| 1078 | $c->stash('pluginsin_list', $plinlist); |
| 1079 | if($plinlist){ |
| 1080 | foreach my $entry (@$plinlist){ |
| 1081 | @$clean_pllist = grep{!($_->{client_id} eq $entry->{client_id})} @$clean_pllist ; |
Helge | db720ea | 2023-03-20 09:39:36 +0100 | [diff] [blame] | 1082 | } |
Helge | 278fbca | 2022-11-29 18:49:15 +0100 | [diff] [blame] | 1083 | } |
Helge | d36478d | 2023-06-08 17:43:01 +0200 | [diff] [blame] | 1084 | $c->stash('plugin_list', $clean_pllist); |
| 1085 | } |
| 1086 | ) |
| 1087 | ->catch( |
| 1088 | sub { |
| 1089 | $fl = 1; |
| 1090 | } |
| 1091 | ) |
| 1092 | ->finally( |
| 1093 | sub { |
| 1094 | if($fl){ |
| 1095 | return $c->render(template => 'auth/marketplace-fail') |
| 1096 | } |
| 1097 | return $c->render(template => 'auth/marketplace'); |
| 1098 | } |
| 1099 | )->wait; |
| 1100 | } |
| 1101 | )->name('marketplace'); |
Helge | 278fbca | 2022-11-29 18:49:15 +0100 | [diff] [blame] | 1102 | |
| 1103 | |
Helge | d36478d | 2023-06-08 17:43:01 +0200 | [diff] [blame] | 1104 | # Route to install plugin |
| 1105 | $r->post('/settings/marketplace/install')->to( |
| 1106 | cb => sub { |
| 1107 | my $c = shift; |
| 1108 | _set_no_cache($c->res->headers); |
| 1109 | my $v = $c->validation; |
| 1110 | $v->required('client-id'); |
| 1111 | |
| 1112 | if ($v->has_error) { |
| 1113 | return $c->render( |
Helge | db720ea | 2023-03-20 09:39:36 +0100 | [diff] [blame] | 1114 | json => [], |
| 1115 | status => 400 |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1116 | ); |
| 1117 | }; |
| 1118 | |
| 1119 | unless ($c->auth->token) { |
| 1120 | return $c->render( |
Helge | db720ea | 2023-03-20 09:39:36 +0100 | [diff] [blame] | 1121 | content => 'Unauthorized', |
| 1122 | status => 401 |
Helge | db720ea | 2023-03-20 09:39:36 +0100 | [diff] [blame] | 1123 | ); |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1124 | }; |
Helge | db720ea | 2023-03-20 09:39:36 +0100 | [diff] [blame] | 1125 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1126 | my $mclient_id = $v->param('client-id'); |
| 1127 | $c->render_later; |
Helge | 278fbca | 2022-11-29 18:49:15 +0100 | [diff] [blame] | 1128 | |
Helge | d36478d | 2023-06-08 17:43:01 +0200 | [diff] [blame] | 1129 | state $p_url = Mojo::URL->new($c->korap->api)->path('plugins/install'); |
| 1130 | |
| 1131 | return $c->korap_request(post => $p_url, {} => form => { |
| 1132 | super_client_id => $client_id, |
| 1133 | super_client_secret => $client_secret, |
| 1134 | client_id => $mclient_id |
| 1135 | })->then( |
| 1136 | sub { |
| 1137 | my $tx = shift; |
| 1138 | my $json = $tx->result->json; |
| 1139 | # Response is fine |
| 1140 | if ($tx->res->is_success) { |
| 1141 | return Mojo::Promise->resolve($json); |
| 1142 | }; |
| 1143 | #Log errors |
| 1144 | $c->log->error($tx->res->to_string); |
| 1145 | # Failure |
| 1146 | return Mojo::Promise->reject; |
| 1147 | } |
| 1148 | ) |
| 1149 | ->catch( |
| 1150 | sub { |
| 1151 | $c->notify('error' => $c->loc('Auth_installFail')); |
| 1152 | } |
| 1153 | ) |
| 1154 | ->finally( |
| 1155 | sub { |
| 1156 | return $c->redirect_to('marketplace'); |
| 1157 | } |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1158 | ); |
Helge | d36478d | 2023-06-08 17:43:01 +0200 | [diff] [blame] | 1159 | } |
| 1160 | )->name('install-plugin'); |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1161 | |
Helge | d36478d | 2023-06-08 17:43:01 +0200 | [diff] [blame] | 1162 | # Route to plugin deinstallation |
| 1163 | $r->post('/settings/marketplace/uninstall')->to( |
| 1164 | cb => sub { |
| 1165 | my $c = shift; |
| 1166 | _set_no_cache($c->res->headers); |
| 1167 | my $v = $c->validation; |
| 1168 | $v->required('client-id'); |
| 1169 | |
| 1170 | if ($v->has_error) { |
| 1171 | return $c->render( |
| 1172 | json => [], |
| 1173 | status => 400 |
| 1174 | ); |
| 1175 | }; |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1176 | |
Helge | d36478d | 2023-06-08 17:43:01 +0200 | [diff] [blame] | 1177 | unless ($c->auth->token) { |
| 1178 | return $c->render( |
| 1179 | content => 'Unauthorized', |
| 1180 | status => 401 |
| 1181 | ); |
| 1182 | }; |
| 1183 | |
| 1184 | my $uclient_id = $v->param('client-id'); |
| 1185 | |
| 1186 | $c->render_later; |
| 1187 | state $s_url = Mojo::URL->new($c->korap->api)->path('plugins/uninstall'); |
| 1188 | return $c->korap_request(post => $s_url, {} => form => { |
| 1189 | super_client_id => $client_id, |
| 1190 | super_client_secret => $client_secret, |
| 1191 | client_id => $uclient_id |
| 1192 | })->then( |
| 1193 | sub { |
| 1194 | my $tx = shift; |
| 1195 | my $json = $tx->result->json; |
| 1196 | # Response is fine |
| 1197 | if ($tx->res->is_success) { |
| 1198 | return Mojo::Promise->resolve($json); |
| 1199 | }; |
| 1200 | $c->log->error($tx->res->to_string); |
| 1201 | # Failure |
| 1202 | return Mojo::Promise->reject($json // 'No response'); |
| 1203 | } |
| 1204 | ) |
| 1205 | ->catch( |
| 1206 | sub { |
| 1207 | $c->notify('error' => $c->loc('Auth_uninstallFail')); |
| 1208 | } |
| 1209 | ) |
| 1210 | ->finally( |
| 1211 | sub { |
| 1212 | return $c->redirect_to('marketplace'); |
| 1213 | } |
| 1214 | ); |
| 1215 | } |
| 1216 | )->name('uninstall-plugin'); |
| 1217 | |
| 1218 | # Route to OAuth settings |
| 1219 | $r->get('/settings/oauth')->to( |
| 1220 | cb => sub { |
| 1221 | my $c = shift; |
| 1222 | _set_no_cache($c->res->headers); |
| 1223 | unless ($c->auth->token) { |
| 1224 | return $c->render( |
| 1225 | template => 'exception', |
| 1226 | msg => $c->loc('Auth_authenticationFail'), |
| 1227 | status => 401 |
| 1228 | ); |
| 1229 | }; |
| 1230 | # Wait for async result |
| 1231 | $c->render_later; |
| 1232 | $c->auth->client_list_p->then( |
| 1233 | sub { |
| 1234 | $c->stash('client_list' => shift); |
| 1235 | } |
| 1236 | )->catch( |
| 1237 | sub { |
| 1238 | return; |
| 1239 | } |
| 1240 | )->finally( |
| 1241 | sub { |
| 1242 | return $c->render(template => 'auth/clients') |
| 1243 | } |
| 1244 | ); |
| 1245 | } |
| 1246 | )->name('oauth-settings'); |
| 1247 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1248 | |
| 1249 | # Route to oauth client registration |
| 1250 | $r->post('/settings/oauth/register')->to( |
| 1251 | cb => sub { |
| 1252 | my $c = shift; |
| 1253 | |
| 1254 | _set_no_cache($c->res->headers); |
| 1255 | |
| 1256 | my $v = $c->validation; |
| 1257 | |
| 1258 | unless ($c->auth->token) { |
| 1259 | return $c->render( |
| 1260 | content => 'Unauthorized', |
| 1261 | status => 401 |
| 1262 | ); |
| 1263 | }; |
| 1264 | |
| 1265 | $v->csrf_protect; |
| 1266 | $v->required('name', 'trim', 'not_empty')->size(3, 255); |
| 1267 | $v->required('type')->in('PUBLIC', 'CONFIDENTIAL'); |
| 1268 | $v->required('desc', 'trim', 'not_empty')->size(3, 255); |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1269 | $v->optional('redirect_uri', 'trim', 'not_empty')->like(qr/^(http|$)/i); |
| 1270 | $v->optional('src', 'not_empty'); |
Helge | 9f9d485 | 2024-12-09 16:06:34 +0100 | [diff] [blame] | 1271 | |
| 1272 | my $src = $v->param('src'); |
| 1273 | if ($src && ref $src && $src->size > 0){ |
| 1274 | $v->required('url', 'trim', 'not_empty')->like(qr/^(http|$)/i); |
| 1275 | } |
| 1276 | else{ |
| 1277 | $v->optional('url', 'trim', 'not_empty')->like(qr/^(http|$)/i); |
| 1278 | } |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1279 | |
| 1280 | $c->stash(template => 'auth/clients'); |
| 1281 | |
| 1282 | # Render with error |
| 1283 | if ($v->has_error) { |
| 1284 | if ($v->has_error('csrf_token')) { |
| 1285 | $c->notify(error => $c->loc('Auth_csrfFail')); |
| 1286 | } |
| 1287 | else { |
| 1288 | $c->notify(error => $c->loc('Auth_paramError')); |
| 1289 | }; |
| 1290 | return $c->render; |
| 1291 | } elsif ($c->req->is_limit_exceeded) { |
| 1292 | $c->notify(error => $c->loc('Auth_fileSizeExceeded')); |
| 1293 | return $c->render; |
| 1294 | }; |
| 1295 | |
| 1296 | my $type = $v->param('type'); |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1297 | my $src_json; |
Helge | 9f9d485 | 2024-12-09 16:06:34 +0100 | [diff] [blame] | 1298 | |
| 1299 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1300 | |
| 1301 | my $json_obj = { |
| 1302 | name => $v->param('name'), |
| 1303 | type => $type, |
| 1304 | description => $v->param('desc'), |
| 1305 | url => $v->param('url'), |
| 1306 | redirect_uri => $v->param('redirect_uri') |
| 1307 | }; |
| 1308 | |
| 1309 | # Check plugin source |
| 1310 | if ($src) { |
| 1311 | |
| 1312 | # Source need to be a file upload |
| 1313 | if (!ref $src || !$src->isa('Mojo::Upload')) { |
| 1314 | $c->notify(error => $c->loc('Auth_jsonRequired')); |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 1315 | return $c->render; |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1316 | }; |
| 1317 | |
| 1318 | # Uploads can't be too large |
| 1319 | if ($src->size > 1_000_000) { |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 1320 | $c->notify(error => $c->loc('Auth_fileSizeExceeded')); |
| 1321 | return $c->render; |
| 1322 | }; |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 1323 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1324 | # Check upload is not empty |
| 1325 | if ($src->size > 0 && $src->filename ne '') { |
Helge | 9f9d485 | 2024-12-09 16:06:34 +0100 | [diff] [blame] | 1326 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1327 | # Plugins need to be confidential |
| 1328 | if ($type ne 'CONFIDENTIAL') { |
| 1329 | $c->notify(error => $c->loc('Auth_confidentialRequired')); |
| 1330 | return $c->render; |
| 1331 | }; |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 1332 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1333 | my $asset = $src->asset; |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 1334 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1335 | # Check for json |
| 1336 | eval { |
| 1337 | $src_json = decode_json($asset->slurp); |
| 1338 | }; |
| 1339 | |
| 1340 | if ($@ || !ref $src_json || ref $src_json ne 'HASH') { |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 1341 | $c->notify(error => $c->loc('Auth_jsonRequired')); |
| 1342 | return $c->render; |
| 1343 | }; |
| 1344 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1345 | $json_obj->{source} = $src_json; |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 1346 | }; |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1347 | }; |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 1348 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1349 | # Wait for async result |
| 1350 | $c->render_later; |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 1351 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1352 | # Register on server |
| 1353 | state $url = Mojo::URL->new($c->korap->api)->path('oauth2/client/register'); |
| 1354 | $c->korap_request('POST', $url => {} => json => $json_obj)->then( |
| 1355 | sub { |
| 1356 | my $tx = shift; |
| 1357 | my $result = $tx->result; |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 1358 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1359 | if ($result->is_error) { |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 1360 | my $json = $result->json; |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1361 | if ($json && $json->{error}) { |
| 1362 | $c->notify( |
| 1363 | error => $json->{error} . |
| 1364 | ($json->{error_description} ? ': ' . $json->{error_description} : '') |
| 1365 | ) |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 1366 | }; |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 1367 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1368 | return Mojo::Promise->reject; |
| 1369 | }; |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 1370 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1371 | my $json = $result->json; |
| 1372 | |
| 1373 | my $client_id = $json->{client_id}; |
| 1374 | my $client_secret = $json->{client_secret}; |
| 1375 | |
| 1376 | $c->stash('client_name' => $v->param('name')); |
| 1377 | $c->stash('client_desc' => $v->param('desc')); |
| 1378 | $c->stash('client_type' => $v->param('type')); |
| 1379 | $c->stash('client_url' => $v->param('url')); |
| 1380 | $c->stash('client_src' => $v->param('source')); |
| 1381 | $c->stash('client_redirect_uri' => $v->param('redirect_uri')); |
| 1382 | $c->stash('client_id' => $client_id); |
| 1383 | |
| 1384 | if ($client_secret) { |
| 1385 | $c->stash('client_secret' => $client_secret); |
| 1386 | }; |
| 1387 | |
| 1388 | $c->notify(success => $c->loc('Auth_en_registerSuccess')); |
| 1389 | |
| 1390 | return $c->render(template => 'auth/client'); |
| 1391 | } |
| 1392 | )->catch( |
| 1393 | sub { |
| 1394 | $c->notify('error' => $c->loc('Auth_en_registerFail')); |
| 1395 | } |
| 1396 | )->finally( |
| 1397 | sub { |
| 1398 | return $c->redirect_to('settings' => { scope => 'oauth' }); |
| 1399 | } |
| 1400 | ); |
| 1401 | } |
| 1402 | )->name('oauth-register'); |
| 1403 | |
| 1404 | |
| 1405 | # Unregister client page |
| 1406 | $r->get('/settings/oauth/:client_id/unregister')->to( |
| 1407 | cb => sub { |
| 1408 | my $c = shift; |
| 1409 | _set_no_cache($c->res->headers); |
| 1410 | $c->render(template => 'auth/unregister'); |
| 1411 | } |
| 1412 | )->name('oauth-unregister'); |
| 1413 | |
| 1414 | |
| 1415 | # Unregister client |
| 1416 | $r->post('/settings/oauth/:client_id/unregister')->to( |
| 1417 | cb => sub { |
| 1418 | my $c = shift; |
| 1419 | _set_no_cache($c->res->headers); |
| 1420 | |
| 1421 | my $v = $c->validation; |
| 1422 | |
| 1423 | unless ($c->auth->token) { |
| 1424 | return $c->render( |
| 1425 | content => 'Unauthorized', |
| 1426 | status => 401 |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 1427 | ); |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1428 | }; |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 1429 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1430 | $v->csrf_protect; |
| 1431 | $v->required('client-name', 'trim')->size(3, 255); |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 1432 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1433 | # Render with error |
| 1434 | if ($v->has_error) { |
| 1435 | if ($v->has_error('csrf_token')) { |
| 1436 | $c->notify(error => $c->loc('Auth_csrfFail')); |
| 1437 | } |
| 1438 | else { |
| 1439 | $c->notify(error => $c->loc('Auth_paramError')); |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 1440 | }; |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1441 | return $c->redirect_to('oauth-settings'); |
| 1442 | }; |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 1443 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1444 | my $client_id = $c->stash('client_id'); |
| 1445 | my $client_name = $v->param('client-name'); |
| 1446 | my $client_secret = $v->param('client-secret'); |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 1447 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1448 | # Get list of registered clients |
| 1449 | my $r_url = Mojo::URL->new($c->korap->api)->path('oauth2/client/deregister/')->path( |
| 1450 | $client_id |
| 1451 | ); |
| 1452 | |
| 1453 | my $send = {}; |
| 1454 | |
| 1455 | if ($client_secret) { |
| 1456 | $send->{client_secret} = $client_secret; |
| 1457 | }; |
| 1458 | |
| 1459 | # Get the list of all clients |
| 1460 | return $c->korap_request(delete => $r_url, {} => form => $send)->then( |
| 1461 | sub { |
| 1462 | my $tx = shift; |
| 1463 | |
| 1464 | # Response is fine |
| 1465 | if ($tx->res->is_success) { |
| 1466 | # Okay |
| 1467 | $c->notify(success => 'Successfully deleted ' . $client_name); |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 1468 | } |
| 1469 | else { |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1470 | |
| 1471 | # Failure |
| 1472 | my $json = $tx->result->json; |
| 1473 | if ($json && $json->{error_description}) { |
| 1474 | $c->notify(error => $json->{error_description}); |
| 1475 | } else { |
| 1476 | $c->notify(error => $c->loc('Auth_responseError')); |
| 1477 | }; |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 1478 | }; |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1479 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 1480 | return $c->redirect_to('oauth-settings'); |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1481 | } |
| 1482 | ); |
| 1483 | } |
| 1484 | )->name('oauth-unregister-post'); |
| 1485 | |
| 1486 | |
| 1487 | # OAuth Client authorization |
| 1488 | $r->get('/settings/oauth/authorize')->to( |
| 1489 | cb => sub { |
| 1490 | my $c = shift; |
| 1491 | |
| 1492 | _set_no_cache($c->res->headers); |
| 1493 | |
| 1494 | my $v = $c->validation; |
| 1495 | $v->required('client_id', 'trim'); |
| 1496 | $v->required('scope', 'trim'); |
| 1497 | $v->optional('state', 'trim'); |
| 1498 | $v->optional('redirect_uri', 'trim', 'not_empty')->like(qr/^(http|$)/i); |
| 1499 | |
| 1500 | # Redirect with error |
| 1501 | if ($v->has_error) { |
| 1502 | |
| 1503 | if ($v->has_error('client_id')) { |
| 1504 | $c->notify(error => $c->loc('Auth_clientIDFail')); |
| 1505 | } |
| 1506 | elsif ($v->has_error('scope')) { |
| 1507 | $c->notify(error => $c->loc('Auth_scopeFail')); |
| 1508 | } |
| 1509 | else { |
| 1510 | $c->notify(error => $c->loc('Auth_paramError')); |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 1511 | }; |
| 1512 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1513 | # If logged in, go to oauth settings - otherwise to index |
| 1514 | return $c->redirect_to($c->auth->token ? 'oauth-settings' : 'index'); |
| 1515 | }; |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 1516 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1517 | foreach (qw!scope client_id state redirect_uri!) { |
| 1518 | $c->stash($_, $v->param($_)); |
| 1519 | }; |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 1520 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1521 | # Wait for async result |
| 1522 | $c->render_later; |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 1523 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1524 | my $client_id = $v->param('client_id'); |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 1525 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1526 | my $client_information = $c->auth->client_info_p($client_id)->then( |
| 1527 | sub { |
| 1528 | my $cl = shift; |
| 1529 | $c->stash(client_name => $cl->{'client_name'}); |
| 1530 | $c->stash(client_type => $cl->{'client_type'}); |
| 1531 | $c->stash(client_desc => $cl->{'client_description'}); |
| 1532 | $c->stash(client_url => $cl->{'client_url'}); |
| 1533 | $c->stash(redirect_uri_server => $cl->{'client_redirect_uri'}); |
| 1534 | } |
| 1535 | )->then( |
| 1536 | sub { |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 1537 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1538 | # Check, if the client redirect_uri is valid |
| 1539 | my $redirect_uri_server = $c->stash('redirect_uri_server'); |
| 1540 | my $redirect_uri = $v->param('redirect_uri'); |
| 1541 | my ($server_o, $client_o); |
| 1542 | |
| 1543 | # Both exist |
| 1544 | if ($redirect_uri_server && $redirect_uri) { |
| 1545 | $server_o = Mojo::URL->new($redirect_uri_server); |
| 1546 | $client_o = Mojo::URL->new($redirect_uri); |
| 1547 | |
| 1548 | # Host not valid - take registered URI |
| 1549 | if ($server_o->host ne $client_o->host) { |
| 1550 | $c->notify(warn => 'redirect_uri host differs from registered host'); |
| 1551 | $client_o = $server_o; |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 1552 | } |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 1553 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1554 | # Port not valid - take registered URI |
| 1555 | elsif (($server_o->port // '') ne ($client_o->port // '')) { |
| 1556 | $c->notify(warn => 'redirect_uri port differs from registered port'); |
| 1557 | $client_o = $server_o; |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 1558 | }; |
| 1559 | } |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 1560 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1561 | # Client sent exists |
| 1562 | elsif ($redirect_uri) { |
| 1563 | $client_o = Mojo::URL->new($redirect_uri); |
| 1564 | $c->stash(redirect_warning => $c->loc('oauthGrantRedirectWarn')) |
Akron | 2c142ab | 2023-01-30 13:21:57 +0100 | [diff] [blame] | 1565 | } |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1566 | |
| 1567 | # Server registered exists |
| 1568 | elsif ($redirect_uri_server) { |
| 1569 | $client_o = Mojo::URL->new($redirect_uri_server); |
Akron | 2c142ab | 2023-01-30 13:21:57 +0100 | [diff] [blame] | 1570 | } |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1571 | |
| 1572 | # Redirect unknown |
Akron | 2c142ab | 2023-01-30 13:21:57 +0100 | [diff] [blame] | 1573 | else { |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1574 | $c->notify(error => 'redirect_uri not set'); |
Akron | 001dcd2 | 2023-02-07 08:38:11 +0100 | [diff] [blame] | 1575 | |
| 1576 | # If logged in, go to oauth settings - otherwise to index |
| 1577 | return $c->redirect_to($c->auth->token ? 'oauth-settings' : 'index'); |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 1578 | }; |
| 1579 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1580 | # No userinfo allowed |
| 1581 | if ($client_o->userinfo) { |
| 1582 | $c->notify(warn => 'redirect_uri contains userinfo'); |
| 1583 | $client_o->userinfo(''); |
| 1584 | }; |
| 1585 | |
| 1586 | # HTTPS warning |
| 1587 | # if ($client_o->scheme ne 'https') { |
| 1588 | # $c->notify(warn => 'redirect_uri is not HTTPS'); |
| 1589 | # }; |
| 1590 | |
| 1591 | # Sign redirection URL |
| 1592 | $c->stash(redirect_uri => $client_o->to_string); |
| 1593 | $c->stash(close_redirect_uri => '' . $c->close_redirect_to($client_o)); |
| 1594 | |
| 1595 | # Get auth token |
| 1596 | my $auth_token = $c->auth->token; |
| 1597 | |
| 1598 | # User is not logged in - log in before! |
| 1599 | unless ($auth_token) { |
| 1600 | return $c->render(template => 'auth/login'); |
| 1601 | }; |
| 1602 | |
| 1603 | # Grant authorization |
| 1604 | return $c->render(template => 'auth/grant_scope'); |
| 1605 | } |
| 1606 | )->catch( |
| 1607 | sub { |
| 1608 | my $error = shift; |
| 1609 | $c->notify(error => $error); |
| 1610 | |
| 1611 | # If logged in, go to oauth settings - otherwise to index |
| 1612 | return $c->redirect_to($c->auth->token ? 'oauth-settings' : 'index'); |
| 1613 | } |
| 1614 | ); |
| 1615 | } |
| 1616 | )->name('oauth-grant-scope'); |
| 1617 | |
| 1618 | |
| 1619 | # OAuth Client authorization |
| 1620 | # This will return a location information including some info |
| 1621 | $r->post('/settings/oauth/authorize')->to( |
| 1622 | cb => sub { |
| 1623 | my $c = shift; |
| 1624 | |
| 1625 | _set_no_cache($c->res->headers); |
| 1626 | |
| 1627 | # It's necessary that it's clear this was triggered by |
| 1628 | # KorAP and not by the client! |
| 1629 | my $v = $c->validation; |
| 1630 | $v->csrf_protect; |
| 1631 | $v->required('client_id', 'trim'); |
| 1632 | $v->required('scope', 'trim'); |
| 1633 | $v->optional('state', 'trim'); |
| 1634 | |
| 1635 | # Only signed redirects are allowed |
| 1636 | $v->required('redirect_uri', 'trim', 'not_empty')->closed_redirect; |
| 1637 | |
| 1638 | # Render with error |
| 1639 | if ($v->has_error) { |
| 1640 | |
| 1641 | # If logged in, go to oauth settings - otherwise to index |
| 1642 | my $url = $c->url_for($c->auth->token ? 'oauth-settings' : 'index'); |
| 1643 | |
| 1644 | if ($v->has_error('client_id')) { |
| 1645 | $url->query([error_description => $c->loc('Auth_clientIDFail')]); |
| 1646 | } |
| 1647 | elsif ($v->has_error('scope')) { |
| 1648 | $url->query([error_description => $c->loc('Auth_scopeFail')]); |
| 1649 | } |
| 1650 | elsif ($v->has_error('csrf_token')) { |
| 1651 | $url->query([error_description => $c->loc('Auth_csrfFail')]); |
| 1652 | } |
| 1653 | else { |
| 1654 | $url->query([error_description => $c->loc('Auth_paramError')]); |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 1655 | }; |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 1656 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1657 | return $c->redirect_to($url); |
| 1658 | }; |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 1659 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1660 | $c->stash(redirect_uri => Mojo::URL->new($v->param('redirect_uri'))); |
Akron | 9ccf69a | 2023-01-31 14:21:37 +0100 | [diff] [blame] | 1661 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1662 | return $c->auth->new_token_p( |
| 1663 | client_id => $v->param('client_id'), |
| 1664 | redirect_uri => $c->stash('redirect_uri'), |
| 1665 | state => $v->param('state'), |
| 1666 | scope => $v->param('scope'), |
| 1667 | )->catch( |
| 1668 | sub { |
| 1669 | my $err_msg = shift; |
| 1670 | my $url = $c->stash('redirect_uri'); |
Akron | 9ccf69a | 2023-01-31 14:21:37 +0100 | [diff] [blame] | 1671 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1672 | # Redirect! |
| 1673 | if ($url) { |
| 1674 | if ($err_msg) { |
| 1675 | $url = $url->query([error_description => $err_msg]); |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 1676 | }; |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 1677 | } |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1678 | |
| 1679 | # Do not redirect! |
| 1680 | else { |
| 1681 | $c->notify(error => $err_msg); |
| 1682 | |
| 1683 | # If logged in, go to oauth settings - otherwise to index |
| 1684 | $url = $c->url_for($c->auth->token ? 'oauth-settings' : 'index'); |
| 1685 | }; |
| 1686 | |
| 1687 | return Mojo::Promise->resolve($url); |
| 1688 | } |
| 1689 | )->then( |
| 1690 | sub { |
| 1691 | my $loc = shift; |
| 1692 | return $c->redirect_to($loc); |
| 1693 | } |
| 1694 | )->wait; |
| 1695 | return $c->rendered; |
| 1696 | } |
| 1697 | )->name('oauth-grant-scope-post'); |
Akron | 4cefe1f | 2019-09-04 10:11:28 +0200 | [diff] [blame] | 1698 | |
| 1699 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1700 | # Show information of a client |
| 1701 | $r->get('/settings/oauth/:client_id')->to( |
| 1702 | cb => sub { |
| 1703 | my $c = shift; |
Akron | 4cefe1f | 2019-09-04 10:11:28 +0200 | [diff] [blame] | 1704 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1705 | _set_no_cache($c->res->headers); |
Akron | 4cefe1f | 2019-09-04 10:11:28 +0200 | [diff] [blame] | 1706 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1707 | $c->render_later; |
Akron | 4cefe1f | 2019-09-04 10:11:28 +0200 | [diff] [blame] | 1708 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1709 | $c->auth->client_list_p->then( |
| 1710 | sub { |
| 1711 | my $json = shift; |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 1712 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1713 | my ($item) = grep { |
| 1714 | $c->stash('client_id') eq $_->{client_id} |
| 1715 | } @$json; |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 1716 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1717 | unless ($item) { |
| 1718 | return Mojo::Promise->reject; |
| 1719 | }; |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 1720 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1721 | $c->stash(client_name => $item->{client_name}); |
| 1722 | $c->stash(client_desc => $item->{client_description}); |
| 1723 | $c->stash(client_url => $item->{client_url}); |
| 1724 | $c->stash(client_type => ($item->{client_type} // 'PUBLIC')); |
| 1725 | $c->stash(client_redirect_uri => $item->{client_redirect_uri}); |
| 1726 | $c->stash(client_src => encode_json($item->{source})) if $item->{source}; |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 1727 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1728 | $c->auth->token_list_p($c->stash('client_id')); |
| 1729 | } |
| 1730 | )->then( |
| 1731 | sub { |
| 1732 | my $json = shift; |
Akron | 4cefe1f | 2019-09-04 10:11:28 +0200 | [diff] [blame] | 1733 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1734 | $c->stash(tokens => $json); |
Akron | 4cefe1f | 2019-09-04 10:11:28 +0200 | [diff] [blame] | 1735 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1736 | return Mojo::Promise->resolve; |
| 1737 | } |
| 1738 | )->catch( |
| 1739 | sub { |
| 1740 | return $c->reply->not_found; |
| 1741 | } |
| 1742 | )->finally( |
| 1743 | sub { |
| 1744 | return $c->render(template => 'auth/client') |
| 1745 | } |
| 1746 | ); |
Akron | 4cefe1f | 2019-09-04 10:11:28 +0200 | [diff] [blame] | 1747 | |
Akron | a4f1a97 | 2023-06-09 10:36:14 +0200 | [diff] [blame] | 1748 | return; |
| 1749 | } |
| 1750 | )->name('oauth-tokens'); |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 1751 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 1752 | |
| 1753 | # Ask if new token should be issued |
| 1754 | $r->get('/settings/oauth/:client_id/token')->to( |
| 1755 | cb => sub { |
| 1756 | my $c = shift; |
| 1757 | _set_no_cache($c->res->headers); |
| 1758 | $c->render(template => 'auth/issue-token'); |
| 1759 | } |
| 1760 | )->name('oauth-issue-token'); |
| 1761 | |
| 1762 | |
| 1763 | # Ask if a token should be revoked |
| 1764 | $r->post('/settings/oauth/:client_id/token/revoke')->to( |
| 1765 | cb => sub { |
| 1766 | shift->render(template => 'auth/revoke-token'); |
| 1767 | } |
| 1768 | )->name('oauth-revoke-token'); |
| 1769 | |
| 1770 | |
| 1771 | # Issue new token |
| 1772 | $r->post('/settings/oauth/:client_id/token')->to( |
| 1773 | cb => sub { |
| 1774 | my $c = shift; |
| 1775 | _set_no_cache($c->res->headers); |
| 1776 | |
| 1777 | my $v = $c->validation; |
| 1778 | |
| 1779 | unless ($c->auth->token) { |
| 1780 | return $c->render( |
| 1781 | content => 'Unauthorized', |
| 1782 | status => 401 |
| 1783 | ); |
| 1784 | }; |
| 1785 | |
| 1786 | $v->csrf_protect; |
| 1787 | $v->optional('client-secret'); |
| 1788 | $v->required('name', 'trim'); |
| 1789 | |
| 1790 | # Render with error |
| 1791 | if ($v->has_error) { |
| 1792 | if ($v->has_error('csrf_token')) { |
| 1793 | $c->notify(error => $c->loc('Auth_csrfFail')); |
| 1794 | } |
| 1795 | else { |
| 1796 | $c->notify(error => $c->loc('Auth_paramError')); |
| 1797 | }; |
| 1798 | return $c->redirect_to('oauth-settings') |
| 1799 | }; |
| 1800 | |
| 1801 | # Get authorization token |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 1802 | my $client_id = $c->stash('client_id'); |
| 1803 | my $name = $v->param('name'); |
Akron | 409f6b8 | 2023-05-08 10:42:36 +0200 | [diff] [blame] | 1804 | my $redirect_url = $c->url_for->query({name => $name})->to_abs; |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 1805 | |
Akron | e3daaeb | 2023-05-08 09:44:18 +0200 | [diff] [blame] | 1806 | $c->auth->new_token_p( |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 1807 | client_id => $client_id, |
| 1808 | redirect_uri => $redirect_url, |
Akron | a278b31 | 2023-05-08 11:19:13 +0200 | [diff] [blame] | 1809 | # TODO: State |
| 1810 | scope => 'search match_info', |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 1811 | )->then( |
| 1812 | sub { |
Akron | e3daaeb | 2023-05-08 09:44:18 +0200 | [diff] [blame] | 1813 | my ($loc, $client_id, $redirect_url, $code, $scope, $name) = @_; |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 1814 | |
| 1815 | # Get OAuth access token |
| 1816 | state $r_url = Mojo::URL->new($c->korap->api)->path('oauth2/token'); |
| 1817 | return $c->kalamar_ua->post_p($r_url, {} => form => { |
| 1818 | client_id => $client_id, |
| 1819 | # NO CLIENT_SECRET YET SUPPORTED |
| 1820 | grant_type => 'authorization_code', |
| 1821 | code => $code, |
| 1822 | redirect_uri => $redirect_url |
| 1823 | })->then( |
| 1824 | sub { |
| 1825 | my $tx = shift; |
| 1826 | my $json = $tx->res->json; |
| 1827 | |
| 1828 | if ($tx->res->is_error) { |
| 1829 | $c->notify(error => 'Unable to fetch new token'); |
| 1830 | return Mojo::Promise->reject; |
| 1831 | }; |
| 1832 | |
| 1833 | $c->notify(success => 'New access token created'); |
| 1834 | |
| 1835 | $c->redirect_to('oauth-tokens' => { client_id => $client_id }) |
| 1836 | } |
| 1837 | )->catch( |
| 1838 | sub { |
| 1839 | my $err_msg = shift; |
| 1840 | |
| 1841 | # Only raised in case of connection errors |
| 1842 | if ($err_msg) { |
| 1843 | $c->notify(error => { src => 'Backend' } => $err_msg) |
| 1844 | }; |
| 1845 | |
| 1846 | $c->render( |
| 1847 | status => 400, |
| 1848 | template => 'failure' |
| 1849 | ); |
| 1850 | } |
| 1851 | ) |
| 1852 | |
| 1853 | # Start IOLoop |
| 1854 | ->wait; |
| 1855 | |
| 1856 | } |
| 1857 | )->catch( |
| 1858 | sub { |
| 1859 | my $err_msg = shift; |
| 1860 | |
| 1861 | # Only raised in case of connection errors |
| 1862 | if ($err_msg) { |
| 1863 | $c->notify(error => { src => 'Backend' } => $err_msg) |
| 1864 | }; |
| 1865 | |
| 1866 | return $c->render( |
| 1867 | status => 400, |
| 1868 | template => 'failure' |
| 1869 | ); |
| 1870 | } |
| 1871 | ) |
| 1872 | |
| 1873 | # Start IOLoop |
| 1874 | ->wait; |
| 1875 | |
| 1876 | return 1; |
| 1877 | } |
| 1878 | )->name('oauth-issue-token-post'); |
| 1879 | |
| 1880 | |
| 1881 | # Revoke token |
| 1882 | $r->delete('/settings/oauth/:client_id/token')->to( |
| 1883 | cb => sub { |
| 1884 | my $c = shift; |
| 1885 | |
| 1886 | my $v = $c->validation; |
| 1887 | |
| 1888 | unless ($c->auth->token) { |
| 1889 | return $c->render( |
| 1890 | content => 'Unauthorized', |
| 1891 | status => 401 |
| 1892 | ); |
| 1893 | }; |
| 1894 | |
| 1895 | $v->csrf_protect; |
| 1896 | $v->required('token', 'trim'); |
| 1897 | $v->optional('name', 'trim'); |
| 1898 | my $private_client_id = $c->stash('client_id'); |
| 1899 | |
| 1900 | # Render with error |
| 1901 | if ($v->has_error) { |
| 1902 | if ($v->has_error('csrf_token')) { |
| 1903 | $c->notify(error => $c->loc('Auth_csrfFail')); |
| 1904 | } |
| 1905 | else { |
| 1906 | $c->notify(error => $c->loc('Auth_paramError')); |
| 1907 | }; |
| 1908 | return $c->redirect_to('oauth-tokens', client_id => $private_client_id); |
| 1909 | }; |
| 1910 | |
| 1911 | # Revoke token using super client privileges |
| 1912 | state $r_url = Mojo::URL->new($c->korap->api)->path('oauth2/revoke/super'); |
| 1913 | |
| 1914 | my $token = $v->param('token'); |
| 1915 | |
| 1916 | return $c->korap_request(post => $r_url, {} => form => { |
| 1917 | super_client_id => $client_id, |
| 1918 | super_client_secret => $client_secret, |
| 1919 | token => $token |
| 1920 | })->then( |
| 1921 | sub { |
| 1922 | my $tx = shift; |
| 1923 | |
| 1924 | # Response is fine |
| 1925 | if ($tx->res->is_success) { |
| 1926 | $c->notify(success => $c->loc('Auth_revokeSuccess')); |
| 1927 | return Mojo::Promise->resolve; |
| 1928 | }; |
| 1929 | |
| 1930 | return Mojo::Promise->reject; |
| 1931 | } |
| 1932 | )->catch( |
| 1933 | sub { |
| 1934 | my $err_msg = shift; |
| 1935 | if ($err_msg) { |
| 1936 | $c->notify(error => { src => 'Backend' } => $err_msg ); |
| 1937 | } |
| 1938 | else { |
| 1939 | $c->notify(error => $c->loc('Auth_revokeFail')); |
| 1940 | }; |
| 1941 | } |
| 1942 | )->finally( |
| 1943 | sub { |
| 1944 | return $c->redirect_to('oauth-tokens', client_id => $private_client_id); |
| 1945 | } |
| 1946 | ) |
| 1947 | |
| 1948 | # Start IOLoop |
| 1949 | ->wait; |
| 1950 | } |
| 1951 | )->name('oauth-revoke-token-delete'); |
| 1952 | |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 1953 | $app->log->info('Successfully registered Auth plugin'); |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 1954 | }; |
| 1955 | |
Akron | b3f3359 | 2020-03-16 15:14:44 +0100 | [diff] [blame] | 1956 | |
Akron | ad011bb | 2021-06-10 12:16:36 +0200 | [diff] [blame] | 1957 | # Set 'no caching' headers |
| 1958 | sub _set_no_cache { |
| 1959 | my $h = shift; |
| 1960 | $h->cache_control('max-age=0, no-cache, no-store, must-revalidate'); |
| 1961 | $h->expires('Thu, 01 Jan 1970 00:00:00 GMT'); |
| 1962 | $h->header('Pragma','no-cache'); |
| 1963 | }; |
| 1964 | |
| 1965 | |
Akron | 6a228db | 2021-10-14 15:57:00 +0200 | [diff] [blame] | 1966 | sub check_decode { |
| 1967 | no warnings 'uninitialized'; |
| 1968 | my $str = shift; |
| 1969 | my $str2 = is_utf8($str) ? b($str)->decode : $str; |
| 1970 | if (defined($str2) && $str2 && length($str2) > 1) { |
| 1971 | return $str2 |
| 1972 | }; |
| 1973 | return; |
| 1974 | }; |
| 1975 | |
| 1976 | |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 1977 | 1; |
Akron | a9c8b0e | 2018-11-16 20:20:28 +0100 | [diff] [blame] | 1978 | |
Akron | c82b1bc | 2018-11-18 18:06:14 +0100 | [diff] [blame] | 1979 | |
Akron | a9c8b0e | 2018-11-16 20:20:28 +0100 | [diff] [blame] | 1980 | __END__ |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 1981 | |
| 1982 | =pod |
| 1983 | |
| 1984 | =encoding utf8 |
| 1985 | |
| 1986 | =head1 NAME |
| 1987 | |
| 1988 | Kalamar::Plugin::Auth - OAuth-2.0-based authorization plugin |
| 1989 | |
| 1990 | =head1 DESCRIPTION |
| 1991 | |
| 1992 | L<Kalamar::Plugin::Auth> is an OAuth-2.0-based authorization |
| 1993 | plugin for L<Kalamar>. It requires a C<Kustvakt> full server |
| 1994 | with OAuth 2.0 capabilities. |
| 1995 | It is activated by loading C<Auth> as a plugin in the C<Kalamar.plugins> |
| 1996 | parameter in the Kalamar configuration. |
| 1997 | |
| 1998 | =head1 CONFIGURATION |
| 1999 | |
| 2000 | L<Kalamar::Plugin::Auth> supports the following parameter for the |
| 2001 | C<Kalamar-Auth> configuration section in the Kalamar configuration: |
| 2002 | |
| 2003 | =over 2 |
| 2004 | |
| 2005 | =item B<client_id> |
| 2006 | |
| 2007 | The client identifier of Kalamar to be send with every OAuth 2.0 |
| 2008 | management request. |
| 2009 | |
| 2010 | =item B<client_secret> |
| 2011 | |
| 2012 | The client secret of Kalamar to be send with every OAuth 2.0 |
| 2013 | management request. |
| 2014 | |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 2015 | =item B<experimental_client_registration> |
| 2016 | |
| 2017 | Activates the oauth client registration flow. |
| 2018 | |
| 2019 | =back |
| 2020 | |
| 2021 | =head2 COPYRIGHT AND LICENSE |
| 2022 | |
Akron | d91a1ca | 2022-05-20 16:45:01 +0200 | [diff] [blame] | 2023 | Copyright (C) 2015-2022, L<IDS Mannheim|http://www.ids-mannheim.de/> |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 2024 | Author: L<Nils Diewald|http://nils-diewald.de/> |
| 2025 | |
| 2026 | Kalamar is developed as part of the L<KorAP|http://korap.ids-mannheim.de/> |
| 2027 | Corpus Analysis Platform at the |
| 2028 | L<Leibniz Institute for the German Language (IDS)|http://ids-mannheim.de/>, |
| 2029 | member of the |
| 2030 | L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de> |
| 2031 | and supported by the L<KobRA|http://www.kobra.tu-dortmund.de> project, |
| 2032 | funded by the |
| 2033 | L<Federal Ministry of Education and Research (BMBF)|http://www.bmbf.de/en/>. |
| 2034 | |
| 2035 | Kalamar is free software published under the |
| 2036 | L<BSD-2 License|https://raw.githubusercontent.com/KorAP/Kalamar/master/LICENSE>. |
| 2037 | |
| 2038 | =cut |