AuthorizationServer/ImplicitGrant.pm

filter: Statementstmt 94.1% 48 / 51 Branchbran 90.9% 20 / 22 Conditioncond 63.1% 12 / 19 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 90.0% 9 / 10 Podpod 0.0% 0 / 1 totaltotal 83.0% 93 / 112 CC 21 SCAR 19.3
CC
21
Cov
87%
_verify_client
10.0
_verify_access_token
8.5
BUILD
6.1
CRAP 6.9
« AuthorizationServer/Defaults.pm ↑ Summary AuthorizationServer/AuthorizationCodeGrant.pm »
1 package Net::OAuth2::AuthorizationServer::ImplicitGrant;
2  
3 =head1 NAME
4  
5 Net::OAuth2::AuthorizationServer::ImplicitGrant - OAuth2 Resource Owner Implicit Grant
6  
7 You "SHOULD NOT" use this grant type (see L<https://tools.ietf.org/html/draft-ietf-oauth-security-topics-15>)
8  
9 =head1 SYNOPSIS
10  
11 my $Grant = Net::OAuth2::AuthorizationServer::ImplicitGrant->new(
12 clients => {
13 TrendyNewService => {
14 # optional
15 redirect_uri => 'https://...',
16 # optional
17 scopes => {
18 post_images => 1,
19 annoy_friends => 1,
20 },
21 },
22 }
23 );
24  
25 # verify a client against known clients
26 my ( $is_valid,$error,$scopes ) = $Grant->verify_client(
27 client_id => $client_id,
28 redirect_uri => $uri, # optional
29 scopes => [ qw/ list of scopes / ], # optional
30 );
31  
32 if ( ! $Grant->login_resource_owner ) {
33 # resource owner needs to login
34 ...
35 }
36  
37 # have resource owner confirm (and perhaps modify) scopes
38 my ( $confirmed,$error,$scopes_ref ) = $Grant->confirm_by_resource_owner(
39 client_id => $client_id,
40 scopes => [ qw/ list of scopes / ],
41 );
42  
43 # generate a token
44 my $token = $Grant->token(
45 client_id => $client_id,
46 scopes => $scopes_ref,
47 redirect_uri => $redirect_uri,
48 user_id => $user_id, # optional
49 jwt_claims_cb => sub { ... }, # optional, see jwt_claims_cb in Manual
50 );
51  
52 # store access token
53 $Grant->store_access_token(
54 client_id => $client,
55 access_token => $access_token,
56 scopes => $scopes_ref,
57 );
58  
59 # verify an access token
60 my ( $is_valid,$error ) = $Grant->verify_access_token(
61 access_token => $access_token,
62 scopes => $scopes_ref,
63 );
64  
65 =head1 DESCRIPTION
66  
67 This module implements the OAuth2 "Resource Owner Implicit Grant" flow as described
68 at L<http://tools.ietf.org/html/rfc6749#section-4.2>.
69  
70 =head1 CONSTRUCTOR ARGUMENTS
71  
72 Along with those detailed at L<Net::OAuth2::AuthorizationServer::Manual/"CONSTRUCTOR ARGUMENTS">
73 the following are supported by this grant type:
74  
75 =head1 CALLBACK FUNCTIONS
76  
77 The following callbacks are supported by this grant type:
78  
79 verify_client_cb
80 login_resource_owner_cb
81 confirm_by_resource_owner_cb
82 store_access_token_cb
83 verify_access_token_cb
84  
85 Please see L<Net::OAuth2::AuthorizationServer::Manual/"CALLBACK FUNCTIONS"> for
86 documentation on each callback function.
87  
88 =cut
89  
90 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
91 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
94 5 with 'Net::OAuth2::AuthorizationServer::Defaults';
95  
96 5 ▶use Carp qw/ croak /;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 5 ▶use Types::Standard qw/ :all /;
Subroutine BEGIN: called CC=1 SCAR=0.0
98  
99 6 ▶sub _uses_auth_codes { 0 };
Subroutine _uses_auth_codes: called CC=1 SCAR=0.0
100 0 ▶sub _uses_user_passwords { 0 };
Subroutine _uses_user_passwords: not called CC=1 SCAR=6.9
101  
102 12 ▶sub BUILD {
Subroutine BUILD: called CC=5 SCAR=18.1
Pod undocumented
103 12 my ( $self, $args ) = @_;
104  
105 12 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_has_clients || ($args->{"verify_client_cb"} || $args->{"store_access_token_cb"} || $args->{"verify_access_token_cb"})) 2 10 12
Condition44%4/9
$self->_has_clients || ($args->{'verify_client_cb'} || $args->{'store_access_token_cb'} || $args->{'verify_access_token_cb'})
l!l&&r!l&&!r
1002
$args->{"verify_client_cb"} or $args->{"store_access_token_cb"} or $args->{"verify_access_token_cb"}
l!l&&r!l&&!r
002
$args->{"verify_client_cb"} or $args->{"store_access_token_cb"}
l!l&&r!l&&!r
002
MC/DC25%1/4
$self->_has_clients || ($args->{'verify_client_cb'} || $args->{'store_access_token_cb'} || $args->{'verify_access_token_cb'})
$self->_has_clients $args->{"verify_client_cb"} $args->{"store_access_token_cb"} $args->{"verify_access_token_cb"}
Truth table40%2/5
A || B or C or D
A= $self->_has_clientsB= $args->{"verify_client_cb"}C= $args->{"store_access_token_cb"}D= $args->{"verify_access_token_cb"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
106 # if we don't have a list of clients
107 !$self->_has_clients
108  
109 # we must know how to verify clients and tokens
110 and ( !$args->{ verify_client_cb }
111 and !$args->{ store_access_token_cb }
112 and !$args->{ verify_access_token_cb } )
113 )
114 {
115 2 croak __PACKAGE__ . " requires either clients or overrides";
116 }
117 }
118  
119 32 ▶sub _verify_client {
Subroutine _verify_client: called CC=10 SCAR=23.0
120 32 my ( $self, %args ) = @_;
121  
122 my ( $client_id, $scopes_ref, $redirect_uri )
123 32 = @args{ qw/ client_id scopes redirect_uri / };
124  
125 32 ▶ if ( my $client = $self->clients->{ $client_id } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $client = $self->clients->{$client_id}) 28 4 32
126 28 my $client_scopes = [];
127  
128 28 ▶ foreach my $scope ( @{ $scopes_ref // [] } ) {
Condition50%1/2
$scopes_ref // []
l!l
280
MC/DC0%0/1
$scopes_ref // []
$scopes_ref
Truth table50%1/2
A
A= $scopes_ref
Aresult
11
00
129 64 ▶ if ( ! exists($self->clients->{ $client_id }{ scopes }{ $scope }) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $self->clients->{$client_id}{'scopes'}{$scope}) { } 4 60 64
elsif ($self->clients->{$client_id}{'scopes'}{$scope}) { } 56 4 60
130 4 return ( 0, 'invalid_scope' );
131 }
132 elsif ( $self->clients->{ $client_id }{ scopes }{ $scope } ) {
133 56 push @{$client_scopes}, $scope;
134 }
135 }
136
137 24 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->clients->{$client_id}{"redirect_uri"} and !$redirect_uri || $redirect_uri ne $self->clients->{$client_id}{"redirect_uri"}) 8 16 24
Condition100%6/6
$self->clients->{$client_id}{"redirect_uri"} and !$redirect_uri || $redirect_uri ne $self->clients->{$client_id}{"redirect_uri"}
!ll&&!rl&&r
1248
not $redirect_uri or $redirect_uri ne $self->clients->{$client_id}{"redirect_uri"}
l!l&&r!l&&!r
444
MC/DC100%3/3
$self->clients->{$client_id}{"redirect_uri"} and !$redirect_uri || $redirect_uri ne $self->clients->{$client_id}{"redirect_uri"}
$self->clients->{$client_id}{"redirect_uri"} not $redirect_uri $redirect_uri ne $self->clients->{$client_id}{"redirect_uri"}
Truth table100%4/4
A and not(B) or C
A= $self->clients->{$client_id}{"redirect_uri"}B= not $redirect_uriC= $redirect_uri ne $self->clients->{$client_id}{"redirect_uri"}
ABCresult
0XX0
1000
11X1
1011
138 # redirect_uri is optional
139 $self->clients->{ $client_id }{ redirect_uri }
140 && (
141 ! $redirect_uri
142 || $redirect_uri ne $self->clients->{ $client_id }{ redirect_uri }
143 )
144 ) {
145 8 return ( 0, 'invalid_request' );
146 }
147  
148 16 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->clients->{$client_id}{"client_secret"}) 4 12 16
149 # implies Authorization Code Grant, not Implicit Grant
150 $self->clients->{ $client_id }{ client_secret }
151 ) {
152 4 return ( 0, 'unauthorized_client' );
153 }
154  
155 12 return ( 1, undef, $client_scopes );
156 }
157  
158 4 return ( 0, 'unauthorized_client' );
159 }
160  
161 40 ▶sub _verify_access_token {
Subroutine _verify_access_token: called CC=8 SCAR=21.4
162 40 my ( $self, %args ) = @_;
163  
164 40 delete( $args{is_refresh_token} ); # not supported by implicit grant
165  
166 40 ▶ return $self->_verify_access_token_jwt( %args ) if $self->jwt_secret;
Branch: 1 branch
BranchTrueFalseTotal
if $self->jwt_secret 20 20 40
167  
168 my ( $a_token, $scopes_ref ) =
169 20 @args{ qw/ access_token scopes / };
170  
171 20 ▶ if ( exists( $self->access_tokens->{ $a_token } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->access_tokens->{$a_token}) 12 8 20
172  
173 12 ▶ if ( $self->access_tokens->{ $a_token }{ expires } <= time ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->access_tokens->{$a_token}{'expires'} <= time) { } 0 12 12
elsif ($scopes_ref) { } 12 0 12
174 0 $self->_revoke_access_token( $a_token );
175 0 return ( 0, 'invalid_grant' );
176 }
177 elsif ( $scopes_ref ) {
178  
179 12 ▶ foreach my $scope ( @{ $scopes_ref // [] } ) {
Condition50%1/2
$scopes_ref // []
l!l
120
MC/DC0%0/1
$scopes_ref // []
$scopes_ref
Truth table50%1/2
A
A= $scopes_ref
Aresult
11
00
180 return ( 0, 'invalid_grant' )
181 20 ▶ if !$self->_has_scope( $scope, $self->access_tokens->{ $a_token }{ scope } );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_scope($scope, $self->access_tokens->{$a_token}{"scope"}) 4 16 20
182 }
183  
184 }
185  
186 8 return ( $self->access_tokens->{ $a_token }{ client_id }, undef );
187 }
188  
189 8 return ( 0, 'invalid_grant' );
190 }
191  
192 =head1 AUTHOR
193  
194 Lee Johnson - C<leejo@cpan.org>
195  
196 =head1 LICENSE
197  
198 This library is free software; you can redistribute it and/or modify it under
199 the same terms as Perl itself. If you would like to contribute documentation
200 or file a bug report then please raise an issue / pull request:
201  
202 https://github.com/Humanstate/net-oauth2-authorizationserver
203  
204 =cut
205  
206 5 __PACKAGE__->meta->make_immutable;
« AuthorizationServer/Defaults.pm ↑ Summary AuthorizationServer/AuthorizationCodeGrant.pm »