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