« blib/lib/Google/Auth/IDTokens/Verifier.pm
↑ Summary
blib/lib/Google/Auth/ExternalAccountCredentials/Aws.pm »
| 1 | package Google::Auth::UserRefreshCredentials; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Moo; | ||
| 7 | extends 'Google::Auth::Credentials'; | ||
| 8 | |||
| 9 | use JSON::PP; | ||
| 10 | use LWP::UserAgent; | ||
| 11 | use Google::Auth; | ||
| 12 | use Google::Auth::Exceptions; | ||
| 13 | use Google::Auth::RetryHelper; | ||
| 14 | use Log::Any qw($log); | ||
| 15 | |||
| 16 | has json_key => ( | ||
| 17 | is => 'ro', | ||
| 18 | required => 0, | ||
| 19 | ); | ||
| 20 | |||
| 21 | has client_id => ( | ||
| 22 | is => 'ro', | ||
| 23 | required => 0, | ||
| 24 | ); | ||
| 25 | |||
| 26 | has client_secret => ( | ||
| 27 | is => 'ro', | ||
| 28 | required => 0, | ||
| 29 | ); | ||
| 30 | |||
| 31 | has refresh_token => ( | ||
| 32 | is => 'rw', | ||
| 33 | required => 0, | ||
| 34 | ); | ||
| 35 | |||
| 36 | has code => ( | ||
| 37 | is => 'rw', | ||
| 38 | required => 0, | ||
| 39 | ); | ||
| 40 | |||
| 41 | has redirect_uri => ( | ||
| 42 | is => 'ro', | ||
| 43 | required => 0, | ||
| 44 | ); | ||
| 45 | |||
| 46 | has token_uri => ( | ||
| 47 | is => 'ro', | ||
| 48 | required => 0, | ||
| 49 | default => sub { | ||
| 50 | $ENV{GOOGLE_AUTH_TOKEN_URI} || 'https://oauth2.googleapis.com/token'; | ||
| 51 | }, | ||
| 52 | ); | ||
| 53 | |||
| 54 | has code_verifier => ( | ||
| 55 | is => 'ro', | ||
| 56 | required => 0, | ||
| 57 | ); | ||
| 58 | |||
| 59 | has scope => ( | ||
| 60 | is => 'ro', | ||
| 61 | required => 0, | ||
| 62 | ); | ||
| 63 | |||
| 64 | has ua => ( | ||
| 65 | is => 'ro', | ||
| 66 | default => sub { | ||
| 67 | my $ua = LWP::UserAgent->new(timeout => 10); | ||
| 68 | $ua->env_proxy; | ||
| 69 | return $ua; | ||
| 70 | }, | ||
| 71 | ); | ||
| 72 | |||
| 73 | around BUILDARGS => sub { | ||
| 74 | my ($orig, $class, @args) = @_; | ||
| 75 | my $args = $class->$orig(@args); | ||
| 76 | |||
| 77 | if (my $json = $args->{json_key}) { | ||
| 78 | $args->{client_id} //= $json->{client_id}; | ||
| 79 | $args->{client_secret} //= $json->{client_secret}; | ||
| 80 | $args->{refresh_token} //= $json->{refresh_token}; | ||
| 81 | $args->{token_uri} //= $json->{token_uri} if defined $json->{token_uri}; | ||
| 82 | } | ||
| 83 | |||
| 84 | return $args; | ||
| 85 | }; | ||
| 86 | |||
| 87 | sub fetch_access_token { | ||
| 88 | my ($self, %options) = @_; | ||
| 89 | |||
| 90 | my $client_id = $self->client_id; | ||
| 91 | my $client_secret = $self->client_secret; | ||
| 92 | my $refresh_token = $self->refresh_token; | ||
| 93 | my $token_uri = $self->token_uri; | ||
| 94 | |||
| 95 | $self->_validate_url($token_uri, 'token_uri'); | ||
| 96 | |||
| 97 | if (!defined $client_id || !defined $client_secret) { | ||
| 98 | $log->errorf( | ||
| 99 | 'Missing client_id or client_secret for UserRefreshCredentials token exchange' | ||
| 100 | ); | ||
| 101 | Google::Auth::Error->throw( | ||
| 102 | 'Missing client_id or client_secret to fetch token'); | ||
| 103 | } | ||
| 104 | |||
| 105 | my $post_body; | ||
| 106 | if ($self->code) { | ||
| 107 | if (!defined $self->redirect_uri) { | ||
| 108 | Google::Auth::Error->throw( | ||
| 109 | 'Missing redirect_uri for authorization_code grant'); | ||
| 110 | } | ||
| 111 | $post_body = { | ||
| 112 | 'grant_type' => 'authorization_code', | ||
| 113 | 'client_id' => $client_id, | ||
| 114 | 'client_secret' => $client_secret, | ||
| 115 | 'code' => $self->code, | ||
| 116 | 'redirect_uri' => $self->redirect_uri, | ||
| 117 | }; | ||
| 118 | $post_body->{code_verifier} = $self->code_verifier | ||
| 119 | if $self->code_verifier; | ||
| 120 | } elsif ($refresh_token) { | ||
| 121 | $post_body = { | ||
| 122 | 'grant_type' => 'refresh_token', | ||
| 123 | 'client_id' => $client_id, | ||
| 124 | 'client_secret' => $client_secret, | ||
| 125 | 'refresh_token' => $refresh_token, | ||
| 126 | }; | ||
| 127 | } else { | ||
| 128 | Google::Auth::Error->throw('Missing refresh_token or code to fetch token'); | ||
| 129 | } | ||
| 130 | |||
| 131 | my $ua = $self->ua; | ||
| 132 | |||
| 133 | my $response = Google::Auth::RetryHelper->execute_with_retry( | ||
| 134 | sub { | ||
| 135 | my $res = $ua->post($token_uri, $post_body); | ||
| 136 | if (!$res->is_success) { | ||
| 137 | $log->warnf('Token request failed at %s: status %s', | ||
| 138 | $token_uri, $res->code); | ||
| 139 | Google::Auth::Error->throw('HTTP request failed with status ' . | ||
| 140 | $res->code . ': ' . $res->decoded_content); | ||
| 141 | } | ||
| 142 | return $res; | ||
| 143 | }, | ||
| 144 | %options | ||
| 145 | ); | ||
| 146 | |||
| 147 | my $res_data = decode_json($response->decoded_content); | ||
| 148 | my $token = $res_data->{access_token}; | ||
| 149 | my $expires = $res_data->{expires_in} // 3600; | ||
| 150 | |||
| 151 | $self->access_token($token); | ||
| 152 | $self->expires_at(time() + $expires); | ||
| 153 | |||
| 154 | if ($res_data->{refresh_token}) { | ||
| 155 | $self->refresh_token($res_data->{refresh_token}); | ||
| 156 | } | ||
| 157 | |||
| 158 | return $token; | ||
| 159 | } | ||
| 160 | |||
| 161 | 1; |