« blib/lib/Lemonldap/NG/Portal/Plugins/Upgrade.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/Lib/LDAP.pm »
| 1 | package Lemonldap::NG::Portal::Plugins::OIDC::NativeSso; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Digest::SHA qw(sha256_hex); | ||
| 5 | use Mouse; | ||
| 6 | use Lemonldap::NG::Common::JWT qw(getJWTPayload); | ||
| 7 | use Lemonldap::NG::Portal::Main::Constants qw( | ||
| 8 | PE_BADCREDENTIALS | ||
| 9 | PE_ERROR | ||
| 10 | PE_OK | ||
| 11 | PE_SENDRESPONSE | ||
| 12 | ); | ||
| 13 | use String::Random qw/random_string/; | ||
| 14 | |||
| 15 | our $VERSION = '2.23.0'; | ||
| 16 | |||
| 17 | extends 'Lemonldap::NG::Portal::Lib::OIDCPlugin'; | ||
| 18 | |||
| 19 | use constant hook => { | ||
| 20 | |||
| 21 | # Step 1 verify that device_sso scope is authorized during authorization request | ||
| 22 | oidcResolveScope => 'resolveScope', | ||
| 23 | |||
| 24 | # Step 2: generate device_secret | ||
| 25 | oidcGenerateAccessToken => 'accessTokenHook', | ||
| 26 | oidcGenerateIDToken => 'addDsHash', | ||
| 27 | oidcGenerateTokenResponse => 'tokenResponse', | ||
| 28 | |||
| 29 | # Step 3: give new access_token when device_secret is valid | ||
| 30 | oidcGotTokenExchange => 'tokenExchangeHook', | ||
| 31 | }; | ||
| 32 | |||
| 33 | use constant sessionKind => 'OIDCI'; | ||
| 34 | |||
| 35 | # STEP 1: verify that device_sso scope is authorized during authorization request | ||
| 36 | |||
| 37 | sub resolveScope { | ||
| 38 | my ( $self, $req, $scopeList, $rp ) = @_; | ||
| 39 | unless ( | ||
| 40 | $self->oidc->rpOptions->{$rp}->{oidcRPMetaDataOptionsAllowNativeSso} ) | ||
| 41 | { | ||
| 42 | my @scopes; | ||
| 43 | |||
| 44 | # To keep $scopeList reference, empty related array then populate it | ||
| 45 | while (@$scopeList) { | ||
| 46 | my $tmp = shift(@$scopeList); | ||
| 47 | if ( $tmp eq 'device_sso' ) { | ||
| 48 | $self->userLogger->error( | ||
| 49 | "Request scope device_sso for an unauthorized RP: $rp"); | ||
| 50 | } | ||
| 51 | else { | ||
| 52 | push @scopes, $tmp unless $tmp eq 'device_sso'; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | push @$scopeList, @scopes; | ||
| 56 | } | ||
| 57 | return PE_OK; | ||
| 58 | } | ||
| 59 | |||
| 60 | # STEP 2: generate device_secret | ||
| 61 | sub accessTokenHook { | ||
| 62 | my ( $self, $req, $payload, $rp ) = @_; | ||
| 63 | if ( $payload->{scope} =~ /\bdevice_sso\b/ ) { | ||
| 64 | $req->data->{oidcNativeSso} = { | ||
| 65 | scope => $payload->{scope}, | ||
| 66 | ds_hash => random_string( 's' x 32 ), | ||
| 67 | }; | ||
| 68 | } | ||
| 69 | return PE_OK; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub addDsHash { | ||
| 73 | my ( $self, $req, $payload, $rp ) = @_; | ||
| 74 | if ( $req->data->{oidcNativeSso} ) { | ||
| 75 | $payload->{ds_hash} = $req->data->{oidcNativeSso}->{ds_hash}; | ||
| 76 | } | ||
| 77 | return PE_OK; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub tokenResponse { | ||
| 81 | my ( $self, $req, $rp, $tokenResponse, $oidcSession, $sessionInfo ) = @_; | ||
| 82 | if ( $req->data->{oidcNativeSso} ) { | ||
| 83 | my %userInfo = ( ds_hash => $req->data->{oidcNativeSso}->{ds_hash} ); | ||
| 84 | for my $userKey ( grep !/^(_session|_utime$|_lastSeen$)/, | ||
| 85 | keys %$sessionInfo ) | ||
| 86 | { | ||
| 87 | $userInfo{$userKey} = $sessionInfo->{$userKey}; | ||
| 88 | } | ||
| 89 | my $deviceSecret = $self->oidc->getOpenIDConnectSession( | ||
| 90 | undef, | ||
| 91 | 'device_secret', | ||
| 92 | |||
| 93 | # Fix device_secret timeout to the same value than offline token | ||
| 94 | $self->oidc->rpOptions->{$rp}->{oidcRPMetaDataOptionsAllowOffline} | ||
| 95 | ? ( $self->oidc->rpOptions->{$rp} | ||
| 96 | ->{oidcRPMetaDataOptionsOfflineSessionExpiration} | ||
| 97 | || $self->conf->{oidcServiceOfflineSessionExpiration} ) | ||
| 98 | : $self->conf->{timeout}, | ||
| 99 | { | ||
| 100 | %userInfo, | ||
| 101 | redirect_uri => $oidcSession->{redirect_uri}, | ||
| 102 | scope => $req->data->{oidcNativeSso}->{scope}, | ||
| 103 | client_id => $self->conf->{oidcRPMetaDataOptions}->{$rp} | ||
| 104 | ->{oidcRPMetaDataOptionsClientID}, | ||
| 105 | _session_uid => $sessionInfo->{_user}, | ||
| 106 | auth_time => $sessionInfo->{_lastAuthnUTime}, | ||
| 107 | grant_type => "authorizationcode", | ||
| 108 | id_token_h => sha256_hex( $tokenResponse->{id_token} ), | ||
| 109 | }, | ||
| 110 | ); | ||
| 111 | $tokenResponse->{device_secret} = $deviceSecret->id; | ||
| 112 | } | ||
| 113 | return PE_OK; | ||
| 114 | } | ||
| 115 | |||
| 116 | use constant T_LOG_PREFIX => 'Native SSO token exchange: '; | ||
| 117 | |||
| 118 | # STEP 3: give new access_token when device_secret is valid | ||
| 119 | sub tokenExchangeHook { | ||
| 120 | my ( $self, $req, $rp ) = @_; | ||
| 121 | |||
| 122 | # Ignore request if token exchange isn't for this plugin | ||
| 123 | return PE_OK | ||
| 124 | unless $req->param('actor_token_type') eq | ||
| 125 | 'urn:x-oath:params:oauth:token-type:device-secret'; | ||
| 126 | |||
| 127 | if ( $self->conf->{oidcRPMetaDataOptions}->{$rp} | ||
| 128 | ->{oidcRPMetaDataOptionsAllowNativeSso} ) | ||
| 129 | { | ||
| 130 | # STEP 3.1: Check parameters | ||
| 131 | |||
| 132 | # Required parameter list | ||
| 133 | foreach ( | ||
| 134 | qw(audience subject_token subject_token_type actor_token actor_token_type) | ||
| 135 | ) | ||
| 136 | { | ||
| 137 | unless ( $req->param($_) ) { | ||
| 138 | $self->userLogger->error( T_LOG_PREFIX . "missing $_" ); | ||
| 139 | return PE_ERROR; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | # Check for static parameters | ||
| 144 | unless ( $req->param('subject_token_type') eq | ||
| 145 | 'urn:ietf:params:oauth:token-type:id_token' ) | ||
| 146 | { | ||
| 147 | $self->userLogger->error( | ||
| 148 | T_LOG_PREFIX . 'Bad subject_token_type or actor_token_type' ); | ||
| 149 | return PE_ERROR; | ||
| 150 | } | ||
| 151 | |||
| 152 | # Check for audience that should be the issuer | ||
| 153 | # (given by "iss" field of id_token) | ||
| 154 | |||
| 155 | unless ( $req->param('audience') eq $self->oidc->iss ) { | ||
| 156 | $self->userLogger->error( T_LOG_PREFIX | ||
| 157 | . 'missing or bad audience ' | ||
| 158 | . $req->param('audience') ); | ||
| 159 | return PE_ERROR; | ||
| 160 | } | ||
| 161 | |||
| 162 | # Check for device_secret | ||
| 163 | my $deviceSecret = $req->param('actor_token'); | ||
| 164 | my $session = | ||
| 165 | $self->oidc->getOpenIDConnectSession( $deviceSecret, | ||
| 166 | "device_secret" ); | ||
| 167 | unless ($session) { | ||
| 168 | $self->userLogger->error( | ||
| 169 | T_LOG_PREFIX . "bad device_secret $deviceSecret" ); | ||
| 170 | return PE_ERROR; | ||
| 171 | } | ||
| 172 | |||
| 173 | # Check for id_token | ||
| 174 | my $idTokenHint = $req->param('subject_token'); | ||
| 175 | unless ( sha256_hex($idTokenHint) eq $session->data->{id_token_h} ) { | ||
| 176 | $self->userLogger->error( | ||
| 177 | T_LOG_PREFIX . 'subject_token does not match actor_token' ); | ||
| 178 | return PE_ERROR; | ||
| 179 | } | ||
| 180 | |||
| 181 | # Check also for ds_hash | ||
| 182 | my $dsHash = getJWTPayload($idTokenHint)->{ds_hash}; | ||
| 183 | unless ( $dsHash eq $session->data->{ds_hash} ) { | ||
| 184 | $self->userLogger->error( T_LOG_PREFIX . 'ds_hash mismatch' ); | ||
| 185 | return PE_ERROR; | ||
| 186 | } | ||
| 187 | |||
| 188 | # STEP 3.2: generate new refresh_token and access_token | ||
| 189 | my ( $access_token, $refresh_token ); | ||
| 190 | |||
| 191 | $self->oidc->getAttributesForUser( $req, $session ) or return PE_ERROR; | ||
| 192 | |||
| 193 | if ( | ||
| 194 | $self->oidc->rpOptions->{$rp}->{oidcRPMetaDataOptionsAllowOffline} ) | ||
| 195 | { | ||
| 196 | my %refreshInfo = ( | ||
| 197 | map { | ||
| 198 | /^(?:_session_.*|_utime$|ds_hash|id_token_h|client_id|redirect_uri|_type)$/ | ||
| 199 | ? () | ||
| 200 | : ( $_ => $session->data->{$_} ) | ||
| 201 | } keys %{ $session->data } | ||
| 202 | ); | ||
| 203 | my $refreshTokenSession = $self->oidc->newRefreshToken( | ||
| 204 | $rp, | ||
| 205 | { | ||
| 206 | %refreshInfo, | ||
| 207 | |||
| 208 | # TODO: adapt scope if asked | ||
| 209 | #scope => | ||
| 210 | client_id => $self->oidc->rpOptions->{$rp} | ||
| 211 | ->{oidcRPMetaDataOptionsClientID}, | ||
| 212 | _session_uid => $session->data->{_session_uid}, | ||
| 213 | auth_time => $session->data->{_lastAuthnUTime}, | ||
| 214 | } | ||
| 215 | ); | ||
| 216 | $refresh_token = $refreshTokenSession->id; | ||
| 217 | } | ||
| 218 | |||
| 219 | $access_token = $self->oidc->newAccessToken( | ||
| 220 | $req, $rp, | ||
| 221 | $session->data->{scope}, | ||
| 222 | $session->data, | ||
| 223 | { | ||
| 224 | # TODO: offline_session_id must point to refresh_token | ||
| 225 | offline_session_id => $refresh_token, | ||
| 226 | grant_type => $session->data->{grant_type}, | ||
| 227 | } | ||
| 228 | ); | ||
| 229 | |||
| 230 | unless ($access_token) { | ||
| 231 | $self->userLogger->error("Unable to create Access Token"); | ||
| 232 | return PE_ERROR; | ||
| 233 | } | ||
| 234 | |||
| 235 | # Send response | ||
| 236 | |||
| 237 | my $response = { | ||
| 238 | issued_token_type => | ||
| 239 | 'urn:ietf:params:oauth:token-type:access_token', | ||
| 240 | token_type => 'Bearer', | ||
| 241 | access_token => $access_token, | ||
| 242 | expires_in => 'TODO', | ||
| 243 | ( $refresh_token ? ( refresh_token => $refresh_token ) : () ), | ||
| 244 | }; | ||
| 245 | |||
| 246 | $req->response( $self->p->sendJSONresponse( $req, $response ) ); | ||
| 247 | return PE_SENDRESPONSE; | ||
| 248 | } | ||
| 249 | else { | ||
| 250 | $self->userLogger->error( | ||
| 251 | "$rp isn't allowed to request for a device_secret"); | ||
| 252 | return PE_ERROR; | ||
| 253 | } | ||
| 254 | |||
| 255 | # Else ignore this request | ||
| 256 | return PE_OK; | ||
| 257 | } | ||
| 258 | |||
| 259 | package Lemonldap::NG::Portal::Plugins::OIDCNativeSso; | ||
| 260 | |||
| 261 | our @ISA = ('Lemonldap::NG::Portal::Plugins::OIDC::NativeSso'); | ||
| 262 | |||
| 263 | 1; |