blob: a1d390893ab97e656a9931d54088008a46492bd6 [file] [log] [blame]
Akron0e1ed242018-10-11 13:22:00 +02001#!/usr/bin/env perl
2use Mojolicious::Lite;
3use Mojo::ByteStream 'b';
4use Mojo::Date;
5use Mojo::JSON qw/true false encode_json decode_json/;
6use strict;
7use warnings;
8use Mojo::JWT;
9use Mojo::File;
10
11# This is an API fake server with fixtures
12
13my $secret = 's3cr3t';
14
15helper jwt_encode => sub {
16 shift;
17 return Mojo::JWT->new(
18 secret => $secret,
19 token_type => 'api_token',
20 expires => time + (3 * 34 * 60 * 60),
21 claims => { @_ }
22 );
23};
24
25helper jwt_decode => sub {
26 my ($c, $auth) = @_;
27 $auth =~ s/\s*api_token\s+//;
28 return Mojo::JWT->new(secret => $secret)->decode($auth);
29};
30
31
32# Base page
33get '/' => sub {
34 shift->render(text => 'Query fake server available');
35};
36
37# Search fixtures
38get '/search' => sub {
39 my $c = shift;
40 my $v = $c->validation;
41 $v->optional('q');
42 $v->optional('page');
43 $v->optional('ql');
44 $v->optional('count');
45 $v->optional('context');
46
47 # Response q=x&ql=cosmas3
48 if ($v->param('ql') && $v->param('ql') eq 'cosmas3') {
49 return $c->render(
50 status => 400,
51 json => {
52 "\@context" => "http://korap.ids-mannheim.de/ns/koral/0.3/context.jsonld",
53 "errors" => [[307,"cosmas3 is not a supported query language!"]]
54 });
55 };
56
57 if ($v->param('q')) {
58
59 my $q = $v->param('q');
60
61 # Response q=server_fail
62 if ($q eq 'server_fail') {
63 return $c->render(
64 status => 500,
65 inline => 'Oooops'
66 );
67 }
68
69 # Response q=[orth=das
70 if ($q eq '[orth=das') {
71 return $c->render(
72 status => 400,
73 json => {
74 "\@context" => "http://korap.ids-mannheim.de/ns/koral/0.3/context.jsonld",
75 "errors" => [
76 [302,"Parantheses/brackets unbalanced.",0],
77 [302,"Could not parse query >>> [orth=das <<<."]
78 ]
79 }
80 );
81 }
82 };
83
84 my $response = decode_json(
85 Mojo::File->new(__FILE__)->parent->child('response_baum.jsonld')
86 );
87
88
89 # Check authentification
90 if (my $auth = $c->req->headers->header('Authorization')) {
91 if (my $jwt = $c->jwt_decode($auth)) {
92 $response->{meta}->{authorized} = $jwt->{username} if $jwt->{username};
93 };
94 };
95
96 if ($v->param('page')) {
97 $response->{meta}->{startIndex} = $v->param("startIndex");
98 };
99
100
101 # Simple search fixture
102 return $c->render(
103 json => $response
104 );
105};
106
107
108
109############
110# Auth API #
111############
112
113# Request API token
114get '/auth/logout' => sub {
115 my $c = shift;
116
117 if (my $auth = $c->req->headers->header('Authorization')) {
118 if (my $jwt = $c->jwt_decode($auth)) {
119 my $user = $jwt->{username} if $jwt->{username};
120
121 $c->app->log->debug('Server-Logout: ' . $user);
122 return $c->render(json => { msg => [[0, 'Fine!']]});
123 };
124 };
125
126 return $c->render(status => 400, json => { error => [[0, 'No!']]});
127};
128
129
130# Request API token
131get '/auth/apiToken' => sub {
132 my $c = shift;
133
134 # Get auth header
135 my $auth = $c->req->headers->authorization;
136
137 # Authorization missing or not basic
138 if (!$auth || $auth !~ s/\s*Basic\s+//gi) {
139 return $c->render(
140 json => {
141 error => [[2, 'x']]
142 }
143 );
144 };
145
146 # Decode header
147 my ($username, $pwd) = @{b($auth)->b64_decode->split(':')->to_array};
148
149 # the password is 'pass'
150 if ($pwd) {
151
152 # the password is 'pass'
153 if ($pwd eq 'pass') {
154
155 # Render info with token
156 my $jwt = $c->jwt_encode(username => $username);
157
158 # Render in the Kustvakt fashion:
159 return $c->render(
160 format => 'html',
161 text => encode_json({
162 %{$jwt->claims},
163 expires => $jwt->expires,
164 token => $jwt->encode,
165 token_type => 'api_token'
166 })
167 );
168 };
169
170 return $c->render(
171 json => {
172 error => [[2004, undef]]
173 }
174 );
175 };
176
177 return $c->render(
178 json => {
179 error => [[2004, undef]]
180 }
181 );
182};
183
184app->start;
185
186
187__END__
188
189
190 # Temporary:
191 my $collection_query = {
192 '@type' => "koral:docGroup",
193 "operation" => "operation:or",
194 "operands" => [
195 {
196 '@type' => "koral:docGroup",
197 "operation" => "operation:and",
198 "operands" => [
199 {
200 '@type' => "koral:doc",
201 "key" => "title",
202 "match" => "match:eq",
203 "value" => "Der Birnbaum",
204 "type" => "type:string"
205 },
206 {
207 '@type' => "koral:doc",
208 "key" => "pubPlace",
209 "match" => "match:eq",
210 "value" => "Mannheim",
211 "type" => "type:string"
212 },
213 {
214 '@type' => "koral:docGroup",
215 "operation" => "operation:or",
216 "operands" => [
217 {
218 '@type' => "koral:doc",
219 "key" => "subTitle",
220 "match" => "match:eq",
221 "value" => "Aufzucht oder Pflege",
222 "type" => "type:string"
223 },
224 {
225 '@type' => "koral:doc",
226 "key" => "subTitle",
227 "match" => "match:eq",
228 "value" => "Gedichte",
229 "type" => "type:string"
230 }
231 ]
232 }
233 ]
234 },
235 {
236 '@type' => "koral:doc",
237 "key" => "pubDate",
238 "match" => "match:geq",
239 "value" => "2015-03-05",
240 "type" => "type:date"
241 }
242 ]
243 };