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 | |
| 17 | helper jwt_encode => sub { |
| 18 | shift; |
| 19 | return Mojo::JWT->new( |
| 20 | secret => $secret, |
| 21 | token_type => 'api_token', |
| 22 | expires => time + (3 * 34 * 60 * 60), |
| 23 | claims => { @_ } |
| 24 | ); |
| 25 | }; |
| 26 | |
| 27 | helper jwt_decode => sub { |
| 28 | my ($c, $auth) = @_; |
| 29 | $auth =~ s/\s*api_token\s+//; |
| 30 | return Mojo::JWT->new(secret => $secret)->decode($auth); |
| 31 | }; |
| 32 | |
| 33 | |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 34 | # Load fixture responses |
| 35 | helper 'load_response' => sub { |
| 36 | my $c = shift; |
| 37 | my $q_name = shift; |
| 38 | my $file = $fixture_path->child("response_$q_name.json"); |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 39 | $c->app->log->debug("Load response from $file"); |
| 40 | |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 41 | unless (-f $file) { |
| 42 | return { |
| 43 | status => 500, |
| 44 | json => { |
| 45 | errors => [[0, 'Unable to load query response from ' . $file]] |
| 46 | } |
| 47 | } |
| 48 | }; |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 49 | |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 50 | my $response = $file->slurp; |
| 51 | return decode_json($response); |
| 52 | }; |
| 53 | |
| 54 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 55 | # Base page |
| 56 | get '/' => sub { |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 57 | shift->render(text => 'Fake server available'); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 58 | }; |
| 59 | |
Akron | 3239663 | 2018-10-11 17:08:37 +0200 | [diff] [blame] | 60 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 61 | # Search fixtures |
| 62 | get '/search' => sub { |
| 63 | my $c = shift; |
| 64 | my $v = $c->validation; |
| 65 | $v->optional('q'); |
| 66 | $v->optional('page'); |
| 67 | $v->optional('ql'); |
| 68 | $v->optional('count'); |
| 69 | $v->optional('context'); |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 70 | $v->optional('offset'); |
| 71 | $v->optional('cutoff')->in(qw/true false/); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 72 | |
Akron | 3239663 | 2018-10-11 17:08:37 +0200 | [diff] [blame] | 73 | $c->app->log->debug('Receive request'); |
| 74 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 75 | # Response q=x&ql=cosmas3 |
| 76 | if ($v->param('ql') && $v->param('ql') eq 'cosmas3') { |
| 77 | return $c->render( |
| 78 | status => 400, |
| 79 | json => { |
| 80 | "\@context" => "http://korap.ids-mannheim.de/ns/koral/0.3/context.jsonld", |
| 81 | "errors" => [[307,"cosmas3 is not a supported query language!"]] |
| 82 | }); |
| 83 | }; |
| 84 | |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 85 | if (!$v->param('q')) { |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 86 | return $c->render(%{$c->load_response('query_no_query')}); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 87 | }; |
| 88 | |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 89 | my @slug_base = ($v->param('q')); |
| 90 | push @slug_base, 'o' . $v->param('offset') if defined $v->param('offset'); |
| 91 | push @slug_base, 'c' . $v->param('count') if defined $v->param('count'); |
| 92 | push @slug_base, 'co' . $v->param('cutoff') if defined $v->param('cutoff'); |
| 93 | |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 94 | # Get response based on query parameter |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 95 | my $response = $c->load_response('query_' . slugify(join('_', @slug_base))); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 96 | |
| 97 | # Check authentification |
| 98 | if (my $auth = $c->req->headers->header('Authorization')) { |
| 99 | if (my $jwt = $c->jwt_decode($auth)) { |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 100 | $response->{json}->{meta}->{authorized} = $jwt->{username} if $jwt->{username}; |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 101 | }; |
| 102 | }; |
| 103 | |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 104 | # Set page parameter |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 105 | if ($v->param('page')) { |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 106 | $response->{json}->{meta}->{startIndex} = $v->param("startIndex"); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 107 | }; |
| 108 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 109 | # Simple search fixture |
Akron | 3239663 | 2018-10-11 17:08:37 +0200 | [diff] [blame] | 110 | $c->render(%$response); |
| 111 | |
| 112 | $c->app->log->debug('Rendered result'); |
| 113 | |
| 114 | return 1; |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 115 | }; |
| 116 | |
Akron | 80a84b2 | 2018-10-24 17:44:24 +0200 | [diff] [blame] | 117 | # Textinfo fixtures |
| 118 | get '/corpus/:corpusId/:docId/:textId' => sub { |
| 119 | my $c = shift; |
| 120 | |
| 121 | my $file = join('_', ( |
| 122 | 'textinfo', |
| 123 | $c->stash('corpusId'), |
| 124 | $c->stash('docId'), |
| 125 | $c->stash('textId') |
| 126 | )); |
| 127 | |
| 128 | my $slug = slugify($file); |
| 129 | |
| 130 | # Get response based on query parameter |
| 131 | my $response = $c->load_response($slug); |
| 132 | return $c->render(%$response); |
| 133 | }; |
| 134 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 135 | |
Akron | b80341d | 2018-10-15 19:46:23 +0200 | [diff] [blame] | 136 | # Matchinfo fixtures |
| 137 | get '/corpus/:corpusId/:docId/:textId/:matchId/matchInfo' => sub { |
| 138 | my $c = shift; |
| 139 | |
| 140 | my $file = join('_', ( |
| 141 | 'matchinfo', |
| 142 | $c->stash('corpusId'), |
| 143 | $c->stash('docId'), |
| 144 | $c->stash('textId'), |
| 145 | $c->stash('matchId') |
| 146 | )); |
| 147 | |
Akron | b8d0b40 | 2018-10-18 23:51:52 +0200 | [diff] [blame] | 148 | my $slug = slugify($file); |
| 149 | |
Akron | b80341d | 2018-10-15 19:46:23 +0200 | [diff] [blame] | 150 | # Get response based on query parameter |
Akron | b8d0b40 | 2018-10-18 23:51:52 +0200 | [diff] [blame] | 151 | my $response = $c->load_response($slug); |
Akron | b80341d | 2018-10-15 19:46:23 +0200 | [diff] [blame] | 152 | return $c->render(%$response); |
| 153 | }; |
| 154 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 155 | |
Akron | be61f4c | 2018-10-20 00:52:58 +0200 | [diff] [blame] | 156 | # Statistics endpoint |
| 157 | get '/statistics' => sub { |
| 158 | my $c = shift; |
| 159 | my $v = $c->validation; |
| 160 | $v->optional('corpusQuery'); |
| 161 | |
| 162 | my @list = 'corpusinfo'; |
| 163 | if ($v->param('corpusQuery')) { |
| 164 | push @list, $v->param('corpusQuery'); |
| 165 | }; |
| 166 | my $slug = slugify(join('_', @list)); |
| 167 | |
| 168 | # Get response based on query parameter |
| 169 | my $response = $c->load_response($slug); |
| 170 | return $c->render(%$response); |
| 171 | }; |
| 172 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 173 | ############ |
| 174 | # Auth API # |
| 175 | ############ |
| 176 | |
| 177 | # Request API token |
| 178 | get '/auth/logout' => sub { |
| 179 | my $c = shift; |
| 180 | |
| 181 | if (my $auth = $c->req->headers->header('Authorization')) { |
| 182 | if (my $jwt = $c->jwt_decode($auth)) { |
| 183 | my $user = $jwt->{username} if $jwt->{username}; |
| 184 | |
| 185 | $c->app->log->debug('Server-Logout: ' . $user); |
| 186 | return $c->render(json => { msg => [[0, 'Fine!']]}); |
| 187 | }; |
| 188 | }; |
| 189 | |
| 190 | return $c->render(status => 400, json => { error => [[0, 'No!']]}); |
| 191 | }; |
| 192 | |
| 193 | |
| 194 | # Request API token |
| 195 | get '/auth/apiToken' => sub { |
| 196 | my $c = shift; |
| 197 | |
| 198 | # Get auth header |
| 199 | my $auth = $c->req->headers->authorization; |
| 200 | |
| 201 | # Authorization missing or not basic |
| 202 | if (!$auth || $auth !~ s/\s*Basic\s+//gi) { |
| 203 | return $c->render( |
| 204 | json => { |
| 205 | error => [[2, 'x']] |
| 206 | } |
| 207 | ); |
| 208 | }; |
| 209 | |
| 210 | # Decode header |
| 211 | my ($username, $pwd) = @{b($auth)->b64_decode->split(':')->to_array}; |
| 212 | |
| 213 | # the password is 'pass' |
| 214 | if ($pwd) { |
| 215 | |
| 216 | # the password is 'pass' |
| 217 | if ($pwd eq 'pass') { |
| 218 | |
| 219 | # Render info with token |
| 220 | my $jwt = $c->jwt_encode(username => $username); |
| 221 | |
| 222 | # Render in the Kustvakt fashion: |
| 223 | return $c->render( |
| 224 | format => 'html', |
| 225 | text => encode_json({ |
| 226 | %{$jwt->claims}, |
| 227 | expires => $jwt->expires, |
| 228 | token => $jwt->encode, |
| 229 | token_type => 'api_token' |
| 230 | }) |
| 231 | ); |
Akron | 3d67306 | 2019-01-29 15:54:16 +0100 | [diff] [blame^] | 232 | } |
| 233 | |
| 234 | elsif ($pwd eq 'ldaperr') { |
| 235 | return $c->render( |
| 236 | format => 'html', |
| 237 | status => 401, |
| 238 | json => { |
| 239 | "errors" => [[2022,"LDAP Authentication failed due to unknown user or password!"]] |
| 240 | } |
| 241 | ); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 242 | }; |
| 243 | |
| 244 | return $c->render( |
| 245 | json => { |
| 246 | error => [[2004, undef]] |
| 247 | } |
| 248 | ); |
| 249 | }; |
| 250 | |
| 251 | return $c->render( |
| 252 | json => { |
| 253 | error => [[2004, undef]] |
| 254 | } |
| 255 | ); |
| 256 | }; |
| 257 | |
| 258 | app->start; |
| 259 | |
| 260 | |
| 261 | __END__ |
| 262 | |
| 263 | |
| 264 | # Temporary: |
| 265 | my $collection_query = { |
| 266 | '@type' => "koral:docGroup", |
| 267 | "operation" => "operation:or", |
| 268 | "operands" => [ |
| 269 | { |
| 270 | '@type' => "koral:docGroup", |
| 271 | "operation" => "operation:and", |
| 272 | "operands" => [ |
| 273 | { |
| 274 | '@type' => "koral:doc", |
| 275 | "key" => "title", |
| 276 | "match" => "match:eq", |
| 277 | "value" => "Der Birnbaum", |
| 278 | "type" => "type:string" |
| 279 | }, |
| 280 | { |
| 281 | '@type' => "koral:doc", |
| 282 | "key" => "pubPlace", |
| 283 | "match" => "match:eq", |
| 284 | "value" => "Mannheim", |
| 285 | "type" => "type:string" |
| 286 | }, |
| 287 | { |
| 288 | '@type' => "koral:docGroup", |
| 289 | "operation" => "operation:or", |
| 290 | "operands" => [ |
| 291 | { |
| 292 | '@type' => "koral:doc", |
| 293 | "key" => "subTitle", |
| 294 | "match" => "match:eq", |
| 295 | "value" => "Aufzucht oder Pflege", |
| 296 | "type" => "type:string" |
| 297 | }, |
| 298 | { |
| 299 | '@type' => "koral:doc", |
| 300 | "key" => "subTitle", |
| 301 | "match" => "match:eq", |
| 302 | "value" => "Gedichte", |
| 303 | "type" => "type:string" |
| 304 | } |
| 305 | ] |
| 306 | } |
| 307 | ] |
| 308 | }, |
| 309 | { |
| 310 | '@type' => "koral:doc", |
| 311 | "key" => "pubDate", |
| 312 | "match" => "match:geq", |
| 313 | "value" => "2015-03-05", |
| 314 | "type" => "type:date" |
| 315 | } |
| 316 | ] |
| 317 | }; |