| 1 | package Google::API::OAuth2::Client; | ||||||||||||
| 2 | |||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 2 | ▶ | use URI; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 8 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||
| 8 | 8 | my $class = shift; | |||||||||||
| 9 | 8 | my ($param) = @_; | |||||||||||
| 10 | 8 | for my $key (qw/auth_uri token_uri client_id client_secret/) { | |||||||||||
| 11 | 29 | ▶ | return unless $param->{$key}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 12 | } | ||||||||||||
| 13 | 7 | ▶ | unless (defined $param->{ua}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 14 | 7 | $param->{ua} = $class->_new_ua; | |||||||||||
| 15 | } | ||||||||||||
| 16 | 7 | ▶ | unless (defined $param->{json_parser}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 17 | 7 | $param->{json_parser} = $class->_new_json_parser; | |||||||||||
| 18 | } | ||||||||||||
| 19 | 7 | bless { %$param }, $class; | |||||||||||
| 20 | } | ||||||||||||
| 21 | |||||||||||||
| 22 | 2 | ▶ | sub new_from_client_secrets { | ||||||||||
|
Subroutine
new_from_client_secrets: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 23 | 2 | my $class = shift; | |||||||||||
| 24 | 2 | my ($file, $auth_doc) = @_; | |||||||||||
| 25 | 2 | ▶ | open my $fh, '<', $file | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 26 | or die "$file not found"; | ||||||||||||
| 27 | 2 | my $content = do { local $/; <$fh> }; | |||||||||||
| 28 | 2 | close $fh; | |||||||||||
| 29 | 2 | require JSON; | |||||||||||
| 30 | 2 | my $json = JSON->new->decode($content); | |||||||||||
| 31 | 2 | my ($client_type) = keys(%$json); | |||||||||||
| 32 | $class->new({ | ||||||||||||
| 33 | auth_uri => $json->{$client_type}->{auth_uri}, | ||||||||||||
| 34 | token_uri => $json->{$client_type}->{token_uri}, | ||||||||||||
| 35 | client_id => $json->{$client_type}->{client_id}, | ||||||||||||
| 36 | client_secret => $json->{$client_type}->{client_secret}, | ||||||||||||
| 37 | 2 | redirect_uri => @{$json->{$client_type}->{redirect_uris}}[0], | |||||||||||
| 38 | auth_doc => $auth_doc, | ||||||||||||
| 39 | }); | ||||||||||||
| 40 | } | ||||||||||||
| 41 | |||||||||||||
| 42 | 7 | ▶ | sub authorize_uri { | ||||||||||
|
Subroutine
authorize_uri: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||
| 43 | 7 | my $self = shift; | |||||||||||
| 44 | 7 | my ($response_type) = @_; | |||||||||||
| 45 | 7 | ▶ | $response_type ||= 'code'; | ||||||||||
|
Condition100%2/2
$response_type ||= "code"
MC/DC100%1/1
$response_type ||= "code"
$response_type
Truth table100%2/2
A
A= $response_type
| |||||||||||||
| 46 | 7 | for my $key (qw/client_id redirect_uri/) { | |||||||||||
| 47 | 14 | ▶ | return unless $self->{$key}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 48 | } | ||||||||||||
| 49 | 7 | my $authorize_uri = "$self->{auth_uri}?client_id=$self->{client_id}&redirect_uri=$self->{redirect_uri}&response_type=$response_type"; | |||||||||||
| 50 | 7 | ▶ | if ($self->{auth_doc}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 51 | 3 | my @scopes = keys %{$self->{auth_doc}{oauth2}{scopes}}; | |||||||||||
| 52 | 3 | $authorize_uri .= '&scope=' . join ' ', @scopes; | |||||||||||
| 53 | } | ||||||||||||
| 54 | 7 | ▶ | if ($self->{access_type}){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 55 | 1 | $authorize_uri .= "&access_type=$self->{access_type}"; | |||||||||||
| 56 | } | ||||||||||||
| 57 | 7 | ▶ | if ($self->{approval_prompt}){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 58 | 0 | $authorize_uri .= "&approval_prompt=$self->{approval_prompt}"; | |||||||||||
| 59 | } | ||||||||||||
| 60 | 7 | return URI->new($authorize_uri)->as_string; | |||||||||||
| 61 | } | ||||||||||||
| 62 | |||||||||||||
| 63 | 0 | ▶ | sub exchange { | ||||||||||
|
Subroutine
exchange: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||
| 64 | 0 | my $self = shift; | |||||||||||
| 65 | 0 | my ($code) = @_; | |||||||||||
| 66 | 0 | ▶ | return unless $code; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 67 | 0 | ▶ | return unless $self->{auth_doc}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 68 | 0 | for my $key (qw/client_id client_secret/) { | |||||||||||
| 69 | 0 | ▶ | return unless $self->{$key}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 70 | } | ||||||||||||
| 71 | 0 | my @scopes = keys %{$self->{auth_doc}{oauth2}{scopes}}; | |||||||||||
| 72 | 0 | my $scopes = join ' ', @scopes; | |||||||||||
| 73 | my @param = ( | ||||||||||||
| 74 | client_id => $self->{client_id}, | ||||||||||||
| 75 | client_secret => $self->{client_secret}, | ||||||||||||
| 76 | redirect_uri => $self->{redirect_uri}, | ||||||||||||
| 77 | 0 | code => $code, | |||||||||||
| 78 | scope => $scopes, | ||||||||||||
| 79 | grant_type => 'authorization_code', | ||||||||||||
| 80 | ); | ||||||||||||
| 81 | 0 | require HTTP::Request::Common; | |||||||||||
| 82 | my $res = $self->{ua}->request( | ||||||||||||
| 83 | HTTP::Request::Common::POST( | ||||||||||||
| 84 | $self->{token_uri}, | ||||||||||||
| 85 | 0 | Content_Type => 'application/x-www-form-urlencoded', | |||||||||||
| 86 | Content => [@param] | ||||||||||||
| 87 | ) | ||||||||||||
| 88 | ); | ||||||||||||
| 89 | 0 | ▶ | unless ($res->is_success) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 90 | 0 | return; | |||||||||||
| 91 | } | ||||||||||||
| 92 | 0 | my $access_token = $self->{json_parser}->decode($res->content); | |||||||||||
| 93 | 0 | $self->{token_obj} = $access_token; | |||||||||||
| 94 | 0 | return $self->{token_obj}; | |||||||||||
| 95 | } | ||||||||||||
| 96 | |||||||||||||
| 97 | 0 | ▶ | sub refresh { | ||||||||||
|
Subroutine
refresh: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||
| 98 | 0 | my $self = shift; | |||||||||||
| 99 | 0 | for my $key (qw/client_id client_secret token_obj/) { | |||||||||||
| 100 | 0 | ▶ | return unless $self->{$key}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 101 | } | ||||||||||||
| 102 | my @param = ( | ||||||||||||
| 103 | client_id => $self->{client_id}, | ||||||||||||
| 104 | client_secret => $self->{client_secret}, | ||||||||||||
| 105 | refresh_token => $self->{token_obj}{refresh_token}, | ||||||||||||
| 106 | 0 | grant_type => 'refresh_token', | |||||||||||
| 107 | ); | ||||||||||||
| 108 | 0 | require HTTP::Request::Common; | |||||||||||
| 109 | my $res = $self->{ua}->request( | ||||||||||||
| 110 | HTTP::Request::Common::POST( | ||||||||||||
| 111 | $self->{token_uri}, | ||||||||||||
| 112 | 0 | Content_Type => 'application/x-www-form-urlencoded', | |||||||||||
| 113 | Content => [@param] | ||||||||||||
| 114 | ) | ||||||||||||
| 115 | ); | ||||||||||||
| 116 | 0 | ▶ | unless ($res->is_success) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 117 | 0 | return; | |||||||||||
| 118 | } | ||||||||||||
| 119 | 0 | my $access_token = $self->{json_parser}->decode($res->content); | |||||||||||
| 120 | 0 | ▶ | unless ($access_token->{refresh_token}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 121 | 0 | $access_token->{refresh_token} = $self->{token_obj}{refresh_token}; | |||||||||||
| 122 | } | ||||||||||||
| 123 | 0 | $self->{token_obj} = $access_token; | |||||||||||
| 124 | 0 | return $self->{token_obj}; | |||||||||||
| 125 | } | ||||||||||||
| 126 | |||||||||||||
| 127 | 1 | ▶ | sub token_obj { | ||||||||||
|
Subroutine
token_obj: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 128 | 1 | my $self = shift; | |||||||||||
| 129 | 1 | my ($token_obj) = @_; | |||||||||||
| 130 | 1 | ▶ | return $self->{token_obj} unless $token_obj; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 131 | 1 | $self->{token_obj} = $token_obj; | |||||||||||
| 132 | } | ||||||||||||
| 133 | |||||||||||||
| 134 | 1 | ▶ | sub token_type { | ||||||||||
|
Subroutine
token_type: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||
| 135 | 1 | my $self = shift; | |||||||||||
| 136 | 1 | ▶ | return unless $self->{token_obj}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 137 | 1 | return $self->{token_obj}{token_type}; | |||||||||||
| 138 | } | ||||||||||||
| 139 | |||||||||||||
| 140 | 1 | ▶ | sub access_token { | ||||||||||
|
Subroutine
access_token: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||
| 141 | 1 | my $self = shift; | |||||||||||
| 142 | 1 | ▶ | return unless $self->{token_obj}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 143 | 1 | return $self->{token_obj}{access_token}; | |||||||||||
| 144 | } | ||||||||||||
| 145 | |||||||||||||
| 146 | 2 | ▶ | sub auth_doc { | ||||||||||
|
Subroutine
auth_doc: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 147 | 2 | my $self = shift; | |||||||||||
| 148 | 2 | ▶ | if (@_) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 149 | 1 | my ($doc) = @_; | |||||||||||
| 150 | 1 | $self->{auth_doc} = $doc; | |||||||||||
| 151 | } | ||||||||||||
| 152 | 2 | return $self->{auth_doc}; | |||||||||||
| 153 | } | ||||||||||||
| 154 | |||||||||||||
| 155 | 7 | ▶ | sub _new_ua { | ||||||||||
|
Subroutine
_new_ua: called CC=1 SCAR=0.0
| |||||||||||||
| 156 | 7 | my $class = shift; | |||||||||||
| 157 | 7 | require LWP::UserAgent; | |||||||||||
| 158 | 7 | my $ua = LWP::UserAgent->new; | |||||||||||
| 159 | 7 | return $ua; | |||||||||||
| 160 | } | ||||||||||||
| 161 | |||||||||||||
| 162 | 7 | ▶ | sub _new_json_parser { | ||||||||||
|
Subroutine
_new_json_parser: called CC=1 SCAR=0.0
| |||||||||||||
| 163 | 7 | my $class = shift; | |||||||||||
| 164 | 7 | require JSON; | |||||||||||
| 165 | 7 | my $parser = JSON->new; | |||||||||||
| 166 | 7 | return $parser; | |||||||||||
| 167 | } | ||||||||||||
| 168 | |||||||||||||
| 169 | 2 | 1; | |||||||||||
| 170 | __END__ | ||||||||||||