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