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

filter: Statementstmt 82.0% 41 / 50 Branchbran 60.0% 6 / 10 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 2 totaltotal 74.6% 56 / 75 CC 7 SCAR 9.0
CC
7
Cov
76%
get_auth_token
3.7
_auth_route
3.5
ajax_success
2.4
CRAP 2.5
« blib/lib/Lemonldap/NG/Portal/Lib/OverConf.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Auth/Apache.pm »
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(
11 PE_OK
12 PE_ERROR
13 6 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 6 our $VERSION = '2.22.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 default => sub {
39 my $ott =
40 2 ▶ $_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken');
Subroutine __ANON__: called CC=1 SCAR=0.0
41 2 $ott->cache(0);
42 2 return $ott;
43 }
44 6 );
45  
46 around 'init' => sub {
47 8 ▶ my $orig = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
48 8 my $self = shift;
49  
50 8 my $route_name = 'auth' . $self->auth_id;
51  
52 # Try to avoid a warning in logs
53 8 ▶ unless ( $self->p->unAuthRoutes->{GET}->{$route_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->p->unAuthRoutes->{"GET"}{$route_name}) 7 1 8
54 7 $self->addUnauthRoute(
55 $route_name => '_auth_route',
56 ['GET']
57 );
58  
59 # Used for session upgrade/reauthn
60 7 $self->addAuthRoute(
61 $route_name => '_auth_route',
62 ['GET']
63 );
64 }
65 8 return $self->$orig();
66 6 };
67  
68 sub _auth_route {
69 4 ▶ my ( $self, $req, @path ) = @_;
Subroutine _auth_route: called CC=3 SCAR=12.6
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
BranchTrueFalseTotal
if ($res and $res > 0) 0 4 4
Condition33%1/3
$res and $res > 0
!ll&&!rl&&r
400
MC/DC0%0/2
$res and $res > 0
$res $res > 0
Truth table33%1/3
A and B
A= $resB= $res > 0
ABresult
0X0
100
111
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 sub get_auth_token {
85 2 ▶ my ( $self, $req, $token_id ) = @_;
Subroutine get_auth_token: called CC=3 SCAR=13.1
Pod undocumented
86 2 my $token = $self->authott->getToken($token_id);
87 2 ▶ if ($token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token) 2 0 2
88 2 ▶ if ( $token->{type} eq ( 'auth_token_' . $self->auth_id ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{'type'} eq 'auth_token_' . $self->auth_id) { } 2 0 2
89 # Fix #3180
90 2 utf8::downgrade($token->{user});
91 2 return $token;
92 }
93 else {
94 0 $self->logger->error( "Unexpected token type: " . $token->{type} );
95 0 return;
96 }
97 }
98  
99 0 $self->logger->error("Could not fetch user token $token_id");
100 0 return;
101 }
102  
103 # You should call this method in your auth_route method to create the Authentication token,
104 # $user is the main identified, that UserDB will lookup. Extra information may
105 # be stored in the session by setAuthSessionInfo
106 sub ajax_success {
107 4 ▶ my ( $self, $req, $user, $extraInfo ) = @_;
Subroutine ajax_success: called CC=2 SCAR=8.5
Pod undocumented
108 4 my $token = $self->authott->createToken( {
109 user => $user,
110 type => 'auth_token_' . $self->auth_id,
111 extraInfo => $extraInfo,
112 }
113 );
114 4 ▶ if ($token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token) { } 4 0 4
115 4 return $self->sendJSONresponse(
116 $req,
117 {
118 ajax_auth_token => $token,
119 error => PE_OK,
120 }
121 );
122 }
123 else {
124 0 $self->logger->error("Could not create user token for $user");
125 0 $req->wantErrorRender(1);
126 0 return $self->p->doPE($req, PE_ERROR);
127 }
128 }
129  
130 6 1;
« blib/lib/Lemonldap/NG/Portal/Lib/OverConf.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Auth/Apache.pm »