Lemonldap/NG/Handler/Lib/OAuth2.pm

filter: Statementstmt 89.2% 91 / 102 Branchbran 77.7% 28 / 36 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 5 totaltotal 81.3% 131 / 161 CC 22 SCAR 19.3
CC
22
Cov
85%
fetchId
11.4
retrieveSession
6.9
getOIDCInfos
4.0
CRAP 6.9
« Lemonldap/NG/Handler/Lib/PSGI.pm ↑ Summary Lemonldap/NG/Handler/Lib/ZimbraPreAuth.pm »
1 package Lemonldap::NG::Handler::Lib::OAuth2;
2 1 ▶use Lemonldap::NG::Common::JWT qw(getAccessTokenSessionId);
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '2.23.0';
7  
8 7 ▶sub retrieveSession {
Subroutine retrieveSession: called CC=6 SCAR=19.4
Pod undocumented
9 7 my ( $class, $req, $id ) = @_;
10 7 my ($offlineId) = $id =~ /^O-(.*)/;
11  
12 # Retrieve regular session if this is not an offline access token
13 7 ▶ unless ($offlineId) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($offlineId) 6 1 7
14 6 my $data =
15 $class->Lemonldap::NG::Handler::Main::retrieveSession( $req, $id );
16 6 ▶ if ( ref($data) eq "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq 'HASH') { } 6 0 6
17 6 $data = { %{$data}, $class->_getTokenAttributes($req) };
18  
19 # Update cache
20 6 $class->data($data);
21 }
22 else {
23 0 $req->data->{oauth2_error} = 'invalid_token';
24 }
25 6 return $data;
26 }
27  
28 # 2. Get the session from cache or backend
29 my $session = $req->data->{session} = (
30 Lemonldap::NG::Common::Session->new( {
31 hashStore => $class->tsv->{hashedSessionStore},
32 storageModule => $class->tsv->{oidcStorageModule},
33 storageModuleOptions => $class->tsv->{oidcStorageOptions},
34 cacheModule => $class->tsv->{sessionCacheModule},
35 cacheModuleOptions => $class->tsv->{sessionCacheOptions},
36 1 id => $offlineId,
37 kind => "OIDCI",
38 }
39 )
40 );
41  
42 1 ▶ unless ( $session->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->error) { } 0 1 1
43 1 my $data = { %{ $session->data }, $class->_getTokenAttributes($req) };
44  
45 1 $class->data($data);
46 1 $class->logger->debug("Get session $offlineId from Handler::Main::Run");
47  
48 # Verify that session is valid
49 $class->logger->error(
50 '_utime is not defined. This should not happen. Check if it is well transmitted to handler'
51 1 ▶ ) unless $session->data->{_utime};
Branch: 1 branch
BranchTrueFalseTotal
unless $session->data->{"_utime"} 0 1 1
52  
53 1 my $ttl = $class->tsv->{timeout} - time + $session->data->{_utime};
54 1 $class->logger->debug("Session TTL = $ttl");
55  
56 1 ▶ if ( time - $session->data->{_utime} > $class->tsv->{timeout} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&CORE::GLOBAL::time() - $session->data->{"_utime"} > $class->tsv->{"timeout"}) 0 1 1
57 0 $class->logger->info("Session $id expired");
58  
59 # Clean cached data
60 0 $class->data( {} );
61 0 return 0;
62 }
63  
64 1 return $data;
65 }
66 else {
67 0 $class->logger->info("Session $offlineId can not be retrieved");
68 0 $class->logger->info( $session->error );
69  
70 0 return 0;
71 }
72 }
73  
74 13 ▶sub fetchId {
Subroutine fetchId: called CC=11 SCAR=24.3
Pod undocumented
75 13 my ( $class, $req ) = @_;
76 13 my $access_token;
77 13 my $authorization = $req->{env}->{HTTP_AUTHORIZATION};
78  
79 13 ▶ if ( $authorization
Branch: 1 branch
BranchTrueFalseTotal
if ($authorization and $access_token = ($authorization =~ /^Bearer (.+)$/i)[0]) { } 12 1 13
Condition66%2/3
$authorization and $access_token = ($authorization =~ /^Bearer (.+)$/i)[0]
!ll&&!rl&&r
1012
MC/DC50%1/2
$authorization and $access_token = ($authorization =~ /^Bearer (.+)$/i)[0]
$authorization $access_token = ($authorization =~ /^Bearer (.+)$/i)[0]
Truth table66%2/3
A and B
A= $authorizationB= $access_token = ($authorization =~ /^Bearer (.+)$/i)[0]
ABresult
0X0
100
111
80 and ( $access_token = ( $authorization =~ /^Bearer (.+)$/i )[0] ) )
81 {
82 12 $class->logger->debug("Found OAuth2 Access Token ($access_token)");
83 }
84 else {
85 1 return $class->Lemonldap::NG::Handler::Main::fetchId($req);
86 }
87  
88 # Get access token session
89 12 my $access_token_sid = getAccessTokenSessionId($access_token);
90 12 ▶ unless ($access_token_sid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($access_token_sid) 0 12 12
91 0 $req->data->{oauth2_error} = 'invalid_token';
92 0 return;
93 }
94 12 my $infos = $class->getOIDCInfos($access_token_sid);
95 12 ▶ unless ($infos) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($infos) 1 11 12
96 1 $req->data->{oauth2_error} = 'invalid_token';
97 1 return;
98 }
99  
100 # Store scope and rpid for future session attributes
101 11 ▶ if ( $infos->{rp} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($infos->{"rp"}) 10 1 11
102 10 my $rp = $infos->{rp};
103 10 $req->data->{_accessToken} = $access_token;
104 10 $req->data->{_scope} = $infos->{scope};
105 10 $req->data->{_oidc_grant_type} = $infos->{grant_type};
106 10 $req->data->{_audiences} = $infos->{aud};
107 10 $req->data->{_clientConfKey} = $rp;
108 10 ▶ if ( $class->tsv->{oauth2Options}->{$rp}
Branch: 1 branch
BranchTrueFalseTotal
if ($class->tsv->{"oauth2Options"}{$rp} and $class->tsv->{"oauth2Options"}{$rp}{"clientId"}) 10 0 10
Condition33%1/3
$class->tsv->{"oauth2Options"}{$rp} and $class->tsv->{"oauth2Options"}{$rp}{"clientId"}
!ll&&!rl&&r
0010
MC/DC0%0/2
$class->tsv->{"oauth2Options"}{$rp} and $class->tsv->{"oauth2Options"}{$rp}{"clientId"}
$class->tsv->{"oauth2Options"}{$rp} $class->tsv->{"oauth2Options"}{$rp}{"clientId"}
Truth table33%1/3
A and B
A= $class->tsv->{"oauth2Options"}{$rp}B= $class->tsv->{"oauth2Options"}{$rp}{"clientId"}
ABresult
0X0
100
111
109 and $class->tsv->{oauth2Options}->{$rp}->{clientId} )
110 {
111 $req->data->{_clientId} =
112 10 $class->tsv->{oauth2Options}->{$rp}->{clientId};
113 }
114 }
115  
116 # If this token is tied to a regular session ID
117 11 ▶ if ( my $_session_id = $infos->{user_session_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $_session_id = $infos->{"user_session_id"}) 9 2 11
118 9 $class->logger->debug( 'Get user session id ' . $_session_id );
119 9 return $_session_id;
120 }
121  
122 # If this token is tied to an Offline session
123 2 ▶ if ( my $_session_id = $infos->{offline_session_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $_session_id = $infos->{"offline_session_id"}) 1 1 2
124 1 $class->logger->debug( 'Get offline session id ' . $_session_id );
125 1 return "O-$_session_id";
126 }
127  
128 1 my $value = $class->Lemonldap::NG::Handler::Main::fetchId($req);
129 1 ▶ unless ($value) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value) 1 0 1
130 1 $req->data->{oauth2_error} = 'invalid_token';
131 }
132 1 return $value;
133 }
134  
135 ## @rmethod protected hash getOIDCInfos(id)
136 # Tries to retrieve the OIDC session, get infos
137 # @return OIDC session infos
138 12 ▶sub getOIDCInfos {
Subroutine getOIDCInfos: called CC=4 SCAR=13.9
Pod undocumented
139 12 my ( $class, $id ) = @_;
140 12 my $infos = {};
141  
142 # Get the session
143 my $oidcSession = Lemonldap::NG::Common::Session->new( {
144 hashStore => $class->tsv->{hashedSessionStore},
145 storageModule => $class->tsv->{oidcStorageModule},
146 storageModuleOptions => $class->tsv->{oidcStorageOptions},
147 cacheModule => $class->tsv->{sessionCacheModule},
148 cacheModuleOptions => $class->tsv->{sessionCacheOptions},
149 12 id => $id,
150 kind => "OIDCI",
151 }
152 );
153  
154 12 ▶ unless ( $oidcSession->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oidcSession->error) { } 1 11 12
155 11 $class->logger->debug("Get OIDC session $id");
156  
157 # Verify that session is valid
158 11 ▶ unless ( $oidcSession->data->{_utime} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($oidcSession->data->{"_utime"}) 0 11 11
159 0 $class->logger->error("_utime missing from Access Token session");
160 0 return;
161 }
162  
163 11 my $ttl = $class->tsv->{timeout} - time + $oidcSession->data->{_utime};
164 11 $class->logger->debug("Session TTL = $ttl");
165  
166 11 ▶ if ( time - $oidcSession->data->{_utime} > $class->tsv->{timeout} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&CORE::GLOBAL::time() - $oidcSession->data->{"_utime"} > $class->tsv->{"timeout"}) 1 10 11
167 1 $class->logger->info("Access Token session $id expired");
168 1 return;
169 }
170  
171 10 $infos = { %{ $oidcSession->data } };
172 }
173 else {
174 1 $class->logger->info("OIDC session $id can not be retrieved");
175 1 $class->logger->info( $oidcSession->error );
176 }
177  
178 11 return $infos;
179 }
180  
181 # Hide AccesToken to the protected application
182 4 ▶sub hideCookie {
Subroutine hideCookie: called CC=1 SCAR=0.0
Pod undocumented
183 4 my ( $class, $req ) = @_;
184 4 $class->logger->debug('Removing Authorization header');
185 4 $class->unset_header_in( $req, 'Authorization' );
186 4 $class->Lemonldap::NG::Handler::Main::hideCookie($req);
187 }
188  
189 ## The OAuth2 handler does not redirect, we simply return a 401 with relevant
190 # information as described in https://tools.ietf.org/html/rfc6750#section-3
191 3 ▶sub goToPortal {
Subroutine goToPortal: called CC=2 SCAR=6.9
Pod undocumented
192 3 my ( $class, $req, $url, $arg, $path ) = @_;
193  
194 3 my $oauth2_error = '';
195 3 ▶ if ( $req->data->{oauth2_error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->data->{"oauth2_error"}) 2 1 3
196 2 $oauth2_error = ' error="' . $req->data->{oauth2_error} . '"';
197 }
198 3 $class->set_header_out( $req,
199 'WWW-Authenticate' => "Bearer" . $oauth2_error );
200 3 return $class->HTTP_UNAUTHORIZED;
201 }
202  
203 7 ▶sub _getTokenAttributes {
Subroutine _getTokenAttributes: called CC=3 SCAR=11.0
204 7 my ( $class, $req ) = @_;
205 7 my %res;
206 7 for my $attr (
207 qw/_scope _clientConfKey _clientId _oidc_grant_type _accessToken _audiences/
208 )
209 {
210 42 ▶ if ( $req->data->{$attr} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->data->{$attr}) 30 12 42
211 30 $res{$attr} = $req->data->{$attr};
212 }
213 }
214 7 return %res;
215 }
216  
217 1 1;
« Lemonldap/NG/Handler/Lib/PSGI.pm ↑ Summary Lemonldap/NG/Handler/Lib/ZimbraPreAuth.pm »