| 1 | #!perl | ||
| 2 | package Moo::Google::Server; | ||
| 3 | $Moo::Google::Server::VERSION = '0.03'; | ||
| 4 | |||
| 5 | # ABSTRACT: Mojolicious::Lite web server for getting Google Mojo tokens via Oauth 2.0 | ||
| 6 | |||
| 7 | use Mojolicious::Lite; | ||
| 8 | use Data::Dumper; | ||
| 9 | use Config::JSON; | ||
| 10 | use Tie::File; | ||
| 11 | use Crypt::JWT qw(decode_jwt); | ||
| 12 | use feature 'say'; | ||
| 13 | use Mojo::Util 'getopt'; | ||
| 14 | use Mojolicious::Plugin::OAuth2; | ||
| 15 | |||
| 16 | # use Mojo::JWT; | ||
| 17 | |||
| 18 | # sub return_json_filename { | ||
| 19 | # use Cwd; | ||
| 20 | # my $cwd = getcwd; | ||
| 21 | # opendir my $dir, $cwd or die "Cannot open directory: $!"; | ||
| 22 | # my @files = readdir $dir; | ||
| 23 | # my @j = grep { $_ =~ /\w+.json/ } @files; | ||
| 24 | # return $j[0]; | ||
| 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') | ||
| 38 | , #$config->{gapi}{client_secret}, | ||
| 39 | authorize_url => | ||
| 40 | 'https://accounts.google.com/o/oauth2/v2/auth?response_type=code', | ||
| 41 | token_url => 'https://www.googleapis.com/oauth2/v4/token' | ||
| 42 | } | ||
| 43 | }; | ||
| 44 | |||
| 45 | |||
| 46 | helper get_new_tokens => sub { | ||
| 47 | my ( $c, $auth_code ) = @_; | ||
| 48 | my $hash = {}; | ||
| 49 | $hash->{code} = $c->param('code'); | ||
| 50 | $hash->{redirect_uri} = $c->url_for->to_abs->to_string; | ||
| 51 | $hash->{client_id} = $config->get('gapi/client_id'); | ||
| 52 | $hash->{client_secret} = $config->get('gapi/client_secret'); | ||
| 53 | $hash->{grant_type} = 'authorization_code'; | ||
| 54 | my $tokens = $c->ua->post( | ||
| 55 | 'https://www.googleapis.com/oauth2/v4/token' => form => $hash ) | ||
| 56 | ->res->json; | ||
| 57 | return $tokens; | ||
| 58 | }; | ||
| 59 | |||
| 60 | |||
| 61 | helper get_email => sub { | ||
| 62 | my ( $c, $access_token ) = @_; | ||
| 63 | my %h = ( 'Authorization' => 'Bearer ' . $access_token ); | ||
| 64 | $c->ua->get( | ||
| 65 | 'https://www.googleapis.com/auth/plus.profile.emails.read' => form => | ||
| 66 | \%h )->res->json; | ||
| 67 | }; | ||
| 68 | |||
| 69 | get "/" => sub { | ||
| 70 | my $c = shift; | ||
| 71 | app->log->info( | ||
| 72 | "Will store tokens at" . $config->getFilename( $config->pathToFile ) ); | ||
| 73 | if ( $c->param('code') ) { | ||
| 74 | app->log->info( | ||
| 75 | "Authorization code was retrieved: " . $c->param('code') ); | ||
| 76 | |||
| 77 | my $tokens = $c->get_new_tokens( $c->param('code') ); | ||
| 78 | app->log->info( "App got new tokens: " . Dumper $tokens); | ||
| 79 | |||
| 80 | if ($tokens) { | ||
| 81 | my $user_data; | ||
| 82 | |||
| 83 | # warn Dumper $user_data; | ||
| 84 | # you can use https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=XYZ123 for development | ||
| 85 | |||
| 86 | if ( $tokens->{id_token} ) { | ||
| 87 | |||
| 88 | # my $jwt = Mojo::JWT->new(claims => $tokens->{id_token}); | ||
| 89 | # warn "Mojo header:".Dumper $jwt->header; | ||
| 90 | |||
| 91 | # my $keys = $c->get_all_google_jwk_keys(); # arrayref | ||
| 92 | # my ($header, $data) = decode_jwt( token => $tokens->{id_token}, decode_header => 1, key => '' ); # exctract kid | ||
| 93 | # warn "Decode header :".Dumper $header; | ||
| 94 | |||
| 95 | $user_data = decode_jwt( | ||
| 96 | token => $tokens->{id_token}, | ||
| 97 | kid_keys => | ||
| 98 | $c->ua->get('https://www.googleapis.com/oauth2/v3/certs') | ||
| 99 | ->res->json, | ||
| 100 | ); | ||
| 101 | |||
| 102 | warn "Decoded user data:" . Dumper $user_data; | ||
| 103 | } | ||
| 104 | |||
| 105 | #$user_data->{email}; | ||
| 106 | #$user_data->{family_name} | ||
| 107 | #$user_data->{given_name} | ||
| 108 | |||
| 109 | # $tokensfile->set('tokens/'.$user_data->{email}, $tokens->{access_token}); | ||
| 110 | $config->addToHash( 'gapi/tokens/' . $user_data->{email}, | ||
| 111 | 'access_token', $tokens->{access_token} ); | ||
| 112 | |||
| 113 | if ( $tokens->{refresh_token} ) { | ||
| 114 | $config->addToHash( 'gapi/tokens/' . $user_data->{email}, | ||
| 115 | 'refresh_token', $tokens->{refresh_token} ); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | $c->render( json => $config->get('gapi') ); | ||
| 120 | } | ||
| 121 | else { | ||
| 122 | $c->render( template => 'oauth' ); | ||
| 123 | } | ||
| 124 | }; | ||
| 125 | |||
| 126 | app->start; | ||
| 127 | |||
| 128 | =pod | ||
| 129 | |||
| 130 | =encoding UTF-8 | ||
| 131 | |||
| 132 | =head1 NAME | ||
| 133 | |||
| 134 | Moo::Google::Server - Mojolicious::Lite web server for getting Google Mojo tokens via Oauth 2.0 | ||
| 135 | |||
| 136 | =head1 VERSION | ||
| 137 | |||
| 138 | version 0.03 | ||
| 139 | |||
| 140 | =head1 METHODS | ||
| 141 | |||
| 142 | =head2 get_new_tokens | ||
| 143 | |||
| 144 | Get new access_token by auth_code | ||
| 145 | |||
| 146 | =head2 get_email | ||
| 147 | |||
| 148 | Get email address of API user | ||
| 149 | |||
| 150 | =head1 AUTHOR | ||
| 151 | |||
| 152 | Pavel Serikov <pavelsr@cpan.org> | ||
| 153 | |||
| 154 | =head1 COPYRIGHT AND LICENSE | ||
| 155 | |||
| 156 | This software is copyright (c) 2017 by Pavel Serikov. | ||
| 157 | |||
| 158 | This is free software; you can redistribute it and/or modify it under | ||
| 159 | the same terms as the Perl 5 programming language system itself. | ||
| 160 | |||
| 161 | =cut | ||
| 162 | |||
| 163 | __DATA__ |