Google/Server.pm untested

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