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

filter: Statementstmt 23.0% 36 / 156 Branchbran 2.2% 1 / 44 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 36.3% 8 / 22 Podpod 0.0% 0 / 8 totaltotal 18.9% 45 / 237 CC 28 SCAR 51.6
CC
28
Cov
18%
extractFormInfo
420.0
__ANON__
6.0
__ANON__
6.0
CRAP 173.5
« blib/lib/Lemonldap/NG/Portal/UserDB/SAML.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Lib/OpenID/Server.pm »
1 package Lemonldap::NG::Portal::Auth::GitHub;
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 GitHub 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 # GitHub 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 githubAuthorizationEndpoint => (
48 is => 'ro',
49 lazy => 1,
50 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
51 $_[0]->conf->{githubAuthorizationEndpoint}
52 0 ▶ || 'https://github.com/login/oauth/authorize';
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->conf->{"githubAuthorizationEndpoint"} or "https://github.com/login/oauth/authorize" 0 0 0
53 }
54 2 );
55  
56 has githubTokenEndpoint => (
57 is => 'ro',
58 lazy => 1,
59 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
60 $_[0]->conf->{githubTokenEndpoint}
61 0 ▶ || 'https://github.com/login/oauth/access_token';
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->conf->{"githubTokenEndpoint"} or "https://github.com/login/oauth/access_token" 0 0 0
62 }
63 2 );
64  
65 has githubUserEndpoint => (
66 is => 'ro',
67 lazy => 1,
68 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
69 $_[0]->conf->{githubUserEndpoint}
70 0 ▶ || 'https://api.github.com/user';
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->conf->{"githubUserEndpoint"} or "https://api.github.com/user" 0 0 0
71 }
72 2 );
73  
74 has githubPublicKeysEndpoint => (
75 is => 'ro',
76 lazy => 1,
77 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
78 $_[0]->conf->{githubPublicKeysEndpoint}
79 0 ▶ || 'https://api.github.com/user/keys';
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->conf->{"githubPublicKeysEndpoint"} or "https://api.github.com/user/keys" 0 0 0
80 }
81 2 );
82  
83 has githubGPGKeysEndpoint => (
84 is => 'ro',
85 lazy => 1,
86 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
87 $_[0]->conf->{githubGPGKeysEndpoint}
88 0 ▶ || 'https://api.github.com/user/gpg_keys';
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->conf->{"githubGPGKeysEndpoint"} or "https://api.github.com/user/gpg_keys" 0 0 0
89 }
90 2 );
91  
92 1 ▶sub init {
Subroutine init: called CC=3 SCAR=12.0
Pod undocumented
93 1 my ($self) = @_;
94  
95 1 my $ret = 1;
96 1 foreach my $arg (qw(githubClientID githubClientSecret)) {
97 2 ▶ unless ( $self->conf->{$arg} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->conf->{$arg}) 0 2 2
98 0 $ret = 0;
99 0 $self->error("Parameter $arg is required");
100 }
101 }
102  
103 1 return $ret;
104 }
105  
106 # RUNNING METHODS
107  
108 0 ▶sub extractFormInfo {
Subroutine extractFormInfo: not called CC=20 SCAR=60.4
Pod undocumented
109 0 my ( $self, $req ) = @_;
110 0 my $nonce = time;
111  
112 # Build redirect_uri
113 0 my $callback_url = $req->portal;
114  
115 # Check return values
116 0 my $code = $req->param("code");
117 0 my $state = $req->param("state");
118  
119 # Code
120 0 ▶ if ($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) { } 0 0 0
121  
122 # Restore initial request. This also protects against login CSRF: the
123 # state token can only have been created by this portal
124 0 ▶ unless ($state) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state) 0 0 0
125 0 $self->userLogger->error('GitHub response without state parameter');
126 0 return PE_ERROR;
127 }
128 0 my $stateData = $self->state_ott->getToken($state);
129 0 ▶ unless ( $stateData and ( $stateData->{_type} // '' ) eq 'githubState' )
Branch: 1 branch
BranchTrueFalseTotal
unless ($stateData and ($stateData->{"_type"} // "") eq "githubState") 0 0 0
Condition0%0/5
$stateData and ($stateData->{"_type"} // "") eq "githubState"
!ll&&!rl&&r
000
$stateData->{"_type"} // ""
l!l
00
MC/DC0%0/2
$stateData and ($stateData->{"_type"} // "") eq "githubState"
$stateData $stateData->{"_type"}
Truth table0%0/3
A and B
A= $stateDataB= $stateData->{"_type"}
ABresult
0X0
100
111
130 {
131 0 $self->userLogger->error('Invalid or expired GitHub state');
132 0 return PE_ERROR;
133 }
134 0 ▶ $req->urldc( $stateData->{urldc} ) if $stateData->{urldc};
Branch: 1 branch
BranchTrueFalseTotal
if $stateData->{"urldc"} 0 0 0
135 0 $req->{checkLogins} = $stateData->{checkLogins};
136  
137 0 my %form;
138 0 $form{"code"} = $code;
139 0 ▶ $form{"state"} = $state if $state;
Branch: 1 branch
BranchTrueFalseTotal
if $state 0 0 0
140 0 $form{"client_id"} = $self->conf->{githubClientID};
141 0 $form{"client_secret"} = $self->conf->{githubClientSecret};
142 0 $form{"redirect_uri"} = $callback_url;
143  
144 0 my $response = $self->ua->post(
145 $self->githubTokenEndpoint,
146 \%form,
147 "Content-Type" => 'application/x-www-form-urlencoded',
148 'Accept' => 'application/json'
149 );
150  
151 0 ▶ if ( $response->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_error) 0 0 0
152 0 $self->logger->error(
153 "Bad authorization response: " . $response->message );
154 0 $self->logger->debug( $response->content );
155 0 return PE_ERROR;
156 }
157  
158 0 my $content = $response->decoded_content;
159  
160 0 my $json_hash;
161  
162 0 eval { $json_hash = from_json( $content, { allow_nonref => 1 } ); };
163  
164 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
165 0 $self->logger->error("Unable to decode JSON $content");
166 0 return PE_ERROR;
167 }
168  
169 0 my $access_token = $json_hash->{access_token};
170  
171 0 $self->logger->debug("Get access token $access_token from GitHub");
172  
173 # Call User EndPoint URI
174 0 $self->logger->debug(
175 "Call GitHub User Endpoint " . $self->githubUserEndpoint );
176  
177 0 my $user_response = $self->ua->get( $self->githubUserEndpoint,
178 "Authorization" => "token $access_token" );
179  
180 0 ▶ if ( $user_response->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user_response->is_error) 0 0 0
181 0 $self->logger->error(
182 "Bad authorization response: " . $user_response->message );
183 0 $self->logger->debug( $user_response->content );
184 0 return PE_ERROR;
185 }
186  
187 0 my $user_content = $user_response->decoded_content;
188  
189 0 $self->logger->debug("Response from GitHub User API: $user_content");
190  
191 0 eval { $json_hash = from_json( $user_content, { allow_nonref => 1 } ); };
192 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
193 0 $self->logger->error("Unable to decode JSON $user_content");
194 0 return PE_ERROR;
195 }
196  
197 0 foreach ( keys %$json_hash ) {
198 0 $req->data->{githubData}->{$_} = $json_hash->{$_};
199 }
200  
201 # Fetch SSH keys
202 0 ▶ if ( $self->conf->{githubScope} =~ /public_key/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"githubScope"} =~ /public_key/) 0 0 0
203 0 $self->logger->debug("Scope public_key requested, fetch SSH keys");
204  
205 0 my $public_keys_response = $self->ua->get(
206 $self->githubPublicKeysEndpoint,
207 "Authorization" => "token $access_token"
208 );
209  
210 0 ▶ if ( $public_keys_response->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($public_keys_response->is_error) 0 0 0
211 0 $self->logger->error( "Bad authorization response: "
212 . $public_keys_response->message );
213 0 $self->logger->debug( $public_keys_response->content );
214 0 return PE_ERROR;
215 }
216  
217 0 my $public_keys_content = $public_keys_response->decoded_content;
218  
219 0 $self->logger->debug(
220 "Response from GitHub Keys API: $public_keys_content");
221  
222 0 eval {
223 0 $json_hash =
224 from_json( $public_keys_content, { allow_nonref => 1 } );
225 };
226 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
227 0 $self->logger->error(
228 "Unable to decode JSON $public_keys_content");
229 0 return PE_ERROR;
230 }
231  
232 0 $req->data->{githubData}->{"public_keys"} = $json_hash;
233 }
234  
235 # Fetch GPG keys
236 0 ▶ if ( $self->conf->{githubScope} =~ /gpg_key/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"githubScope"} =~ /gpg_key/) 0 0 0
237 0 $self->logger->debug("Scope gpg_key requested, fetch SSH keys");
238  
239 0 my $gpg_keys_response =
240 $self->ua->get( $self->githubGPGKeysEndpoint,
241 "Authorization" => "token $access_token" );
242  
243 0 ▶ if ( $gpg_keys_response->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gpg_keys_response->is_error) 0 0 0
244 0 $self->logger->error( "Bad authorization response: "
245 . $gpg_keys_response->message );
246 0 $self->logger->debug( $gpg_keys_response->content );
247 0 return PE_ERROR;
248 }
249  
250 0 my $gpg_keys_content = $gpg_keys_response->decoded_content;
251  
252 0 $self->logger->debug(
253 "Response from GitHub GPG Keys API: $gpg_keys_content");
254  
255 0 eval {
256 0 $json_hash =
257 from_json( $gpg_keys_content, { allow_nonref => 1 } );
258 };
259 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
260 0 $self->logger->error("Unable to decode JSON $gpg_keys_content");
261 0 return PE_ERROR;
262 }
263  
264 0 $req->data->{githubData}->{"gpg_keys"} = $json_hash;
265 }
266  
267 $req->user(
268 0 $req->data->{githubData}->{ $self->conf->{githubUserField} } );
269  
270 0 $self->logger->debug( "Good GitHub authentication for " . $req->user );
271  
272 0 return PE_OK;
273 }
274  
275 # No code, redirect to GitHub
276 else {
277 0 $self->logger->debug('Redirection to GitHub');
278  
279 # Store state
280 my $stateToken = $self->state_ott->createToken( {
281 _type => 'githubState',
282 urldc => $req->urldc,
283 checkLogins => $req->{checkLogins},
284 }
285 0 );
286 0 ▶ unless ($stateToken) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stateToken) 0 0 0
287 0 $self->logger->error('Unable to create GitHub state token');
288 0 return PE_ERROR;
289 }
290  
291 0 my $authn_uri = $self->githubAuthorizationEndpoint;
292 0 my $client_id = $self->conf->{githubClientID};
293 0 my $scope = $self->conf->{githubScope};
294 0 $authn_uri .= '?'
295 . build_urlencoded(
296 response_type => 'code',
297 client_id => $client_id,
298 redirect_uri => $callback_url,
299 scope => $scope,
300 state => $stateToken,
301 );
302  
303 0 $req->urldc($authn_uri);
304  
305 0 $self->logger->debug( "Redirect user to " . $req->urldc );
306  
307 0 return PE_REDIRECT;
308 }
309 }
310  
311 0 ▶sub setAuthSessionInfo {
Subroutine setAuthSessionInfo: not called CC=2 SCAR=17.9
Pod undocumented
312 0 my ( $self, $req ) = @_;
313  
314 $req->{sessionInfo}->{authenticationLevel} =
315 0 $self->conf->{githubAuthnLevel};
316  
317 0 foreach ( keys %{ $req->data->{githubData} } ) {
318 $req->{sessionInfo}->{ 'github_' . $_ } =
319 0 $req->data->{githubData}->{$_};
320 }
321  
322 0 return PE_OK;
323 }
324  
325 0 ▶sub authenticate {
Subroutine authenticate: not called CC=1 SCAR=6.9
Pod undocumented
326 0 return PE_OK;
327 }
328  
329 0 ▶sub authFinish {
Subroutine authFinish: not called CC=1 SCAR=6.9
Pod undocumented
330 0 return PE_OK;
331 }
332  
333 0 ▶sub authLogout {
Subroutine authLogout: not called CC=1 SCAR=6.9
Pod undocumented
334 0 return PE_OK;
335 }
336  
337 0 ▶sub authForce {
Subroutine authForce: not called CC=1 SCAR=6.9
Pod undocumented
338 0 return 0;
339 }
340  
341 0 ▶sub getDisplayType {
Subroutine getDisplayType: not called CC=1 SCAR=6.9
Pod undocumented
342 0 return "logo";
343 }
344  
345 2 1;
« blib/lib/Lemonldap/NG/Portal/UserDB/SAML.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Lib/OpenID/Server.pm »