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