« blib/lib/Lemonldap/NG/Portal/Issuer/Get.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/2F/Register/Password.pm »
| 1 | package Lemonldap::NG::Portal::Lib::OIDCTokenExchange; | ||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Lemonldap::NG::Portal::Main::Constants qw( | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | PE_SENDRESPONSE | ||||||||||||||||||||||||||||||
| 6 | PE_OK | ||||||||||||||||||||||||||||||
| 7 | 1 | ); | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Mouse; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||
| 10 | 1 | extends 'Lemonldap::NG::Portal::Lib::OIDCPlugin'; | |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||
| 12 | 1 | our $VERSION = '2.23.4'; | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||
| 14 | # INTERFACE | ||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use constant hook => { oidcGotTokenExchange => 'tokenExchange' }; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||
| 17 | # MAIN METHOD | ||||||||||||||||||||||||||||||
| 18 | # | ||||||||||||||||||||||||||||||
| 19 | # Important note: only PE_OK and PE_RESPONSE are used here because: | ||||||||||||||||||||||||||||||
| 20 | # - oidcGotTokenExchange only accept PE_SENDRESPONSE has positive response | ||||||||||||||||||||||||||||||
| 21 | # - PE_OK permits to continue to check other plugins | ||||||||||||||||||||||||||||||
| 22 | 2 | ▶ | sub tokenExchange { | ||||||||||||||||||||||||||||
|
Subroutine
tokenExchange: called CC=18 SCAR=36.8
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 23 | 2 | my ( $self, $req, $rp ) = @_; | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||
| 25 | 2 | ▶ | unless ( $self->oidc ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 26 | 0 | $self->logger->error('Authentication is not OIDC, aborting'); | |||||||||||||||||||||||||||||
| 27 | 0 | return PE_OK; | |||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||
| 30 | 2 | $self->logger->debug("Matrix Refresh Token exchange for $rp"); | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||
| 32 | # 1. Validate request | ||||||||||||||||||||||||||||||
| 33 | # | ||||||||||||||||||||||||||||||
| 34 | # Note that client_id and grant_type value are verified by | ||||||||||||||||||||||||||||||
| 35 | # ::/lib::OpenIDConnect | ||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||
| 37 | 2 | my $clientId = $req->param('client_id'); # already validated | |||||||||||||||||||||||||||||
| 38 | 2 | my $subjectToken = $req->param('subject_token'); | |||||||||||||||||||||||||||||
| 39 | 2 | ▶ | unless ($subjectToken) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 40 | 0 | $self->userLogger->error('Token exchange called without subject_token'); | |||||||||||||||||||||||||||||
| 41 | 0 | return PE_OK; | |||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||
| 44 | # Even if these fields are required in Oauth2, it seems that it is not | ||||||||||||||||||||||||||||||
| 45 | # required by Keycloak.. | ||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||
| 47 | 2 | my @types; | |||||||||||||||||||||||||||||
| 48 | 2 | foreach my $name (qw(subject_token_type requested_token_type)) { | |||||||||||||||||||||||||||||
| 49 | 4 | my $val = $req->param($name); | |||||||||||||||||||||||||||||
| 50 | 4 | ▶ | if ($val) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 51 | 4 | ▶ | unless ($val =~ s/^urn:ietf:params:oauth:token-type:// | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$val =~ s/^urn:ietf:params:oauth:token-type:// and $val =~ /^(?:(?:refresh|access|id)_token|saml[12])$/
MC/DC0%0/2
$val =~ s/^urn:ietf:params:oauth:token-type:// and $val =~ /^(?:(?:refresh|access|id)_token|saml[12])$/
$val =~ s/^urn:ietf:params:oauth:token-type://
$val =~ /^(?:(?:refresh|access|id)_token|saml[12])$/
Truth table33%1/3
A and B
A= $val =~ s/^urn:ietf:params:oauth:token-type://B= $val =~ /^(?:(?:refresh|access|id)_token|saml[12])$/
| |||||||||||||||||||||||||||||||
| 52 | and $val =~ /^(?:(?:refresh|access|id)_token|saml[12])$/ ) | ||||||||||||||||||||||||||||||
| 53 | { | ||||||||||||||||||||||||||||||
| 54 | 0 | $self->logger->info( | |||||||||||||||||||||||||||||
| 55 | "Malformed $name: " . $req->param('subject_token_type') ); | ||||||||||||||||||||||||||||||
| 56 | 0 | return PE_OK; | |||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||
| 59 | 4 | push @types, $val; | |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||
| 62 | # 2. Check for audience and authorization | ||||||||||||||||||||||||||||||
| 63 | 2 | my $targetClientId = $req->param('audience'); | |||||||||||||||||||||||||||||
| 64 | 2 | ▶ | my $target = { audience => scalar $req->param('audience') // undef, }; | ||||||||||||||||||||||||||||
|
Condition50%1/2
scalar $req->param("audience") // undef
MC/DC0%0/1
scalar $req->param("audience") // undef
scalar $req->param("audience")
Truth table50%1/2
A
A= scalar $req->param("audience")
| |||||||||||||||||||||||||||||||
| 65 | 2 | ▶ | if ( $target->{audience} ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 66 | 2 | ▶ | if ( $target->{audience} eq $clientId ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 67 | 0 | $target->{rp} = $rp; | |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||
| 69 | else { | ||||||||||||||||||||||||||||||
| 70 | my ($res) = grep { | ||||||||||||||||||||||||||||||
| 71 | $self->oidc->rpOptions->{$_}->{oidcRPMetaDataOptionsClientID} | ||||||||||||||||||||||||||||||
| 72 | 6 | eq $targetClientId | |||||||||||||||||||||||||||||
| 73 | 2 | } keys %{ $self->oidc->rpOptions }; | |||||||||||||||||||||||||||||
| 74 | 2 | $target->{rp} = $res; | |||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||
| 77 | else { | ||||||||||||||||||||||||||||||
| 78 | 0 | $target->{rp} = $rp; | |||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||
| 81 | 2 | ▶ | unless ( $self->validateAudience( $req, $rp, $target, $types[1] ) ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 82 | 0 | $self->logger->debug( ref($self) . " refused to validate audience" ); | |||||||||||||||||||||||||||||
| 83 | 0 | return PE_OK; | |||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||
| 86 | # 3. Use subclass getUid() to get user ID | ||||||||||||||||||||||||||||||
| 87 | 2 | my $uid = $self->getUid( $req, $rp, $subjectToken, $types[0] ); | |||||||||||||||||||||||||||||
| 88 | 2 | ▶ | return PE_OK unless $uid; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||
| 90 | # 4. Create refresh_token | ||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||
| 92 | 2 | my $opts = { $self->oidc->_storeOpts() }; | |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||
| 94 | # 3.1. Create a new refresh_token if missing | ||||||||||||||||||||||||||||||
| 95 | 2 | $req->user($uid); | |||||||||||||||||||||||||||||
| 96 | 2 | $req->steps( [ | |||||||||||||||||||||||||||||
| 97 | 'getUser', 'setAuthSessionInfo', | ||||||||||||||||||||||||||||||
| 98 | 'setSessionInfo', $self->p->groupsAndMacros, | ||||||||||||||||||||||||||||||
| 99 | 'setLocalGroups' | ||||||||||||||||||||||||||||||
| 100 | ] | ||||||||||||||||||||||||||||||
| 101 | ); | ||||||||||||||||||||||||||||||
| 102 | 2 | ▶ | if ( my $error = $self->p->process($req) ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | if ( $error == PE_OK ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 104 | $self->userLogger->warn( | ||||||||||||||||||||||||||||||
| 105 | 'Token exchange requested for an invalid user (' | ||||||||||||||||||||||||||||||
| 106 | . $req->{user} | ||||||||||||||||||||||||||||||
| 107 | 0 | . ")" ); | |||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||
| 109 | 0 | $self->logger->error("Unable to find user $uid"); | |||||||||||||||||||||||||||||
| 110 | 0 | return PE_OK; | |||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||
| 113 | my $refreshToken = $self->oidc->newRefreshToken( | ||||||||||||||||||||||||||||||
| 114 | $rp, | ||||||||||||||||||||||||||||||
| 115 | { | ||||||||||||||||||||||||||||||
| 116 | 2 | ▶ | %{ $req->sessionInfo }, | ||||||||||||||||||||||||||||
|
Condition40%2/5
scalar $req->param('scope') || 'openid'
$target->{'audience'} || $self->oidc->rpOptions->{$rp}{'oidcRPMetaDataOptionsClientID'}
MC/DC0%0/1
scalar $req->param('scope') || 'openid'
scalar $req->param('scope')
MC/DC0%0/2
$target->{'audience'} || $self->oidc->rpOptions->{$rp}{'oidcRPMetaDataOptionsClientID'}
$target->{'audience'}
$self->oidc->rpOptions->{$rp}{'oidcRPMetaDataOptionsClientID'}
Truth table50%1/2
A
A= scalar $req->param('scope')
Truth table33%1/3
A || B
A= $target->{'audience'}B= $self->oidc->rpOptions->{$rp}{'oidcRPMetaDataOptionsClientID'}
| |||||||||||||||||||||||||||||||
| 117 | scope => scalar $req->param('scope') || 'openid', | ||||||||||||||||||||||||||||||
| 118 | client_id => $target->{audience} | ||||||||||||||||||||||||||||||
| 119 | || $self->oidc->rpOptions->{$rp}->{oidcRPMetaDataOptionsClientID}, | ||||||||||||||||||||||||||||||
| 120 | 2 | _session_uid => $uid, | |||||||||||||||||||||||||||||
| 121 | grant_type => scalar $req->param('grant_type'), | ||||||||||||||||||||||||||||||
| 122 | }, | ||||||||||||||||||||||||||||||
| 123 | ); | ||||||||||||||||||||||||||||||
| 124 | 2 | ▶ | unless ($refreshToken) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 125 | 0 | return PE_OK; | |||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||
| 128 | # 5. Use normal method to create new access_token from refresh_token | ||||||||||||||||||||||||||||||
| 129 | # | ||||||||||||||||||||||||||||||
| 130 | # TODO: remote may want a refresh_token | ||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||
| 132 | 2 | ▶ | my $res = $self->oidc->_handleRefreshTokenGrant( $req, $target->{rp} || $rp, | ||||||||||||||||||||||||||||
|
Condition33%1/3
$target->{'rp'} || $rp
MC/DC0%0/2
$target->{'rp'} || $rp
$target->{'rp'}
$rp
Truth table33%1/3
A || B
A= $target->{'rp'}B= $rp
| |||||||||||||||||||||||||||||||
| 133 | $refreshToken->id, $targetClientId ); | ||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||
| 135 | # Insert refresh_token in response | ||||||||||||||||||||||||||||||
| 136 | 2 | my $tmp = JSON::from_json( $res->[2]->[0] ); | |||||||||||||||||||||||||||||
| 137 | 2 | $tmp->{refresh_token} = $refreshToken->id; | |||||||||||||||||||||||||||||
| 138 | 2 | $res->[2]->[0] = JSON::to_json($tmp); | |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||
| 140 | 2 | $req->response($res); | |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||
| 142 | 2 | return PE_SENDRESPONSE; | |||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||
| 145 | 1 | 1; | |||||||||||||||||||||||||||||
| 146 | __END__ | ||||||||||||||||||||||||||||||