blib/lib/Lemonldap/NG/Portal/Plugins/OIDC/ClientCredentialsGrant.pm

filter: Statementstmt 71.9% 41 / 57 Branchbran 50.0% 10 / 20 Conditioncond 28.5% 2 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 1 totaltotal 62.1% 59 / 95 CC 14 SCAR 29.2
CC
14
Cov
63%
_run
28.1
handleClientCredentialsGrant
3.3
BEGIN
1.0
CRAP 18.5
« blib/lib/Lemonldap/NG/Portal/Plugins/TrustedBrowser.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Main/Process.pm »
1 package Lemonldap::NG::Portal::Plugins::OIDC::ClientCredentialsGrant;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Lemonldap::NG::Portal::Main::Constants qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
6 PE_OK
7 PE_SENDRESPONSE
8 1 );
9  
10 1 our $VERSION = '2.23.0';
11  
12 1 extends 'Lemonldap::NG::Portal::Lib::OIDCPlugin';
13  
14 1 ▶use constant hook => { oidcGotTokenRequest => 'handleClientCredentialsGrant' };
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶sub handleClientCredentialsGrant {
Subroutine handleClientCredentialsGrant: called CC=3 SCAR=12.0
Pod undocumented
17 2 my ( $self, $req, $rp, $grant_type ) = @_;
18 2 ▶ return PE_OK unless $grant_type eq 'client_credentials';
Branch: 1 branch
BranchTrueFalseTotal
unless $grant_type eq "client_credentials" 1 1 2
19  
20 1 ▶ unless ( $self->oidc->rpOptions->{$rp}
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->oidc->rpOptions->{$rp}{"oidcRPMetaDataOptionsAllowClientCredentialsGrant"}) 0 1 1
21 ->{oidcRPMetaDataOptionsAllowClientCredentialsGrant} )
22 {
23 0 $self->logger->warn(
24 "Access to Client Credentials grant is not allowed for RP $rp");
25 0 $req->response(
26 $self->oidc->sendOIDCError( $req, 'unauthorized_client', 400 ) );
27 0 return PE_SENDRESPONSE;
28 }
29  
30 1 $req->response( $self->_run( $req, $rp ) );
31 1 return PE_SENDRESPONSE;
32 }
33  
34 1 ▶sub _run {
Subroutine _run: called CC=12 SCAR=33.4
35 1 my ( $self, $req, $rp ) = @_;
36 1 my $oidc = $self->oidc;
37  
38 # The client credentials grant type MUST only be used by confidential
39 # clients.
40 1 ▶ if ( $oidc->rpOptions->{$rp}->{oidcRPMetaDataOptionsPublic} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oidc->rpOptions->{$rp}{"oidcRPMetaDataOptionsPublic"}) 0 1 1
41 0 $self->logger->error(
42 "Client Credentials grant cannot be used on public clients");
43 0 return $oidc->sendOIDCError( $req, 'unauthorized_client', 400 );
44 }
45 1 my $client_id = $oidc->rpOptions->{$rp}->{oidcRPMetaDataOptionsClientID};
46  
47 # Populate minimal session info
48 1 ▶ my $req_scope = $req->param('scope') || '';
Condition50%1/2
$req->param('scope') || ''
l!l
10
MC/DC0%0/1
$req->param('scope') || ''
$req->param('scope')
Truth table50%1/2
A
A= $req->param('scope')
Aresult
11
00
49 1 my $scope = $oidc->getScope( $req, $rp, $req_scope );
50  
51 1 ▶ unless ($scope) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($scope) 0 1 1
52 0 $self->userLogger->warn( 'Client '
53 . $client_id
54 . " was not granted any requested scopes ($req_scope) for $rp" );
55 0 return $oidc->sendOIDCError( $req, 'invalid_scope', 400 );
56 }
57  
58 my $infos = {
59 1 $self->conf->{whatToTrace} => $client_id,
60 _clientId => $client_id,
61 _clientConfKey => $rp,
62 _scope => $scope,
63 _utime => time,
64 };
65  
66 1 my $h = $self->p->processHook( $req, 'oidcGotClientCredentialsGrant',
67 $infos, $rp );
68 1 ▶ return $oidc->sendOIDCError( $req, 'server_error', 500 )
Branch: 1 branch
BranchTrueFalseTotal
if $h != 0 0 1 1
69 if ( $h != PE_OK );
70  
71 # Update scope in case hook changed it
72 1 $scope = $infos->{_scope};
73  
74 # Run rule against session info
75 1 ▶ if ( my $rule = $oidc->rpRules->{$rp} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rule = $oidc->rpRules->{$rp}) 0 1 1
76 my $ruleVariables =
77 0 ▶ { %{ $infos || {} }, _oidc_grant_type => "clientcredentials", };
Condition0%0/2
$infos || {}
l!l
00
MC/DC0%0/1
$infos || {}
$infos
Truth table0%0/2
A
A= $infos
Aresult
11
00
78 0 ▶ unless ( $rule->( $req, $ruleVariables ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$rule($req, $ruleVariables)) 0 0 0
79 0 $self->userLogger->warn(
80 "Relying party $rp did not validate the provided "
81 . "Access Rule during Client Credentials Grant" );
82 0 return $oidc->sendOIDCError( $req, 'invalid_grant', 400 );
83 }
84 }
85  
86 # Create access token
87 1 my $session = $self->p->getApacheSession( undef, info => $infos );
88 1 ▶ unless ($session) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session) 0 1 1
89 0 $self->logger->error("Unable to create session");
90 0 return $oidc->sendOIDCError( $req, 'server_error', 500 );
91 }
92  
93 1 my $access_token = $oidc->newAccessToken(
94 $req, $rp, $scope,
95 $session->data,
96 {
97 scope => $scope,
98 rp => $rp,
99 user_session_id => $session->id,
100 grant_type => "clientcredentials",
101 }
102 );
103 1 ▶ unless ($access_token) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($access_token) 0 1 1
104 0 $self->userLogger->error("Unable to create Access Token");
105 0 return $oidc->sendOIDCError( $req, 'Unable to create Access Token',
106 500 );
107 }
108  
109 my $expires_in =
110 $oidc->rpOptions->{$rp}->{oidcRPMetaDataOptionsAccessTokenExpiration}
111 1 ▶ || $self->conf->{oidcServiceAccessTokenExpiration};
Condition33%1/3
$oidc->rpOptions->{$rp}{'oidcRPMetaDataOptionsAccessTokenExpiration'} || $self->conf->{'oidcServiceAccessTokenExpiration'}
l!l&&r!l&&!r
100
MC/DC0%0/2
$oidc->rpOptions->{$rp}{'oidcRPMetaDataOptionsAccessTokenExpiration'} || $self->conf->{'oidcServiceAccessTokenExpiration'}
$oidc->rpOptions->{$rp}{'oidcRPMetaDataOptionsAccessTokenExpiration'} $self->conf->{'oidcServiceAccessTokenExpiration'}
Truth table33%1/3
A || B
A= $oidc->rpOptions->{$rp}{'oidcRPMetaDataOptionsAccessTokenExpiration'}B= $self->conf->{'oidcServiceAccessTokenExpiration'}
ABresult
1X1
011
000
112  
113 1 ▶ my $token_response = {
Branch: 1 branch
BranchTrueFalseTotal
$req_scope ne $scope ? : 0 1 1
114 access_token => "$access_token",
115 token_type => 'Bearer',
116 expires_in => $expires_in + 0,
117 ( ( $req_scope ne $scope ) ? ( scope => "$scope" ) : () ),
118 };
119  
120 1 $self->logger->debug("Send token response");
121 1 return $self->p->sendJSONresponse( $req, $token_response );
122 }
123  
124 package Lemonldap::NG::Portal::Plugins::OIDCClientCredentialsGrant;
125  
126 1 our @ISA = ('Lemonldap::NG::Portal::Plugins::OIDC::ClientCredentialsGrant');
127  
128 1 1;
« blib/lib/Lemonldap/NG/Portal/Plugins/TrustedBrowser.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Main/Process.pm »