« blib/lib/Lemonldap/NG/Portal/Plugins/PublicPages.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/Plugins/OIDC/AuthPkce.pm »
| 1 | # WebAuthn second factor authentication | ||||||||||
| 2 | # | ||||||||||
| 3 | # This plugin handle authentications to ask WebAuthn second factor for users that | ||||||||||
| 4 | # have registered their WebAuthn authenticators | ||||||||||
| 5 | package Lemonldap::NG::Portal::2F::WebAuthn; | ||||||||||
| 6 | |||||||||||
| 7 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use Mouse; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use JSON qw(from_json to_json); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 2 | ▶ | use MIME::Base64 qw(encode_base64url decode_base64url); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 2 | ▶ | use Crypt::URandom; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 2 | ▶ | use Lemonldap::NG::Portal::Main::Constants qw( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | PE_OK | ||||||||||
| 15 | PE_WEBAUTHNFAILED | ||||||||||
| 16 | PE_ERROR | ||||||||||
| 17 | PE_SENDRESPONSE | ||||||||||
| 18 | PE_BADCREDENTIALS | ||||||||||
| 19 | 2 | ); | |||||||||
| 20 | |||||||||||
| 21 | 2 | our $VERSION = '2.23.0'; | |||||||||
| 22 | |||||||||||
| 23 | 2 | extends 'Lemonldap::NG::Portal::Main::SecondFactor'; | |||||||||
| 24 | 2 | with 'Lemonldap::NG::Portal::Lib::WebAuthn'; | |||||||||
| 25 | |||||||||||
| 26 | # INITIALIZATION | ||||||||||
| 27 | |||||||||||
| 28 | 2 | has prefix => ( is => 'ro', default => 'webauthn' ); | |||||||||
| 29 | 2 | has logo => ( is => 'rw', default => 'webauthn.png' ); | |||||||||
| 30 | |||||||||||
| 31 | 6 | ▶ | sub init { | ||||||||
|
Subroutine
init: called CC=3 SCAR=11.7
| |||||||||||
| 32 | 6 | my ($self) = @_; | |||||||||
| 33 | |||||||||||
| 34 | # If "activation" is just set to "enabled", | ||||||||||
| 35 | # replace the rule to detect if user has registered its key | ||||||||||
| 36 | $self->conf->{webauthn2fActivation} = | ||||||||||
| 37 | 'has2f("WebAuthn") and $_auth ne "WebAuthn"' | ||||||||||
| 38 | 6 | ▶ | if $self->conf->{webauthn2fActivation} eq '1'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | |||||||||||
| 40 | 6 | ▶ | return $self->SUPER::init() ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | # RUNNING METHODS | ||||||||||
| 44 | |||||||||||
| 45 | # Main method | ||||||||||
| 46 | 1 | ▶ | sub run { | ||||||||
|
Subroutine
run: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||
| 47 | 1 | my ( $self, $req, $token ) = @_; | |||||||||
| 48 | 1 | my $request = $self->generateChallenge( $req, $req->sessionInfo ); | |||||||||
| 49 | 1 | ▶ | unless ($request) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 0 | $self->logger->error( | |||||||||
| 51 | $self->prefix . '2f: no registered device for ' . $req->user ); | ||||||||||
| 52 | 0 | return PE_WEBAUTHNFAILED; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 1 | $self->ott->updateToken( $token, _webauthn_request => $request ); | |||||||||
| 56 | |||||||||||
| 57 | 1 | my $cacheTag = $self->p->cacheTag; | |||||||||
| 58 | 1 | $req->data->{customScript} .= <<"EOF"; | |||||||||
| 59 | <script type="text/javascript" src="$self->{p}->{staticPrefix}/common/js/webauthn-json.browser-global.min.js?v=$cacheTag"></script> | ||||||||||
| 60 | <script type="text/javascript" src="$self->{p}->{staticPrefix}/common/js/webauthncheck.min.js?v=$cacheTag"></script> | ||||||||||
| 61 | EOF | ||||||||||
| 62 | |||||||||||
| 63 | # Prepare form | ||||||||||
| 64 | my $tmp = $self->p->sendHtml( | ||||||||||
| 65 | $req, | ||||||||||
| 66 | 'webauthn2fcheck', | ||||||||||
| 67 | params => { | ||||||||||
| 68 | TARGET => $self->p->relativeUrl( $req, 'webauthn2fcheck' ), | ||||||||||
| 69 | DATA => | ||||||||||
| 70 | to_json( { request => $request, webauthn_autostart => \1 } ), | ||||||||||
| 71 | TOKEN => $token, | ||||||||||
| 72 | CUSTOM_SCRIPT => $req->data->{customScript}, | ||||||||||
| 73 | 1 | $self->get2fTplParams($req), | |||||||||
| 74 | } | ||||||||||
| 75 | ); | ||||||||||
| 76 | |||||||||||
| 77 | 1 | $req->response($tmp); | |||||||||
| 78 | 1 | return PE_SENDRESPONSE; | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 0 | ▶ | sub verify { | ||||||||
|
Subroutine
verify: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 82 | 0 | my ( $self, $req, $session ) = @_; | |||||||||
| 83 | 0 | my $user = $session->{ $self->conf->{whatToTrace} }; | |||||||||
| 84 | 0 | my $credential_json = $req->param('credential'); | |||||||||
| 85 | |||||||||||
| 86 | 0 | ▶ | unless ($credential_json) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 0 | $self->logger->error( | |||||||||
| 88 | $self->prefix . '2f: missing signature parameter' ); | ||||||||||
| 89 | 0 | return PE_WEBAUTHNFAILED; | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 0 | my $signature_options = $session->{_webauthn_request}; | |||||||||
| 93 | 0 | delete $session->{_webauthn_request}; | |||||||||
| 94 | |||||||||||
| 95 | 0 | my $validation_result = eval { | |||||||||
| 96 | 0 | $self->validateAssertion( $req, $session, $signature_options, | |||||||||
| 97 | $credential_json ); | ||||||||||
| 98 | }; | ||||||||||
| 99 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 0 | $self->logger->error( | |||||||||
| 101 | $self->prefix . "2f: validation error for $user ($@)" ); | ||||||||||
| 102 | 0 | return PE_WEBAUTHNFAILED; | |||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | 0 | ▶ | if ( $validation_result->{success} == 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 106 | 0 | $req->data->{_2fDevice} = $validation_result->{matching_credential}; | |||||||||
| 107 | $req->data->{_2fLogInfo} = | ||||||||||
| 108 | 0 | { signature_count => $validation_result->{signature_count} }; | |||||||||
| 109 | 0 | return PE_OK; | |||||||||
| 110 | } | ||||||||||
| 111 | else { | ||||||||||
| 112 | 0 | $self->logger->error( | |||||||||
| 113 | $self->prefix . "2f: validation did not return success for $user" ); | ||||||||||
| 114 | 0 | return PE_WEBAUTHNFAILED; | |||||||||
| 115 | } | ||||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | 2 | 1; | |||||||||