AuthorizationServer/PasswordGrant.pm

filter: Statementstmt 97.0% 33 / 34 Branchbran 83.3% 5 / 6 Conditioncond 64.2% 18 / 28 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 2 totaltotal 76.6% 72 / 94 CC 14 SCAR 16.3
CC
14
Cov
82%
BUILD
10.1
_verify_user_password
6.0
_has_users
3.3
CRAP 5.1
« AuthorizationServer/AuthorizationCodeGrant.pm ↑ Summary AuthorizationServer/ClientCredentialsGrant.pm »
1 package Net::OAuth2::AuthorizationServer::PasswordGrant;
2  
3 =head1 NAME
4  
5 Net::OAuth2::AuthorizationServer::PasswordGrant - OAuth2 Resource Owner Password Credentials Grant
6  
7 You "MUST 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::PasswordGrant->new(
12 clients => {
13 TrendyNewService => {
14 client_secret => 'TopSecretClientSecret',
15 scopes => {
16 post_images => 1,
17 annoy_friends => 1,
18 },
19 },
20 },
21 users => {
22 bob => 'j$s03R#!\fs',
23 tom => 'dE0@@s^tWg1',
24 },
25 );
26  
27 # verify a client and username against known clients/users
28 my ( $client_id,$error,$scopes,$username ) = $Grant->verify_user_password(
29 client_id => $client_id,
30 client_secret => $client_secret,
31 username => $username,
32 password => $password,
33 scopes => [ qw/ list of scopes / ],
34 );
35  
36 if ( ! $Grant->login_resource_owner ) {
37 # resource owner needs to login
38 ...
39 }
40  
41 # have resource owner confirm (and perhaps modify) scopes
42 my ( $confirmed,$error,$scopes_ref ) = $Grant->confirm_by_resource_owner(
43 client_id => $client_id,
44 scopes => [ qw/ list of scopes / ],
45 );
46  
47 # generate a token
48 my $token = $Grant->token(
49 client_id => $client_id,
50 scopes => $scopes_ref,
51 type => 'access', # one of: access, refresh
52 redirect_uri => $redirect_uri,
53 user_id => $user_id, # optional
54 jwt_claims_cb => sub { ... }, # optional, see jwt_claims_cb in Manual
55 );
56  
57 # store access token
58 $Grant->store_access_token(
59 client_id => $client,
60 access_token => $access_token,
61 refresh_token => $refresh_token,
62 scopes => $scopes_ref,
63 old_refresh_token => $old_refresh_token,
64 );
65  
66 # verify an access token
67 my ( $is_valid,$error ) = $Grant->verify_access_token(
68 access_token => $access_token,
69 scopes => $scopes_ref,
70 is_refresh_token => 0,
71 );
72  
73 # or:
74 my ( $oauth_details,$error ) = $Grant->verify_token_and_scope(
75 refresh_token => $refresh_token,
76 auth_header => $http_authorization_header,
77 );
78  
79 =head1 DESCRIPTION
80  
81 This module implements the OAuth2 "Resource Owner Password Credentials Grant" flow as described
82 at L<http://tools.ietf.org/html/rfc6749#section-4.3>.
83  
84 =head1 CONSTRUCTOR ARGUMENTS
85  
86 Along with those detailed at L<Net::OAuth2::AuthorizationServer::Manual/"CONSTRUCTOR ARGUMENTS">
87 the following are supported by this grant type:
88  
89 =head2 users
90  
91 A hashref of client details keyed like so:
92  
93 $username => $password
94  
95 =head1 CALLBACK FUNCTIONS
96  
97 The following callbacks are supported by this grant type:
98  
99 login_resource_owner_cb
100 confirm_by_resource_owner_cb
101 verify_client_cb
102 verify_user_password_cb
103 store_access_token_cb
104 verify_access_token_cb
105  
106 Please see L<Net::OAuth2::AuthorizationServer::Manual/"CALLBACK FUNCTIONS"> for
107 documentation on each callback function.
108  
109 =cut
110  
111 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
112 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
113  
114 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 3 with 'Net::OAuth2::AuthorizationServer::Defaults';
116  
117 3 ▶use Carp qw/ croak /;
Subroutine BEGIN: called CC=1 SCAR=0.0
118 3 ▶use Types::Standard qw/ :all /;
Subroutine BEGIN: called CC=1 SCAR=0.0
119  
120 3 has 'verify_user_password_cb' => (
121 is => 'ro',
122 isa => Maybe [CodeRef],
123 required => 0,
124 );
125  
126 has 'users' => (
127 is => 'ro',
128 isa => Maybe [HashRef],
129 required => 0,
130 2 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
131 3 );
132  
133  
134 12 ▶sub _uses_auth_codes { 0 };
Subroutine _uses_auth_codes: called CC=1 SCAR=0.0
135 0 ▶sub _uses_user_passwords { 1 };
Subroutine _uses_user_passwords: not called CC=1 SCAR=6.9
136  
137 1 ▶sub _has_users { return keys %{ shift->users // {} } ? 1 : 0 }
Subroutine _has_users: called CC=3 SCAR=12.0
Branch: 1 branch
BranchTrueFalseTotal
keys %{(shift())->users // {};} ? : 0 1 1
Condition50%1/2
(shift())->users // {}
l!l
10
MC/DC0%0/1
(shift())->users // {}
(shift())->users
Truth table50%1/2
A
A= (shift())->users
Aresult
11
00
138  
139 6 ▶sub BUILD {
Subroutine BUILD: called CC=7 SCAR=23.2
Pod undocumented
140 6 my ( $self, $args ) = @_;
141  
142 6 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_has_clients || $self->_has_users || ($args->{"verify_client_cb"} || $args->{"verify_user_password_cb"} || $args->{"store_access_token_cb"} || $args->{"verify_access_token_cb"})) 1 5 6
Condition46%7/15
$self->_has_clients || $self->_has_users || ($args->{'verify_client_cb'} || $args->{'verify_user_password_cb'} || $args->{'store_access_token_cb'} || $args->{'verify_access_token_cb'})
l!l&&r!l&&!r
501
$self->_has_clients or $self->_has_users
l!l&&r!l&&!r
501
$args->{"verify_client_cb"} or $args->{"verify_user_password_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->{"verify_user_password_cb"} or $args->{"store_access_token_cb"}
l!l&&r!l&&!r
001
$args->{"verify_client_cb"} or $args->{"verify_user_password_cb"}
l!l&&r!l&&!r
001
MC/DC16%1/6
$self->_has_clients || $self->_has_users || ($args->{'verify_client_cb'} || $args->{'verify_user_password_cb'} || $args->{'store_access_token_cb'} || $args->{'verify_access_token_cb'})
$self->_has_clients $self->_has_users $args->{"verify_client_cb"} $args->{"verify_user_password_cb"} $args->{"store_access_token_cb"} $args->{"verify_access_token_cb"}
Truth table28%2/7
A or B || C or D or E or F
A= $self->_has_clientsB= $self->_has_usersC= $args->{"verify_client_cb"}D= $args->{"verify_user_password_cb"}E= $args->{"store_access_token_cb"}F= $args->{"verify_access_token_cb"}
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
143 # if we don't have a list of clients
144 !$self->_has_clients
145  
146 # and we don't have a list of users
147 and !$self->_has_users
148  
149 # we must know how to verify clients and tokens
150 and ( !$args->{ verify_client_cb }
151 and !$args->{ verify_user_password_cb }
152 and !$args->{ store_access_token_cb }
153 and !$args->{ verify_access_token_cb } )
154 )
155 {
156 1 croak __PACKAGE__ . " requires either clients or overrides";
157 }
158 }
159  
160 20 ▶sub verify_user_password {
Subroutine verify_user_password: called CC=1 SCAR=0.0
Pod undocumented
161 20 _delegate_to_cb_or_private( 'verify_user_password', @_ );
162 }
163  
164 20 ▶sub _verify_user_password {
Subroutine _verify_user_password: called CC=6 SCAR=17.9
165 20 my ( $self, %args ) = @_;
166  
167 my ( $client_id, $client_secret, $username, $password, $scopes ) =
168 20 @args{ qw/ client_id client_secret username password scopes / };
169  
170 20 ▶ my $client = $self->clients->{ $client_id }
Condition100%2/2
$self->clients->{$client_id} || (return 0, 'unauthorized_client')
l!l
164
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
171 || return ( 0, 'unauthorized_client' );
172  
173 16 ▶ if ( !exists( $self->clients->{ $client_id } )
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->clients->{$client_id} && exists $self->users->{$username} or $client_secret ne $self->clients->{$client_id}{'client_secret'} or $password ne $self->users->{$username}) { } 12 4 16
Condition88%8/9
not exists $self->clients->{$client_id} && exists $self->users->{$username} or $client_secret ne $self->clients->{$client_id}{"client_secret"} or $password ne $self->users->{$username}
l!l&&r!l&&!r
844
not exists $self->clients->{$client_id} && exists $self->users->{$username} or $client_secret ne $self->clients->{$client_id}{"client_secret"}
l!l&&r!l&&!r
448
exists $self->clients->{$client_id} && exists $self->users->{$username}
!ll&&!rl&&r
0412
MC/DC75%3/4
not exists $self->clients->{$client_id} && exists $self->users->{$username} or $client_secret ne $self->clients->{$client_id}{"client_secret"} or $password ne $self->users->{$username}
exists $self->clients->{$client_id} exists $self->users->{$username} $client_secret ne $self->clients->{$client_id}{"client_secret"} $password ne $self->users->{$username}
Truth table80%4/5
not(A && B) or C or D
A= exists $self->clients->{$client_id}B= exists $self->users->{$username}C= $client_secret ne $self->clients->{$client_id}{"client_secret"}D= $password ne $self->users->{$username}
ABCDresult
0XXX1
10XX1
111X1
11011
11000
174 or !exists( $self->users->{ $username } )
175 or ( $client_secret ne $self->clients->{ $client_id }{ client_secret } )
176 or ( $password ne $self->users->{ $username } )
177 ) {
178 12 return ( 0, 'invalid_grant' );
179 }
180 else {
181 return (
182 {
183 4 client_id => $client_id,
184 scopes => $scopes,
185 username => $username
186 },
187 undef,
188 $scopes, # here for back compat
189 $username, # here for back compat
190 );
191 }
192  
193 }
194  
195 =head1 AUTHOR
196  
197 Lee Johnson - C<leejo@cpan.org>
198  
199 =head1 LICENSE
200  
201 This library is free software; you can redistribute it and/or modify it under
202 the same terms as Perl itself. If you would like to contribute documentation
203 or file a bug report then please raise an issue / pull request:
204  
205 https://github.com/Humanstate/net-oauth2-authorizationserver
206  
207 =cut
208  
209 3 __PACKAGE__->meta->make_immutable;
« AuthorizationServer/AuthorizationCodeGrant.pm ↑ Summary AuthorizationServer/ClientCredentialsGrant.pm »