blib/lib/Lemonldap/NG/Portal/2F/Yubikey.pm untested

filter: Statementstmt 0.0% 0 / 99 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 170 CC 21 SCAR 36.7
CC
21
Cov
0%
init
90.0
_findYubikeyForCode
30.0
verify
30.0
CRAP 39.2
1 # Yubico OTP second factor authentication
2 #
3 # This plugin handle authentications to ask Yubikey second factor for users that
4 # have registered their Yubikey using legacy OTP mode
5 package Lemonldap::NG::Portal::2F::Yubikey;
6  
7 use strict;
8 use Mouse;
9 use JSON qw(from_json to_json);
10 use Lemonldap::NG::Common::Util qw/display2F/;
11 use Lemonldap::NG::Portal::Main::Constants qw(
12 PE_OK
13 PE_ERROR
14 PE_BADOTP
15 PE_FORMEMPTY
16 PE_SENDRESPONSE
17 );
18  
19 our $VERSION = '2.23.0';
20  
21 extends 'Lemonldap::NG::Portal::Main::SecondFactor';
22 with 'Lemonldap::NG::Portal::Lib::2fDevices';
23  
24 # INITIALIZATION
25  
26 has type => ( is => 'rw', default => 'UBK' );
27 has prefix => ( is => 'ro', default => 'yubikey' );
28 has logo => ( is => 'rw', default => 'yubikey.png' );
29 has yubi => ( is => 'rw' );
30  
31 sub init {
32 my ($self) = @_;
33  
34 eval { require Auth::Yubikey_WebClient };
35 if ($@) {
36 $self->logger->error("Can't load Yubikey_WebClient library: $@");
37 $self->error("Can't load Yubikey_WebClient library: $@");
38 return 0;
39 }
40  
41 # Try to be smart about Yubikey 2F activation
42 if ( $self->conf->{yubikey2fActivation} eq '1' ) {
43 my @newrules;
44  
45 # If self registration is enabled,
46 # detect if user has registered its key
47 push @newrules, 'has2f("UBK")'
48 if $self->conf->{yubikey2fSelfRegistration};
49  
50 # If Yubikey looked up from an attribute, test attribute's presence
51 if ( $self->conf->{yubikey2fFromSessionAttribute} ) {
52 my $attr = $self->conf->{yubikey2fFromSessionAttribute};
53 push @newrules, "\$$attr";
54 }
55  
56 # Aggregate rules
57 if (@newrules) {
58 my $rule = join( " || ", @newrules );
59 $self->conf->{yubikey2fActivation} = $rule;
60 $self->logger->debug(
61 $self->prefix . "2f: activation rule -> $rule" );
62 }
63 }
64  
65 unless ($self->conf->{yubikey2fClientID}
66 and $self->conf->{yubikey2fSecretKey} )
67 {
68 $self->error( $self->prefix
69 . '2f: missing mandatory parameters (client ID or secret key)' );
70 return 0;
71 }
72  
73 $self->yubi(
74 Auth::Yubikey_WebClient->new( {
75 id => $self->conf->{yubikey2fClientID},
76 api => $self->conf->{yubikey2fSecretKey},
77 (
78 $self->conf->{yubikey2fNonce}
79 ? ( nonce => $self->conf->{yubikey2fNonce} )
80 : ()
81 ),
82 (
83 $self->conf->{yubikey2fUrl}
84 ? ( url => $self->conf->{yubikey2fUrl} )
85 : ()
86 )
87 }
88 )
89 );
90 return $self->SUPER::init();
91 }
92  
93 sub _findYubikeyForCode {
94 my ( $self, $req, $session, $code ) = @_;
95 my ( $yubikey, @ubk2f );
96  
97 my $id_from_code = substr( $code, 0, $self->conf->{yubikey2fPublicIDSize} );
98  
99 # First, lookup from session attribute
100 if ( $self->conf->{yubikey2fFromSessionAttribute} ) {
101 my $attr = $self->conf->{yubikey2fFromSessionAttribute};
102 $yubikey = $session->{$attr};
103 if ($yubikey) {
104 if ( $yubikey eq $id_from_code ) {
105 return { _yubikey => $yubikey, };
106 }
107 else {
108 return;
109 }
110 }
111 }
112  
113 # If we didn't find a key, lookup psession
114 if ( !$yubikey and $session->{_2fDevices} ) {
115 @ubk2f = $self->find2fDevicesByType( $req, $session, $self->type );
116 my @results = grep { $_->{_yubikey} eq $id_from_code } @ubk2f;
117 return $results[0];
118 }
119 return;
120 }
121  
122 sub _hasYubikey {
123 my ( $self, $req, $session ) = @_;
124 my ( $yubikey, @ubk2f );
125  
126 # Does the user have a session attribute
127 if ( $self->conf->{yubikey2fFromSessionAttribute} ) {
128 my $attr = $self->conf->{yubikey2fFromSessionAttribute};
129 $yubikey = $session->{$attr};
130 }
131  
132 # If we didn't find a value, lookup registered devices
133 if ( !$yubikey and $session->{_2fDevices} ) {
134 @ubk2f = $self->find2fDevicesByType( $req, $session, $self->type );
135 $yubikey = $_->{_yubikey} foreach @ubk2f;
136 }
137  
138 return ( $yubikey ? 1 : 0 );
139 }
140  
141 sub run {
142 my ( $self, $req, $token ) = @_;
143 unless ( $self->_hasYubikey( $req, $req->sessionInfo ) ) {
144 $self->userLogger->warn( $self->prefix
145 . '2f: user '
146 . $req->{sessionInfo}->{ $self->conf->{whatToTrace} }
147 . ' has no device registered' );
148 return PE_BADOTP;
149 }
150  
151 # Prepare form
152 my $tmp = $self->p->sendHtml(
153 $req,
154 'ext2fcheck',
155 params => {
156 TOKEN => $token,
157 TARGET => $self->p->relativeUrl(
158 $req, 'yubikey2fcheck', { skin => $self->p->getSkin($req) }
159 ),
160 INPUTLOGO => $self->logo,
161 LEGEND => 'clickOnYubikey',
162 $self->get2fTplParams($req),
163 }
164 );
165 $self->logger->debug( $self->prefix . '2f: display form' );
166  
167 $req->response($tmp);
168 return PE_SENDRESPONSE;
169 }
170  
171 sub verify {
172 my ( $self, $req, $session ) = @_;
173 my $code;
174 unless ( $code = $req->param('code') ) {
175 $self->userLogger->error( $self->prefix . '2f: no code provided' );
176 return PE_FORMEMPTY;
177 }
178  
179 # Verify OTP
180 my $yubikey = $self->_findYubikeyForCode( $req, $session, $code );
181 unless ($yubikey) {
182 $self->userLogger->warn( $self->prefix . '2f: device not registered' );
183 return PE_BADOTP;
184 }
185  
186 $self->logger->debug( $self->prefix
187 . "2f: validating $code of $yubikey against external API" );
188 if ( $self->yubi->otp($code) ne 'OK' ) {
189 $self->userLogger->warn( $self->prefix . '2f: verification failed' );
190 return PE_BADOTP;
191 }
192 my $uid = $session->{ $self->conf->{whatToTrace} };
193 if ( $yubikey->{epoch} ) {
194 $req->data->{_2fDevice} = $yubikey;
195 $self->userLogger->info(
196 "User $uid authenticated with 2F device: " . display2F($yubikey) );
197 }
198 else {
199 $self->userLogger->info("User $uid authenticated with Yubikey");
200 }
201  
202 return PE_OK;
203 }
204  
205 1