2017-02-04 08:55:47 +01:00
|
|
|
# Self U2F registration
|
2017-02-02 22:48:32 +01:00
|
|
|
package Lemonldap::NG::Portal::Register::U2F;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use Mouse;
|
|
|
|
|
|
|
|
our $VERSION = '2.0.0';
|
|
|
|
|
2017-02-04 08:55:47 +01:00
|
|
|
extends 'Lemonldap::NG::Portal::Lib::U2F';
|
2017-02-02 22:48:32 +01:00
|
|
|
|
2017-02-04 08:55:47 +01:00
|
|
|
# INITIALIZATION
|
2017-02-03 18:14:13 +01:00
|
|
|
|
2017-02-02 22:48:32 +01:00
|
|
|
sub init {
|
|
|
|
my ($self) = @_;
|
2017-02-04 08:55:47 +01:00
|
|
|
return 0 unless $self->SUPER::init;
|
2017-02-08 14:01:02 +01:00
|
|
|
$self->addAuthRoute( u2fregister => { ':action' => 'run' }, ['POST'] );
|
|
|
|
$self->addAuthRoute( 'u2fregister.html' => undef, ['GET'] );
|
2017-02-02 22:48:32 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-02-04 08:55:47 +01:00
|
|
|
# RUNNING METHODS
|
|
|
|
|
|
|
|
# Main method
|
2017-02-02 22:48:32 +01:00
|
|
|
sub run {
|
|
|
|
my ( $self, $req ) = @_;
|
2017-02-08 14:01:02 +01:00
|
|
|
my $action = $req->param('action');
|
2017-02-02 22:48:32 +01:00
|
|
|
|
2017-02-08 14:01:02 +01:00
|
|
|
if ( $action eq 'register' ) {
|
|
|
|
my $challenge = $self->crypter->registrationChallenge;
|
|
|
|
return [ 200, [ 'Content-Type' => 'application/json' ], [$challenge] ];
|
|
|
|
}
|
|
|
|
if ( $action eq 'registration' ) {
|
|
|
|
my $resp;
|
|
|
|
unless ( $resp = $req->param('registration') ) {
|
|
|
|
return $self->p->sendError( $req, 'Missing registration parameter',
|
|
|
|
400 );
|
|
|
|
}
|
|
|
|
$self->lmLog( "Get registration data $resp", 'debug' );
|
|
|
|
my ( $keyHandle, $userKey ) = $self->crypter->registrationVerify($resp);
|
2017-02-03 07:23:39 +01:00
|
|
|
if ( $keyHandle and $userKey ) {
|
2017-02-04 08:55:47 +01:00
|
|
|
$self->p->updatePersistentSession(
|
|
|
|
$req,
|
|
|
|
{
|
2017-02-08 19:10:06 +01:00
|
|
|
_u2fKeyHandle => $self->encode_base64url( $keyHandle, '' ),
|
|
|
|
_u2fUserKey => $self->encode_base64url( $userKey, '' )
|
2017-02-07 23:04:49 +01:00
|
|
|
}
|
|
|
|
);
|
2017-02-08 14:01:02 +01:00
|
|
|
return [
|
|
|
|
200, [ 'Content-Type' => 'application/json' ],
|
|
|
|
['{"result":1}']
|
|
|
|
];
|
2017-02-03 07:23:39 +01:00
|
|
|
}
|
2017-02-08 14:01:02 +01:00
|
|
|
my $err = Crypt::U2F::Server::Simple::lastError();
|
|
|
|
$self->p->userError("U2F Registration failed: $err");
|
|
|
|
return $self->p->sendError( $req, $err, 200 );
|
2017-02-02 22:48:32 +01:00
|
|
|
}
|
2017-02-08 14:01:02 +01:00
|
|
|
if ( $action eq 'verify' ) {
|
|
|
|
my ( $err, $error ) = $self->loadUser($req);
|
|
|
|
if ( $err == -1 ) {
|
|
|
|
return $self->p->sendError( $req, "U2F error: $error", 200 );
|
|
|
|
}
|
|
|
|
elsif ( $err == 0 ) {
|
|
|
|
return $self->p->sendError( $req, "noU2FKeyFound" );
|
|
|
|
}
|
|
|
|
my $challenge = $self->crypter->authenticationChallenge;
|
|
|
|
return [ 200, [ 'Content-Type' => 'application/json' ], [$challenge] ];
|
|
|
|
}
|
|
|
|
if ( $action eq 'signature' ) {
|
|
|
|
my $resp;
|
|
|
|
unless ( $resp = $req->param('signature') ) {
|
|
|
|
return $self->p->sendError( $req, 'Missing signature parameter',
|
|
|
|
400 );
|
|
|
|
}
|
|
|
|
my ( $err, $error ) = $self->loadUser($req);
|
|
|
|
if ( $err == -1 ) {
|
|
|
|
return $self->p->sendError( $req, "U2F error: $error", 200 );
|
2017-02-07 23:04:49 +01:00
|
|
|
}
|
2017-02-08 14:01:02 +01:00
|
|
|
elsif ( $err == 0 ) {
|
|
|
|
return $self->p->sendError( $req, "noU2FKeyFound" );
|
|
|
|
}
|
|
|
|
my $res = ( $self->crypter->authenticationVerify($resp) ? 1 : 0 );
|
|
|
|
return [
|
|
|
|
200, [ 'Content-Type' => 'application/json' ],
|
|
|
|
[qq'{"result":$res}']
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub loadUser {
|
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
my $uid = $req->userData->{ $self->conf->{whatToTrace} };
|
2017-02-08 19:10:06 +01:00
|
|
|
my $session = $self->p->getPersistentSession($uid);
|
2017-02-08 14:01:02 +01:00
|
|
|
my $kh = $session->data->{_u2fKeyHandle};
|
|
|
|
my $uk = $session->data->{_u2fUserKey};
|
|
|
|
unless ( $kh and $uk ) {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-02-08 19:10:06 +01:00
|
|
|
$self->crypter->{keyHandle} = $self->decode_base64url($kh);
|
|
|
|
$self->crypter->{publicKey} = $self->decode_base64url($uk);
|
2017-02-08 14:01:02 +01:00
|
|
|
unless ( $self->crypter->setKeyHandle and $self->crypter->setPublicKey ) {
|
|
|
|
my $error = Crypt::U2F::Server::Simple::lastError();
|
|
|
|
return ( -1, $error );
|
|
|
|
}
|
|
|
|
return 1;
|
2017-02-02 22:48:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|