Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | use Mojolicious::Lite; |
| 3 | use Mojo::ByteStream 'b'; |
| 4 | use Mojo::Date; |
| 5 | use Mojo::JSON qw/true false encode_json decode_json/; |
| 6 | use strict; |
| 7 | use warnings; |
| 8 | use Mojo::JWT; |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 9 | use Mojo::File qw/path/; |
| 10 | use Mojo::Util qw/slugify/; |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 11 | |
| 12 | # This is an API fake server with fixtures |
| 13 | |
| 14 | my $secret = 's3cr3t'; |
Akron | 73f3608 | 2018-10-25 15:34:59 +0200 | [diff] [blame] | 15 | my $fixture_path = path(Mojo::File->new(__FILE__)->dirname)->child('..', 'fixtures'); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 16 | |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 17 | our %tokens = ( |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 18 | 'access_token' => "4dcf8784ccfd26fac9bdb82778fe60e2", |
| 19 | 'refresh_token' => "hlWci75xb8atDiq3924NUSvOdtAh7Nlf9z", |
| 20 | 'access_token_2' => "abcde", |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 21 | 'access_token_3' => 'jvgjbvjgzucgdwuiKHJK', |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 22 | 'refresh_token_2' => "fghijk", |
| 23 | 'new_client_id' => 'fCBbQkA2NDA3MzM1Yw==', |
| 24 | 'new_client_secret' => 'KUMaFxs6R1WGud4HM22w3HbmYKHMnNHIiLJ2ihaWtB4N5JxGzZgyqs5GTLutrORj', |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 25 | 'auth_token_1' => 'mscajfdghnjdfshtkjcuynxahgz5il' |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 26 | ); |
| 27 | |
| 28 | helper get_token => sub { |
| 29 | my ($c, $token) = @_; |
| 30 | return $tokens{$token} |
| 31 | }; |
| 32 | |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 33 | # Legacy: |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 34 | helper jwt_encode => sub { |
| 35 | shift; |
| 36 | return Mojo::JWT->new( |
| 37 | secret => $secret, |
| 38 | token_type => 'api_token', |
| 39 | expires => time + (3 * 34 * 60 * 60), |
| 40 | claims => { @_ } |
| 41 | ); |
| 42 | }; |
| 43 | |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 44 | # Legacy; |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 45 | helper jwt_decode => sub { |
| 46 | my ($c, $auth) = @_; |
| 47 | $auth =~ s/\s*api_token\s+//; |
| 48 | return Mojo::JWT->new(secret => $secret)->decode($auth); |
| 49 | }; |
| 50 | |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 51 | # Expiration helper |
| 52 | helper expired => sub { |
| 53 | my ($c, $auth, $set) = @_; |
| 54 | |
| 55 | |
| 56 | $auth =~ s/^[^ ]+? //; |
| 57 | if ($set) { |
| 58 | $c->app->log->debug("Set $auth for expiration"); |
| 59 | $c->app->defaults('auth_' . $auth => 1); |
| 60 | return 1; |
| 61 | }; |
| 62 | |
| 63 | $c->app->log->debug("Check $auth for expiration: " . ( |
| 64 | $c->app->defaults('auth_' . $auth) // '0' |
| 65 | )); |
| 66 | |
| 67 | return $c->app->defaults('auth_' . $auth); |
| 68 | }; |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 69 | |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 70 | # Load fixture responses |
| 71 | helper 'load_response' => sub { |
| 72 | my $c = shift; |
| 73 | my $q_name = shift; |
| 74 | my $file = $fixture_path->child("response_$q_name.json"); |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 75 | $c->app->log->debug("Load response from $file"); |
| 76 | |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 77 | unless (-f $file) { |
| 78 | return { |
| 79 | status => 500, |
| 80 | json => { |
| 81 | errors => [[0, 'Unable to load query response from ' . $file]] |
| 82 | } |
| 83 | } |
| 84 | }; |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 85 | |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 86 | my $response = $file->slurp; |
Akron | a3c353c | 2019-02-14 23:50:00 +0100 | [diff] [blame] | 87 | my $decode = decode_json($response); |
| 88 | unless ($decode) { |
| 89 | return { |
| 90 | status => 500, |
| 91 | json => { |
| 92 | errors => [[0, 'Unable to parse JSON']] |
| 93 | } |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | return $decode; |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 98 | }; |
| 99 | |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 100 | app->defaults('oauth.client_list' => []); |
| 101 | |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 102 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 103 | # Base page |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 104 | get '/v1.0/' => sub { |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 105 | shift->render(text => 'Fake server available'); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 106 | }; |
| 107 | |
Akron | 3239663 | 2018-10-11 17:08:37 +0200 | [diff] [blame] | 108 | |
Akron | d00b427 | 2020-02-05 17:00:33 +0100 | [diff] [blame] | 109 | get '/v1.0/redirect-target-a' => sub { |
| 110 | shift->render(text => 'Redirect Target!'); |
| 111 | } => 'redirect-target'; |
| 112 | |
| 113 | |
| 114 | # Base page |
| 115 | get '/v1.0/redirect' => sub { |
| 116 | my $c = shift; |
| 117 | $c->res->code(308); |
| 118 | $c->res->headers->location($c->url_for('redirect-target')->to_abs); |
| 119 | return $c->render(text => ''); |
| 120 | }; |
| 121 | |
| 122 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 123 | # Search fixtures |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 124 | get '/v1.0/search' => sub { |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 125 | my $c = shift; |
| 126 | my $v = $c->validation; |
| 127 | $v->optional('q'); |
| 128 | $v->optional('page'); |
| 129 | $v->optional('ql'); |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 130 | $v->optional('cq'); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 131 | $v->optional('count'); |
| 132 | $v->optional('context'); |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 133 | $v->optional('offset'); |
Akron | c58bfc4 | 2020-10-05 12:09:45 +0200 | [diff] [blame] | 134 | $v->optional('pipes'); |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 135 | $v->optional('cutoff')->in(qw/true false/); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 136 | |
Akron | 3239663 | 2018-10-11 17:08:37 +0200 | [diff] [blame] | 137 | $c->app->log->debug('Receive request'); |
| 138 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 139 | # Response q=x&ql=cosmas3 |
| 140 | if ($v->param('ql') && $v->param('ql') eq 'cosmas3') { |
| 141 | return $c->render( |
| 142 | status => 400, |
| 143 | json => { |
| 144 | "\@context" => "http://korap.ids-mannheim.de/ns/koral/0.3/context.jsonld", |
| 145 | "errors" => [[307,"cosmas3 is not a supported query language!"]] |
| 146 | }); |
| 147 | }; |
| 148 | |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 149 | if (!$v->param('q')) { |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 150 | return $c->render(%{$c->load_response('query_no_query')}); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 151 | }; |
| 152 | |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 153 | my @slug_base = ($v->param('q')); |
| 154 | push @slug_base, 'o' . $v->param('offset') if defined $v->param('offset'); |
| 155 | push @slug_base, 'c' . $v->param('count') if defined $v->param('count'); |
| 156 | push @slug_base, 'co' . $v->param('cutoff') if defined $v->param('cutoff'); |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 157 | push @slug_base, 'cq' if defined $v->param('cq'); |
Akron | c58bfc4 | 2020-10-05 12:09:45 +0200 | [diff] [blame] | 158 | push @slug_base, 'p' . $v->param('pipes') if defined $v->param('pipes'); |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 159 | |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 160 | # Get response based on query parameter |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 161 | my $response = $c->load_response('query_' . slugify(join('_', @slug_base))); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 162 | |
| 163 | # Check authentification |
| 164 | if (my $auth = $c->req->headers->header('Authorization')) { |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 165 | |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 166 | $c->app->log->debug("There is an authorization header $auth"); |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 167 | my $jwt; |
| 168 | if ($auth =~ /^Bearer/) { |
| 169 | # Username unknown in OAuth2 |
| 170 | $response->{json}->{meta}->{authorized} = 'yes'; |
| 171 | } |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 172 | elsif ($auth =~ /^api_token/ && ($jwt = $c->jwt_decode($auth))) { |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 173 | $response->{json}->{meta}->{authorized} = $jwt->{username} if $jwt->{username}; |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 174 | }; |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 175 | |
| 176 | # Code is expired |
| 177 | if ($c->expired($auth)) { |
| 178 | |
| 179 | $c->app->log->debug("The access token has expired"); |
| 180 | |
| 181 | return $c->render( |
| 182 | status => 401, |
| 183 | json => { |
| 184 | errors => [[2003, 'Access token is expired']] |
| 185 | } |
| 186 | ); |
| 187 | } |
| 188 | |
| 189 | # Auth token is invalid |
| 190 | if ($auth =~ /^Bearer inv4lid/) { |
| 191 | $c->app->log->debug("The access token is invalid"); |
| 192 | |
| 193 | return $c->render( |
| 194 | status => 401, |
| 195 | json => { |
| 196 | errors => [[2011, 'Access token is invalid']] |
| 197 | } |
| 198 | ); |
| 199 | } |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 200 | }; |
| 201 | |
Akron | c58bfc4 | 2020-10-05 12:09:45 +0200 | [diff] [blame] | 202 | if ($v->param('pipes')) { |
| 203 | $response->{json}->{meta}->{pipes} = $v->param('pipes'); |
Akron | 7b9a196 | 2020-07-02 09:52:53 +0200 | [diff] [blame] | 204 | }; |
| 205 | |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 206 | # Set page parameter |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 207 | if ($v->param('page')) { |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 208 | $response->{json}->{meta}->{startIndex} = $v->param("startIndex"); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 209 | }; |
| 210 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 211 | # Simple search fixture |
Akron | 3239663 | 2018-10-11 17:08:37 +0200 | [diff] [blame] | 212 | $c->render(%$response); |
| 213 | |
| 214 | $c->app->log->debug('Rendered result'); |
| 215 | |
| 216 | return 1; |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 217 | }; |
| 218 | |
Akron | 80a84b2 | 2018-10-24 17:44:24 +0200 | [diff] [blame] | 219 | # Textinfo fixtures |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 220 | get '/v1.0/corpus/:corpusId/:docId/:textId' => sub { |
Akron | 80a84b2 | 2018-10-24 17:44:24 +0200 | [diff] [blame] | 221 | my $c = shift; |
| 222 | |
| 223 | my $file = join('_', ( |
| 224 | 'textinfo', |
| 225 | $c->stash('corpusId'), |
| 226 | $c->stash('docId'), |
| 227 | $c->stash('textId') |
| 228 | )); |
| 229 | |
| 230 | my $slug = slugify($file); |
| 231 | |
| 232 | # Get response based on query parameter |
| 233 | my $response = $c->load_response($slug); |
| 234 | return $c->render(%$response); |
| 235 | }; |
| 236 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 237 | |
Akron | b80341d | 2018-10-15 19:46:23 +0200 | [diff] [blame] | 238 | # Matchinfo fixtures |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 239 | get '/v1.0/corpus/:corpusId/:docId/:textId/:matchId/matchInfo' => sub { |
Akron | b80341d | 2018-10-15 19:46:23 +0200 | [diff] [blame] | 240 | my $c = shift; |
| 241 | |
| 242 | my $file = join('_', ( |
| 243 | 'matchinfo', |
| 244 | $c->stash('corpusId'), |
| 245 | $c->stash('docId'), |
| 246 | $c->stash('textId'), |
| 247 | $c->stash('matchId') |
| 248 | )); |
| 249 | |
Akron | b8d0b40 | 2018-10-18 23:51:52 +0200 | [diff] [blame] | 250 | my $slug = slugify($file); |
| 251 | |
Akron | b80341d | 2018-10-15 19:46:23 +0200 | [diff] [blame] | 252 | # Get response based on query parameter |
Akron | b8d0b40 | 2018-10-18 23:51:52 +0200 | [diff] [blame] | 253 | my $response = $c->load_response($slug); |
Akron | b80341d | 2018-10-15 19:46:23 +0200 | [diff] [blame] | 254 | return $c->render(%$response); |
| 255 | }; |
| 256 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 257 | |
Akron | be61f4c | 2018-10-20 00:52:58 +0200 | [diff] [blame] | 258 | # Statistics endpoint |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 259 | get '/v1.0/statistics' => sub { |
Akron | be61f4c | 2018-10-20 00:52:58 +0200 | [diff] [blame] | 260 | my $c = shift; |
| 261 | my $v = $c->validation; |
Akron | 5fa61e9 | 2019-07-15 11:56:11 +0200 | [diff] [blame] | 262 | $v->optional('cq'); |
Akron | be61f4c | 2018-10-20 00:52:58 +0200 | [diff] [blame] | 263 | |
| 264 | my @list = 'corpusinfo'; |
Akron | 5fa61e9 | 2019-07-15 11:56:11 +0200 | [diff] [blame] | 265 | if ($v->param('cq')) { |
| 266 | push @list, $v->param('cq'); |
Akron | be61f4c | 2018-10-20 00:52:58 +0200 | [diff] [blame] | 267 | }; |
| 268 | my $slug = slugify(join('_', @list)); |
| 269 | |
| 270 | # Get response based on query parameter |
| 271 | my $response = $c->load_response($slug); |
| 272 | return $c->render(%$response); |
| 273 | }; |
| 274 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 275 | ############ |
| 276 | # Auth API # |
| 277 | ############ |
| 278 | |
| 279 | # Request API token |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 280 | get '/v1.0/auth/logout' => sub { |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 281 | my $c = shift; |
| 282 | |
| 283 | if (my $auth = $c->req->headers->header('Authorization')) { |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 284 | |
| 285 | if ($auth =~ /^Bearer/) { |
| 286 | $c->app->log->debug('Server-Logout: ' . $auth); |
| 287 | return $c->render(json => { msg => [[0, 'Fine!']]}); |
| 288 | } |
| 289 | |
| 290 | elsif (my $jwt = $c->jwt_decode($auth)) { |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 291 | my $user = $jwt->{username} if $jwt->{username}; |
| 292 | |
| 293 | $c->app->log->debug('Server-Logout: ' . $user); |
| 294 | return $c->render(json => { msg => [[0, 'Fine!']]}); |
| 295 | }; |
| 296 | }; |
| 297 | |
| 298 | return $c->render(status => 400, json => { error => [[0, 'No!']]}); |
| 299 | }; |
| 300 | |
| 301 | |
| 302 | # Request API token |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 303 | get '/v1.0/auth/apiToken' => sub { |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 304 | my $c = shift; |
| 305 | |
| 306 | # Get auth header |
| 307 | my $auth = $c->req->headers->authorization; |
| 308 | |
| 309 | # Authorization missing or not basic |
| 310 | if (!$auth || $auth !~ s/\s*Basic\s+//gi) { |
| 311 | return $c->render( |
| 312 | json => { |
| 313 | error => [[2, 'x']] |
| 314 | } |
| 315 | ); |
| 316 | }; |
| 317 | |
| 318 | # Decode header |
| 319 | my ($username, $pwd) = @{b($auth)->b64_decode->split(':')->to_array}; |
| 320 | |
| 321 | # the password is 'pass' |
| 322 | if ($pwd) { |
| 323 | |
| 324 | # the password is 'pass' |
| 325 | if ($pwd eq 'pass') { |
| 326 | |
| 327 | # Render info with token |
| 328 | my $jwt = $c->jwt_encode(username => $username); |
| 329 | |
| 330 | # Render in the Kustvakt fashion: |
| 331 | return $c->render( |
| 332 | format => 'html', |
| 333 | text => encode_json({ |
| 334 | %{$jwt->claims}, |
| 335 | expires => $jwt->expires, |
| 336 | token => $jwt->encode, |
| 337 | token_type => 'api_token' |
| 338 | }) |
| 339 | ); |
Akron | 3d67306 | 2019-01-29 15:54:16 +0100 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | elsif ($pwd eq 'ldaperr') { |
| 343 | return $c->render( |
| 344 | format => 'html', |
| 345 | status => 401, |
| 346 | json => { |
| 347 | "errors" => [[2022,"LDAP Authentication failed due to unknown user or password!"]] |
| 348 | } |
| 349 | ); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 350 | }; |
| 351 | |
| 352 | return $c->render( |
| 353 | json => { |
| 354 | error => [[2004, undef]] |
| 355 | } |
| 356 | ); |
| 357 | }; |
| 358 | |
| 359 | return $c->render( |
| 360 | json => { |
| 361 | error => [[2004, undef]] |
| 362 | } |
| 363 | ); |
| 364 | }; |
| 365 | |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 366 | |
| 367 | # Request API token |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 368 | post '/v1.0/oauth2/token' => sub { |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 369 | my $c = shift; |
| 370 | |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 371 | my $grant_type = $c->param('grant_type') // 'undefined'; |
| 372 | |
| 373 | if ($grant_type eq 'password') { |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 374 | |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 375 | # Check for wrong client id |
| 376 | if ($c->param('client_id') ne '2') { |
| 377 | return $c->render( |
| 378 | json => { |
| 379 | "error_description" => "Unknown client with " . $_->{client_id}, |
| 380 | "error" => "invalid_client" |
| 381 | }, |
| 382 | status => 401 |
| 383 | ); |
| 384 | } |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 385 | |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 386 | # Check for wrong client secret |
| 387 | elsif ($c->param('client_secret') ne 'k414m4r-s3cr3t') { |
| 388 | return $c->render( |
| 389 | json => { |
| 390 | "error_description" => "Invalid client credentials", |
| 391 | "error" => "invalid_client" |
| 392 | }, |
| 393 | status => 401 |
| 394 | ); |
| 395 | } |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 396 | |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 397 | # Check for wrong user name |
| 398 | elsif ($c->param('username') ne 'test') { |
| 399 | return $c->render(json => { |
| 400 | error => [[2004, undef]] |
| 401 | }); |
| 402 | } |
| 403 | |
| 404 | # Check for ldap error |
| 405 | elsif ($c->param('password') eq 'ldaperr') { |
| 406 | return $c->render( |
| 407 | format => 'html', |
| 408 | status => 401, |
| 409 | json => { |
| 410 | "errors" => [ |
| 411 | [ |
| 412 | 2022, |
| 413 | "LDAP Authentication failed due to unknown user or password!" |
| 414 | ] |
| 415 | ] |
| 416 | } |
| 417 | ); |
| 418 | } |
| 419 | |
| 420 | # Check for wrong password |
| 421 | elsif ($c->param('password') ne 'pass') { |
| 422 | return $c->render(json => { |
| 423 | format => 'html', |
| 424 | status => 401, |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 425 | "errors" => [[2022,"LDAP Authentication failed due to unknown user or password!"]] |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 426 | }); |
| 427 | } |
| 428 | |
| 429 | # Return fine access |
| 430 | return $c->render( |
| 431 | json => { |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 432 | "access_token" => $c->get_token('access_token'), |
| 433 | "refresh_token" => $c->get_token('refresh_token'), |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 434 | "scope" => "all", |
| 435 | "token_type" => "Bearer", |
| 436 | "expires_in" => 86400 |
| 437 | }); |
| 438 | } |
| 439 | |
| 440 | # Refresh token |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 441 | elsif ($grant_type eq 'refresh_token') { |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 442 | |
| 443 | if ($c->param('refresh_token') eq 'inv4lid') { |
| 444 | return $c->render( |
| 445 | status => 400, |
| 446 | json => { |
| 447 | "error_description" => "Refresh token is expired", |
| 448 | "error" => "invalid_grant" |
| 449 | } |
| 450 | ); |
| 451 | }; |
| 452 | |
| 453 | $c->app->log->debug("Refresh the token in the mock server!"); |
| 454 | |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 455 | return $c->render( |
| 456 | status => 200, |
| 457 | json => { |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 458 | "access_token" => $c->get_token("access_token_2"), |
| 459 | "refresh_token" => $c->get_token("refresh_token_2"), |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 460 | "token_type" => "Bearer", |
| 461 | "expires_in" => 86400 |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 462 | } |
| 463 | ); |
| 464 | } |
| 465 | |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 466 | # Get auth_token_1 |
| 467 | elsif ($grant_type eq 'authorization_code') { |
| 468 | if ($c->param('code') eq $tokens{auth_token_1}) { |
| 469 | return $c->render( |
| 470 | status => 200, |
| 471 | json => { |
| 472 | "access_token" => $tokens{access_token_3}, |
| 473 | "expires_in" => 31536000, |
| 474 | "scope" => 'match_info search openid', |
| 475 | "token_type" => "Bearer" |
| 476 | } |
| 477 | ); |
| 478 | }; |
| 479 | } |
| 480 | |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 481 | # Unknown token grant |
| 482 | else { |
| 483 | return $c->render( |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 484 | status => 400, |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 485 | json => { |
| 486 | "errors" => [ |
| 487 | [ |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 488 | 0, "Grant Type unknown", $grant_type |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 489 | ] |
| 490 | ] |
| 491 | } |
| 492 | ) |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 493 | } |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 494 | }; |
| 495 | |
Akron | 4cefe1f | 2019-09-04 10:11:28 +0200 | [diff] [blame] | 496 | # Revoke API token |
| 497 | post '/v1.0/oauth2/revoke' => sub { |
| 498 | my $c = shift; |
| 499 | |
| 500 | my $refresh_token = $c->param('token'); |
| 501 | |
| 502 | if ($c->param('client_secret') ne 'k414m4r-s3cr3t') { |
| 503 | return $c->render( |
| 504 | json => { |
| 505 | "error_description" => "Invalid client credentials", |
| 506 | "error" => "invalid_client" |
| 507 | }, |
| 508 | status => 401 |
| 509 | ); |
| 510 | }; |
| 511 | |
| 512 | return $c->render( |
| 513 | text => '' |
| 514 | ) |
| 515 | }; |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 516 | |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 517 | # Register a client |
| 518 | post '/v1.0/oauth2/client/register' => sub { |
| 519 | my $c = shift; |
| 520 | my $json = $c->req->json; |
| 521 | |
Akron | dc50c89 | 2021-05-05 18:12:02 +0200 | [diff] [blame] | 522 | if ($json->{redirectURI}) { |
| 523 | return $c->render( |
| 524 | status => 400, |
| 525 | json => { |
| 526 | errors => [ |
| 527 | [ |
| 528 | 201, |
| 529 | "Unrecognized field \"redirectURI\" (class de.ids_mannheim.korap.web.input.OAuth2ClientJson), not marked as ignorable (5 known properties: \"redirect_uri\", \"type\", \"name\", \"description\", \"url\"])\n at [Source: (org.eclipse.jetty.server.HttpInputOverHTTP); line: 1, column: 94] (through reference chain: de.ids_mannheim.korap.web.input.OAuth2ClientJson[\"redirectURI\"])" |
| 530 | ] |
| 531 | ] |
| 532 | } |
| 533 | ); |
| 534 | }; |
| 535 | |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 536 | my $name = $json->{name}; |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 537 | my $desc = $json->{description}; |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 538 | my $type = $json->{type}; |
| 539 | my $url = $json->{url}; |
Akron | dc50c89 | 2021-05-05 18:12:02 +0200 | [diff] [blame] | 540 | my $redirect_url = $json->{redirect_uri}; |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 541 | |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 542 | my $list = $c->app->defaults('oauth.client_list'); |
| 543 | |
| 544 | push @$list, { |
Akron | dc50c89 | 2021-05-05 18:12:02 +0200 | [diff] [blame] | 545 | "client_id" => $tokens{new_client_id}, |
| 546 | "client_name" => $name, |
Akron | bc94a9c | 2021-04-15 00:07:35 +0200 | [diff] [blame^] | 547 | "client_description" => $desc, |
| 548 | "client_url" => $url |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 549 | }; |
| 550 | |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 551 | # Confidential server application |
| 552 | if ($type eq 'CONFIDENTIAL') { |
| 553 | return $c->render(json => { |
| 554 | client_id => $tokens{new_client_id}, |
| 555 | client_secret => $tokens{new_client_secret} |
| 556 | }); |
| 557 | }; |
| 558 | |
| 559 | # Desktop application |
| 560 | return $c->render(json => { |
| 561 | client_id => $tokens{new_client_id} |
| 562 | }); |
| 563 | }; |
| 564 | |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 565 | |
Akron | 0f1b93b | 2020-03-17 11:37:19 +0100 | [diff] [blame] | 566 | # Register a client |
| 567 | post '/v1.0/oauth2/client/list' => sub { |
| 568 | my $c = shift; |
| 569 | |
| 570 | # $c->param('client_secret'); |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 571 | |
| 572 | # Is empty [] when nothing registered |
| 573 | |
Akron | 0f1b93b | 2020-03-17 11:37:19 +0100 | [diff] [blame] | 574 | return $c->render( |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 575 | json => $c->stash('oauth.client_list'), |
| 576 | status => 200 |
| 577 | ); |
| 578 | }; |
| 579 | |
Akron | bc94a9c | 2021-04-15 00:07:35 +0200 | [diff] [blame^] | 580 | |
| 581 | # Get token list |
| 582 | post '/v1.0/oauth2/token/list' => sub { |
| 583 | my $c = shift; |
| 584 | return $c->render(json => [ |
| 585 | { |
| 586 | "client_description" => "Nur ein Beispiel", |
| 587 | "client_id" => $tokens{new_client_id}, |
| 588 | "client_name" => "Beispiel", |
| 589 | "client_url" => "", |
| 590 | "created_date" => "2021-04-14T19:40:26.742+02:00[Europe\/Berlin]", |
| 591 | "expires_in" => "31533851", |
| 592 | "scope" => [ |
| 593 | "match_info", |
| 594 | "search", |
| 595 | "openid" |
| 596 | ], |
| 597 | "token" => "jhkhkjhk_hjgjsfz67i", |
| 598 | "user_authentication_time" => "2021-04-14T19:39:41.81+02:00[Europe\/Berlin]" |
| 599 | } |
| 600 | ]); |
| 601 | }; |
| 602 | |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 603 | del '/v1.0/oauth2/client/deregister/:client_id' => sub { |
| 604 | my $c = shift; |
| 605 | my $client_id = $c->stash('client_id'); |
| 606 | |
| 607 | my $list = $c->app->defaults('oauth.client_list'); |
| 608 | |
| 609 | my $break = -1; |
| 610 | for (my $i = 0; $i < @$list; $i++) { |
Akron | dc50c89 | 2021-05-05 18:12:02 +0200 | [diff] [blame] | 611 | if ($list->[$i]->{client_id} eq $client_id) { |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 612 | $break = $i; |
| 613 | last; |
| 614 | }; |
| 615 | }; |
| 616 | |
| 617 | if ($break != -1) { |
| 618 | splice @$list, $break, 1; |
| 619 | } |
| 620 | |
| 621 | else { |
| 622 | return $c->render( |
| 623 | json => { |
| 624 | error_description => "Unknown client with $client_id.", |
| 625 | error => "invalid_client" |
Akron | 0f1b93b | 2020-03-17 11:37:19 +0100 | [diff] [blame] | 626 | }, |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 627 | status => 401 |
| 628 | ); |
| 629 | }; |
| 630 | |
| 631 | return $c->render( |
| 632 | json => $c->stash('oauth.client_list'), |
Akron | 0f1b93b | 2020-03-17 11:37:19 +0100 | [diff] [blame] | 633 | status => 200 |
| 634 | ); |
| 635 | }; |
| 636 | |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 637 | post '/v1.0/oauth2/authorize' => sub { |
| 638 | my $c = shift; |
| 639 | my $type = $c->param('response_type'); |
| 640 | my $client_id = $c->param('client_id'); |
| 641 | my $redirect_uri = $c->param('redirect_uri'); |
| 642 | |
| 643 | if ($type eq 'code') { |
| 644 | |
| 645 | return $c->redirect_to( |
| 646 | Mojo::URL->new($redirect_uri)->query({ |
| 647 | code => $tokens{auth_token_1}, |
| 648 | scope => 'match_info search openid' |
| 649 | }) |
| 650 | ); |
| 651 | } |
| 652 | }; |
| 653 | |
Akron | 0f1b93b | 2020-03-17 11:37:19 +0100 | [diff] [blame] | 654 | |
| 655 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 656 | app->start; |
| 657 | |
| 658 | |
| 659 | __END__ |
| 660 | |
| 661 | |
| 662 | # Temporary: |
| 663 | my $collection_query = { |
| 664 | '@type' => "koral:docGroup", |
| 665 | "operation" => "operation:or", |
| 666 | "operands" => [ |
| 667 | { |
| 668 | '@type' => "koral:docGroup", |
| 669 | "operation" => "operation:and", |
| 670 | "operands" => [ |
| 671 | { |
| 672 | '@type' => "koral:doc", |
| 673 | "key" => "title", |
| 674 | "match" => "match:eq", |
| 675 | "value" => "Der Birnbaum", |
| 676 | "type" => "type:string" |
| 677 | }, |
| 678 | { |
| 679 | '@type' => "koral:doc", |
| 680 | "key" => "pubPlace", |
| 681 | "match" => "match:eq", |
| 682 | "value" => "Mannheim", |
| 683 | "type" => "type:string" |
| 684 | }, |
| 685 | { |
| 686 | '@type' => "koral:docGroup", |
| 687 | "operation" => "operation:or", |
| 688 | "operands" => [ |
| 689 | { |
| 690 | '@type' => "koral:doc", |
| 691 | "key" => "subTitle", |
| 692 | "match" => "match:eq", |
| 693 | "value" => "Aufzucht oder Pflege", |
| 694 | "type" => "type:string" |
| 695 | }, |
| 696 | { |
| 697 | '@type' => "koral:doc", |
| 698 | "key" => "subTitle", |
| 699 | "match" => "match:eq", |
| 700 | "value" => "Gedichte", |
| 701 | "type" => "type:string" |
| 702 | } |
| 703 | ] |
| 704 | } |
| 705 | ] |
| 706 | }, |
| 707 | { |
| 708 | '@type' => "koral:doc", |
| 709 | "key" => "pubDate", |
| 710 | "match" => "match:geq", |
| 711 | "value" => "2015-03-05", |
| 712 | "type" => "type:date" |
| 713 | } |
| 714 | ] |
| 715 | }; |