| 1 | ##@file | ||||||||||||||||||||||||||||
| 2 | # Ajax-based authentication methods (SSL, Kerberos, WebAuthn...) | ||||||||||||||||||||||||||||
| 3 | # This class lets you easily implement a Javascript-based authentication | ||||||||||||||||||||||||||||
| 4 | # method. | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package Lemonldap::NG::Portal::Auth::_Ajax; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use Mouse::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Lemonldap::NG::Portal::Main::Constants qw( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | PE_OK | ||||||||||||||||||||||||||||
| 12 | PE_ERROR | ||||||||||||||||||||||||||||
| 13 | 6 | ); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 6 | our $VERSION = '2.23.0'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # addUnauthRoute/addAuthRoute are provided by deriving your plugin from | ||||||||||||||||||||||||||||
| 18 | # 'Lemonldap::NG::Portal::Main::Auth' or 'Lemonldap::NG::Portal::Main::Plugin' | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # You must provide an auth_id attribute that returns a simple string | ||||||||||||||||||||||||||||
| 21 | # identifying your plugin, eg: 'ssl', 'krb', ... | ||||||||||||||||||||||||||||
| 22 | # You must also implement an auth_route method that will be served on | ||||||||||||||||||||||||||||
| 23 | # /auth[auth_id] | ||||||||||||||||||||||||||||
| 24 | # The auth_route method validates your JS-supplied challenge, and must call | ||||||||||||||||||||||||||||
| 25 | # ajax_success to return the authentication token | ||||||||||||||||||||||||||||
| 26 | 6 | requires qw(auth_id addUnauthRoute addAuthRoute auth_route); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 6 | has InitCmd => ( | |||||||||||||||||||||||||||
| 29 | is => 'ro', | ||||||||||||||||||||||||||||
| 30 | default => | ||||||||||||||||||||||||||||
| 31 | q@$self->p->setHiddenFormValue( $req, ajax_auth_token => 0, '', 0 )@ | ||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # Auth Token | ||||||||||||||||||||||||||||
| 35 | has authott => ( | ||||||||||||||||||||||||||||
| 36 | is => 'rw', | ||||||||||||||||||||||||||||
| 37 | lazy => 1, | ||||||||||||||||||||||||||||
| 38 | 2 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | my $ott = | ||||||||||||||||||||||||||||
| 40 | 2 | $_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken'); | |||||||||||||||||||||||||||
| 41 | 2 | $ott->cache(0); | |||||||||||||||||||||||||||
| 42 | 2 | return $ott; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 6 | ); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 26 | ▶ | around 'init' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 47 | 26 | my $orig = shift; | |||||||||||||||||||||||||||
| 48 | 26 | my $self = shift; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 26 | my $route_name = 'auth' . $self->auth_id; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # Try to avoid a warning in logs | ||||||||||||||||||||||||||||
| 53 | 26 | ▶ | unless ( $self->p->unAuthRoutes->{GET}->{$route_name} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 25 | $self->addUnauthRoute( | |||||||||||||||||||||||||||
| 55 | $route_name => '_auth_route', | ||||||||||||||||||||||||||||
| 56 | ['GET'] | ||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # Used for session upgrade/reauthn | ||||||||||||||||||||||||||||
| 60 | 25 | $self->addAuthRoute( | |||||||||||||||||||||||||||
| 61 | $route_name => '_auth_route', | ||||||||||||||||||||||||||||
| 62 | ['GET'] | ||||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 26 | return $self->$orig(); | |||||||||||||||||||||||||||
| 66 | 6 | }; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 4 | ▶ | sub _auth_route { | ||||||||||||||||||||||||||
|
Subroutine
_auth_route: called CC=3 SCAR=12.6
| |||||||||||||||||||||||||||||
| 69 | 4 | my ( $self, $req, @path ) = @_; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # Run beforeAuth steps | ||||||||||||||||||||||||||||
| 72 | 4 | $req->steps( [ @{ $self->p->beforeAuth } ] ); | |||||||||||||||||||||||||||
| 73 | 4 | my $res = $self->p->process($req); | |||||||||||||||||||||||||||
| 74 | 4 | ▶ | if ( $res && $res > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$res and $res > 0
MC/DC0%0/2
$res and $res > 0
$res
$res > 0
Truth table33%1/3
A and B
A= $resB= $res > 0
| |||||||||||||||||||||||||||||
| 75 | 0 | $req->wantErrorRender(1); | |||||||||||||||||||||||||||
| 76 | 0 | return $self->p->doPE( $req, $res ); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 4 | return $self->auth_route( $req, @path ); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # You should call this method in your 'extractFormInfo' to validate the | ||||||||||||||||||||||||||||
| 83 | # Authentication token | ||||||||||||||||||||||||||||
| 84 | 2 | ▶ | sub get_auth_token { | ||||||||||||||||||||||||||
|
Subroutine
get_auth_token: called CC=3 SCAR=13.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 85 | 2 | my ( $self, $req, $token_id ) = @_; | |||||||||||||||||||||||||||
| 86 | 2 | my $token = $self->authott->getToken($token_id); | |||||||||||||||||||||||||||
| 87 | 2 | ▶ | if ($token) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 2 | ▶ | if ( $token->{type} eq ( 'auth_token_' . $self->auth_id ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # Fix #3180 | ||||||||||||||||||||||||||||
| 91 | 2 | utf8::downgrade( $token->{user} ); | |||||||||||||||||||||||||||
| 92 | 2 | return $token; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | else { | ||||||||||||||||||||||||||||
| 95 | 0 | $self->logger->error( "Unexpected token type: " . $token->{type} ); | |||||||||||||||||||||||||||
| 96 | 0 | return; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | $self->logger->error("Could not fetch user token $token_id"); | |||||||||||||||||||||||||||
| 101 | 0 | return; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # You should call this method in your auth_route method to create the Authentication token, | ||||||||||||||||||||||||||||
| 105 | # $user is the main identified, that UserDB will lookup. Extra information may | ||||||||||||||||||||||||||||
| 106 | # be stored in the session by setAuthSessionInfo | ||||||||||||||||||||||||||||
| 107 | 4 | ▶ | sub ajax_success { | ||||||||||||||||||||||||||
|
Subroutine
ajax_success: called CC=2 SCAR=8.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 108 | 4 | my ( $self, $req, $user, $extraInfo ) = @_; | |||||||||||||||||||||||||||
| 109 | 4 | my $token = $self->authott->createToken( { | |||||||||||||||||||||||||||
| 110 | user => $user, | ||||||||||||||||||||||||||||
| 111 | type => 'auth_token_' . $self->auth_id, | ||||||||||||||||||||||||||||
| 112 | extraInfo => $extraInfo, | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | ); | ||||||||||||||||||||||||||||
| 115 | 4 | ▶ | if ($token) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 4 | return $self->sendJSONresponse( | |||||||||||||||||||||||||||
| 117 | $req, | ||||||||||||||||||||||||||||
| 118 | { | ||||||||||||||||||||||||||||
| 119 | ajax_auth_token => $token, | ||||||||||||||||||||||||||||
| 120 | error => PE_OK, | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | ); | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | else { | ||||||||||||||||||||||||||||
| 125 | 0 | $self->logger->error("Could not create user token for $user"); | |||||||||||||||||||||||||||
| 126 | 0 | $req->wantErrorRender(1); | |||||||||||||||||||||||||||
| 127 | 0 | return $self->p->doPE( $req, PE_ERROR ); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 6 | 1; | |||||||||||||||||||||||||||