| Akron | 80f731d | 2017-04-03 20:10:58 +0200 | [diff] [blame^] | 1 | #!/usr/bin/env perl |
| 2 | use Mojolicious::Lite; |
| 3 | use Mojo::ByteStream 'b'; |
| 4 | use Mojo::Date; |
| 5 | |
| 6 | # This is an API fake server with fixtures |
| 7 | |
| 8 | # Request API token |
| 9 | get '/auth/apiToken' => sub { |
| 10 | my $c = shift; |
| 11 | |
| 12 | # Get auth header |
| 13 | my $auth = $c->req->headers->authorization; |
| 14 | |
| 15 | # Authorization missing or not basic |
| 16 | if (!$auth || $auth =~ s/\s*Basic\s+//gi) { |
| 17 | return $c->render( |
| 18 | json => { |
| 19 | error => [2, 'x'] |
| 20 | } |
| 21 | ); |
| 22 | }; |
| 23 | |
| 24 | # Decode header |
| 25 | my ($username, $pwd) = @{b($auth)->b64_decode->split(':')->to_array}; |
| 26 | |
| 27 | if ($pwd eq 'test') { |
| 28 | |
| 29 | # Render info with token |
| 30 | return $c->render( |
| 31 | json => { |
| 32 | username => $username, |
| 33 | expires => Mojo::Date->new(time + (3 * 34 * 60 * 60)), |
| 34 | token => 'abcdefg', |
| 35 | token_type => 'api_token' |
| 36 | } |
| 37 | ); |
| 38 | }; |
| 39 | |
| 40 | return $c->render( |
| 41 | json => { |
| 42 | error => [] |
| 43 | } |
| 44 | ); |
| 45 | }; |
| 46 | |
| 47 | app->start; |
| 48 | |
| 49 | 1; |