« blib/lib/Lemonldap/NG/Portal/UserDB/OpenID.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/Main/Plugins.pm »
| 1 | package Lemonldap::NG::Portal::Auth::WebAuthn; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use Mouse; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Lemonldap::NG::Portal::Main::Constants qw( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | PE_ERROR | ||||||||||||||||||||||||||||
| 8 | PE_WEBAUTHNFAILED | ||||||||||||||||||||||||||||
| 9 | PE_FIRSTACCESS | ||||||||||||||||||||||||||||
| 10 | PE_SENDRESPONSE | ||||||||||||||||||||||||||||
| 11 | PE_TOKENEXPIRED | ||||||||||||||||||||||||||||
| 12 | PE_OK | ||||||||||||||||||||||||||||
| 13 | 3 | ); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 3 | our $VERSION = '2.21.0'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 3 | extends 'Lemonldap::NG::Portal::Auth::_WebForm'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 3 | has auth_id => ( is => 'ro', default => 'webauthn' ); | |||||||||||||||||||||||||||
| 20 | 3 | has type => ( is => 'ro', default => 'WebAuthn' ); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 3 | with 'Lemonldap::NG::Portal::Lib::WebAuthn'; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub initDisplay { | ||||||||||||||||||||||||||
|
Subroutine
initDisplay: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 25 | 0 | my ( $self, $req, $auto_start ) = @_; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 0 | my $request = $self->get_challenge($req); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | if ($request) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | my $data = to_json( { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | request => $request, | ||||||||||||||||||||||||||||
| 33 | webauthn_autostart => ( $auto_start ? \1 : \0 ) | ||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | $req->data->{customScript} .= <<EOF; | |||||||||||||||||||||||||||
| 38 | <script type="application/init"> | ||||||||||||||||||||||||||||
| 39 | $data | ||||||||||||||||||||||||||||
| 40 | </script> | ||||||||||||||||||||||||||||
| 41 | EOF | ||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | else { | ||||||||||||||||||||||||||||
| 44 | 0 | die "Could not generate WebAuthn challenge"; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | my $cacheTag = $self->p->cacheTag; | |||||||||||||||||||||||||||
| 48 | 0 | $req->data->{customScript} .= <<"EOF"; | |||||||||||||||||||||||||||
| 49 | <script type="text/javascript" src="$self->{p}->{staticPrefix}/common/js/webauthn-json.browser-global.min.js?v=$cacheTag"></script> | ||||||||||||||||||||||||||||
| 50 | <script type="text/javascript" src="$self->{p}->{staticPrefix}/common/js/webauthncheck.min.js?v=$cacheTag"></script> | ||||||||||||||||||||||||||||
| 51 | EOF | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # INITIALIZATION | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 2 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 57 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 2 | $self->authnLevel( $self->conf->{webauthnAuthnLevel} ); | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 2 | ▶ | unless ( $self->p->unAuthRoutes->{GET}->{webauthn} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 2 | $self->addUnauthRoute( | |||||||||||||||||||||||||||
| 63 | webauthn => 'init_challenge', | ||||||||||||||||||||||||||||
| 64 | ['POST'] | ||||||||||||||||||||||||||||
| 65 | ); | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # Used for session upgrade/reauthn | ||||||||||||||||||||||||||||
| 68 | 2 | $self->addAuthRoute( | |||||||||||||||||||||||||||
| 69 | webauthn => 'init_challenge', | ||||||||||||||||||||||||||||
| 70 | ['POST'] | ||||||||||||||||||||||||||||
| 71 | ); | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 2 | return $self->SUPER::init; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | sub auth_route { | ||||||||||||||||||||||||||
|
Subroutine
auth_route: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | 0 | my ( $self, $req ) = @_; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | sub get_challenge { | ||||||||||||||||||||||||||
|
Subroutine
get_challenge: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 82 | 0 | my ( $self, $req ) = @_; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | my $request = $self->generateDiscoverableChallenge($req); | |||||||||||||||||||||||||||
| 85 | 0 | ▶ | if ($request) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # Request is persisted on the server | ||||||||||||||||||||||||||||
| 88 | # | ||||||||||||||||||||||||||||
| 89 | 0 | my $webauthn_data = { authentication_options => $request, }; | |||||||||||||||||||||||||||
| 90 | 0 | ▶ | if ( my $token = $req->token ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | $self->ott->updateToken( $token, "webauthn", $webauthn_data ); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | else { | ||||||||||||||||||||||||||||
| 95 | 0 | $req->token( | |||||||||||||||||||||||||||
| 96 | $self->ott->createToken( { | ||||||||||||||||||||||||||||
| 97 | webauthn => $webauthn_data | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | ) | ||||||||||||||||||||||||||||
| 100 | ); | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | return $request; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 0 | return; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub extractFormInfo { | ||||||||||||||||||||||||||
|
Subroutine
extractFormInfo: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 109 | 0 | my ( $self, $req ) = @_; | |||||||||||||||||||||||||||
| 110 | 0 | ▶ | if ( my $credential = $req->param('credential') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
my $credential = $req->param("credential") and my $token = $req->param("token")
MC/DC0%0/2
my $credential = $req->param("credential") and my $token = $req->param("token")
my $credential = $req->param("credential")
my $token = $req->param("token")
Truth table0%0/3
A and B
A= my $credential = $req->param("credential")B= my $token = $req->param("token")
| |||||||||||||||||||||||||||||
| 111 | and my $token = $req->param('token') ) | ||||||||||||||||||||||||||||
| 112 | { | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | my $token = $self->ott->getToken($token); | |||||||||||||||||||||||||||
| 115 | 0 | ▶ | if ($token) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | $req->data->{webauthn_credential} = $credential; | |||||||||||||||||||||||||||
| 117 | $req->data->{webauthn_options} = | ||||||||||||||||||||||||||||
| 118 | 0 | $token->{webauthn}->{authentication_options}; | |||||||||||||||||||||||||||
| 119 | 0 | my $user_data = $self->getUserFromCredential( $req, $credential ); | |||||||||||||||||||||||||||
| 120 | 0 | ▶ | if ($user_data) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | $req->user( $user_data->{uid} ); | |||||||||||||||||||||||||||
| 122 | 0 | $req->data->{webauthn__2fDevices} = $user_data->{_2fDevices}; | |||||||||||||||||||||||||||
| 123 | $req->data->{webauthn__webAuthnUserHandle} = | ||||||||||||||||||||||||||||
| 124 | 0 | $user_data->{_webAuthnUserHandle}; | |||||||||||||||||||||||||||
| 125 | 0 | return PE_OK; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | else { | ||||||||||||||||||||||||||||
| 128 | 0 | return PE_WEBAUTHNFAILED; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | else { | ||||||||||||||||||||||||||||
| 132 | 0 | return PE_TOKENEXPIRED; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | else { | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | $self->initDisplay( $req, 0 ); | |||||||||||||||||||||||||||
| 138 | 0 | return PE_FIRSTACCESS; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | ▶ | sub authenticate { | ||||||||||||||||||||||||||
|
Subroutine
authenticate: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 143 | 0 | my ( $self, $req ) = @_; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | my $user = $req->user; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | my $authentication_options = $req->data->{webauthn_options}; | |||||||||||||||||||||||||||
| 148 | 0 | ▶ | if ( !$authentication_options ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 0 | $self->logger->error( | |||||||||||||||||||||||||||
| 150 | "WebAuthn: missing authentication options for $user"); | ||||||||||||||||||||||||||||
| 151 | 0 | return PE_WEBAUTHNFAILED; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | 0 | my $_2fDevices = $req->data->{webauthn__2fDevices}; | |||||||||||||||||||||||||||
| 154 | 0 | ▶ | if ( !$_2fDevices ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 0 | $self->logger->error("WebAuthn: no 2FA registrations for $user"); | |||||||||||||||||||||||||||
| 156 | 0 | return PE_WEBAUTHNFAILED; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | my $session_info = { | ||||||||||||||||||||||||||||
| 159 | _2fDevices => $_2fDevices, | ||||||||||||||||||||||||||||
| 160 | _webAuthnUserHandle => $req->data->{webauthn__webAuthnUserHandle}, | ||||||||||||||||||||||||||||
| 161 | 0 | }; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | my $credential_json = $req->data->{webauthn_credential}; | |||||||||||||||||||||||||||
| 164 | 0 | ▶ | if ( !$credential_json ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 0 | $self->logger->error("WebAuthn: missing credential for $user"); | |||||||||||||||||||||||||||
| 166 | 0 | return PE_WEBAUTHNFAILED; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 0 | my $validation_result = eval { | |||||||||||||||||||||||||||
| 170 | 0 | $self->validateAssertion( $req, $session_info, $authentication_options, | |||||||||||||||||||||||||||
| 171 | $credential_json ); | ||||||||||||||||||||||||||||
| 172 | }; | ||||||||||||||||||||||||||||
| 173 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 0 | $self->logger->error("WebAuthn: validation error for $user ($@)"); | |||||||||||||||||||||||||||
| 175 | 0 | return PE_WEBAUTHNFAILED; | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | 0 | return PE_OK; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | ▶ | sub authLogout { | ||||||||||||||||||||||||||
|
Subroutine
authLogout: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 181 | 0 | return PE_OK; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 0 | ▶ | sub setAuthSessionInfo { | ||||||||||||||||||||||||||
|
Subroutine
setAuthSessionInfo: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 185 | 0 | my ( $self, $req ) = @_; | |||||||||||||||||||||||||||
| 186 | 0 | $req->{sessionInfo}->{authenticationLevel} = $self->authnLevel; | |||||||||||||||||||||||||||
| 187 | 0 | return PE_OK; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 0 | ▶ | sub getDisplayType { | ||||||||||||||||||||||||||
|
Subroutine
getDisplayType: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 191 | 0 | return "webauthnform"; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | # Define which error codes will stop Combination process | ||||||||||||||||||||||||||||
| 195 | # @param res error code | ||||||||||||||||||||||||||||
| 196 | # @return result 1 if stop is needed | ||||||||||||||||||||||||||||
| 197 | 0 | ▶ | sub stop { | ||||||||||||||||||||||||||
|
Subroutine
stop: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 198 | 0 | my ( $self, $res ) = @_; | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 0 | ▶ | return 1 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | if ( $res == PE_FIRSTACCESS ); | ||||||||||||||||||||||||||||
| 202 | 0 | return 0; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 3 | 1; | |||||||||||||||||||||||||||