| 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.19.0'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | sub retrieveSession { | ||||||||||||||||||||||||||||
| 9 | 4 | ▶ | my ( $class, $req, $id ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
retrieveSession: called CC=6 SCAR=19.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 10 | 4 | my ($offlineId) = $id =~ /^O-(.*)/; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | # Retrieve regular session if this is not an offline access token | ||||||||||||||||||||||||||||
| 13 | 4 | ▶ | unless ($offlineId) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 14 | 3 | my $data = | |||||||||||||||||||||||||||
| 15 | $class->Lemonldap::NG::Handler::Main::retrieveSession( $req, $id ); | ||||||||||||||||||||||||||||
| 16 | 3 | ▶ | if ( ref($data) eq "HASH" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | 3 | $data = { %{$data}, $class->_getTokenAttributes($req) }; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # Update cache | ||||||||||||||||||||||||||||
| 20 | 3 | $class->data($data); | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | else { | ||||||||||||||||||||||||||||
| 23 | 0 | $req->data->{oauth2_error} = 'invalid_token'; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | 3 | 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
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 1 | my $data = { %{ $session->data }, $class->_getTokenAttributes($req) }; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1 | $class->data($data); | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 1 | $class->logger->debug("Get session $offlineId from Handler::Main::Run"); | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # Verify that session is valid | ||||||||||||||||||||||||||||
| 51 | $class->logger->error( | ||||||||||||||||||||||||||||
| 52 | "_utime is not defined. This should not happen. Check if it is well transmitted to handler" | ||||||||||||||||||||||||||||
| 53 | 1 | ▶ | ) unless $session->data->{_utime}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 1 | my $ttl = $class->tsv->{timeout} - time + $session->data->{_utime}; | |||||||||||||||||||||||||||
| 56 | 1 | $class->logger->debug( "Session TTL = " . $ttl ); | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 1 | ▶ | if ( time - $session->data->{_utime} > $class->tsv->{timeout} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | $class->logger->info("Session $id expired"); | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # Clean cached data | ||||||||||||||||||||||||||||
| 62 | 0 | $class->data( {} ); | |||||||||||||||||||||||||||
| 63 | 0 | return 0; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 1 | return $data; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | else { | ||||||||||||||||||||||||||||
| 69 | 0 | $class->logger->info("Session $offlineId can not be retrieved"); | |||||||||||||||||||||||||||
| 70 | 0 | $class->logger->info( $session->error ); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | return 0; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | sub fetchId { | ||||||||||||||||||||||||||||
| 77 | 8 | ▶ | my ( $class, $req ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
fetchId: called CC=11 SCAR=24.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 8 | my $access_token; | |||||||||||||||||||||||||||
| 80 | 8 | my $authorization = $req->{env}->{HTTP_AUTHORIZATION}; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 8 | ▶ | if ( $authorization | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$authorization and $access_token = ($authorization =~ /^Bearer (.+)$/i)[0]
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]
| |||||||||||||||||||||||||||||
| 83 | and ( $access_token = ( $authorization =~ /^Bearer (.+)$/i )[0] ) ) | ||||||||||||||||||||||||||||
| 84 | { | ||||||||||||||||||||||||||||
| 85 | 7 | $class->logger->debug("Found OAuth2 Access Token ($access_token)"); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | else { | ||||||||||||||||||||||||||||
| 88 | 1 | return $class->Lemonldap::NG::Handler::Main::fetchId($req); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # Get access token session | ||||||||||||||||||||||||||||
| 92 | 7 | my $access_token_sid = getAccessTokenSessionId($access_token); | |||||||||||||||||||||||||||
| 93 | 7 | ▶ | unless ($access_token_sid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | $req->data->{oauth2_error} = 'invalid_token'; | |||||||||||||||||||||||||||
| 95 | 0 | return; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | 7 | my $infos = $class->getOIDCInfos($access_token_sid); | |||||||||||||||||||||||||||
| 98 | 7 | ▶ | unless ($infos) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 1 | $req->data->{oauth2_error} = 'invalid_token'; | |||||||||||||||||||||||||||
| 100 | 1 | return; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # Store scope and rpid for future session attributes | ||||||||||||||||||||||||||||
| 104 | 6 | ▶ | if ( $infos->{rp} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 5 | my $rp = $infos->{rp}; | |||||||||||||||||||||||||||
| 106 | 5 | $req->data->{_scope} = $infos->{scope}; | |||||||||||||||||||||||||||
| 107 | 5 | $req->data->{_oidc_grant_type} = $infos->{grant_type}; | |||||||||||||||||||||||||||
| 108 | 5 | $req->data->{_clientConfKey} = $rp; | |||||||||||||||||||||||||||
| 109 | 5 | ▶ | if ( $class->tsv->{oauth2Options}->{$rp} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$class->tsv->{"oauth2Options"}{$rp} and $class->tsv->{"oauth2Options"}{$rp}{"clientId"}
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"}
| |||||||||||||||||||||||||||||
| 110 | and $class->tsv->{oauth2Options}->{$rp}->{clientId} ) | ||||||||||||||||||||||||||||
| 111 | { | ||||||||||||||||||||||||||||
| 112 | $req->data->{_clientId} = | ||||||||||||||||||||||||||||
| 113 | 5 | $class->tsv->{oauth2Options}->{$rp}->{clientId}; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | # If this token is tied to a regular session ID | ||||||||||||||||||||||||||||
| 118 | 6 | ▶ | if ( my $_session_id = $infos->{user_session_id} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 4 | $class->logger->debug( 'Get user session id ' . $_session_id ); | |||||||||||||||||||||||||||
| 120 | 4 | return $_session_id; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # If this token is tied to an Offline session | ||||||||||||||||||||||||||||
| 124 | 2 | ▶ | if ( my $_session_id = $infos->{offline_session_id} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 1 | $class->logger->debug( 'Get offline session id ' . $_session_id ); | |||||||||||||||||||||||||||
| 126 | 1 | return "O-$_session_id"; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 1 | my $value = $class->Lemonldap::NG::Handler::Main::fetchId($req); | |||||||||||||||||||||||||||
| 130 | 1 | ▶ | unless ($value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 1 | $req->data->{oauth2_error} = 'invalid_token'; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | 1 | return $value; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | ## @rmethod protected hash getOIDCInfos(id) | ||||||||||||||||||||||||||||
| 137 | # Tries to retrieve the OIDC session, get infos | ||||||||||||||||||||||||||||
| 138 | # @return OIDC session infos | ||||||||||||||||||||||||||||
| 139 | sub getOIDCInfos { | ||||||||||||||||||||||||||||
| 140 | 7 | ▶ | my ( $class, $id ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
getOIDCInfos: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 141 | 7 | my $infos = {}; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # Get the session | ||||||||||||||||||||||||||||
| 144 | my $oidcSession = Lemonldap::NG::Common::Session->new( { | ||||||||||||||||||||||||||||
| 145 | hashStore => $class->tsv->{hashedSessionStore}, | ||||||||||||||||||||||||||||
| 146 | storageModule => $class->tsv->{oidcStorageModule}, | ||||||||||||||||||||||||||||
| 147 | storageModuleOptions => $class->tsv->{oidcStorageOptions}, | ||||||||||||||||||||||||||||
| 148 | cacheModule => $class->tsv->{sessionCacheModule}, | ||||||||||||||||||||||||||||
| 149 | cacheModuleOptions => $class->tsv->{sessionCacheOptions}, | ||||||||||||||||||||||||||||
| 150 | 7 | id => $id, | |||||||||||||||||||||||||||
| 151 | kind => "OIDCI", | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | ); | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 7 | ▶ | unless ( $oidcSession->error ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 6 | $class->logger->debug("Get OIDC session $id"); | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | # Verify that session is valid | ||||||||||||||||||||||||||||
| 159 | 6 | ▶ | unless ( $oidcSession->data->{_utime} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 0 | $class->logger->error("_utime missing from Access Token session"); | |||||||||||||||||||||||||||
| 161 | 0 | return; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 6 | my $ttl = $class->tsv->{timeout} - time + $oidcSession->data->{_utime}; | |||||||||||||||||||||||||||
| 165 | 6 | $class->logger->debug("Session TTL = $ttl"); | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 6 | ▶ | if ( time - $oidcSession->data->{_utime} > $class->tsv->{timeout} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 1 | $class->logger->info("Access Token session $id expired"); | |||||||||||||||||||||||||||
| 169 | 1 | return; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 5 | $infos = { %{ $oidcSession->data } }; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | else { | ||||||||||||||||||||||||||||
| 175 | 1 | $class->logger->info("OIDC session $id can not be retrieved"); | |||||||||||||||||||||||||||
| 176 | 1 | $class->logger->info( $oidcSession->error ); | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 6 | return $infos; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | ## The OAuth2 handler does not redirect, we simply return a 401 with relevant | ||||||||||||||||||||||||||||
| 183 | # information as described in https://tools.ietf.org/html/rfc6750#section-3 | ||||||||||||||||||||||||||||
| 184 | sub goToPortal { | ||||||||||||||||||||||||||||
| 185 | 3 | ▶ | my ( $class, $req, $url, $arg, $path ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
goToPortal: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 3 | my $oauth2_error = ''; | |||||||||||||||||||||||||||
| 188 | 3 | ▶ | if ( $req->data->{oauth2_error} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 2 | $oauth2_error = ' error="' . $req->data->{oauth2_error} . '"'; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | 3 | $class->set_header_out( $req, | |||||||||||||||||||||||||||
| 192 | 'WWW-Authenticate' => "Bearer" . $oauth2_error ); | ||||||||||||||||||||||||||||
| 193 | 3 | return $class->HTTP_UNAUTHORIZED; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | sub _getTokenAttributes { | ||||||||||||||||||||||||||||
| 197 | 4 | ▶ | my ( $class, $req ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_getTokenAttributes: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 198 | 4 | my %res; | |||||||||||||||||||||||||||
| 199 | 4 | for my $attr (qw/_scope _clientConfKey _clientId _oidc_grant_type/) { | |||||||||||||||||||||||||||
| 200 | 16 | ▶ | if ( $req->data->{$attr} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 12 | $res{$attr} = $req->data->{$attr}; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | 4 | return %res; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 1 | 1; | |||||||||||||||||||||||||||