AuthorizationServer/AuthorizationCodeGrant.pm

filter: Statementstmt 98.5% 67 / 68 Branchbran 100% 14 / 14 Conditioncond 65.4% 36 / 55 MC/DCmcdc 34.7% 8 / 23 Subroutinesub 95.0% 19 / 20 Podpod 0.0% 0 / 3 totaltotal 78.6% 144 / 183 CC 27 SCAR 19.1
CC
27
Cov
85%
BUILD
11.5
_verify_auth_code
11.1
_verify_auth_code_jwt
9.3
CRAP 6.8
« AuthorizationServer/ImplicitGrant.pm ↑ Summary AuthorizationServer/PasswordGrant.pm »
1 package Net::OAuth2::AuthorizationServer::AuthorizationCodeGrant;
2  
3 =head1 NAME
4  
5 Net::OAuth2::AuthorizationServer::AuthorizationCodeGrant - OAuth2 Authorization Code Grant
6  
7 =head1 SYNOPSIS
8  
9 my $Grant = Net::OAuth2::AuthorizationServer::AuthorizationCodeGrant->new(
10 clients => {
11 TrendyNewService => {
12 client_secret => 'TopSecretClientSecret',
13 scopes => {
14 post_images => 1,
15 annoy_friends => 1,
16 },
17 },
18 }
19 );
20  
21 # verify a client against known clients
22 my ( $is_valid,$error ) = $Grant->verify_client(
23 client_id => $client_id,
24 scopes => [ qw/ list of scopes / ],
25 );
26  
27 if ( ! $Grant->login_resource_owner ) {
28 # resource owner needs to login
29 ...
30 }
31  
32 # have resource owner confirm (and perhaps modify) scopes
33 my ( $confirmed,$error,$scopes_ref ) = $Grant->confirm_by_resource_owner(
34 client_id => $client_id,
35 scopes => [ qw/ list of scopes / ],
36 );
37  
38 # generate a token
39 my $token = $Grant->token(
40 client_id => $client_id,
41 scopes => $scopes_ref,
42 type => 'auth', # one of: auth, access, refresh
43 redirect_uri => $redirect_uri,
44 user_id => $user_id, # optional
45 jwt_claims_cb => sub { ... }, # optional, see jwt_claims_cb in Manual
46 );
47  
48 # store the auth code
49 $Grant->store_auth_code(
50 auth_code => $auth_code,
51 client_id => $client_id,
52 redirect_uri => $uri,
53 scopes => $scopes_ref,
54 );
55  
56 # verify an auth code
57 my ( $client,$error,$scope,$user_id ) = $Grant->verify_auth_code(
58 client_id => $client_id,
59 client_secret => $client_secret,
60 auth_code => $auth_code,
61 redirect_uri => $uri,
62 );
63  
64 # store access token
65 $Grant->store_access_token(
66 client_id => $client,
67 auth_code => $auth_code,
68 access_token => $access_token,
69 refresh_token => $refresh_token,
70 scopes => $scopes_ref,
71 old_refresh_token => $old_refresh_token,
72 );
73  
74 # verify an access token
75 my ( $is_valid,$error ) = $Grant->verify_access_token(
76 access_token => $access_token,
77 scopes => [ qw/ list of scopes / ],
78 is_refresh_token => 0,
79 );
80  
81 # or:
82 my ( $client,$error,$scope,$user_id ) = $Grant->verify_token_and_scope(
83 refresh_token => $refresh_token,
84 auth_header => $http_authorization_header,
85 );
86  
87 =head1 DESCRIPTION
88  
89 This module implements the OAuth2 "Authorization Code Grant" flow as described
90 at L<http://tools.ietf.org/html/rfc6749#section-4.1>.
91  
92 =head1 CONSTRUCTOR ARGUMENTS
93  
94 Along with those detailed at L<Net::OAuth2::AuthorizationServer::Manual/"CONSTRUCTOR ARGUMENTS">
95 the following are supported by this grant type:
96  
97 =head2 auth_code_ttl
98  
99 The validity period of the generated authorization code in seconds. Defaults to
100 600 seconds (10 minutes)
101  
102 =head1 CALLBACK FUNCTIONS
103  
104 The following callbacks are supported by this grant type:
105  
106 verify_client_cb
107 login_resource_owner_cb
108 confirm_by_resource_owner_cb
109 store_auth_code_cb
110 verify_auth_code_cb
111 store_access_token_cb
112 verify_access_token_cb
113  
114 Please see L<Net::OAuth2::AuthorizationServer::Manual/"CALLBACK FUNCTIONS"> for
115 documentation on each callback function.
116  
117 =cut
118  
119 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
120 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
121  
122 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
123 3 with 'Net::OAuth2::AuthorizationServer::Defaults';
124  
125 3 ▶use Types::Standard qw/ :all /;
Subroutine BEGIN: called CC=1 SCAR=0.0
126 3 ▶use Carp qw/ croak /;
Subroutine BEGIN: called CC=1 SCAR=0.0
127 3 ▶use MIME::Base64 qw/ decode_base64 /;
Subroutine BEGIN: called CC=1 SCAR=0.0
128 3 ▶use Crypt::JWT qw/ decode_jwt /;
Subroutine BEGIN: called CC=1 SCAR=0.0
129 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
130  
131 has 'auth_code_ttl' => (
132 is => 'ro',
133 isa => Int,
134 required => 0,
135 6 ▶ default => sub { 600 },
Subroutine __ANON__: called CC=1 SCAR=0.0
136 3 );
137  
138 has 'auth_codes' => (
139 is => 'ro',
140 isa => Maybe [HashRef],
141 required => 0,
142 6 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
143 3 );
144  
145 3 has [
146 qw/
147 store_auth_code_cb
148 verify_auth_code_cb
149 /
150 ] => (
151 is => 'ro',
152 isa => Maybe [CodeRef],
153 required => 0,
154 );
155  
156 16 ▶sub _uses_auth_codes { 1 };
Subroutine _uses_auth_codes: called CC=1 SCAR=0.0
157 0 ▶sub _uses_user_passwords { 0 };
Subroutine _uses_user_passwords: not called CC=1 SCAR=6.9
158  
159 6 ▶sub BUILD {
Subroutine BUILD: called CC=7 SCAR=24.4
Pod undocumented
160 6 my ( $self, $args ) = @_;
161  
162 6 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_has_clients || ($args->{"verify_client_cb"} || $args->{"store_auth_code_cb"} || $args->{"verify_auth_code_cb"} || $args->{"store_access_token_cb"} || $args->{"verify_access_token_cb"})) 1 5 6
Condition40%6/15
$self->_has_clients || ($args->{'verify_client_cb'} || $args->{'store_auth_code_cb'} || $args->{'verify_auth_code_cb'} || $args->{'store_access_token_cb'} || $args->{'verify_access_token_cb'})
l!l&&r!l&&!r
501
$args->{"verify_client_cb"} or $args->{"store_auth_code_cb"} or $args->{"verify_auth_code_cb"} or $args->{"store_access_token_cb"} or $args->{"verify_access_token_cb"}
l!l&&r!l&&!r
001
$args->{"verify_client_cb"} or $args->{"store_auth_code_cb"} or $args->{"verify_auth_code_cb"} or $args->{"store_access_token_cb"}
l!l&&r!l&&!r
001
$args->{"verify_client_cb"} or $args->{"store_auth_code_cb"} or $args->{"verify_auth_code_cb"}
l!l&&r!l&&!r
001
$args->{"verify_client_cb"} or $args->{"store_auth_code_cb"}
l!l&&r!l&&!r
001
MC/DC16%1/6
$self->_has_clients || ($args->{'verify_client_cb'} || $args->{'store_auth_code_cb'} || $args->{'verify_auth_code_cb'} || $args->{'store_access_token_cb'} || $args->{'verify_access_token_cb'})
$self->_has_clients $args->{"verify_client_cb"} $args->{"store_auth_code_cb"} $args->{"verify_auth_code_cb"} $args->{"store_access_token_cb"} $args->{"verify_access_token_cb"}
Truth table28%2/7
A || B or C or D or E or F
A= $self->_has_clientsB= $args->{"verify_client_cb"}C= $args->{"store_auth_code_cb"}D= $args->{"verify_auth_code_cb"}E= $args->{"store_access_token_cb"}F= $args->{"verify_access_token_cb"}
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
163 # if we don't have a list of clients
164 !$self->_has_clients
165  
166 # we must know how to verify clients and tokens
167 and ( !$args->{ verify_client_cb }
168 and !$args->{ store_auth_code_cb }
169 and !$args->{ verify_auth_code_cb }
170 and !$args->{ store_access_token_cb }
171 and !$args->{ verify_access_token_cb } )
172 )
173 {
174 1 croak __PACKAGE__ . " requires either clients or overrides";
175 }
176 }
177  
178 4 ▶sub store_auth_code {
Subroutine store_auth_code: called CC=1 SCAR=0.0
Pod undocumented
179 4 _delegate_to_cb_or_private( 'store_auth_code', @_ );
180 }
181  
182 22 ▶sub verify_auth_code {
Subroutine verify_auth_code: called CC=1 SCAR=0.0
Pod undocumented
183 22 _delegate_to_cb_or_private( 'verify_auth_code', @_ );
184 }
185  
186 4 ▶sub _store_auth_code {
Subroutine _store_auth_code: called CC=3 SCAR=11.2
187 4 my ( $self, %args ) = @_;
188  
189 my ( $auth_code, $client_id, $expires_in, $uri, $scopes_ref ) =
190 4 @args{ qw/ auth_code client_id expires_in redirect_uri scopes / };
191  
192 4 ▶ return 1 if $self->jwt_secret;
Branch: 1 branch
BranchTrueFalseTotal
if $self->jwt_secret 2 2 4
193  
194 2 ▶ $expires_in //= $self->auth_code_ttl;
Condition33%1/3
$expires_in //= $self->auth_code_ttl
l!l&&r!l&&!r
020
MC/DC0%0/2
$expires_in //= $self->auth_code_ttl
$expires_in $self->auth_code_ttl
Truth table33%1/3
A //= B
A= $expires_inB= $self->auth_code_ttl
ABresult
1X1
011
000
195  
196 2 $self->auth_codes->{ $auth_code } = {
197 client_id => $client_id,
198 expires => time + $expires_in,
199 redirect_uri => $uri,
200 scope => $scopes_ref,
201 };
202  
203 2 return 1;
204 }
205  
206 22 ▶sub _verify_auth_code {
Subroutine _verify_auth_code: called CC=11 SCAR=24.0
207 22 my ( $self, %args ) = @_;
208  
209 my ( $client_id, $client_secret, $auth_code, $uri ) =
210 22 @args{ qw/ client_id client_secret auth_code redirect_uri / };
211  
212 22 ▶ my $client = $self->clients->{ $client_id }
Condition100%2/2
$self->clients->{$client_id} || (return 0, 'unauthorized_client')
l!l
184
MC/DC100%1/1
$self->clients->{$client_id} || (return 0, 'unauthorized_client')
$self->clients->{$client_id}
Truth table100%2/2
A
A= $self->clients->{$client_id}
Aresult
11
00
213 || return ( 0, 'unauthorized_client' );
214  
215 18 ▶ return $self->_verify_auth_code_jwt( %args ) if $self->jwt_secret;
Branch: 1 branch
BranchTrueFalseTotal
if $self->jwt_secret 8 10 18
216  
217 10 my ( $sec, $usec, $rand ) = split( '-', decode_base64( $auth_code ) );
218  
219 10 ▶ if ( !exists( $self->auth_codes->{ $auth_code } )
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->auth_codes->{$auth_code} && exists $self->clients->{$client_id} or $client_secret ne $self->clients->{$client_id}{'client_secret'} or $self->auth_codes->{$auth_code}{'access_token'} or $uri and $self->auth_codes->{$auth_code}{'redirect_uri'} ne $uri or $self->auth_codes->{$auth_code}{'expires'} <= time) { } 8 2 10
Condition83%15/18
not exists $self->auth_codes->{$auth_code} && exists $self->clients->{$client_id} or $client_secret ne $self->clients->{$client_id}{"client_secret"} or $self->auth_codes->{$auth_code}{"access_token"} or $uri and $self->auth_codes->{$auth_code}{"redirect_uri"} ne $uri or $self->auth_codes->{$auth_code}{"expires"} <= time
l!l&&r!l&&!r
802
not exists $self->auth_codes->{$auth_code} && exists $self->clients->{$client_id} or $client_secret ne $self->clients->{$client_id}{"client_secret"} or $self->auth_codes->{$auth_code}{"access_token"} or $uri and $self->auth_codes->{$auth_code}{"redirect_uri"} ne $uri
l!l&&r!l&&!r
622
not exists $self->auth_codes->{$auth_code} && exists $self->clients->{$client_id} or $client_secret ne $self->clients->{$client_id}{"client_secret"} or $self->auth_codes->{$auth_code}{"access_token"}
l!l&&r!l&&!r
424
not exists $self->auth_codes->{$auth_code} && exists $self->clients->{$client_id} or $client_secret ne $self->clients->{$client_id}{"client_secret"}
l!l&&r!l&&!r
226
exists $self->auth_codes->{$auth_code} && exists $self->clients->{$client_id}
!ll&&!rl&&r
208
$uri and $self->auth_codes->{$auth_code}{"redirect_uri"} ne $uri
!ll&&!rl&&r
022
MC/DC57%4/7
not exists $self->auth_codes->{$auth_code} && exists $self->clients->{$client_id} or $client_secret ne $self->clients->{$client_id}{"client_secret"} or $self->auth_codes->{$auth_code}{"access_token"} or $uri and $self->auth_codes->{$auth_code}{"redirect_uri"} ne $uri or $self->auth_codes->{$auth_code}{"expires"} <= time
exists $self->auth_codes->{$auth_code} exists $self->clients->{$client_id} $client_secret ne $self->clients->{$client_id}{"client_secret"} $self->auth_codes->{$auth_code}{"access_token"} $uri $self->auth_codes->{$auth_code}{"redirect_uri"} ne $uri $self->auth_codes->{$auth_code}{"expires"} <= time
Truth table55%5/9
not(A && B) or C or D or E and F or G
A= exists $self->auth_codes->{$auth_code}B= exists $self->clients->{$client_id}C= $client_secret ne $self->clients->{$client_id}{"client_secret"}D= $self->auth_codes->{$auth_code}{"access_token"}E= $uriF= $self->auth_codes->{$auth_code}{"redirect_uri"} ne $uriG= $self->auth_codes->{$auth_code}{"expires"} <= time
ABCDEFGresult
0XXXXXX1
10XXXXX1
111XXXX1
1101XXX1
110011X1
11000X11
11001011
11000X00
11001000
220 or !exists( $self->clients->{ $client_id } )
221 or ( $client_secret ne $self->clients->{ $client_id }{ client_secret } )
222 or $self->auth_codes->{ $auth_code }{ access_token }
223 or ( $uri && $self->auth_codes->{ $auth_code }{ redirect_uri } ne $uri )
224 or ( $self->auth_codes->{ $auth_code }{ expires } <= time ) )
225 {
226  
227 8 ▶ if ( my $access_token = $self->auth_codes->{ $auth_code }{ access_token } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $access_token = $self->auth_codes->{$auth_code}{"access_token"}) 2 6 8
228  
229 # this auth code has already been used to generate an access token
230 # so we need to revoke the access token that was previously generated
231 2 $self->_revoke_access_token( $access_token );
232 }
233  
234 8 return ( 0, 'invalid_grant' );
235 }
236 else {
237 2 return ( 1, undef, @{ $self->auth_codes->{ $auth_code } }{ qw/ scope user_id / } );
238 }
239  
240 }
241  
242 8 ▶sub _verify_auth_code_jwt {
Subroutine _verify_auth_code_jwt: called CC=9 SCAR=22.3
243 8 my ( $self, %args ) = @_;
244  
245 my ( $client_id, $client_secret, $auth_code, $uri ) =
246 8 @args{ qw/ client_id client_secret auth_code redirect_uri / };
247  
248 8 ▶ my $client = $self->clients->{ $client_id }
Condition50%1/2
$self->clients->{$client_id} || (return 0, 'unauthorized_client')
l!l
80
MC/DC0%0/1
$self->clients->{$client_id} || (return 0, 'unauthorized_client')
$self->clients->{$client_id}
Truth table50%1/2
A
A= $self->clients->{$client_id}
Aresult
11
00
249 || return ( 0, 'unauthorized_client' );
250  
251 return ( 0, 'invalid_grant' )
252 8 ▶ if ( $client_secret ne $client->{ client_secret } );
Branch: 1 branch
BranchTrueFalseTotal
if $client_secret ne $client->{"client_secret"} 2 6 8
253  
254 6 my ( $auth_code_payload,$invalid_jwt );
255  
256 6 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
257 6 $auth_code_payload =
258 decode_jwt(
259 alg => $self->jwt_algorithm,
260 key => $self->jwt_secret,
261 token => $auth_code,
262 );
263 }
264 2 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
265 2 $invalid_jwt = 1;
266 6 };
267  
268 6 ▶ if ( !$auth_code_payload
Branch: 1 branch
BranchTrueFalseTotal
if (not $auth_code_payload or $invalid_jwt or $auth_code_payload->{"type"} ne "auth" or $auth_code_payload->{"client"} ne $client_id or $uri and $auth_code_payload->{"aud"} ne $uri) 4 2 6
Condition73%11/15
not $auth_code_payload or $invalid_jwt or $auth_code_payload->{"type"} ne "auth" or $auth_code_payload->{"client"} ne $client_id or $uri and $auth_code_payload->{"aud"} ne $uri
l!l&&r!l&&!r
222
not $auth_code_payload or $invalid_jwt or $auth_code_payload->{"type"} ne "auth" or $auth_code_payload->{"client"} ne $client_id
l!l&&r!l&&!r
204
not $auth_code_payload or $invalid_jwt or $auth_code_payload->{"type"} ne "auth"
l!l&&r!l&&!r
204
not $auth_code_payload or $invalid_jwt
l!l&&r!l&&!r
204
$uri and $auth_code_payload->{"aud"} ne $uri
!ll&&!rl&&r
022
MC/DC33%2/6
not $auth_code_payload or $invalid_jwt or $auth_code_payload->{"type"} ne "auth" or $auth_code_payload->{"client"} ne $client_id or $uri and $auth_code_payload->{"aud"} ne $uri
not $auth_code_payload $invalid_jwt $auth_code_payload->{"type"} ne "auth" $auth_code_payload->{"client"} ne $client_id $uri $auth_code_payload->{"aud"} ne $uri
Truth table42%3/7
not(A) or B or C or D or E and F
A= not $auth_code_payloadB= $invalid_jwtC= $auth_code_payload->{"type"} ne "auth"D= $auth_code_payload->{"client"} ne $client_idE= $uriF= $auth_code_payload->{"aud"} ne $uri
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
0000111
00000X0
0000100
269 or $invalid_jwt
270 or $auth_code_payload->{ type } ne 'auth'
271 or $auth_code_payload->{ client } ne $client_id
272 or ( $uri && $auth_code_payload->{ aud } ne $uri ) )
273 {
274 4 return ( 0, 'invalid_grant' );
275 }
276  
277 2 my $scope = $auth_code_payload->{ scopes };
278 2 my $user_id = $auth_code_payload->{ user_id };
279  
280 2 return ( $client_id, undef, $scope, $user_id );
281 }
282  
283 =head1 AUTHOR
284  
285 Lee Johnson - C<leejo@cpan.org>
286  
287 =head1 LICENSE
288  
289 This library is free software; you can redistribute it and/or modify it under
290 the same terms as Perl itself. If you would like to contribute documentation
291 or file a bug report then please raise an issue / pull request:
292  
293 https://github.com/Humanstate/net-oauth2-authorizationserver
294  
295 =cut
296  
297 3 __PACKAGE__->meta->make_immutable;
« AuthorizationServer/ImplicitGrant.pm ↑ Summary AuthorizationServer/PasswordGrant.pm »