« blib/lib/Lemonldap/NG/Portal/Plugins/NewLocationWarning.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/CertificateResetByMail/LDAP.pm »
| 1 | # TOTP second factor authentication | ||||||||||
| 2 | # | ||||||||||
| 3 | # This plugin handle authentications to ask TOTP second factor for users that | ||||||||||
| 4 | # have registered their TOTP secret | ||||||||||
| 5 | package Lemonldap::NG::Portal::2F::TOTP; | ||||||||||
| 6 | |||||||||||
| 7 | 28 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 28 | ▶ | use Mouse; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 28 | ▶ | use JSON qw(from_json to_json); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 28 | ▶ | use Lemonldap::NG::Portal::Main::Constants qw( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | PE_OK | ||||||||||
| 12 | PE_ERROR | ||||||||||
| 13 | PE_BADOTP | ||||||||||
| 14 | PE_FORMEMPTY | ||||||||||
| 15 | PE_SENDRESPONSE | ||||||||||
| 16 | 28 | ); | |||||||||
| 17 | 28 | ▶ | use Lemonldap::NG::Common::Util qw/display2F/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | 28 | our $VERSION = '2.23.0'; | |||||||||
| 20 | |||||||||||
| 21 | 28 | extends qw( | |||||||||
| 22 | Lemonldap::NG::Portal::Main::SecondFactor | ||||||||||
| 23 | Lemonldap::NG::Common::TOTP | ||||||||||
| 24 | ); | ||||||||||
| 25 | 28 | with 'Lemonldap::NG::Portal::Lib::2fDevices'; | |||||||||
| 26 | |||||||||||
| 27 | # INITIALIZATION | ||||||||||
| 28 | |||||||||||
| 29 | 28 | has prefix => ( is => 'ro', default => 'totp' ); | |||||||||
| 30 | 28 | has logo => ( is => 'rw', default => 'totp.png' ); | |||||||||
| 31 | |||||||||||
| 32 | 28 | ▶ | sub init { | ||||||||
|
Subroutine
init: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 33 | 28 | my ($self) = @_; | |||||||||
| 34 | |||||||||||
| 35 | # If "activation" is just set to "enabled", | ||||||||||
| 36 | # replace the rule to detect if user has registered its key | ||||||||||
| 37 | $self->conf->{totp2fActivation} = 'has2f("TOTP")' | ||||||||||
| 38 | 28 | ▶ | if $self->conf->{totp2fActivation} eq '1'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | |||||||||||
| 40 | 28 | return $self->SUPER::init(); | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | # RUNNING METHODS | ||||||||||
| 44 | |||||||||||
| 45 | 0 | ▶ | sub run { | ||||||||
|
Subroutine
run: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 46 | 0 | my ( $self, $req, $token ) = @_; | |||||||||
| 47 | 0 | $self->logger->debug( $self->prefix . '2f: generate form' ); | |||||||||
| 48 | |||||||||||
| 49 | # Prepare form | ||||||||||
| 50 | 0 | my $tmp = $self->p->sendHtml( | |||||||||
| 51 | $req, | ||||||||||
| 52 | 'totp2fcheck', | ||||||||||
| 53 | params => { | ||||||||||
| 54 | TARGET => $self->p->relativeUrl( $req, 'totp2fcheck' ), | ||||||||||
| 55 | TOKEN => $token, | ||||||||||
| 56 | $self->get2fTplParams($req), | ||||||||||
| 57 | } | ||||||||||
| 58 | ); | ||||||||||
| 59 | 0 | $self->logger->debug( $self->prefix . '2f: prepare verification' ); | |||||||||
| 60 | |||||||||||
| 61 | 0 | $req->response($tmp); | |||||||||
| 62 | 0 | return PE_SENDRESPONSE; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 0 | ▶ | sub verify { | ||||||||
|
Subroutine
verify: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||
| 66 | 0 | my ( $self, $req, $session ) = @_; | |||||||||
| 67 | 0 | my ( $code, $secret, @totp2f ); | |||||||||
| 68 | 0 | my $uid = $session->{ $self->conf->{whatToTrace} }; | |||||||||
| 69 | 0 | $self->logger->debug( $self->prefix . '2f: verification' ); | |||||||||
| 70 | |||||||||||
| 71 | 0 | ▶ | unless ( $code = $req->param('code') ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 0 | $self->userLogger->error( $self->prefix . '2f: no code provided' ); | |||||||||
| 73 | 0 | return PE_FORMEMPTY; | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 0 | @totp2f = $self->find2fDevicesByType( $req, $session, $self->type ); | |||||||||
| 77 | |||||||||||
| 78 | 0 | foreach my $device (@totp2f) { | |||||||||
| 79 | 0 | $self->logger->debug( "Trying TOTP device " . display2F($device) ); | |||||||||
| 80 | |||||||||||
| 81 | 0 | ▶ | if ( my $secret = $device->{_secret} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | |||||||||||
| 83 | my $verify_result = $self->verify_totp( | ||||||||||
| 84 | interval => $self->conf->{totp2fInterval}, | ||||||||||
| 85 | digits => $self->conf->{totp2fDigits}, | ||||||||||
| 86 | hash => $self->conf->{totp2fAlgorithm}, | ||||||||||
| 87 | range_tolerance => $self->conf->{totp2fRange}, | ||||||||||
| 88 | 0 | stored_secret => $secret, | |||||||||
| 89 | code => $code, | ||||||||||
| 90 | ); | ||||||||||
| 91 | |||||||||||
| 92 | 0 | ▶ | if ( $verify_result->{result} == 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 0 | $req->data->{_2fDevice} = $device; | |||||||||
| 94 | $req->data->{_2fLogInfo} = | ||||||||||
| 95 | 0 | { range => $verify_result->{offset} }; | |||||||||
| 96 | 0 | return PE_OK; | |||||||||
| 97 | } | ||||||||||
| 98 | } | ||||||||||
| 99 | else { | ||||||||||
| 100 | 0 | $self->logger->warn( "TOTP device " | |||||||||
| 101 | . display2F($device) | ||||||||||
| 102 | . " has no secret for user $uid" ); | ||||||||||
| 103 | } | ||||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | 0 | $self->userLogger->notice( $self->prefix | |||||||||
| 107 | . '2f: code did not match any of the registered TOTP for ' | ||||||||||
| 108 | . $uid ); | ||||||||||
| 109 | 0 | return PE_BADOTP; | |||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | 28 | 1; | |||||||||