blob: 538915654314ea40f463c18070b97e6f57e97a64 [file] [log] [blame]
Akron0e1ed242018-10-11 13:22:00 +02001#!/usr/bin/env perl
2use Mojolicious::Lite;
3use Mojo::ByteStream 'b';
4use Mojo::Date;
5use Mojo::JSON qw/true false encode_json decode_json/;
6use strict;
7use warnings;
8use Mojo::JWT;
Akron6d49c1f2018-10-11 14:22:21 +02009use Mojo::File qw/path/;
10use Mojo::Util qw/slugify/;
Akron0e1ed242018-10-11 13:22:00 +020011
12# This is an API fake server with fixtures
13
14my $secret = 's3cr3t';
Akron73f36082018-10-25 15:34:59 +020015my $fixture_path = path(Mojo::File->new(__FILE__)->dirname)->child('..', 'fixtures');
Akron0e1ed242018-10-11 13:22:00 +020016
Akroncdfd9d52019-07-23 11:35:00 +020017our %tokens = (
Akron59992122019-10-29 11:28:45 +010018 'access_token' => "4dcf8784ccfd26fac9bdb82778fe60e2",
19 'refresh_token' => "hlWci75xb8atDiq3924NUSvOdtAh7Nlf9z",
20 'access_token_2' => "abcde",
Akron83209f72021-01-29 17:54:15 +010021 'access_token_3' => 'jvgjbvjgzucgdwuiKHJK',
Akron59992122019-10-29 11:28:45 +010022 'refresh_token_2' => "fghijk",
23 'new_client_id' => 'fCBbQkA2NDA3MzM1Yw==',
24 'new_client_secret' => 'KUMaFxs6R1WGud4HM22w3HbmYKHMnNHIiLJ2ihaWtB4N5JxGzZgyqs5GTLutrORj',
Akron83209f72021-01-29 17:54:15 +010025 'auth_token_1' => 'mscajfdghnjdfshtkjcuynxahgz5il'
Akroncdfd9d52019-07-23 11:35:00 +020026);
27
28helper get_token => sub {
29 my ($c, $token) = @_;
30 return $tokens{$token}
31};
32
Akron33f5c672019-06-24 19:40:47 +020033# Legacy:
Akron0e1ed242018-10-11 13:22:00 +020034helper 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
Akron33f5c672019-06-24 19:40:47 +020044# Legacy;
Akron0e1ed242018-10-11 13:22:00 +020045helper 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
Akroncdfd9d52019-07-23 11:35:00 +020051# Expiration helper
52helper 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};
Akron0e1ed242018-10-11 13:22:00 +020069
Akron6d49c1f2018-10-11 14:22:21 +020070# Load fixture responses
71helper 'load_response' => sub {
72 my $c = shift;
73 my $q_name = shift;
74 my $file = $fixture_path->child("response_$q_name.json");
Akron8ea84292018-10-24 13:41:52 +020075 $c->app->log->debug("Load response from $file");
76
Akron6d49c1f2018-10-11 14:22:21 +020077 unless (-f $file) {
78 return {
79 status => 500,
80 json => {
81 errors => [[0, 'Unable to load query response from ' . $file]]
82 }
83 }
84 };
Akron8ea84292018-10-24 13:41:52 +020085
Akron6d49c1f2018-10-11 14:22:21 +020086 my $response = $file->slurp;
Akrona3c353c2019-02-14 23:50:00 +010087 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;
Akron6d49c1f2018-10-11 14:22:21 +020098};
99
Akron1a9d5be2020-03-19 17:28:33 +0100100app->defaults('oauth.client_list' => []);
101
Akron6d49c1f2018-10-11 14:22:21 +0200102
Akron0e1ed242018-10-11 13:22:00 +0200103# Base page
Akron63d963b2019-07-05 15:35:51 +0200104get '/v1.0/' => sub {
Akron6d49c1f2018-10-11 14:22:21 +0200105 shift->render(text => 'Fake server available');
Akron0e1ed242018-10-11 13:22:00 +0200106};
107
Akron32396632018-10-11 17:08:37 +0200108
Akrond00b4272020-02-05 17:00:33 +0100109get '/v1.0/redirect-target-a' => sub {
110 shift->render(text => 'Redirect Target!');
111} => 'redirect-target';
112
113
114# Base page
115get '/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
Akron0e1ed242018-10-11 13:22:00 +0200123# Search fixtures
Akron63d963b2019-07-05 15:35:51 +0200124get '/v1.0/search' => sub {
Akron0e1ed242018-10-11 13:22:00 +0200125 my $c = shift;
126 my $v = $c->validation;
127 $v->optional('q');
128 $v->optional('page');
129 $v->optional('ql');
Akroncd42a142019-07-12 18:55:37 +0200130 $v->optional('cq');
Akron0e1ed242018-10-11 13:22:00 +0200131 $v->optional('count');
132 $v->optional('context');
Akron8ea84292018-10-24 13:41:52 +0200133 $v->optional('offset');
Akronc58bfc42020-10-05 12:09:45 +0200134 $v->optional('pipes');
Akron8ea84292018-10-24 13:41:52 +0200135 $v->optional('cutoff')->in(qw/true false/);
Akron0e1ed242018-10-11 13:22:00 +0200136
Akron32396632018-10-11 17:08:37 +0200137 $c->app->log->debug('Receive request');
138
Akron0e1ed242018-10-11 13:22:00 +0200139 # 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
Akron6d49c1f2018-10-11 14:22:21 +0200149 if (!$v->param('q')) {
Akron8ea84292018-10-24 13:41:52 +0200150 return $c->render(%{$c->load_response('query_no_query')});
Akron0e1ed242018-10-11 13:22:00 +0200151 };
152
Akroncce055c2021-07-02 12:18:03 +0200153 if ($v->param('q') eq 'error') {
154 return $c->render(
155 status => 500,
156 inline => '<html><head>ERROR</head></html>'
157 );
158 };
159
Akron8ea84292018-10-24 13:41:52 +0200160 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');
Akroncd42a142019-07-12 18:55:37 +0200164 push @slug_base, 'cq' if defined $v->param('cq');
Akronc58bfc42020-10-05 12:09:45 +0200165 push @slug_base, 'p' . $v->param('pipes') if defined $v->param('pipes');
Akron8ea84292018-10-24 13:41:52 +0200166
Akron6d49c1f2018-10-11 14:22:21 +0200167 # Get response based on query parameter
Akron8ea84292018-10-24 13:41:52 +0200168 my $response = $c->load_response('query_' . slugify(join('_', @slug_base)));
Akron0e1ed242018-10-11 13:22:00 +0200169
170 # Check authentification
171 if (my $auth = $c->req->headers->header('Authorization')) {
Akron33f5c672019-06-24 19:40:47 +0200172
Akroncdfd9d52019-07-23 11:35:00 +0200173 $c->app->log->debug("There is an authorization header $auth");
Akron33f5c672019-06-24 19:40:47 +0200174 my $jwt;
175 if ($auth =~ /^Bearer/) {
176 # Username unknown in OAuth2
177 $response->{json}->{meta}->{authorized} = 'yes';
178 }
Akroncdfd9d52019-07-23 11:35:00 +0200179 elsif ($auth =~ /^api_token/ && ($jwt = $c->jwt_decode($auth))) {
Akron6d49c1f2018-10-11 14:22:21 +0200180 $response->{json}->{meta}->{authorized} = $jwt->{username} if $jwt->{username};
Akron0e1ed242018-10-11 13:22:00 +0200181 };
Akroncdfd9d52019-07-23 11:35:00 +0200182
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 }
Akron0e1ed242018-10-11 13:22:00 +0200207 };
208
Akronc58bfc42020-10-05 12:09:45 +0200209 if ($v->param('pipes')) {
210 $response->{json}->{meta}->{pipes} = $v->param('pipes');
Akron7b9a1962020-07-02 09:52:53 +0200211 };
212
Akron6d49c1f2018-10-11 14:22:21 +0200213 # Set page parameter
Akron0e1ed242018-10-11 13:22:00 +0200214 if ($v->param('page')) {
Akron6d49c1f2018-10-11 14:22:21 +0200215 $response->{json}->{meta}->{startIndex} = $v->param("startIndex");
Akron0e1ed242018-10-11 13:22:00 +0200216 };
217
Akron0e1ed242018-10-11 13:22:00 +0200218 # Simple search fixture
Akron32396632018-10-11 17:08:37 +0200219 $c->render(%$response);
220
221 $c->app->log->debug('Rendered result');
222
223 return 1;
Akron0e1ed242018-10-11 13:22:00 +0200224};
225
Akron80a84b22018-10-24 17:44:24 +0200226# Textinfo fixtures
Akron63d963b2019-07-05 15:35:51 +0200227get '/v1.0/corpus/:corpusId/:docId/:textId' => sub {
Akron80a84b22018-10-24 17:44:24 +0200228 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
Akron0e1ed242018-10-11 13:22:00 +0200244
Akronb80341d2018-10-15 19:46:23 +0200245# Matchinfo fixtures
Akron63d963b2019-07-05 15:35:51 +0200246get '/v1.0/corpus/:corpusId/:docId/:textId/:matchId/matchInfo' => sub {
Akronb80341d2018-10-15 19:46:23 +0200247 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
Akronb8d0b402018-10-18 23:51:52 +0200257 my $slug = slugify($file);
258
Akronb80341d2018-10-15 19:46:23 +0200259 # Get response based on query parameter
Akronb8d0b402018-10-18 23:51:52 +0200260 my $response = $c->load_response($slug);
Akronb80341d2018-10-15 19:46:23 +0200261 return $c->render(%$response);
262};
263
Akron0e1ed242018-10-11 13:22:00 +0200264
Akronbe61f4c2018-10-20 00:52:58 +0200265# Statistics endpoint
Akron63d963b2019-07-05 15:35:51 +0200266get '/v1.0/statistics' => sub {
Akronbe61f4c2018-10-20 00:52:58 +0200267 my $c = shift;
268 my $v = $c->validation;
Akron5fa61e92019-07-15 11:56:11 +0200269 $v->optional('cq');
Akronbe61f4c2018-10-20 00:52:58 +0200270
271 my @list = 'corpusinfo';
Akron5fa61e92019-07-15 11:56:11 +0200272 if ($v->param('cq')) {
273 push @list, $v->param('cq');
Akronbe61f4c2018-10-20 00:52:58 +0200274 };
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
Akron0e1ed242018-10-11 13:22:00 +0200282############
283# Auth API #
284############
285
286# Request API token
Akron63d963b2019-07-05 15:35:51 +0200287get '/v1.0/auth/logout' => sub {
Akron0e1ed242018-10-11 13:22:00 +0200288 my $c = shift;
289
290 if (my $auth = $c->req->headers->header('Authorization')) {
Akroncdfd9d52019-07-23 11:35:00 +0200291
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)) {
Akron0e1ed242018-10-11 13:22:00 +0200298 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
Akron63d963b2019-07-05 15:35:51 +0200310get '/v1.0/auth/apiToken' => sub {
Akron0e1ed242018-10-11 13:22:00 +0200311 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 );
Akron3d673062019-01-29 15:54:16 +0100347 }
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 );
Akron0e1ed242018-10-11 13:22:00 +0200357 };
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
Akron33f5c672019-06-24 19:40:47 +0200373
374# Request API token
Akron63d963b2019-07-05 15:35:51 +0200375post '/v1.0/oauth2/token' => sub {
Akron33f5c672019-06-24 19:40:47 +0200376 my $c = shift;
377
Akron63d963b2019-07-05 15:35:51 +0200378 my $grant_type = $c->param('grant_type') // 'undefined';
379
380 if ($grant_type eq 'password') {
Akron33f5c672019-06-24 19:40:47 +0200381
Akron8bbbecf2019-07-01 18:57:30 +0200382 # 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 }
Akron33f5c672019-06-24 19:40:47 +0200392
Akron8bbbecf2019-07-01 18:57:30 +0200393 # 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 }
Akron33f5c672019-06-24 19:40:47 +0200403
Akron8bbbecf2019-07-01 18:57:30 +0200404 # 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,
Akron33f5c672019-06-24 19:40:47 +0200432 "errors" => [[2022,"LDAP Authentication failed due to unknown user or password!"]]
Akron8bbbecf2019-07-01 18:57:30 +0200433 });
434 }
435
436 # Return fine access
437 return $c->render(
438 json => {
Akroncdfd9d52019-07-23 11:35:00 +0200439 "access_token" => $c->get_token('access_token'),
440 "refresh_token" => $c->get_token('refresh_token'),
Akron8bbbecf2019-07-01 18:57:30 +0200441 "scope" => "all",
442 "token_type" => "Bearer",
443 "expires_in" => 86400
444 });
445 }
446
447 # Refresh token
Akron63d963b2019-07-05 15:35:51 +0200448 elsif ($grant_type eq 'refresh_token') {
Akroncdfd9d52019-07-23 11:35:00 +0200449
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
Akron8bbbecf2019-07-01 18:57:30 +0200462 return $c->render(
463 status => 200,
464 json => {
Akroncdfd9d52019-07-23 11:35:00 +0200465 "access_token" => $c->get_token("access_token_2"),
466 "refresh_token" => $c->get_token("refresh_token_2"),
Akron8bbbecf2019-07-01 18:57:30 +0200467 "token_type" => "Bearer",
468 "expires_in" => 86400
Akron33f5c672019-06-24 19:40:47 +0200469 }
470 );
471 }
472
Akron83209f72021-01-29 17:54:15 +0100473 # 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
Akron8bbbecf2019-07-01 18:57:30 +0200488 # Unknown token grant
489 else {
490 return $c->render(
Akron63d963b2019-07-05 15:35:51 +0200491 status => 400,
Akron8bbbecf2019-07-01 18:57:30 +0200492 json => {
493 "errors" => [
494 [
Akron63d963b2019-07-05 15:35:51 +0200495 0, "Grant Type unknown", $grant_type
Akron8bbbecf2019-07-01 18:57:30 +0200496 ]
497 ]
498 }
499 )
Akron33f5c672019-06-24 19:40:47 +0200500 }
Akron33f5c672019-06-24 19:40:47 +0200501};
502
Akron4cefe1f2019-09-04 10:11:28 +0200503# Revoke API token
504post '/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};
Akron33f5c672019-06-24 19:40:47 +0200523
Akron59992122019-10-29 11:28:45 +0100524# Register a client
525post '/v1.0/oauth2/client/register' => sub {
526 my $c = shift;
527 my $json = $c->req->json;
528
Akrondc50c892021-05-05 18:12:02 +0200529 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
Akron59992122019-10-29 11:28:45 +0100543 my $name = $json->{name};
Akron1a9d5be2020-03-19 17:28:33 +0100544 my $desc = $json->{description};
Akron59992122019-10-29 11:28:45 +0100545 my $type = $json->{type};
546 my $url = $json->{url};
Akrondc50c892021-05-05 18:12:02 +0200547 my $redirect_url = $json->{redirect_uri};
Akron59992122019-10-29 11:28:45 +0100548
Akron1a9d5be2020-03-19 17:28:33 +0100549 my $list = $c->app->defaults('oauth.client_list');
550
551 push @$list, {
Akrondc50c892021-05-05 18:12:02 +0200552 "client_id" => $tokens{new_client_id},
553 "client_name" => $name,
Akronbc94a9c2021-04-15 00:07:35 +0200554 "client_description" => $desc,
555 "client_url" => $url
Akron1a9d5be2020-03-19 17:28:33 +0100556 };
557
Akron59992122019-10-29 11:28:45 +0100558 # 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
Akron33f5c672019-06-24 19:40:47 +0200572
Akron0f1b93b2020-03-17 11:37:19 +0100573# Register a client
574post '/v1.0/oauth2/client/list' => sub {
575 my $c = shift;
576
Akron276afc02021-06-14 11:00:21 +0200577 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
Akron0f1b93b2020-03-17 11:37:19 +0100589 # $c->param('client_secret');
Akron1a9d5be2020-03-19 17:28:33 +0100590
591 # Is empty [] when nothing registered
592
Akron0f1b93b2020-03-17 11:37:19 +0100593 return $c->render(
Akron1a9d5be2020-03-19 17:28:33 +0100594 json => $c->stash('oauth.client_list'),
595 status => 200
596 );
597};
598
Akronbc94a9c2021-04-15 00:07:35 +0200599
600# Get token list
601post '/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
Akron1a9d5be2020-03-19 17:28:33 +0100622del '/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++) {
Akrondc50c892021-05-05 18:12:02 +0200630 if ($list->[$i]->{client_id} eq $client_id) {
Akron1a9d5be2020-03-19 17:28:33 +0100631 $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"
Akron0f1b93b2020-03-17 11:37:19 +0100645 },
Akron1a9d5be2020-03-19 17:28:33 +0100646 status => 401
647 );
648 };
649
650 return $c->render(
651 json => $c->stash('oauth.client_list'),
Akron0f1b93b2020-03-17 11:37:19 +0100652 status => 200
653 );
654};
655
Akron83209f72021-01-29 17:54:15 +0100656post '/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
Akron0f1b93b2020-03-17 11:37:19 +0100673
Akronabdf9a92021-01-12 19:06:57 +0100674#######################
675# Query Reference API #
676#######################
677
678use CHI;
679my $chi = CHI->new(
680 driver => 'Memory',
681 global => 1
682);
683
684# Store query
685put '/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
731get '/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
758get '/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
771del '/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
Akronc1aaf932021-06-09 12:19:15 +0200793post '/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
Akron0f1b93b2020-03-17 11:37:19 +0100803
Akron0e1ed242018-10-11 13:22:00 +0200804app->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 };