blib/lib/Lemonldap/NG/Portal/Auth/LinkedIn.pm

filter: Statementstmt 24.4% 35 / 143 Branchbran 2.7% 1 / 36 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 38.1% 8 / 21 Podpod 0.0% 0 / 8 totaltotal 20.4% 44 / 215 CC 24 SCAR 47.9
CC
24
Cov
20%
extractFormInfo
306.0
__ANON__
6.0
__ANON__
6.0
CRAP 120.3
« blib/lib/Lemonldap/NG/Portal/Plugins/Impersonation.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Auth/LDAP.pm »
1 package Lemonldap::NG::Portal::Auth::LinkedIn;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use MIME::Base64 qw/encode_base64 decode_base64/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Lemonldap::NG::Common::FormEncode;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Lemonldap::NG::Common::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Lemonldap::NG::Portal::Main::Constants qw(PE_OK PE_ERROR PE_REDIRECT);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = '2.23.3';
12  
13 2 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 0 ▶ builder => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
22  
23 # TODO : LWP options to use a proxy for example
24 0 my $ua = Lemonldap::NG::Common::UserAgent->new( $_[0]->{conf} );
25 0 $ua->env_proxy();
26 0 return $ua;
27 }
28 2 );
29  
30 # Used to protect the authorization request against CSRF and to restore the
31 # initial request across the LinkedIn round trip
32 has state_ott => (
33 is => 'rw',
34 lazy => 1,
35 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
36 0 my $ott = $_[0]->{p}->loadModule('::Lib::OneTimeToken');
37 0 $ott->timeout( $_[0]->conf->{timeout} );
38  
39 # LinkedIn sends the user back to any portal of the farm, so the state
40 # must not be stored in the local cache, whatever tokenUseGlobalStorage
41 # says
42 0 $ott->cache(undef);
43 0 return $ott;
44 }
45 2 );
46  
47 has linkedInAuthorizationEndpoint => (
48 is => 'ro',
49 lazy => 1,
50 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
51 $_[0]->conf->{linkedInAuthorizationEndpoint}
52 0 ▶ || 'https://www.linkedin.com/oauth/v2/authorization';
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->conf->{"linkedInAuthorizationEndpoint"} or "https://www.linkedin.com/oauth/v2/authorization" 0 0 0
53 }
54 2 );
55  
56 has linkedInTokenEndpoint => (
57 is => 'ro',
58 lazy => 1,
59 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
60 $_[0]->conf->{linkedInTokenEndpoint}
61 0 ▶ || 'https://www.linkedin.com/oauth/v2/accessToken';
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->conf->{"linkedInTokenEndpoint"} or "https://www.linkedin.com/oauth/v2/accessToken" 0 0 0
62 }
63 2 );
64  
65 has linkedInPeopleEndpoint => (
66 is => 'ro',
67 lazy => 1,
68 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
69 $_[0]->conf->{linkedInPeopleEndpoint}
70 0 ▶ || 'https://api.linkedin.com/v2/me';
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->conf->{"linkedInPeopleEndpoint"} or "https://api.linkedin.com/v2/me" 0 0 0
71 }
72 2 );
73  
74 has linkedInEmailEndpoint => (
75 is => 'ro',
76 lazy => 1,
77 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
78 $_[0]->conf->{linkedInEmailEndpoint}
79 0 ▶ || 'https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))';
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->conf->{"linkedInEmailEndpoint"} or "https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))" 0 0 0
80 }
81 2 );
82  
83 1 ▶sub init {
Subroutine init: called CC=3 SCAR=12.0
Pod undocumented
84 1 my ($self) = @_;
85  
86 1 my $ret = 1;
87 1 foreach my $arg (qw(linkedInClientID linkedInClientSecret)) {
88 2 ▶ unless ( $self->conf->{$arg} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->conf->{$arg}) 0 2 2
89 0 $ret = 0;
90 0 $self->error("Parameter $arg is required");
91 }
92 }
93  
94 1 return $ret;
95 }
96  
97 # RUNNING METHODS
98  
99 0 ▶sub extractFormInfo {
Subroutine extractFormInfo: not called CC=17 SCAR=57.2
Pod undocumented
100 0 my ( $self, $req ) = @_;
101 0 my $nonce = time;
102  
103 # Build redirect_uri
104 0 my $callback_url = $req->portal;
105  
106 # Check return values
107 0 my $error = $req->param("error");
108 0 my $code = $req->param("code");
109 0 my $state = $req->param("state");
110  
111 # Error
112 0 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
113 0 $self->logger->error(
114 "Error $error with LinkedIn: " . $req->param("error_description") );
115 0 return PE_ERROR;
116 }
117  
118 # Code
119 0 ▶ if ($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) { } 0 0 0
120  
121 # Restore initial request. This also protects against login CSRF: the
122 # state token can only have been created by this portal
123 0 ▶ unless ($state) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state) 0 0 0
124 0 $self->userLogger->error(
125 'LinkedIn response without state parameter');
126 0 return PE_ERROR;
127 }
128 0 my $stateData = $self->state_ott->getToken($state);
129 0 ▶ unless ( $stateData
Branch: 1 branch
BranchTrueFalseTotal
unless ($stateData and ($stateData->{"_type"} // "") eq "linkedInState") 0 0 0
Condition0%0/5
$stateData and ($stateData->{"_type"} // "") eq "linkedInState"
!ll&&!rl&&r
000
$stateData->{"_type"} // ""
l!l
00
MC/DC0%0/2
$stateData and ($stateData->{"_type"} // "") eq "linkedInState"
$stateData $stateData->{"_type"}
Truth table0%0/3
A and B
A= $stateDataB= $stateData->{"_type"}
ABresult
0X0
100
111
130 and ( $stateData->{_type} // '' ) eq 'linkedInState' )
131 {
132 0 $self->userLogger->error('Invalid or expired LinkedIn state');
133 0 return PE_ERROR;
134 }
135 0 ▶ $req->urldc( $stateData->{urldc} ) if $stateData->{urldc};
Branch: 1 branch
BranchTrueFalseTotal
if $stateData->{"urldc"} 0 0 0
136 0 $req->{checkLogins} = $stateData->{checkLogins};
137  
138 0 my %form;
139 0 $form{"code"} = $code;
140 0 $form{"client_id"} = $self->conf->{linkedInClientID};
141 0 $form{"client_secret"} = $self->conf->{linkedInClientSecret};
142 0 $form{"redirect_uri"} = $callback_url;
143 0 $form{"grant_type"} = "authorization_code";
144  
145 0 my $response = $self->ua->post( $self->linkedInTokenEndpoint,
146 \%form, "Content-Type" => 'application/x-www-form-urlencoded' );
147  
148 0 ▶ if ( $response->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_error) 0 0 0
149 0 $self->logger->error(
150 "Bad authorization response: " . $response->message );
151 0 $self->logger->debug( $response->content );
152 0 return PE_ERROR;
153 }
154  
155 0 my $content = $response->decoded_content;
156  
157 0 my $json_hash;
158  
159 0 eval { $json_hash = from_json( $content, { allow_nonref => 1 } ); };
160  
161 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
162 0 $self->logger->error("Unable to decode JSON $content");
163 0 return PE_ERROR;
164 }
165  
166 0 my $access_token = $json_hash->{access_token};
167  
168 0 $self->logger->debug("Get access token $access_token from LinkedIn");
169  
170 # Call People EndPoint URI
171 0 $self->logger->debug(
172 "Call LinkedIn People Endpoint " . $self->linkedInPeopleEndpoint );
173  
174 0 my $people_response = $self->ua->get( $self->linkedInPeopleEndpoint,
175 "Authorization" => "Bearer $access_token" );
176  
177 0 ▶ if ( $people_response->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($people_response->is_error) 0 0 0
178 0 $self->logger->error(
179 "Bad authorization response: " . $people_response->message );
180 0 $self->logger->debug( $people_response->content );
181 0 return PE_ERROR;
182 }
183  
184 0 my $people_content = $people_response->decoded_content;
185  
186 0 $self->logger->debug(
187 "Response from LinkedIn People API: $people_content");
188  
189 0 eval {
190 0 $json_hash = from_json( $people_content, { allow_nonref => 1 } );
191 };
192 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
193 0 $self->logger->error("Unable to decode JSON $people_content");
194 0 return PE_ERROR;
195 }
196  
197 0 foreach ( keys %$json_hash ) {
198 0 $req->data->{linkedInData}->{$_} = $json_hash->{$_};
199 }
200  
201 # Call Email EndPoint URI
202 0 ▶ if ( $self->conf->{linkedInScope} =~ /r_emailaddress/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"linkedInScope"} =~ /r_emailaddress/) 0 0 0
203  
204 0 $self->logger->debug( "Call LinkedIn Email Endpoint "
205 . $self->linkedInEmailEndpoint );
206  
207 0 my $email_response = $self->ua->get( $self->linkedInEmailEndpoint,
208 "Authorization" => "Bearer $access_token" );
209  
210 0 ▶ if ( $email_response->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($email_response->is_error) 0 0 0
211 0 $self->logger->error(
212 "Bad authorization response: " . $email_response->message );
213 0 $self->logger->debug( $email_response->content );
214 0 return PE_ERROR;
215 }
216  
217 0 my $email_content = $email_response->decoded_content;
218  
219 0 $self->logger->debug(
220 "Response from LinkedIn Email API: $email_content");
221  
222 0 eval {
223 0 $json_hash = from_json( $email_content, { allow_nonref => 1 } );
224 };
225 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
226 0 $self->logger->error("Unable to decode JSON $email_content");
227 0 return PE_ERROR;
228 }
229  
230 $req->data->{linkedInData}->{"emailAddress"} =
231 0 $json_hash->{"elements"}->[0]{"handle~"}->{"emailAddress"};
232 }
233  
234 $req->user(
235 0 $req->data->{linkedInData}->{ $self->conf->{linkedInUserField} } );
236  
237 0 $self->logger->debug(
238 "Good LinkedIn authentication for " . $req->user );
239  
240 0 return PE_OK;
241 }
242  
243 # No code, redirect to LinkedIn
244 else {
245 0 $self->logger->debug('Redirection to LinkedIn');
246  
247 # Store state
248 my $stateToken = $self->state_ott->createToken(
249 {
250 _type => 'linkedInState',
251 urldc => $req->urldc,
252 checkLogins => $req->{checkLogins},
253 }
254 0 );
255 0 ▶ unless ($stateToken) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stateToken) 0 0 0
256 0 $self->logger->error('Unable to create LinkedIn state token');
257 0 return PE_ERROR;
258 }
259  
260 0 my $authn_uri = $self->linkedInAuthorizationEndpoint;
261 0 my $client_id = $self->conf->{linkedInClientID};
262 0 my $scope = $self->conf->{linkedInScope};
263 0 $authn_uri .= '?'
264 . build_urlencoded(
265 response_type => 'code',
266 client_id => $client_id,
267 redirect_uri => $callback_url,
268 scope => $scope,
269 state => $stateToken,
270 );
271  
272 0 $req->urldc($authn_uri);
273  
274 0 $self->logger->debug( "Redirect user to " . $req->urldc );
275  
276 0 return PE_REDIRECT;
277 }
278 }
279  
280 0 ▶sub setAuthSessionInfo {
Subroutine setAuthSessionInfo: not called CC=2 SCAR=17.9
Pod undocumented
281 0 my ( $self, $req ) = @_;
282  
283 $req->{sessionInfo}->{authenticationLevel} =
284 0 $self->conf->{linkedInAuthnLevel};
285  
286 0 foreach ( keys %{ $req->data->{linkedInData} } ) {
287 $req->{sessionInfo}->{ 'linkedIn_' . $_ } =
288 0 $req->data->{linkedInData}->{$_};
289 }
290  
291 0 return PE_OK;
292 }
293  
294 0 ▶sub authenticate {
Subroutine authenticate: not called CC=1 SCAR=6.9
Pod undocumented
295 0 return PE_OK;
296 }
297  
298 0 ▶sub authFinish {
Subroutine authFinish: not called CC=1 SCAR=6.9
Pod undocumented
299 0 return PE_OK;
300 }
301  
302 0 ▶sub authLogout {
Subroutine authLogout: not called CC=1 SCAR=6.9
Pod undocumented
303 0 return PE_OK;
304 }
305  
306 0 ▶sub authForce {
Subroutine authForce: not called CC=1 SCAR=6.9
Pod undocumented
307 0 return 0;
308 }
309  
310 0 ▶sub getDisplayType {
Subroutine getDisplayType: not called CC=1 SCAR=6.9
Pod undocumented
311 0 return "logo";
312 }
313  
314 2 1;
« blib/lib/Lemonldap/NG/Portal/Plugins/Impersonation.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Auth/LDAP.pm »