« blib/lib/Lemonldap/NG/Portal/Plugins/RESTServer.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/2F/Register/WebAuthn.pm »
| 1 | package Lemonldap::NG::Portal::Auth::GitHub; | ||||||||||
| 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 githubAuthorizationEndpoint => ( | ||||||||||
| 31 | is => 'ro', | ||||||||||
| 32 | lazy => 1, | ||||||||||
| 33 | default => sub { | ||||||||||
| 34 | $_[0]->conf->{githubAuthorizationEndpoint} | ||||||||||
| 35 | 0 | ▶ | || 'https://github.com/login/oauth/authorize'; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||
| 36 | } | ||||||||||
| 37 | 1 | ); | |||||||||
| 38 | |||||||||||
| 39 | has githubTokenEndpoint => ( | ||||||||||
| 40 | is => 'ro', | ||||||||||
| 41 | lazy => 1, | ||||||||||
| 42 | default => sub { | ||||||||||
| 43 | $_[0]->conf->{githubTokenEndpoint} | ||||||||||
| 44 | 0 | ▶ | || 'https://github.com/login/oauth/access_token'; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||
| 45 | } | ||||||||||
| 46 | 1 | ); | |||||||||
| 47 | |||||||||||
| 48 | has githubUserEndpoint => ( | ||||||||||
| 49 | is => 'ro', | ||||||||||
| 50 | lazy => 1, | ||||||||||
| 51 | default => sub { | ||||||||||
| 52 | $_[0]->conf->{githubUserEndpoint} | ||||||||||
| 53 | 0 | ▶ | || 'https://api.github.com/user'; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||
| 54 | } | ||||||||||
| 55 | 1 | ); | |||||||||
| 56 | |||||||||||
| 57 | has githubPublicKeysEndpoint => ( | ||||||||||
| 58 | is => 'ro', | ||||||||||
| 59 | lazy => 1, | ||||||||||
| 60 | default => sub { | ||||||||||
| 61 | $_[0]->conf->{githubPublicKeysEndpoint} | ||||||||||
| 62 | 0 | ▶ | || 'https://api.github.com/user/keys'; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||
| 63 | } | ||||||||||
| 64 | 1 | ); | |||||||||
| 65 | |||||||||||
| 66 | has githubGPGKeysEndpoint => ( | ||||||||||
| 67 | is => 'ro', | ||||||||||
| 68 | lazy => 1, | ||||||||||
| 69 | default => sub { | ||||||||||
| 70 | $_[0]->conf->{githubGPGKeysEndpoint} | ||||||||||
| 71 | 0 | ▶ | || 'https://api.github.com/user/gpg_keys'; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||
| 72 | } | ||||||||||
| 73 | 1 | ); | |||||||||
| 74 | |||||||||||
| 75 | sub init { | ||||||||||
| 76 | 0 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
init: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 77 | |||||||||||
| 78 | 0 | my $ret = 1; | |||||||||
| 79 | 0 | foreach my $arg (qw(githubClientID githubClientSecret)) { | |||||||||
| 80 | 0 | ▶ | unless ( $self->conf->{$arg} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 0 | $ret = 0; | |||||||||
| 82 | 0 | $self->error("Parameter $arg is required"); | |||||||||
| 83 | } | ||||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | 0 | return $ret; | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | # RUNNING METHODS | ||||||||||
| 90 | |||||||||||
| 91 | sub extractFormInfo { | ||||||||||
| 92 | 0 | ▶ | my ( $self, $req ) = @_; | ||||||||
|
Subroutine
extractFormInfo: not called CC=15 SCAR=54.8
Pod
undocumented
| |||||||||||
| 93 | 0 | my $nonce = time; | |||||||||
| 94 | |||||||||||
| 95 | # Build redirect_uri | ||||||||||
| 96 | 0 | my $callback_url = $req->portal; | |||||||||
| 97 | |||||||||||
| 98 | # Check return values | ||||||||||
| 99 | 0 | my $code = $req->param("code"); | |||||||||
| 100 | 0 | my $state = $req->param("state"); | |||||||||
| 101 | |||||||||||
| 102 | # Code | ||||||||||
| 103 | 0 | ▶ | if ($code) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | 0 | my %form; | |||||||||
| 105 | 0 | $form{"code"} = $code; | |||||||||
| 106 | 0 | ▶ | $form{"state"} = $state if $state; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | 0 | $form{"client_id"} = $self->conf->{githubClientID}; | |||||||||
| 108 | 0 | $form{"client_secret"} = $self->conf->{githubClientSecret}; | |||||||||
| 109 | 0 | $form{"redirect_uri"} = $callback_url; | |||||||||
| 110 | |||||||||||
| 111 | 0 | my $response = $self->ua->post( | |||||||||
| 112 | $self->githubTokenEndpoint, | ||||||||||
| 113 | \%form, | ||||||||||
| 114 | "Content-Type" => 'application/x-www-form-urlencoded', | ||||||||||
| 115 | 'Accept' => 'application/json' | ||||||||||
| 116 | ); | ||||||||||
| 117 | |||||||||||
| 118 | 0 | ▶ | if ( $response->is_error ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | 0 | $self->logger->error( | |||||||||
| 120 | "Bad authorization response: " . $response->message ); | ||||||||||
| 121 | 0 | $self->logger->debug( $response->content ); | |||||||||
| 122 | 0 | return PE_ERROR; | |||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | 0 | my $content = $response->decoded_content; | |||||||||
| 126 | |||||||||||
| 127 | 0 | my $json_hash; | |||||||||
| 128 | |||||||||||
| 129 | 0 | eval { $json_hash = from_json( $content, { allow_nonref => 1 } ); }; | |||||||||
| 130 | |||||||||||
| 131 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | 0 | $self->logger->error("Unable to decode JSON $content"); | |||||||||
| 133 | 0 | return PE_ERROR; | |||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | 0 | my $access_token = $json_hash->{access_token}; | |||||||||
| 137 | |||||||||||
| 138 | 0 | $self->logger->debug("Get access token $access_token from GitHub"); | |||||||||
| 139 | |||||||||||
| 140 | # Call User EndPoint URI | ||||||||||
| 141 | 0 | $self->logger->debug( | |||||||||
| 142 | "Call GitHub User Endpoint " . $self->githubUserEndpoint ); | ||||||||||
| 143 | |||||||||||
| 144 | 0 | my $user_response = $self->ua->get( $self->githubUserEndpoint, | |||||||||
| 145 | "Authorization" => "token $access_token" ); | ||||||||||
| 146 | |||||||||||
| 147 | 0 | ▶ | if ( $user_response->is_error ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 148 | 0 | $self->logger->error( | |||||||||
| 149 | "Bad authorization response: " . $user_response->message ); | ||||||||||
| 150 | 0 | $self->logger->debug( $user_response->content ); | |||||||||
| 151 | 0 | return PE_ERROR; | |||||||||
| 152 | } | ||||||||||
| 153 | |||||||||||
| 154 | 0 | my $user_content = $user_response->decoded_content; | |||||||||
| 155 | |||||||||||
| 156 | 0 | $self->logger->debug("Response from GitHub User API: $user_content"); | |||||||||
| 157 | |||||||||||
| 158 | 0 | eval { $json_hash = from_json( $user_content, { allow_nonref => 1 } ); }; | |||||||||
| 159 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 160 | 0 | $self->logger->error("Unable to decode JSON $user_content"); | |||||||||
| 161 | 0 | return PE_ERROR; | |||||||||
| 162 | } | ||||||||||
| 163 | |||||||||||
| 164 | 0 | foreach ( keys %$json_hash ) { | |||||||||
| 165 | 0 | $req->data->{githubData}->{$_} = $json_hash->{$_}; | |||||||||
| 166 | } | ||||||||||
| 167 | |||||||||||
| 168 | # Fetch SSH keys | ||||||||||
| 169 | 0 | ▶ | if ( $self->conf->{githubScope} =~ /public_key/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 170 | 0 | $self->logger->debug("Scope public_key requested, fetch SSH keys"); | |||||||||
| 171 | |||||||||||
| 172 | 0 | my $public_keys_response = $self->ua->get( | |||||||||
| 173 | $self->githubPublicKeysEndpoint, | ||||||||||
| 174 | "Authorization" => "token $access_token" | ||||||||||
| 175 | ); | ||||||||||
| 176 | |||||||||||
| 177 | 0 | ▶ | if ( $public_keys_response->is_error ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 178 | 0 | $self->logger->error( "Bad authorization response: " | |||||||||
| 179 | . $public_keys_response->message ); | ||||||||||
| 180 | 0 | $self->logger->debug( $public_keys_response->content ); | |||||||||
| 181 | 0 | return PE_ERROR; | |||||||||
| 182 | } | ||||||||||
| 183 | |||||||||||
| 184 | 0 | my $public_keys_content = $public_keys_response->decoded_content; | |||||||||
| 185 | |||||||||||
| 186 | 0 | $self->logger->debug( | |||||||||
| 187 | "Response from GitHub Keys API: $public_keys_content"); | ||||||||||
| 188 | |||||||||||
| 189 | 0 | eval { | |||||||||
| 190 | 0 | $json_hash = | |||||||||
| 191 | from_json( $public_keys_content, { allow_nonref => 1 } ); | ||||||||||
| 192 | }; | ||||||||||
| 193 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 194 | 0 | $self->logger->error( | |||||||||
| 195 | "Unable to decode JSON $public_keys_content"); | ||||||||||
| 196 | 0 | return PE_ERROR; | |||||||||
| 197 | } | ||||||||||
| 198 | |||||||||||
| 199 | 0 | $req->data->{githubData}->{"public_keys"} = $json_hash; | |||||||||
| 200 | } | ||||||||||
| 201 | |||||||||||
| 202 | # Fetch GPG keys | ||||||||||
| 203 | 0 | ▶ | if ( $self->conf->{githubScope} =~ /gpg_key/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 204 | 0 | $self->logger->debug("Scope gpg_key requested, fetch SSH keys"); | |||||||||
| 205 | |||||||||||
| 206 | 0 | my $gpg_keys_response = | |||||||||
| 207 | $self->ua->get( $self->githubGPGKeysEndpoint, | ||||||||||
| 208 | "Authorization" => "token $access_token" ); | ||||||||||
| 209 | |||||||||||
| 210 | 0 | ▶ | if ( $gpg_keys_response->is_error ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 211 | 0 | $self->logger->error( "Bad authorization response: " | |||||||||
| 212 | . $gpg_keys_response->message ); | ||||||||||
| 213 | 0 | $self->logger->debug( $gpg_keys_response->content ); | |||||||||
| 214 | 0 | return PE_ERROR; | |||||||||
| 215 | } | ||||||||||
| 216 | |||||||||||
| 217 | 0 | my $gpg_keys_content = $gpg_keys_response->decoded_content; | |||||||||
| 218 | |||||||||||
| 219 | 0 | $self->logger->debug( | |||||||||
| 220 | "Response from GitHub GPG Keys API: $gpg_keys_content"); | ||||||||||
| 221 | |||||||||||
| 222 | 0 | eval { | |||||||||
| 223 | 0 | $json_hash = | |||||||||
| 224 | from_json( $gpg_keys_content, { allow_nonref => 1 } ); | ||||||||||
| 225 | }; | ||||||||||
| 226 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 227 | 0 | $self->logger->error("Unable to decode JSON $gpg_keys_content"); | |||||||||
| 228 | 0 | return PE_ERROR; | |||||||||
| 229 | } | ||||||||||
| 230 | |||||||||||
| 231 | 0 | $req->data->{githubData}->{"gpg_keys"} = $json_hash; | |||||||||
| 232 | } | ||||||||||
| 233 | |||||||||||
| 234 | # Extract state | ||||||||||
| 235 | 0 | ▶ | if ($state) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 236 | 0 | my $stateSession = $self->p->getApacheSession( $state, 1 ); | |||||||||
| 237 | |||||||||||
| 238 | 0 | $req->urldc( $stateSession->data->{urldc} ); | |||||||||
| 239 | 0 | $req->{checkLogins} = $stateSession->data->{checkLogins}; | |||||||||
| 240 | |||||||||||
| 241 | 0 | $stateSession->remove; | |||||||||
| 242 | } | ||||||||||
| 243 | |||||||||||
| 244 | $req->user( | ||||||||||
| 245 | 0 | $req->data->{githubData}->{ $self->conf->{githubUserField} } ); | |||||||||
| 246 | |||||||||||
| 247 | 0 | $self->logger->debug( "Good GitHub authentication for " . $req->user ); | |||||||||
| 248 | |||||||||||
| 249 | 0 | return PE_OK; | |||||||||
| 250 | } | ||||||||||
| 251 | |||||||||||
| 252 | # No code, redirect to GitHub | ||||||||||
| 253 | else { | ||||||||||
| 254 | 0 | $self->logger->debug('Redirection to GitHub'); | |||||||||
| 255 | |||||||||||
| 256 | # Store state | ||||||||||
| 257 | 0 | my $stateSession = | |||||||||
| 258 | $self->p->getApacheSession( undef, 1, 0, 'GitHubState' ); | ||||||||||
| 259 | |||||||||||
| 260 | 0 | my $stateInfos = {}; | |||||||||
| 261 | 0 | $stateInfos->{_utime} = time() + $self->conf->{timeout}; | |||||||||
| 262 | 0 | $stateInfos->{urldc} = $req->urldc; | |||||||||
| 263 | 0 | $stateInfos->{checkLogins} = $req->{checkLogins}; | |||||||||
| 264 | |||||||||||
| 265 | 0 | $stateSession->update($stateInfos); | |||||||||
| 266 | |||||||||||
| 267 | 0 | my $authn_uri = $self->githubAuthorizationEndpoint; | |||||||||
| 268 | 0 | my $client_id = $self->conf->{githubClientID}; | |||||||||
| 269 | 0 | my $scope = $self->conf->{githubScope}; | |||||||||
| 270 | 0 | $authn_uri .= '?' | |||||||||
| 271 | . build_urlencoded( | ||||||||||
| 272 | response_type => 'code', | ||||||||||
| 273 | client_id => $client_id, | ||||||||||
| 274 | redirect_uri => $callback_url, | ||||||||||
| 275 | scope => $scope, | ||||||||||
| 276 | state => $stateSession->id, | ||||||||||
| 277 | ); | ||||||||||
| 278 | |||||||||||
| 279 | 0 | $req->urldc($authn_uri); | |||||||||
| 280 | |||||||||||
| 281 | 0 | $self->logger->debug( "Redirect user to " . $req->urldc ); | |||||||||
| 282 | |||||||||||
| 283 | 0 | return PE_REDIRECT; | |||||||||
| 284 | } | ||||||||||
| 285 | } | ||||||||||
| 286 | |||||||||||
| 287 | sub setAuthSessionInfo { | ||||||||||
| 288 | 0 | ▶ | my ( $self, $req ) = @_; | ||||||||
|
Subroutine
setAuthSessionInfo: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 289 | |||||||||||
| 290 | $req->{sessionInfo}->{authenticationLevel} = | ||||||||||
| 291 | 0 | $self->conf->{githubAuthnLevel}; | |||||||||
| 292 | |||||||||||
| 293 | 0 | foreach ( keys %{ $req->data->{githubData} } ) { | |||||||||
| 294 | $req->{sessionInfo}->{ 'github_' . $_ } = | ||||||||||
| 295 | 0 | $req->data->{githubData}->{$_}; | |||||||||
| 296 | } | ||||||||||
| 297 | |||||||||||
| 298 | 0 | return PE_OK; | |||||||||
| 299 | } | ||||||||||
| 300 | |||||||||||
| 301 | sub authenticate { | ||||||||||
| 302 | 0 | ▶ | return PE_OK; | ||||||||
|
Subroutine
authenticate: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 303 | } | ||||||||||
| 304 | |||||||||||
| 305 | sub authFinish { | ||||||||||
| 306 | 0 | ▶ | return PE_OK; | ||||||||
|
Subroutine
authFinish: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 307 | } | ||||||||||
| 308 | |||||||||||
| 309 | sub authLogout { | ||||||||||
| 310 | 0 | ▶ | return PE_OK; | ||||||||
|
Subroutine
authLogout: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 311 | } | ||||||||||
| 312 | |||||||||||
| 313 | sub authForce { | ||||||||||
| 314 | 0 | ▶ | return 0; | ||||||||
|
Subroutine
authForce: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 315 | } | ||||||||||
| 316 | |||||||||||
| 317 | sub getDisplayType { | ||||||||||
| 318 | 0 | ▶ | return "logo"; | ||||||||
|
Subroutine
getDisplayType: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 319 | } | ||||||||||
| 320 | |||||||||||
| 321 | 1 | 1; | |||||||||