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 | cce055c | 2021-07-02 12:18:03 +0200 | [diff] [blame] | 153 | if ($v->param('q') eq 'error') { |
| 154 | return $c->render( |
| 155 | status => 500, |
| 156 | inline => '<html><head>ERROR</head></html>' |
| 157 | ); |
| 158 | }; |
| 159 | |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 160 | my @slug_base = ($v->param('q')); |
| 161 | push @slug_base, 'o' . $v->param('offset') if defined $v->param('offset'); |
| 162 | push @slug_base, 'c' . $v->param('count') if defined $v->param('count'); |
| 163 | push @slug_base, 'co' . $v->param('cutoff') if defined $v->param('cutoff'); |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 164 | push @slug_base, 'cq' if defined $v->param('cq'); |
Akron | c58bfc4 | 2020-10-05 12:09:45 +0200 | [diff] [blame] | 165 | push @slug_base, 'p' . $v->param('pipes') if defined $v->param('pipes'); |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 166 | |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 167 | # Get response based on query parameter |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 168 | my $response = $c->load_response('query_' . slugify(join('_', @slug_base))); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 169 | |
| 170 | # Check authentification |
| 171 | if (my $auth = $c->req->headers->header('Authorization')) { |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 172 | |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 173 | $c->app->log->debug("There is an authorization header $auth"); |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 174 | my $jwt; |
| 175 | if ($auth =~ /^Bearer/) { |
| 176 | # Username unknown in OAuth2 |
| 177 | $response->{json}->{meta}->{authorized} = 'yes'; |
| 178 | } |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 179 | elsif ($auth =~ /^api_token/ && ($jwt = $c->jwt_decode($auth))) { |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 180 | $response->{json}->{meta}->{authorized} = $jwt->{username} if $jwt->{username}; |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 181 | }; |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 182 | |
| 183 | # Code is expired |
| 184 | if ($c->expired($auth)) { |
| 185 | |
| 186 | $c->app->log->debug("The access token has expired"); |
| 187 | |
| 188 | return $c->render( |
| 189 | status => 401, |
| 190 | json => { |
| 191 | errors => [[2003, 'Access token is expired']] |
| 192 | } |
| 193 | ); |
| 194 | } |
| 195 | |
| 196 | # Auth token is invalid |
| 197 | if ($auth =~ /^Bearer inv4lid/) { |
| 198 | $c->app->log->debug("The access token is invalid"); |
| 199 | |
| 200 | return $c->render( |
| 201 | status => 401, |
| 202 | json => { |
| 203 | errors => [[2011, 'Access token is invalid']] |
| 204 | } |
| 205 | ); |
| 206 | } |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 207 | }; |
| 208 | |
Akron | c58bfc4 | 2020-10-05 12:09:45 +0200 | [diff] [blame] | 209 | if ($v->param('pipes')) { |
| 210 | $response->{json}->{meta}->{pipes} = $v->param('pipes'); |
Akron | 7b9a196 | 2020-07-02 09:52:53 +0200 | [diff] [blame] | 211 | }; |
| 212 | |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 213 | # Set page parameter |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 214 | if ($v->param('page')) { |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 215 | $response->{json}->{meta}->{startIndex} = $v->param("startIndex"); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 216 | }; |
| 217 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 218 | # Simple search fixture |
Akron | 3239663 | 2018-10-11 17:08:37 +0200 | [diff] [blame] | 219 | $c->render(%$response); |
| 220 | |
| 221 | $c->app->log->debug('Rendered result'); |
| 222 | |
| 223 | return 1; |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 224 | }; |
| 225 | |
Akron | 80a84b2 | 2018-10-24 17:44:24 +0200 | [diff] [blame] | 226 | # Textinfo fixtures |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 227 | get '/v1.0/corpus/:corpusId/:docId/:textId' => sub { |
Akron | 80a84b2 | 2018-10-24 17:44:24 +0200 | [diff] [blame] | 228 | my $c = shift; |
| 229 | |
| 230 | my $file = join('_', ( |
| 231 | 'textinfo', |
| 232 | $c->stash('corpusId'), |
| 233 | $c->stash('docId'), |
| 234 | $c->stash('textId') |
| 235 | )); |
| 236 | |
| 237 | my $slug = slugify($file); |
| 238 | |
| 239 | # Get response based on query parameter |
| 240 | my $response = $c->load_response($slug); |
| 241 | return $c->render(%$response); |
| 242 | }; |
| 243 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 244 | |
Akron | b80341d | 2018-10-15 19:46:23 +0200 | [diff] [blame] | 245 | # Matchinfo fixtures |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 246 | get '/v1.0/corpus/:corpusId/:docId/:textId/:matchId/matchInfo' => sub { |
Akron | b80341d | 2018-10-15 19:46:23 +0200 | [diff] [blame] | 247 | my $c = shift; |
| 248 | |
| 249 | my $file = join('_', ( |
| 250 | 'matchinfo', |
| 251 | $c->stash('corpusId'), |
| 252 | $c->stash('docId'), |
| 253 | $c->stash('textId'), |
| 254 | $c->stash('matchId') |
| 255 | )); |
| 256 | |
Akron | b8d0b40 | 2018-10-18 23:51:52 +0200 | [diff] [blame] | 257 | my $slug = slugify($file); |
| 258 | |
Akron | b80341d | 2018-10-15 19:46:23 +0200 | [diff] [blame] | 259 | # Get response based on query parameter |
Akron | b8d0b40 | 2018-10-18 23:51:52 +0200 | [diff] [blame] | 260 | my $response = $c->load_response($slug); |
Akron | b80341d | 2018-10-15 19:46:23 +0200 | [diff] [blame] | 261 | return $c->render(%$response); |
| 262 | }; |
| 263 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 264 | |
Akron | be61f4c | 2018-10-20 00:52:58 +0200 | [diff] [blame] | 265 | # Statistics endpoint |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 266 | get '/v1.0/statistics' => sub { |
Akron | be61f4c | 2018-10-20 00:52:58 +0200 | [diff] [blame] | 267 | my $c = shift; |
| 268 | my $v = $c->validation; |
Akron | 5fa61e9 | 2019-07-15 11:56:11 +0200 | [diff] [blame] | 269 | $v->optional('cq'); |
Akron | be61f4c | 2018-10-20 00:52:58 +0200 | [diff] [blame] | 270 | |
| 271 | my @list = 'corpusinfo'; |
Akron | 5fa61e9 | 2019-07-15 11:56:11 +0200 | [diff] [blame] | 272 | if ($v->param('cq')) { |
| 273 | push @list, $v->param('cq'); |
Akron | be61f4c | 2018-10-20 00:52:58 +0200 | [diff] [blame] | 274 | }; |
| 275 | my $slug = slugify(join('_', @list)); |
| 276 | |
| 277 | # Get response based on query parameter |
| 278 | my $response = $c->load_response($slug); |
| 279 | return $c->render(%$response); |
| 280 | }; |
| 281 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 282 | ############ |
| 283 | # Auth API # |
| 284 | ############ |
| 285 | |
| 286 | # Request API token |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 287 | get '/v1.0/auth/logout' => sub { |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 288 | my $c = shift; |
| 289 | |
| 290 | if (my $auth = $c->req->headers->header('Authorization')) { |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 291 | |
| 292 | if ($auth =~ /^Bearer/) { |
| 293 | $c->app->log->debug('Server-Logout: ' . $auth); |
| 294 | return $c->render(json => { msg => [[0, 'Fine!']]}); |
| 295 | } |
| 296 | |
| 297 | elsif (my $jwt = $c->jwt_decode($auth)) { |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 298 | my $user = $jwt->{username} if $jwt->{username}; |
| 299 | |
| 300 | $c->app->log->debug('Server-Logout: ' . $user); |
| 301 | return $c->render(json => { msg => [[0, 'Fine!']]}); |
| 302 | }; |
| 303 | }; |
| 304 | |
| 305 | return $c->render(status => 400, json => { error => [[0, 'No!']]}); |
| 306 | }; |
| 307 | |
| 308 | |
| 309 | # Request API token |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 310 | get '/v1.0/auth/apiToken' => sub { |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 311 | my $c = shift; |
| 312 | |
| 313 | # Get auth header |
| 314 | my $auth = $c->req->headers->authorization; |
| 315 | |
| 316 | # Authorization missing or not basic |
| 317 | if (!$auth || $auth !~ s/\s*Basic\s+//gi) { |
| 318 | return $c->render( |
| 319 | json => { |
| 320 | error => [[2, 'x']] |
| 321 | } |
| 322 | ); |
| 323 | }; |
| 324 | |
| 325 | # Decode header |
| 326 | my ($username, $pwd) = @{b($auth)->b64_decode->split(':')->to_array}; |
| 327 | |
| 328 | # the password is 'pass' |
| 329 | if ($pwd) { |
| 330 | |
| 331 | # the password is 'pass' |
| 332 | if ($pwd eq 'pass') { |
| 333 | |
| 334 | # Render info with token |
| 335 | my $jwt = $c->jwt_encode(username => $username); |
| 336 | |
| 337 | # Render in the Kustvakt fashion: |
| 338 | return $c->render( |
| 339 | format => 'html', |
| 340 | text => encode_json({ |
| 341 | %{$jwt->claims}, |
| 342 | expires => $jwt->expires, |
| 343 | token => $jwt->encode, |
| 344 | token_type => 'api_token' |
| 345 | }) |
| 346 | ); |
Akron | 3d67306 | 2019-01-29 15:54:16 +0100 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | elsif ($pwd eq 'ldaperr') { |
| 350 | return $c->render( |
| 351 | format => 'html', |
| 352 | status => 401, |
| 353 | json => { |
| 354 | "errors" => [[2022,"LDAP Authentication failed due to unknown user or password!"]] |
| 355 | } |
| 356 | ); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 357 | }; |
| 358 | |
| 359 | return $c->render( |
| 360 | json => { |
| 361 | error => [[2004, undef]] |
| 362 | } |
| 363 | ); |
| 364 | }; |
| 365 | |
| 366 | return $c->render( |
| 367 | json => { |
| 368 | error => [[2004, undef]] |
| 369 | } |
| 370 | ); |
| 371 | }; |
| 372 | |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 373 | |
| 374 | # Request API token |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 375 | post '/v1.0/oauth2/token' => sub { |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 376 | my $c = shift; |
| 377 | |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 378 | my $grant_type = $c->param('grant_type') // 'undefined'; |
| 379 | |
| 380 | if ($grant_type eq 'password') { |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 381 | |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 382 | # Check for wrong client id |
| 383 | if ($c->param('client_id') ne '2') { |
| 384 | return $c->render( |
| 385 | json => { |
| 386 | "error_description" => "Unknown client with " . $_->{client_id}, |
| 387 | "error" => "invalid_client" |
| 388 | }, |
| 389 | status => 401 |
| 390 | ); |
| 391 | } |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 392 | |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 393 | # Check for wrong client secret |
| 394 | elsif ($c->param('client_secret') ne 'k414m4r-s3cr3t') { |
| 395 | return $c->render( |
| 396 | json => { |
| 397 | "error_description" => "Invalid client credentials", |
| 398 | "error" => "invalid_client" |
| 399 | }, |
| 400 | status => 401 |
| 401 | ); |
| 402 | } |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 403 | |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 404 | # Check for wrong user name |
| 405 | elsif ($c->param('username') ne 'test') { |
| 406 | return $c->render(json => { |
| 407 | error => [[2004, undef]] |
| 408 | }); |
| 409 | } |
| 410 | |
| 411 | # Check for ldap error |
| 412 | elsif ($c->param('password') eq 'ldaperr') { |
| 413 | return $c->render( |
| 414 | format => 'html', |
| 415 | status => 401, |
| 416 | json => { |
| 417 | "errors" => [ |
| 418 | [ |
| 419 | 2022, |
| 420 | "LDAP Authentication failed due to unknown user or password!" |
| 421 | ] |
| 422 | ] |
| 423 | } |
| 424 | ); |
| 425 | } |
| 426 | |
| 427 | # Check for wrong password |
| 428 | elsif ($c->param('password') ne 'pass') { |
| 429 | return $c->render(json => { |
| 430 | format => 'html', |
| 431 | status => 401, |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 432 | "errors" => [[2022,"LDAP Authentication failed due to unknown user or password!"]] |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 433 | }); |
| 434 | } |
| 435 | |
| 436 | # Return fine access |
| 437 | return $c->render( |
| 438 | json => { |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 439 | "access_token" => $c->get_token('access_token'), |
| 440 | "refresh_token" => $c->get_token('refresh_token'), |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 441 | "scope" => "all", |
| 442 | "token_type" => "Bearer", |
| 443 | "expires_in" => 86400 |
| 444 | }); |
| 445 | } |
| 446 | |
| 447 | # Refresh token |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 448 | elsif ($grant_type eq 'refresh_token') { |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 449 | |
| 450 | if ($c->param('refresh_token') eq 'inv4lid') { |
| 451 | return $c->render( |
| 452 | status => 400, |
| 453 | json => { |
| 454 | "error_description" => "Refresh token is expired", |
| 455 | "error" => "invalid_grant" |
| 456 | } |
| 457 | ); |
| 458 | }; |
| 459 | |
| 460 | $c->app->log->debug("Refresh the token in the mock server!"); |
| 461 | |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 462 | return $c->render( |
| 463 | status => 200, |
| 464 | json => { |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 465 | "access_token" => $c->get_token("access_token_2"), |
| 466 | "refresh_token" => $c->get_token("refresh_token_2"), |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 467 | "token_type" => "Bearer", |
| 468 | "expires_in" => 86400 |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 469 | } |
| 470 | ); |
| 471 | } |
| 472 | |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 473 | # Get auth_token_1 |
| 474 | elsif ($grant_type eq 'authorization_code') { |
| 475 | if ($c->param('code') eq $tokens{auth_token_1}) { |
| 476 | return $c->render( |
| 477 | status => 200, |
| 478 | json => { |
| 479 | "access_token" => $tokens{access_token_3}, |
| 480 | "expires_in" => 31536000, |
| 481 | "scope" => 'match_info search openid', |
| 482 | "token_type" => "Bearer" |
| 483 | } |
| 484 | ); |
| 485 | }; |
| 486 | } |
| 487 | |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 488 | # Unknown token grant |
| 489 | else { |
| 490 | return $c->render( |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 491 | status => 400, |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 492 | json => { |
| 493 | "errors" => [ |
| 494 | [ |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 495 | 0, "Grant Type unknown", $grant_type |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 496 | ] |
| 497 | ] |
| 498 | } |
| 499 | ) |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 500 | } |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 501 | }; |
| 502 | |
Akron | 4cefe1f | 2019-09-04 10:11:28 +0200 | [diff] [blame] | 503 | # Revoke API token |
| 504 | post '/v1.0/oauth2/revoke' => sub { |
| 505 | my $c = shift; |
| 506 | |
| 507 | my $refresh_token = $c->param('token'); |
| 508 | |
| 509 | if ($c->param('client_secret') ne 'k414m4r-s3cr3t') { |
| 510 | return $c->render( |
| 511 | json => { |
| 512 | "error_description" => "Invalid client credentials", |
| 513 | "error" => "invalid_client" |
| 514 | }, |
| 515 | status => 401 |
| 516 | ); |
| 517 | }; |
| 518 | |
| 519 | return $c->render( |
| 520 | text => '' |
| 521 | ) |
| 522 | }; |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 523 | |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 524 | # Register a client |
| 525 | post '/v1.0/oauth2/client/register' => sub { |
| 526 | my $c = shift; |
| 527 | my $json = $c->req->json; |
| 528 | |
Akron | dc50c89 | 2021-05-05 18:12:02 +0200 | [diff] [blame] | 529 | if ($json->{redirectURI}) { |
| 530 | return $c->render( |
| 531 | status => 400, |
| 532 | json => { |
| 533 | errors => [ |
| 534 | [ |
| 535 | 201, |
| 536 | "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\"])" |
| 537 | ] |
| 538 | ] |
| 539 | } |
| 540 | ); |
| 541 | }; |
| 542 | |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 543 | my $name = $json->{name}; |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 544 | my $desc = $json->{description}; |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 545 | my $type = $json->{type}; |
| 546 | my $url = $json->{url}; |
Akron | dc50c89 | 2021-05-05 18:12:02 +0200 | [diff] [blame] | 547 | my $redirect_url = $json->{redirect_uri}; |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 548 | |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 549 | my $list = $c->app->defaults('oauth.client_list'); |
| 550 | |
| 551 | push @$list, { |
Akron | dc50c89 | 2021-05-05 18:12:02 +0200 | [diff] [blame] | 552 | "client_id" => $tokens{new_client_id}, |
| 553 | "client_name" => $name, |
Akron | bc94a9c | 2021-04-15 00:07:35 +0200 | [diff] [blame] | 554 | "client_description" => $desc, |
| 555 | "client_url" => $url |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 556 | }; |
| 557 | |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 558 | # Confidential server application |
| 559 | if ($type eq 'CONFIDENTIAL') { |
| 560 | return $c->render(json => { |
| 561 | client_id => $tokens{new_client_id}, |
| 562 | client_secret => $tokens{new_client_secret} |
| 563 | }); |
| 564 | }; |
| 565 | |
| 566 | # Desktop application |
| 567 | return $c->render(json => { |
| 568 | client_id => $tokens{new_client_id} |
| 569 | }); |
| 570 | }; |
| 571 | |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 572 | |
Akron | 0f1b93b | 2020-03-17 11:37:19 +0100 | [diff] [blame] | 573 | # Register a client |
| 574 | post '/v1.0/oauth2/client/list' => sub { |
| 575 | my $c = shift; |
| 576 | |
Akron | 276afc0 | 2021-06-14 11:00:21 +0200 | [diff] [blame] | 577 | my $v = $c->validation; |
| 578 | |
| 579 | $v->required('super_client_id'); |
| 580 | $v->required('super_client_secret'); |
| 581 | |
| 582 | if ($v->has_error) { |
| 583 | return $c->render( |
| 584 | json => [], |
| 585 | status => 400 |
| 586 | ); |
| 587 | }; |
| 588 | |
Akron | 0f1b93b | 2020-03-17 11:37:19 +0100 | [diff] [blame] | 589 | # $c->param('client_secret'); |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 590 | |
| 591 | # Is empty [] when nothing registered |
| 592 | |
Akron | 0f1b93b | 2020-03-17 11:37:19 +0100 | [diff] [blame] | 593 | return $c->render( |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 594 | json => $c->stash('oauth.client_list'), |
| 595 | status => 200 |
| 596 | ); |
| 597 | }; |
| 598 | |
Akron | bc94a9c | 2021-04-15 00:07:35 +0200 | [diff] [blame] | 599 | |
| 600 | # Get token list |
| 601 | post '/v1.0/oauth2/token/list' => sub { |
| 602 | my $c = shift; |
| 603 | return $c->render(json => [ |
| 604 | { |
| 605 | "client_description" => "Nur ein Beispiel", |
| 606 | "client_id" => $tokens{new_client_id}, |
| 607 | "client_name" => "Beispiel", |
| 608 | "client_url" => "", |
| 609 | "created_date" => "2021-04-14T19:40:26.742+02:00[Europe\/Berlin]", |
| 610 | "expires_in" => "31533851", |
| 611 | "scope" => [ |
| 612 | "match_info", |
| 613 | "search", |
| 614 | "openid" |
| 615 | ], |
| 616 | "token" => "jhkhkjhk_hjgjsfz67i", |
| 617 | "user_authentication_time" => "2021-04-14T19:39:41.81+02:00[Europe\/Berlin]" |
| 618 | } |
| 619 | ]); |
| 620 | }; |
| 621 | |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 622 | del '/v1.0/oauth2/client/deregister/:client_id' => sub { |
| 623 | my $c = shift; |
| 624 | my $client_id = $c->stash('client_id'); |
| 625 | |
| 626 | my $list = $c->app->defaults('oauth.client_list'); |
| 627 | |
| 628 | my $break = -1; |
| 629 | for (my $i = 0; $i < @$list; $i++) { |
Akron | dc50c89 | 2021-05-05 18:12:02 +0200 | [diff] [blame] | 630 | if ($list->[$i]->{client_id} eq $client_id) { |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 631 | $break = $i; |
| 632 | last; |
| 633 | }; |
| 634 | }; |
| 635 | |
| 636 | if ($break != -1) { |
| 637 | splice @$list, $break, 1; |
| 638 | } |
| 639 | |
| 640 | else { |
| 641 | return $c->render( |
| 642 | json => { |
| 643 | error_description => "Unknown client with $client_id.", |
| 644 | error => "invalid_client" |
Akron | 0f1b93b | 2020-03-17 11:37:19 +0100 | [diff] [blame] | 645 | }, |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 646 | status => 401 |
| 647 | ); |
| 648 | }; |
| 649 | |
| 650 | return $c->render( |
| 651 | json => $c->stash('oauth.client_list'), |
Akron | 0f1b93b | 2020-03-17 11:37:19 +0100 | [diff] [blame] | 652 | status => 200 |
| 653 | ); |
| 654 | }; |
| 655 | |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 656 | post '/v1.0/oauth2/authorize' => sub { |
| 657 | my $c = shift; |
| 658 | my $type = $c->param('response_type'); |
| 659 | my $client_id = $c->param('client_id'); |
| 660 | my $redirect_uri = $c->param('redirect_uri'); |
| 661 | |
| 662 | if ($type eq 'code') { |
| 663 | |
| 664 | return $c->redirect_to( |
| 665 | Mojo::URL->new($redirect_uri)->query({ |
| 666 | code => $tokens{auth_token_1}, |
| 667 | scope => 'match_info search openid' |
| 668 | }) |
| 669 | ); |
| 670 | } |
| 671 | }; |
| 672 | |
Akron | 0f1b93b | 2020-03-17 11:37:19 +0100 | [diff] [blame] | 673 | |
Akron | abdf9a9 | 2021-01-12 19:06:57 +0100 | [diff] [blame] | 674 | ####################### |
| 675 | # Query Reference API # |
| 676 | ####################### |
| 677 | |
| 678 | use CHI; |
| 679 | my $chi = CHI->new( |
| 680 | driver => 'Memory', |
| 681 | global => 1 |
| 682 | ); |
| 683 | |
| 684 | # Store query |
| 685 | put '/v1.0/query/~:user/:query_name' => sub { |
| 686 | my $c = shift; |
| 687 | my $user = $c->stash('user'); |
| 688 | my $qname = $c->stash('query_name'); |
| 689 | |
| 690 | if ($chi->is_valid($qname)) { |
| 691 | return $c->render( |
| 692 | json => { |
| 693 | errors => [ |
| 694 | { |
| 695 | message => 'Unable to store query reference' |
| 696 | } |
| 697 | ] |
| 698 | }, status => 400 |
| 699 | ); |
| 700 | }; |
| 701 | |
| 702 | my $json = $c->req->json; |
| 703 | |
| 704 | my $store = { |
| 705 | name => $qname, |
| 706 | koralQuery => { '@type' => 'Okay' }, |
| 707 | query => $json->{query}, |
| 708 | queryType => $json->{queryType}, |
| 709 | type => $json->{type}, |
| 710 | queryLanguage => $json->{queryLanguage}, |
| 711 | }; |
| 712 | |
| 713 | if (exists $json->{description}) { |
| 714 | $store->{description} = $json->{description} |
| 715 | }; |
| 716 | |
| 717 | # Set query reference |
| 718 | $chi->set($qname => $store); |
| 719 | |
| 720 | my $queries = $chi->get('~queries') // []; |
| 721 | push @$queries, $qname; |
| 722 | $chi->set('~queries' => $queries); |
| 723 | |
| 724 | return $c->render( |
| 725 | status => 201, |
| 726 | text => '' |
| 727 | ); |
| 728 | }; |
| 729 | |
| 730 | # Get query |
| 731 | get '/v1.0/query/~:user/:query_name' => sub { |
| 732 | my $c = shift; |
| 733 | |
| 734 | my $user = $c->stash('user'); |
| 735 | my $qname = $c->stash('query_name'); |
| 736 | |
| 737 | my $json = $chi->get($qname); |
| 738 | |
| 739 | if ($json) { |
| 740 | return $c->render( |
| 741 | json => $json |
| 742 | ); |
| 743 | }; |
| 744 | |
| 745 | return $c->render( |
| 746 | json => { |
| 747 | errors => [ |
| 748 | { |
| 749 | message => 'Query reference not found' |
| 750 | } |
| 751 | ] |
| 752 | }, status => 404 |
| 753 | ); |
| 754 | }; |
| 755 | |
| 756 | |
| 757 | # Get all queries |
| 758 | get '/v1.0/query/~:user' => sub { |
| 759 | my $c = shift; |
| 760 | my $user = $c->stash('user'); |
| 761 | my $qs = $chi->get('~queries') // []; |
| 762 | my @queries = (); |
| 763 | foreach (@$qs) { |
| 764 | push @queries, $chi->get($_); |
| 765 | }; |
| 766 | return $c->render(json => { refs => \@queries }); |
| 767 | }; |
| 768 | |
| 769 | |
| 770 | # Store query |
| 771 | del '/v1.0/query/~:user/:query_name' => sub { |
| 772 | my $c = shift; |
| 773 | my $user = $c->stash('user'); |
| 774 | my $qname = $c->stash('query_name'); |
| 775 | |
| 776 | $chi->remove($qname); |
| 777 | |
| 778 | my $queries = $chi->get('~queries') // []; |
| 779 | |
| 780 | my @clean = (); |
| 781 | foreach (@$queries) { |
| 782 | push @clean, $_ unless $_ eq $qname |
| 783 | }; |
| 784 | |
| 785 | $chi->set('~queries' => \@clean); |
| 786 | |
| 787 | return $c->render( |
| 788 | status => 200, |
| 789 | text => '' |
| 790 | ); |
| 791 | }; |
| 792 | |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 793 | post '/v1.0/oauth2/revoke/super' => sub { |
| 794 | my $c = shift; |
| 795 | |
| 796 | my $s_client_id = $c->param('super_client_id'); |
| 797 | my $s_client_secret = $c->param('super_client_secret'); |
| 798 | my $token = $c->param('token'); |
| 799 | |
| 800 | return $c->render(text => 'SUCCESS'); |
| 801 | }; |
| 802 | |
Akron | 0f1b93b | 2020-03-17 11:37:19 +0100 | [diff] [blame] | 803 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 804 | app->start; |
| 805 | |
| 806 | |
| 807 | __END__ |
| 808 | |
| 809 | |
| 810 | # Temporary: |
| 811 | my $collection_query = { |
| 812 | '@type' => "koral:docGroup", |
| 813 | "operation" => "operation:or", |
| 814 | "operands" => [ |
| 815 | { |
| 816 | '@type' => "koral:docGroup", |
| 817 | "operation" => "operation:and", |
| 818 | "operands" => [ |
| 819 | { |
| 820 | '@type' => "koral:doc", |
| 821 | "key" => "title", |
| 822 | "match" => "match:eq", |
| 823 | "value" => "Der Birnbaum", |
| 824 | "type" => "type:string" |
| 825 | }, |
| 826 | { |
| 827 | '@type' => "koral:doc", |
| 828 | "key" => "pubPlace", |
| 829 | "match" => "match:eq", |
| 830 | "value" => "Mannheim", |
| 831 | "type" => "type:string" |
| 832 | }, |
| 833 | { |
| 834 | '@type' => "koral:docGroup", |
| 835 | "operation" => "operation:or", |
| 836 | "operands" => [ |
| 837 | { |
| 838 | '@type' => "koral:doc", |
| 839 | "key" => "subTitle", |
| 840 | "match" => "match:eq", |
| 841 | "value" => "Aufzucht oder Pflege", |
| 842 | "type" => "type:string" |
| 843 | }, |
| 844 | { |
| 845 | '@type' => "koral:doc", |
| 846 | "key" => "subTitle", |
| 847 | "match" => "match:eq", |
| 848 | "value" => "Gedichte", |
| 849 | "type" => "type:string" |
| 850 | } |
| 851 | ] |
| 852 | } |
| 853 | ] |
| 854 | }, |
| 855 | { |
| 856 | '@type' => "koral:doc", |
| 857 | "key" => "pubDate", |
| 858 | "match" => "match:geq", |
| 859 | "value" => "2015-03-05", |
| 860 | "type" => "type:date" |
| 861 | } |
| 862 | ] |
| 863 | }; |