« blib/lib/Lemonldap/NG/Portal/Issuer/OpenID.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/Password/Base.pm »
| 1 | package Lemonldap::NG::Portal::Auth::LinkedIn; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use JSON; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Mouse; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use MIME::Base64 qw/encode_base64 decode_base64/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Lemonldap::NG::Common::FormEncode; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Lemonldap::NG::Common::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use Lemonldap::NG::Portal::Main::Constants qw(PE_OK PE_ERROR PE_REDIRECT); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | our $VERSION = '2.19.0'; | |||||||||
| 12 | |||||||||||
| 13 | 1 | extends 'Lemonldap::NG::Portal::Main::Auth'; | |||||||||
| 14 | |||||||||||
| 15 | # INITIALIZATION | ||||||||||
| 16 | |||||||||||
| 17 | # return LWP::UserAgent object | ||||||||||
| 18 | has ua => ( | ||||||||||
| 19 | is => 'rw', | ||||||||||
| 20 | lazy => 1, | ||||||||||
| 21 | builder => sub { | ||||||||||
| 22 | |||||||||||
| 23 | # TODO : LWP options to use a proxy for example | ||||||||||
| 24 | 0 | ▶ | my $ua = Lemonldap::NG::Common::UserAgent->new( $_[0]->{conf} ); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 25 | 0 | $ua->env_proxy(); | |||||||||
| 26 | 0 | return $ua; | |||||||||
| 27 | } | ||||||||||
| 28 | 1 | ); | |||||||||
| 29 | |||||||||||
| 30 | has linkedInAuthorizationEndpoint => ( | ||||||||||
| 31 | is => 'ro', | ||||||||||
| 32 | lazy => 1, | ||||||||||
| 33 | default => sub { | ||||||||||
| 34 | $_[0]->conf->{linkedInAuthorizationEndpoint} | ||||||||||
| 35 | 0 | ▶ | || 'https://www.linkedin.com/oauth/v2/authorization'; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||
| 36 | } | ||||||||||
| 37 | 1 | ); | |||||||||
| 38 | |||||||||||
| 39 | has linkedInTokenEndpoint => ( | ||||||||||
| 40 | is => 'ro', | ||||||||||
| 41 | lazy => 1, | ||||||||||
| 42 | default => sub { | ||||||||||
| 43 | $_[0]->conf->{linkedInTokenEndpoint} | ||||||||||
| 44 | 0 | ▶ | || 'https://www.linkedin.com/oauth/v2/accessToken'; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||
| 45 | } | ||||||||||
| 46 | 1 | ); | |||||||||
| 47 | |||||||||||
| 48 | has linkedInPeopleEndpoint => ( | ||||||||||
| 49 | is => 'ro', | ||||||||||
| 50 | lazy => 1, | ||||||||||
| 51 | default => sub { | ||||||||||
| 52 | $_[0]->conf->{linkedInPeopleEndpoint} | ||||||||||
| 53 | 0 | ▶ | || 'https://api.linkedin.com/v2/me'; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||
| 54 | } | ||||||||||
| 55 | 1 | ); | |||||||||
| 56 | |||||||||||
| 57 | has linkedInEmailEndpoint => ( | ||||||||||
| 58 | is => 'ro', | ||||||||||
| 59 | lazy => 1, | ||||||||||
| 60 | default => sub { | ||||||||||
| 61 | $_[0]->conf->{linkedInEmailEndpoint} | ||||||||||
| 62 | 0 | ▶ | || 'https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))'; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||
| 63 | } | ||||||||||
| 64 | 1 | ); | |||||||||
| 65 | |||||||||||
| 66 | sub init { | ||||||||||
| 67 | 0 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
init: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 68 | |||||||||||
| 69 | 0 | my $ret = 1; | |||||||||
| 70 | 0 | foreach my $arg (qw(linkedInClientID linkedInClientSecret)) { | |||||||||
| 71 | 0 | ▶ | unless ( $self->conf->{$arg} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 0 | $ret = 0; | |||||||||
| 73 | 0 | $self->error("Parameter $arg is required"); | |||||||||
| 74 | } | ||||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 0 | return $ret; | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | # RUNNING METHODS | ||||||||||
| 81 | |||||||||||
| 82 | sub extractFormInfo { | ||||||||||
| 83 | 0 | ▶ | my ( $self, $req ) = @_; | ||||||||
|
Subroutine
extractFormInfo: not called CC=12 SCAR=50.5
Pod
undocumented
| |||||||||||
| 84 | 0 | my $nonce = time; | |||||||||
| 85 | |||||||||||
| 86 | # Build redirect_uri | ||||||||||
| 87 | 0 | my $callback_url = $req->portal; | |||||||||
| 88 | |||||||||||
| 89 | # Check return values | ||||||||||
| 90 | 0 | my $error = $req->param("error"); | |||||||||
| 91 | 0 | my $code = $req->param("code"); | |||||||||
| 92 | 0 | my $state = $req->param("state"); | |||||||||
| 93 | |||||||||||
| 94 | # Error | ||||||||||
| 95 | 0 | ▶ | if ($error) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 0 | $self->logger->error( | |||||||||
| 97 | "Error $error with LinkedIn: " . $req->param("error_description") ); | ||||||||||
| 98 | 0 | return PE_ERROR; | |||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | # Code | ||||||||||
| 102 | 0 | ▶ | if ($code) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 0 | my %form; | |||||||||
| 104 | 0 | $form{"code"} = $code; | |||||||||
| 105 | 0 | $form{"client_id"} = $self->conf->{linkedInClientID}; | |||||||||
| 106 | 0 | $form{"client_secret"} = $self->conf->{linkedInClientSecret}; | |||||||||
| 107 | 0 | $form{"redirect_uri"} = $callback_url; | |||||||||
| 108 | 0 | $form{"grant_type"} = "authorization_code"; | |||||||||
| 109 | |||||||||||
| 110 | 0 | my $response = $self->ua->post( $self->linkedInTokenEndpoint, | |||||||||
| 111 | \%form, "Content-Type" => 'application/x-www-form-urlencoded' ); | ||||||||||
| 112 | |||||||||||
| 113 | 0 | ▶ | if ( $response->is_error ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | 0 | $self->logger->error( | |||||||||
| 115 | "Bad authorization response: " . $response->message ); | ||||||||||
| 116 | 0 | $self->logger->debug( $response->content ); | |||||||||
| 117 | 0 | return PE_ERROR; | |||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | 0 | my $content = $response->decoded_content; | |||||||||
| 121 | |||||||||||
| 122 | 0 | my $json_hash; | |||||||||
| 123 | |||||||||||
| 124 | 0 | eval { $json_hash = from_json( $content, { allow_nonref => 1 } ); }; | |||||||||
| 125 | |||||||||||
| 126 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | 0 | $self->logger->error("Unable to decode JSON $content"); | |||||||||
| 128 | 0 | return PE_ERROR; | |||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | 0 | my $access_token = $json_hash->{access_token}; | |||||||||
| 132 | |||||||||||
| 133 | 0 | $self->logger->debug("Get access token $access_token from LinkedIn"); | |||||||||
| 134 | |||||||||||
| 135 | # Call People EndPoint URI | ||||||||||
| 136 | 0 | $self->logger->debug( | |||||||||
| 137 | "Call LinkedIn People Endpoint " . $self->linkedInPeopleEndpoint ); | ||||||||||
| 138 | |||||||||||
| 139 | 0 | my $people_response = $self->ua->get( $self->linkedInPeopleEndpoint, | |||||||||
| 140 | "Authorization" => "Bearer $access_token" ); | ||||||||||
| 141 | |||||||||||
| 142 | 0 | ▶ | if ( $people_response->is_error ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 143 | 0 | $self->logger->error( | |||||||||
| 144 | "Bad authorization response: " . $people_response->message ); | ||||||||||
| 145 | 0 | $self->logger->debug( $people_response->content ); | |||||||||
| 146 | 0 | return PE_ERROR; | |||||||||
| 147 | } | ||||||||||
| 148 | |||||||||||
| 149 | 0 | my $people_content = $people_response->decoded_content; | |||||||||
| 150 | |||||||||||
| 151 | 0 | $self->logger->debug( | |||||||||
| 152 | "Response from LinkedIn People API: $people_content"); | ||||||||||
| 153 | |||||||||||
| 154 | 0 | eval { | |||||||||
| 155 | 0 | $json_hash = from_json( $people_content, { allow_nonref => 1 } ); | |||||||||
| 156 | }; | ||||||||||
| 157 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 158 | 0 | $self->logger->error("Unable to decode JSON $people_content"); | |||||||||
| 159 | 0 | return PE_ERROR; | |||||||||
| 160 | } | ||||||||||
| 161 | |||||||||||
| 162 | 0 | foreach ( keys %$json_hash ) { | |||||||||
| 163 | 0 | $req->data->{linkedInData}->{$_} = $json_hash->{$_}; | |||||||||
| 164 | } | ||||||||||
| 165 | |||||||||||
| 166 | # Call Email EndPoint URI | ||||||||||
| 167 | 0 | ▶ | if ( $self->conf->{linkedInScope} =~ /r_emailaddress/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 168 | |||||||||||
| 169 | 0 | $self->logger->debug( "Call LinkedIn Email Endpoint " | |||||||||
| 170 | . $self->linkedInEmailEndpoint ); | ||||||||||
| 171 | |||||||||||
| 172 | 0 | my $email_response = $self->ua->get( $self->linkedInEmailEndpoint, | |||||||||
| 173 | "Authorization" => "Bearer $access_token" ); | ||||||||||
| 174 | |||||||||||
| 175 | 0 | ▶ | if ( $email_response->is_error ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 176 | 0 | $self->logger->error( | |||||||||
| 177 | "Bad authorization response: " . $email_response->message ); | ||||||||||
| 178 | 0 | $self->logger->debug( $email_response->content ); | |||||||||
| 179 | 0 | return PE_ERROR; | |||||||||
| 180 | } | ||||||||||
| 181 | |||||||||||
| 182 | 0 | my $email_content = $email_response->decoded_content; | |||||||||
| 183 | |||||||||||
| 184 | 0 | $self->logger->debug( | |||||||||
| 185 | "Response from LinkedIn Email API: $email_content"); | ||||||||||
| 186 | |||||||||||
| 187 | 0 | eval { | |||||||||
| 188 | 0 | $json_hash = from_json( $email_content, { allow_nonref => 1 } ); | |||||||||
| 189 | }; | ||||||||||
| 190 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 191 | 0 | $self->logger->error("Unable to decode JSON $email_content"); | |||||||||
| 192 | 0 | return PE_ERROR; | |||||||||
| 193 | } | ||||||||||
| 194 | |||||||||||
| 195 | $req->data->{linkedInData}->{"emailAddress"} = | ||||||||||
| 196 | 0 | $json_hash->{"elements"}->[0]{"handle~"}->{"emailAddress"}; | |||||||||
| 197 | } | ||||||||||
| 198 | |||||||||||
| 199 | $req->user( | ||||||||||
| 200 | 0 | $req->data->{linkedInData}->{ $self->conf->{linkedInUserField} } ); | |||||||||
| 201 | |||||||||||
| 202 | 0 | $self->logger->debug( | |||||||||
| 203 | "Good LinkedIn authentication for " . $req->user ); | ||||||||||
| 204 | |||||||||||
| 205 | # Extract state | ||||||||||
| 206 | 0 | ▶ | if ($state) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 207 | 0 | my $stateSession = $self->p->getApacheSession( $state, 1 ); | |||||||||
| 208 | |||||||||||
| 209 | 0 | $req->urldc( $stateSession->data->{urldc} ); | |||||||||
| 210 | 0 | $req->{checkLogins} = $stateSession->data->{checkLogins}; | |||||||||
| 211 | |||||||||||
| 212 | 0 | $stateSession->remove; | |||||||||
| 213 | } | ||||||||||
| 214 | |||||||||||
| 215 | 0 | return PE_OK; | |||||||||
| 216 | } | ||||||||||
| 217 | |||||||||||
| 218 | # No code, redirect to LinkedIn | ||||||||||
| 219 | else { | ||||||||||
| 220 | 0 | $self->logger->debug('Redirection to LinkedIn'); | |||||||||
| 221 | |||||||||||
| 222 | # Store state | ||||||||||
| 223 | 0 | my $stateSession = | |||||||||
| 224 | $self->p->getApacheSession( undef, 1, 0, 'LinkedInState' ); | ||||||||||
| 225 | |||||||||||
| 226 | 0 | my $stateInfos = {}; | |||||||||
| 227 | 0 | $stateInfos->{_utime} = time() + $self->conf->{timeout}; | |||||||||
| 228 | 0 | $stateInfos->{urldc} = $req->urldc; | |||||||||
| 229 | 0 | $stateInfos->{checkLogins} = $req->{checkLogins}; | |||||||||
| 230 | |||||||||||
| 231 | 0 | $stateSession->update($stateInfos); | |||||||||
| 232 | |||||||||||
| 233 | 0 | my $authn_uri = $self->linkedInAuthorizationEndpoint; | |||||||||
| 234 | 0 | my $client_id = $self->conf->{linkedInClientID}; | |||||||||
| 235 | 0 | my $scope = $self->conf->{linkedInScope}; | |||||||||
| 236 | 0 | $authn_uri .= '?' | |||||||||
| 237 | . build_urlencoded( | ||||||||||
| 238 | response_type => 'code', | ||||||||||
| 239 | client_id => $client_id, | ||||||||||
| 240 | redirect_uri => $callback_url, | ||||||||||
| 241 | scope => $scope, | ||||||||||
| 242 | state => $stateSession->id, | ||||||||||
| 243 | ); | ||||||||||
| 244 | |||||||||||
| 245 | 0 | $req->urldc($authn_uri); | |||||||||
| 246 | |||||||||||
| 247 | 0 | $self->logger->debug( "Redirect user to " . $req->urldc ); | |||||||||
| 248 | |||||||||||
| 249 | 0 | return PE_REDIRECT; | |||||||||
| 250 | } | ||||||||||
| 251 | } | ||||||||||
| 252 | |||||||||||
| 253 | sub setAuthSessionInfo { | ||||||||||
| 254 | 0 | ▶ | my ( $self, $req ) = @_; | ||||||||
|
Subroutine
setAuthSessionInfo: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 255 | |||||||||||
| 256 | $req->{sessionInfo}->{authenticationLevel} = | ||||||||||
| 257 | 0 | $self->conf->{linkedInAuthnLevel}; | |||||||||
| 258 | |||||||||||
| 259 | 0 | foreach ( keys %{ $req->data->{linkedInData} } ) { | |||||||||
| 260 | $req->{sessionInfo}->{ 'linkedIn_' . $_ } = | ||||||||||
| 261 | 0 | $req->data->{linkedInData}->{$_}; | |||||||||
| 262 | } | ||||||||||
| 263 | |||||||||||
| 264 | 0 | return PE_OK; | |||||||||
| 265 | } | ||||||||||
| 266 | |||||||||||
| 267 | sub authenticate { | ||||||||||
| 268 | 0 | ▶ | return PE_OK; | ||||||||
|
Subroutine
authenticate: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 269 | } | ||||||||||
| 270 | |||||||||||
| 271 | sub authFinish { | ||||||||||
| 272 | 0 | ▶ | return PE_OK; | ||||||||
|
Subroutine
authFinish: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 273 | } | ||||||||||
| 274 | |||||||||||
| 275 | sub authLogout { | ||||||||||
| 276 | 0 | ▶ | return PE_OK; | ||||||||
|
Subroutine
authLogout: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 277 | } | ||||||||||
| 278 | |||||||||||
| 279 | sub authForce { | ||||||||||
| 280 | 0 | ▶ | return 0; | ||||||||
|
Subroutine
authForce: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 281 | } | ||||||||||
| 282 | |||||||||||
| 283 | sub getDisplayType { | ||||||||||
| 284 | 0 | ▶ | return "logo"; | ||||||||
|
Subroutine
getDisplayType: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 285 | } | ||||||||||
| 286 | |||||||||||
| 287 | 1 | 1; | |||||||||