| 1 | #!perl | ||
| 2 | package API::Google::Server; | ||
| 3 | $API::Google::Server::VERSION = '0.12'; | ||
| 4 | # ABSTRACT: Mojolicious::Lite web server for getting Google API tokens via Oauth 2.0 | ||
| 5 | |||
| 6 | use Mojolicious::Lite; | ||
| 7 | use Data::Dumper; | ||
| 8 | use Config::JSON; | ||
| 9 | use Tie::File; | ||
| 10 | use Crypt::JWT qw(decode_jwt); | ||
| 11 | use feature 'say'; | ||
| 12 | use Mojo::Util 'getopt'; | ||
| 13 | use Mojolicious::Plugin::OAuth2; | ||
| 14 | |||
| 15 | # use Mojo::JWT; | ||
| 16 | |||
| 17 | # sub return_json_filename { | ||
| 18 | # use Cwd; | ||
| 19 | # my $cwd = getcwd; | ||
| 20 | # opendir my $dir, $cwd or die "Cannot open directory: $!"; | ||
| 21 | # my @files = readdir $dir; | ||
| 22 | # my @j = grep { $_ =~ /\w+.json/ } @files; | ||
| 23 | # return $j[0]; | ||
| 24 | # } | ||
| 25 | |||
| 26 | |||
| 27 | # my $f = return_json_filename(); | ||
| 28 | |||
| 29 | my $config = Config::JSON->new($ENV{'GOAUTH_TOKENSFILE'}); | ||
| 30 | delete $ENV{'GOAUTH_TOKENSFILE'}; | ||
| 31 | |||
| 32 | # authorize_url and token_url can be retrieved from OAuth discovery document | ||
| 33 | # https://github.com/marcusramberg/Mojolicious-Plugin-OAuth2/issues/52 | ||
| 34 | plugin "OAuth2" => { | ||
| 35 | google => { | ||
| 36 | key => $config->get('gapi/client_id'), # $config->{gapi}{client_id}, | ||
| 37 | secret => $config->get('gapi/client_secret'), #$config->{gapi}{client_secret}, | ||
| 38 | authorize_url => 'https://accounts.google.com/o/oauth2/v2/auth?response_type=code', | ||
| 39 | token_url => 'https://www.googleapis.com/oauth2/v4/token' | ||
| 40 | } | ||
| 41 | }; | ||
| 42 | |||
| 43 | |||
| 44 | |||
| 45 | helper get_new_tokens => sub { | ||
| 46 | my ($c,$auth_code) = @_; | ||
| 47 | my $hash = {}; | ||
| 48 | $hash->{code} = $c->param('code'); | ||
| 49 | $hash->{redirect_uri} = $c->url_for->to_abs->to_string; | ||
| 50 | $hash->{client_id} = $config->get('gapi/client_id'); | ||
| 51 | $hash->{client_secret} = $config->get('gapi/client_secret'); | ||
| 52 | $hash->{grant_type} = 'authorization_code'; | ||
| 53 | my $tokens = $c->ua->post('https://www.googleapis.com/oauth2/v4/token' => form => $hash)->res->json; | ||
| 54 | return $tokens; | ||
| 55 | }; | ||
| 56 | |||
| 57 | # =method get_all_google_jwk_keys | ||
| 58 | |||
| 59 | # Get all Google JWK keys for validation of JSON Web Token | ||
| 60 | |||
| 61 | # Check https://jwt.io/ and https://developers.google.com/identity/protocols/OpenIDConnect#validatinganidtoken for more details | ||
| 62 | |||
| 63 | # return arrayref | ||
| 64 | |||
| 65 | # =cut | ||
| 66 | |||
| 67 | # helper get_all_google_jwk_keys => sub { | ||
| 68 | # my $c = shift; | ||
| 69 | # my $certs = $c->ua->get('https://www.googleapis.com/oauth2/v3/certs')->res->json; | ||
| 70 | # # return $certs; | ||
| 71 | # my @keys = @{$certs->{keys}}; | ||
| 72 | # return \@keys; | ||
| 73 | # # return $certs->{keys}[1]; | ||
| 74 | # }; | ||
| 75 | |||
| 76 | # =method get_google_jwk_key_by_kid | ||
| 77 | |||
| 78 | # Return JWK key with specified kid | ||
| 79 | |||
| 80 | # $c->get_google_cert_by_kid($kid,$crts) # $kid - string, $crts - arrayref | ||
| 81 | |||
| 82 | # Example of usage: | ||
| 83 | |||
| 84 | # $c->get_google_cert_by_kid($header->{kid},$crts) | ||
| 85 | |||
| 86 | # =cut | ||
| 87 | |||
| 88 | |||
| 89 | # helper get_google_jwk_key_by_kid => sub { | ||
| 90 | # my ($c, $kid, $jwks_arrayref) = @_; | ||
| 91 | |||
| 92 | # if (!defined $jwks_arrayref) { | ||
| 93 | # warn 'get_google_cert_by_kid(): $ctrs is not defined, obtaining from Google...'; | ||
| 94 | # $jwks_arrayref = $c->ua->get('https://www.googleapis.com/oauth2/v3/certs')->res->json->{keys}; | ||
| 95 | # } | ||
| 96 | |||
| 97 | # # my @keys = @{$crts->{keys}}; | ||
| 98 | # my @keys = @$jwks_arrayref; | ||
| 99 | # my $size = scalar @keys; | ||
| 100 | # warn "Found $size JWK keys"; | ||
| 101 | |||
| 102 | # if (!defined $kid) { | ||
| 103 | # warn 'get_google_cert_by_kid(): $kid is not defined, will return random certificate'; | ||
| 104 | # return $keys[rand @keys]; | ||
| 105 | # } else { | ||
| 106 | # for (@keys) { | ||
| 107 | # if ($_->{kid} eq $kid) { | ||
| 108 | # return $_; | ||
| 109 | # } | ||
| 110 | # } | ||
| 111 | # } | ||
| 112 | |||
| 113 | # warn 'JWK key with particular kid not found'; | ||
| 114 | # return undef; | ||
| 115 | # }; | ||
| 116 | |||
| 117 | |||
| 118 | |||
| 119 | |||
| 120 | helper get_email => sub { | ||
| 121 | my ($c, $access_token) = @_; | ||
| 122 | my %h = ( | ||
| 123 | 'Authorization' => 'Bearer '.$access_token | ||
| 124 | ); | ||
| 125 | $c->ua->get('https://www.googleapis.com/auth/plus.profile.emails.read' => form => \%h)->res->json; | ||
| 126 | }; | ||
| 127 | |||
| 128 | |||
| 129 | get "/" => sub { | ||
| 130 | my $c = shift; | ||
| 131 | app->log->info("Will store tokens at".$config->getFilename ($config->pathToFile)); | ||
| 132 | if ($c->param('code')) { | ||
| 133 | app->log->info("Authorization code was retrieved: ".$c->param('code')); | ||
| 134 | |||
| 135 | my $tokens = $c->get_new_tokens($c->param('code')); | ||
| 136 | app->log->info("App got new tokens: ".Dumper $tokens); | ||
| 137 | |||
| 138 | if ($tokens) { | ||
| 139 | my $user_data; | ||
| 140 | # warn Dumper $user_data; | ||
| 141 | # you can use https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=XYZ123 for development | ||
| 142 | |||
| 143 | if ($tokens->{id_token}) { | ||
| 144 | |||
| 145 | # my $jwt = Mojo::JWT->new(claims => $tokens->{id_token}); | ||
| 146 | # warn "Mojo header:".Dumper $jwt->header; | ||
| 147 | |||
| 148 | # my $keys = $c->get_all_google_jwk_keys(); # arrayref | ||
| 149 | # my ($header, $data) = decode_jwt( token => $tokens->{id_token}, decode_header => 1, key => '' ); # exctract kid | ||
| 150 | # warn "Decode header :".Dumper $header; | ||
| 151 | |||
| 152 | $user_data = decode_jwt( | ||
| 153 | token => $tokens->{id_token}, | ||
| 154 | kid_keys => $c->ua->get('https://www.googleapis.com/oauth2/v3/certs')->res->json, | ||
| 155 | ); | ||
| 156 | |||
| 157 | warn "Decoded user data:".Dumper $user_data; | ||
| 158 | }; | ||
| 159 | |||
| 160 | #$user_data->{email}; | ||
| 161 | #$user_data->{family_name} | ||
| 162 | #$user_data->{given_name} | ||
| 163 | |||
| 164 | # $tokensfile->set('tokens/'.$user_data->{email}, $tokens->{access_token}); | ||
| 165 | $config->addToHash('gapi/tokens/'.$user_data->{email}, 'access_token', $tokens->{access_token} ); | ||
| 166 | |||
| 167 | if ($tokens->{refresh_token}) { | ||
| 168 | $config->addToHash('gapi/tokens/'.$user_data->{email}, 'refresh_token', $tokens->{refresh_token}); | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | $c->render( json => $config->get('gapi') ); | ||
| 173 | } else { | ||
| 174 | $c->render(template => 'oauth'); | ||
| 175 | } | ||
| 176 | }; | ||
| 177 | |||
| 178 | app->start; | ||
| 179 | |||
| 180 | =pod | ||
| 181 | |||
| 182 | =encoding UTF-8 | ||
| 183 | |||
| 184 | =head1 NAME | ||
| 185 | |||
| 186 | API::Google::Server - Mojolicious::Lite web server for getting Google API tokens via Oauth 2.0 | ||
| 187 | |||
| 188 | =head1 VERSION | ||
| 189 | |||
| 190 | version 0.12 | ||
| 191 | |||
| 192 | =head1 METHODS | ||
| 193 | |||
| 194 | =head2 get_new_tokens | ||
| 195 | |||
| 196 | Get new access_token by auth_code | ||
| 197 | |||
| 198 | =head2 get_email | ||
| 199 | |||
| 200 | Get email address of API user | ||
| 201 | |||
| 202 | =head1 AUTHOR | ||
| 203 | |||
| 204 | Pavel Serikov <pavelsr@cpan.org> | ||
| 205 | |||
| 206 | =head1 COPYRIGHT AND LICENSE | ||
| 207 | |||
| 208 | This software is copyright (c) 2016 by Pavel Serikov. | ||
| 209 | |||
| 210 | This is free software; you can redistribute it and/or modify it under | ||
| 211 | the same terms as the Perl 5 programming language system itself. | ||
| 212 | |||
| 213 | =cut | ||
| 214 | |||
| 215 | __DATA__ |