| 1 | # A generic 2FA module which registers a simple value into the psession | ||||||||||||||||||||
| 2 | # This is meant to be used with sfExtra | ||||||||||||||||||||
| 3 | package Lemonldap::NG::Portal::2F::Register::Generic; | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 1 | ▶ | use Mouse; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | ▶ | use JSON qw(from_json to_json); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 1 | ▶ | use Lemonldap::NG::Common::Crypto; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 1 | ▶ | use Lemonldap::NG::Portal::Main::Constants ':all'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 1 | our $VERSION = '2.23.0'; | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 1 | extends 'Lemonldap::NG::Portal::2F::Register::Base'; | |||||||||||||||||||
| 14 | 1 | with 'Lemonldap::NG::Portal::Lib::2fDevices'; | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | # INITIALIZATION | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | has ott => ( | ||||||||||||||||||||
| 19 | is => 'ro', | ||||||||||||||||||||
| 20 | lazy => 1, | ||||||||||||||||||||
| 21 | 0 | ▶ | default => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 22 | my $ott = | ||||||||||||||||||||
| 23 | 0 | $_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken'); | |||||||||||||||||||
| 24 | my $timeout = $_[0]->{conf}->{sfRegisterTimeout} | ||||||||||||||||||||
| 25 | 0 | ▶ | // $_[0]->{conf}->{formTimeout}; | ||||||||||||||||||
|
Condition0%0/3
$_[0]{"conf"}{"sfRegisterTimeout"} // $_[0]{"conf"}{"formTimeout"}
MC/DC0%0/2
$_[0]{"conf"}{"sfRegisterTimeout"} // $_[0]{"conf"}{"formTimeout"}
$_[0]{"conf"}{"sfRegisterTimeout"}
$_[0]{"conf"}{"formTimeout"}
Truth table0%0/3
A // B
A= $_[0]{"conf"}{"sfRegisterTimeout"}B= $_[0]{"conf"}{"formTimeout"}
| |||||||||||||||||||||
| 26 | 0 | $ott->timeout($timeout); | |||||||||||||||||||
| 27 | 0 | return $ott; | |||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | 1 | ); | |||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | # The verification module that we'll use | ||||||||||||||||||||
| 32 | 1 | has verificationModule => ( | |||||||||||||||||||
| 33 | is => 'rw', | ||||||||||||||||||||
| 34 | isa => 'Lemonldap::NG::Portal::Lib::Code2F', | ||||||||||||||||||||
| 35 | weak_ref => 1, | ||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | # Overriden by sfExtra | ||||||||||||||||||||
| 39 | 1 | has logo => ( is => 'rw', default => 'generic.png' ); | |||||||||||||||||||
| 40 | 1 | has prefix => ( is => 'rw', default => 'generic' ); | |||||||||||||||||||
| 41 | 1 | has template => ( is => 'ro', default => 'generic2fregister' ); | |||||||||||||||||||
| 42 | 1 | has welcome => ( is => 'ro', default => 'generic2fwelcome' ); | |||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 1 | ▶ | use constant supportedActions => { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 45 | sendcode => "sendcode", | ||||||||||||||||||||
| 46 | verify => "verify", | ||||||||||||||||||||
| 47 | delete => "delete", | ||||||||||||||||||||
| 48 | modify => "modify", | ||||||||||||||||||||
| 49 | 1 | }; | |||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 0 | ▶ | sub sendcode { | ||||||||||||||||||
|
Subroutine
sendcode: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||
| 52 | 0 | my ( $self, $req ) = @_; | |||||||||||||||||||
| 53 | 0 | my $user = $req->userData->{ $self->conf->{whatToTrace} }; | |||||||||||||||||||
| 54 | 0 | my $generic = $req->param('generic'); | |||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 0 | ▶ | unless ($generic) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | 0 | return $self->failResponse( $req, PE_REGISTERFORMEMPTY, 200 ); | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 0 | ▶ | return $self->failResponse( $req, 'csrfError', 400 ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | unless $self->checkCsrf($req); | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | # Validate format | ||||||||||||||||||||
| 64 | 0 | ▶ | unless ( $self->validateFormat($generic) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | my $error_label = $self->conf->{generic2fFormatErrorLabel} | ||||||||||||||||||||
| 66 | 0 | ▶ | || 'generic2fFormatError'; | ||||||||||||||||||
|
Condition0%0/2
$self->conf->{'generic2fFormatErrorLabel'} || 'generic2fFormatError'
MC/DC0%0/1
$self->conf->{'generic2fFormatErrorLabel'} || 'generic2fFormatError'
$self->conf->{'generic2fFormatErrorLabel'}
Truth table0%0/2
A
A= $self->conf->{'generic2fFormatErrorLabel'}
| |||||||||||||||||||||
| 67 | 0 | return $self->failResponse( $req, $error_label, 200 ); | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | # Generate and send code | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | # Save current session info into a token | ||||||||||||||||||||
| 73 | 0 | my $sessionInfo = { %{ $req->userData } }; | |||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | # Inject candidate value | ||||||||||||||||||||
| 76 | 0 | $sessionInfo->{destination} = $generic; | |||||||||||||||||||
| 77 | 0 | my $token = $self->ott->createToken($sessionInfo); | |||||||||||||||||||
| 78 | 0 | my $result = | |||||||||||||||||||
| 79 | $self->verificationModule->challenge( $req, $sessionInfo, $token ); | ||||||||||||||||||||
| 80 | 0 | ▶ | return $self->failResponse( $req, 'serverError' ) unless $result; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | # Send response | ||||||||||||||||||||
| 83 | 0 | $self->userLogger->notice( | |||||||||||||||||||
| 84 | $self->prefix . "2f: send verification code to $generic for $user" ); | ||||||||||||||||||||
| 85 | 0 | return $self->successResponse( $req, { result => 1, token => $token } ); | |||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 0 | ▶ | sub verify { | ||||||||||||||||||
|
Subroutine
verify: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 0 | my ( $self, $req ) = @_; | |||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 0 | ▶ | return $self->failResponse( $req, 'csrfError', 400 ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | unless $self->checkCsrf($req); | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 0 | my $user = $req->userData->{ $self->conf->{whatToTrace} }; | |||||||||||||||||||
| 96 | 0 | my $generic = $req->param('generic'); | |||||||||||||||||||
| 97 | 0 | my $tokenid = $req->param("token"); | |||||||||||||||||||
| 98 | 0 | my $genericcode = $req->param('genericcode'); | |||||||||||||||||||
| 99 | 0 | my $genericname = | |||||||||||||||||||
| 100 | $self->checkNameSfa( $req, $self->prefix, $req->param('genericname') ); | ||||||||||||||||||||
| 101 | 0 | ▶ | return $self->failResponse( $req, 'badName', 200 ) unless $genericname; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | # Verify code | ||||||||||||||||||||
| 104 | 0 | my $token = $self->ott->getToken( $tokenid, 1 ); | |||||||||||||||||||
| 105 | 0 | my $res = $self->verificationModule->verify_supplied_code( $req, $token, | |||||||||||||||||||
| 106 | $genericcode ); | ||||||||||||||||||||
| 107 | 0 | ▶ | return $self->failResponse( $req, $res, 400 ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 108 | unless ( $res == PE_OK ); | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | # Now generic is verified, let's store it in persistent data | ||||||||||||||||||||
| 111 | # Reading existing 2FDevices | ||||||||||||||||||||
| 112 | 0 | my @generic2f = | |||||||||||||||||||
| 113 | $self->find2fDevicesByType( $req, $req->userData, $self->prefix ); | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | # Delete previous generic if any | ||||||||||||||||||||
| 116 | 0 | ▶ | if (@generic2f) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | 0 | ▶ | if ( $self->del2fDevices( $req, $req->userData, \@generic2f ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 118 | 0 | $self->logger->debug( $self->prefix . "2f: old device(s) deleted" ); | |||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | else { | ||||||||||||||||||||
| 121 | 0 | $self->logger->error( | |||||||||||||||||||
| 122 | $self->prefix . "2f: unable to delete old device(s)" ); | ||||||||||||||||||||
| 123 | 0 | return $self->failResponse( $req, 'serverError' ); | |||||||||||||||||||
| 124 | } | ||||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | # Add a new one | ||||||||||||||||||||
| 128 | 0 | $res = $self->registerDevice( | |||||||||||||||||||
| 129 | $req, | ||||||||||||||||||||
| 130 | $req->userData, | ||||||||||||||||||||
| 131 | { | ||||||||||||||||||||
| 132 | _generic => $generic, | ||||||||||||||||||||
| 133 | name => $genericname, | ||||||||||||||||||||
| 134 | type => $self->prefix, | ||||||||||||||||||||
| 135 | epoch => time() | ||||||||||||||||||||
| 136 | } | ||||||||||||||||||||
| 137 | ); | ||||||||||||||||||||
| 138 | 0 | ▶ | if ( $res == PE_OK ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 139 | 0 | return $self->successResponse( $req, { result => 1 } ); | |||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | else { | ||||||||||||||||||||
| 142 | 0 | $self->logger->error( $self->prefix . "2f: unable to add device" ); | |||||||||||||||||||
| 143 | 0 | return $self->failResponse( $req, $res ); | |||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 0 | ▶ | sub validateFormat { | ||||||||||||||||||
|
Subroutine
validateFormat: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 148 | 0 | my ( $self, $generic ) = @_; | |||||||||||||||||||
| 149 | 0 | my $format_regex = $self->{conf}->{generic2fFormatRegex}; | |||||||||||||||||||
| 150 | 0 | ▶ | return 1 if ( !$format_regex ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 151 | 0 | return scalar $generic =~ $format_regex; | |||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 1 | 1; | |||||||||||||||||||
| 155 | |||||||||||||||||||||