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 = ( |
| 18 | "access_token" => "4dcf8784ccfd26fac9bdb82778fe60e2", |
| 19 | "refresh_token" => "hlWci75xb8atDiq3924NUSvOdtAh7Nlf9z", |
| 20 | "access_token_2" => "abcde", |
| 21 | "refresh_token_2" => "fghijk" |
| 22 | ); |
| 23 | |
| 24 | helper get_token => sub { |
| 25 | my ($c, $token) = @_; |
| 26 | return $tokens{$token} |
| 27 | }; |
| 28 | |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 29 | # Legacy: |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 30 | helper jwt_encode => sub { |
| 31 | shift; |
| 32 | return Mojo::JWT->new( |
| 33 | secret => $secret, |
| 34 | token_type => 'api_token', |
| 35 | expires => time + (3 * 34 * 60 * 60), |
| 36 | claims => { @_ } |
| 37 | ); |
| 38 | }; |
| 39 | |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 40 | # Legacy; |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 41 | helper jwt_decode => sub { |
| 42 | my ($c, $auth) = @_; |
| 43 | $auth =~ s/\s*api_token\s+//; |
| 44 | return Mojo::JWT->new(secret => $secret)->decode($auth); |
| 45 | }; |
| 46 | |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame^] | 47 | # Expiration helper |
| 48 | helper expired => sub { |
| 49 | my ($c, $auth, $set) = @_; |
| 50 | |
| 51 | |
| 52 | $auth =~ s/^[^ ]+? //; |
| 53 | if ($set) { |
| 54 | $c->app->log->debug("Set $auth for expiration"); |
| 55 | $c->app->defaults('auth_' . $auth => 1); |
| 56 | return 1; |
| 57 | }; |
| 58 | |
| 59 | $c->app->log->debug("Check $auth for expiration: " . ( |
| 60 | $c->app->defaults('auth_' . $auth) // '0' |
| 61 | )); |
| 62 | |
| 63 | return $c->app->defaults('auth_' . $auth); |
| 64 | }; |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 65 | |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 66 | # Load fixture responses |
| 67 | helper 'load_response' => sub { |
| 68 | my $c = shift; |
| 69 | my $q_name = shift; |
| 70 | my $file = $fixture_path->child("response_$q_name.json"); |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 71 | $c->app->log->debug("Load response from $file"); |
| 72 | |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 73 | unless (-f $file) { |
| 74 | return { |
| 75 | status => 500, |
| 76 | json => { |
| 77 | errors => [[0, 'Unable to load query response from ' . $file]] |
| 78 | } |
| 79 | } |
| 80 | }; |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 81 | |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 82 | my $response = $file->slurp; |
Akron | a3c353c | 2019-02-14 23:50:00 +0100 | [diff] [blame] | 83 | my $decode = decode_json($response); |
| 84 | unless ($decode) { |
| 85 | return { |
| 86 | status => 500, |
| 87 | json => { |
| 88 | errors => [[0, 'Unable to parse JSON']] |
| 89 | } |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | return $decode; |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 97 | # Base page |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 98 | get '/v1.0/' => sub { |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 99 | shift->render(text => 'Fake server available'); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 100 | }; |
| 101 | |
Akron | 3239663 | 2018-10-11 17:08:37 +0200 | [diff] [blame] | 102 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 103 | # Search fixtures |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 104 | get '/v1.0/search' => sub { |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 105 | my $c = shift; |
| 106 | my $v = $c->validation; |
| 107 | $v->optional('q'); |
| 108 | $v->optional('page'); |
| 109 | $v->optional('ql'); |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 110 | $v->optional('cq'); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 111 | $v->optional('count'); |
| 112 | $v->optional('context'); |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 113 | $v->optional('offset'); |
| 114 | $v->optional('cutoff')->in(qw/true false/); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 115 | |
Akron | 3239663 | 2018-10-11 17:08:37 +0200 | [diff] [blame] | 116 | $c->app->log->debug('Receive request'); |
| 117 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 118 | # Response q=x&ql=cosmas3 |
| 119 | if ($v->param('ql') && $v->param('ql') eq 'cosmas3') { |
| 120 | return $c->render( |
| 121 | status => 400, |
| 122 | json => { |
| 123 | "\@context" => "http://korap.ids-mannheim.de/ns/koral/0.3/context.jsonld", |
| 124 | "errors" => [[307,"cosmas3 is not a supported query language!"]] |
| 125 | }); |
| 126 | }; |
| 127 | |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 128 | if (!$v->param('q')) { |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 129 | return $c->render(%{$c->load_response('query_no_query')}); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 130 | }; |
| 131 | |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 132 | my @slug_base = ($v->param('q')); |
| 133 | push @slug_base, 'o' . $v->param('offset') if defined $v->param('offset'); |
| 134 | push @slug_base, 'c' . $v->param('count') if defined $v->param('count'); |
| 135 | push @slug_base, 'co' . $v->param('cutoff') if defined $v->param('cutoff'); |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 136 | push @slug_base, 'cq' if defined $v->param('cq'); |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 137 | |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 138 | # Get response based on query parameter |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 139 | my $response = $c->load_response('query_' . slugify(join('_', @slug_base))); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 140 | |
| 141 | # Check authentification |
| 142 | if (my $auth = $c->req->headers->header('Authorization')) { |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 143 | |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame^] | 144 | $c->app->log->debug("There is an authorization header $auth"); |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 145 | my $jwt; |
| 146 | if ($auth =~ /^Bearer/) { |
| 147 | # Username unknown in OAuth2 |
| 148 | $response->{json}->{meta}->{authorized} = 'yes'; |
| 149 | } |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame^] | 150 | elsif ($auth =~ /^api_token/ && ($jwt = $c->jwt_decode($auth))) { |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 151 | $response->{json}->{meta}->{authorized} = $jwt->{username} if $jwt->{username}; |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 152 | }; |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame^] | 153 | |
| 154 | # Code is expired |
| 155 | if ($c->expired($auth)) { |
| 156 | |
| 157 | $c->app->log->debug("The access token has expired"); |
| 158 | |
| 159 | return $c->render( |
| 160 | status => 401, |
| 161 | json => { |
| 162 | errors => [[2003, 'Access token is expired']] |
| 163 | } |
| 164 | ); |
| 165 | } |
| 166 | |
| 167 | # Auth token is invalid |
| 168 | if ($auth =~ /^Bearer inv4lid/) { |
| 169 | $c->app->log->debug("The access token is invalid"); |
| 170 | |
| 171 | return $c->render( |
| 172 | status => 401, |
| 173 | json => { |
| 174 | errors => [[2011, 'Access token is invalid']] |
| 175 | } |
| 176 | ); |
| 177 | } |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 178 | }; |
| 179 | |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 180 | # Set page parameter |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 181 | if ($v->param('page')) { |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 182 | $response->{json}->{meta}->{startIndex} = $v->param("startIndex"); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 183 | }; |
| 184 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 185 | # Simple search fixture |
Akron | 3239663 | 2018-10-11 17:08:37 +0200 | [diff] [blame] | 186 | $c->render(%$response); |
| 187 | |
| 188 | $c->app->log->debug('Rendered result'); |
| 189 | |
| 190 | return 1; |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 191 | }; |
| 192 | |
Akron | 80a84b2 | 2018-10-24 17:44:24 +0200 | [diff] [blame] | 193 | # Textinfo fixtures |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 194 | get '/v1.0/corpus/:corpusId/:docId/:textId' => sub { |
Akron | 80a84b2 | 2018-10-24 17:44:24 +0200 | [diff] [blame] | 195 | my $c = shift; |
| 196 | |
| 197 | my $file = join('_', ( |
| 198 | 'textinfo', |
| 199 | $c->stash('corpusId'), |
| 200 | $c->stash('docId'), |
| 201 | $c->stash('textId') |
| 202 | )); |
| 203 | |
| 204 | my $slug = slugify($file); |
| 205 | |
| 206 | # Get response based on query parameter |
| 207 | my $response = $c->load_response($slug); |
| 208 | return $c->render(%$response); |
| 209 | }; |
| 210 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 211 | |
Akron | b80341d | 2018-10-15 19:46:23 +0200 | [diff] [blame] | 212 | # Matchinfo fixtures |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 213 | get '/v1.0/corpus/:corpusId/:docId/:textId/:matchId/matchInfo' => sub { |
Akron | b80341d | 2018-10-15 19:46:23 +0200 | [diff] [blame] | 214 | my $c = shift; |
| 215 | |
| 216 | my $file = join('_', ( |
| 217 | 'matchinfo', |
| 218 | $c->stash('corpusId'), |
| 219 | $c->stash('docId'), |
| 220 | $c->stash('textId'), |
| 221 | $c->stash('matchId') |
| 222 | )); |
| 223 | |
Akron | b8d0b40 | 2018-10-18 23:51:52 +0200 | [diff] [blame] | 224 | my $slug = slugify($file); |
| 225 | |
Akron | b80341d | 2018-10-15 19:46:23 +0200 | [diff] [blame] | 226 | # Get response based on query parameter |
Akron | b8d0b40 | 2018-10-18 23:51:52 +0200 | [diff] [blame] | 227 | my $response = $c->load_response($slug); |
Akron | b80341d | 2018-10-15 19:46:23 +0200 | [diff] [blame] | 228 | return $c->render(%$response); |
| 229 | }; |
| 230 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 231 | |
Akron | be61f4c | 2018-10-20 00:52:58 +0200 | [diff] [blame] | 232 | # Statistics endpoint |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 233 | get '/v1.0/statistics' => sub { |
Akron | be61f4c | 2018-10-20 00:52:58 +0200 | [diff] [blame] | 234 | my $c = shift; |
| 235 | my $v = $c->validation; |
Akron | 5fa61e9 | 2019-07-15 11:56:11 +0200 | [diff] [blame] | 236 | $v->optional('cq'); |
Akron | be61f4c | 2018-10-20 00:52:58 +0200 | [diff] [blame] | 237 | |
| 238 | my @list = 'corpusinfo'; |
Akron | 5fa61e9 | 2019-07-15 11:56:11 +0200 | [diff] [blame] | 239 | if ($v->param('cq')) { |
| 240 | push @list, $v->param('cq'); |
Akron | be61f4c | 2018-10-20 00:52:58 +0200 | [diff] [blame] | 241 | }; |
| 242 | my $slug = slugify(join('_', @list)); |
| 243 | |
| 244 | # Get response based on query parameter |
| 245 | my $response = $c->load_response($slug); |
| 246 | return $c->render(%$response); |
| 247 | }; |
| 248 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 249 | ############ |
| 250 | # Auth API # |
| 251 | ############ |
| 252 | |
| 253 | # Request API token |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 254 | get '/v1.0/auth/logout' => sub { |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 255 | my $c = shift; |
| 256 | |
| 257 | if (my $auth = $c->req->headers->header('Authorization')) { |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame^] | 258 | |
| 259 | if ($auth =~ /^Bearer/) { |
| 260 | $c->app->log->debug('Server-Logout: ' . $auth); |
| 261 | return $c->render(json => { msg => [[0, 'Fine!']]}); |
| 262 | } |
| 263 | |
| 264 | elsif (my $jwt = $c->jwt_decode($auth)) { |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 265 | my $user = $jwt->{username} if $jwt->{username}; |
| 266 | |
| 267 | $c->app->log->debug('Server-Logout: ' . $user); |
| 268 | return $c->render(json => { msg => [[0, 'Fine!']]}); |
| 269 | }; |
| 270 | }; |
| 271 | |
| 272 | return $c->render(status => 400, json => { error => [[0, 'No!']]}); |
| 273 | }; |
| 274 | |
| 275 | |
| 276 | # Request API token |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 277 | get '/v1.0/auth/apiToken' => sub { |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 278 | my $c = shift; |
| 279 | |
| 280 | # Get auth header |
| 281 | my $auth = $c->req->headers->authorization; |
| 282 | |
| 283 | # Authorization missing or not basic |
| 284 | if (!$auth || $auth !~ s/\s*Basic\s+//gi) { |
| 285 | return $c->render( |
| 286 | json => { |
| 287 | error => [[2, 'x']] |
| 288 | } |
| 289 | ); |
| 290 | }; |
| 291 | |
| 292 | # Decode header |
| 293 | my ($username, $pwd) = @{b($auth)->b64_decode->split(':')->to_array}; |
| 294 | |
| 295 | # the password is 'pass' |
| 296 | if ($pwd) { |
| 297 | |
| 298 | # the password is 'pass' |
| 299 | if ($pwd eq 'pass') { |
| 300 | |
| 301 | # Render info with token |
| 302 | my $jwt = $c->jwt_encode(username => $username); |
| 303 | |
| 304 | # Render in the Kustvakt fashion: |
| 305 | return $c->render( |
| 306 | format => 'html', |
| 307 | text => encode_json({ |
| 308 | %{$jwt->claims}, |
| 309 | expires => $jwt->expires, |
| 310 | token => $jwt->encode, |
| 311 | token_type => 'api_token' |
| 312 | }) |
| 313 | ); |
Akron | 3d67306 | 2019-01-29 15:54:16 +0100 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | elsif ($pwd eq 'ldaperr') { |
| 317 | return $c->render( |
| 318 | format => 'html', |
| 319 | status => 401, |
| 320 | json => { |
| 321 | "errors" => [[2022,"LDAP Authentication failed due to unknown user or password!"]] |
| 322 | } |
| 323 | ); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 324 | }; |
| 325 | |
| 326 | return $c->render( |
| 327 | json => { |
| 328 | error => [[2004, undef]] |
| 329 | } |
| 330 | ); |
| 331 | }; |
| 332 | |
| 333 | return $c->render( |
| 334 | json => { |
| 335 | error => [[2004, undef]] |
| 336 | } |
| 337 | ); |
| 338 | }; |
| 339 | |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 340 | |
| 341 | # Request API token |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 342 | post '/v1.0/oauth2/token' => sub { |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 343 | my $c = shift; |
| 344 | |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 345 | my $grant_type = $c->param('grant_type') // 'undefined'; |
| 346 | |
| 347 | if ($grant_type eq 'password') { |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 348 | |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 349 | # Check for wrong client id |
| 350 | if ($c->param('client_id') ne '2') { |
| 351 | return $c->render( |
| 352 | json => { |
| 353 | "error_description" => "Unknown client with " . $_->{client_id}, |
| 354 | "error" => "invalid_client" |
| 355 | }, |
| 356 | status => 401 |
| 357 | ); |
| 358 | } |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 359 | |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 360 | # Check for wrong client secret |
| 361 | elsif ($c->param('client_secret') ne 'k414m4r-s3cr3t') { |
| 362 | return $c->render( |
| 363 | json => { |
| 364 | "error_description" => "Invalid client credentials", |
| 365 | "error" => "invalid_client" |
| 366 | }, |
| 367 | status => 401 |
| 368 | ); |
| 369 | } |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 370 | |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 371 | # Check for wrong user name |
| 372 | elsif ($c->param('username') ne 'test') { |
| 373 | return $c->render(json => { |
| 374 | error => [[2004, undef]] |
| 375 | }); |
| 376 | } |
| 377 | |
| 378 | # Check for ldap error |
| 379 | elsif ($c->param('password') eq 'ldaperr') { |
| 380 | return $c->render( |
| 381 | format => 'html', |
| 382 | status => 401, |
| 383 | json => { |
| 384 | "errors" => [ |
| 385 | [ |
| 386 | 2022, |
| 387 | "LDAP Authentication failed due to unknown user or password!" |
| 388 | ] |
| 389 | ] |
| 390 | } |
| 391 | ); |
| 392 | } |
| 393 | |
| 394 | # Check for wrong password |
| 395 | elsif ($c->param('password') ne 'pass') { |
| 396 | return $c->render(json => { |
| 397 | format => 'html', |
| 398 | status => 401, |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 399 | "errors" => [[2022,"LDAP Authentication failed due to unknown user or password!"]] |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 400 | }); |
| 401 | } |
| 402 | |
| 403 | # Return fine access |
| 404 | return $c->render( |
| 405 | json => { |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame^] | 406 | "access_token" => $c->get_token('access_token'), |
| 407 | "refresh_token" => $c->get_token('refresh_token'), |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 408 | "scope" => "all", |
| 409 | "token_type" => "Bearer", |
| 410 | "expires_in" => 86400 |
| 411 | }); |
| 412 | } |
| 413 | |
| 414 | # Refresh token |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 415 | elsif ($grant_type eq 'refresh_token') { |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame^] | 416 | |
| 417 | if ($c->param('refresh_token') eq 'inv4lid') { |
| 418 | return $c->render( |
| 419 | status => 400, |
| 420 | json => { |
| 421 | "error_description" => "Refresh token is expired", |
| 422 | "error" => "invalid_grant" |
| 423 | } |
| 424 | ); |
| 425 | }; |
| 426 | |
| 427 | $c->app->log->debug("Refresh the token in the mock server!"); |
| 428 | |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 429 | return $c->render( |
| 430 | status => 200, |
| 431 | json => { |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame^] | 432 | "access_token" => $c->get_token("access_token_2"), |
| 433 | "refresh_token" => $c->get_token("refresh_token_2"), |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 434 | "token_type" => "Bearer", |
| 435 | "expires_in" => 86400 |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 436 | } |
| 437 | ); |
| 438 | } |
| 439 | |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 440 | # Unknown token grant |
| 441 | else { |
| 442 | return $c->render( |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 443 | status => 400, |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 444 | json => { |
| 445 | "errors" => [ |
| 446 | [ |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 447 | 0, "Grant Type unknown", $grant_type |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 448 | ] |
| 449 | ] |
| 450 | } |
| 451 | ) |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 452 | } |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 453 | }; |
| 454 | |
| 455 | |
| 456 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 457 | app->start; |
| 458 | |
| 459 | |
| 460 | __END__ |
| 461 | |
| 462 | |
| 463 | # Temporary: |
| 464 | my $collection_query = { |
| 465 | '@type' => "koral:docGroup", |
| 466 | "operation" => "operation:or", |
| 467 | "operands" => [ |
| 468 | { |
| 469 | '@type' => "koral:docGroup", |
| 470 | "operation" => "operation:and", |
| 471 | "operands" => [ |
| 472 | { |
| 473 | '@type' => "koral:doc", |
| 474 | "key" => "title", |
| 475 | "match" => "match:eq", |
| 476 | "value" => "Der Birnbaum", |
| 477 | "type" => "type:string" |
| 478 | }, |
| 479 | { |
| 480 | '@type' => "koral:doc", |
| 481 | "key" => "pubPlace", |
| 482 | "match" => "match:eq", |
| 483 | "value" => "Mannheim", |
| 484 | "type" => "type:string" |
| 485 | }, |
| 486 | { |
| 487 | '@type' => "koral:docGroup", |
| 488 | "operation" => "operation:or", |
| 489 | "operands" => [ |
| 490 | { |
| 491 | '@type' => "koral:doc", |
| 492 | "key" => "subTitle", |
| 493 | "match" => "match:eq", |
| 494 | "value" => "Aufzucht oder Pflege", |
| 495 | "type" => "type:string" |
| 496 | }, |
| 497 | { |
| 498 | '@type' => "koral:doc", |
| 499 | "key" => "subTitle", |
| 500 | "match" => "match:eq", |
| 501 | "value" => "Gedichte", |
| 502 | "type" => "type:string" |
| 503 | } |
| 504 | ] |
| 505 | } |
| 506 | ] |
| 507 | }, |
| 508 | { |
| 509 | '@type' => "koral:doc", |
| 510 | "key" => "pubDate", |
| 511 | "match" => "match:geq", |
| 512 | "value" => "2015-03-05", |
| 513 | "type" => "type:date" |
| 514 | } |
| 515 | ] |
| 516 | }; |