« blib/lib/Lemonldap/NG/Portal/Plugins/SOAPServer.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/Password/DBI.pm »
| 1 | # Self Yubikey registration | ||
| 2 | package Lemonldap::NG::Portal::2F::Register::Yubikey; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use Mouse; | ||
| 6 | use JSON qw(from_json to_json); | ||
| 7 | use Lemonldap::NG::Portal::Main::Constants qw( | ||
| 8 | PE_FORMEMPTY | ||
| 9 | PE_ERROR | ||
| 10 | PE_OK | ||
| 11 | ); | ||
| 12 | |||
| 13 | our $VERSION = '2.19.0'; | ||
| 14 | |||
| 15 | extends 'Lemonldap::NG::Portal::2F::Register::Base'; | ||
| 16 | with 'Lemonldap::NG::Portal::Lib::2fDevices'; | ||
| 17 | |||
| 18 | # INITIALIZATION | ||
| 19 | |||
| 20 | has type => ( is => 'rw', default => 'UBK' ); | ||
| 21 | has prefix => ( is => 'rw', default => 'yubikey' ); | ||
| 22 | has logo => ( is => 'rw', default => 'yubikey.png' ); | ||
| 23 | has template => ( is => 'ro', default => 'yubikey2fregister' ); | ||
| 24 | has welcome => ( is => 'ro', default => 'clickOnYubikey' ); | ||
| 25 | |||
| 26 | # RUNNING METHODS | ||
| 27 | |||
| 28 | # Main method | ||
| 29 | |||
| 30 | use constant supportedActions => { | ||
| 31 | register => "register", | ||
| 32 | delete => "delete", | ||
| 33 | }; | ||
| 34 | |||
| 35 | sub register { | ||
| 36 | |||
| 37 | my ( $self, $req ) = @_; | ||
| 38 | my $user = $req->userData->{ $self->conf->{whatToTrace} }; | ||
| 39 | my $otp = $req->param('otp'); | ||
| 40 | my $UBKName = | ||
| 41 | $self->checkNameSfa( $req, $self->type, $req->param('UBKName') ); | ||
| 42 | return $self->failHtmlResponse( $req, 'badName' ) unless $UBKName; | ||
| 43 | |||
| 44 | if ( $otp | ||
| 45 | and length($otp) > $self->conf->{yubikey2fPublicIDSize} ) | ||
| 46 | { | ||
| 47 | my $key = substr( $otp, 0, $self->conf->{yubikey2fPublicIDSize} ); | ||
| 48 | |||
| 49 | # Read existing 2F device(s) | ||
| 50 | my @alldevices = $self->find2fDevicesByType( $req, $req->userData ); | ||
| 51 | |||
| 52 | # Search if the Yubikey is already registered | ||
| 53 | if ( grep { $_->{_yubikey} eq $key } @alldevices ) { | ||
| 54 | return $self->failHtmlResponse( $req, | ||
| 55 | 'yourKeyIsAlreadyRegistered' ); | ||
| 56 | } | ||
| 57 | |||
| 58 | my $res = $self->registerDevice( | ||
| 59 | $req, | ||
| 60 | $req->userData, | ||
| 61 | { | ||
| 62 | _yubikey => $key, | ||
| 63 | type => $self->type, | ||
| 64 | name => $UBKName, | ||
| 65 | epoch => time() | ||
| 66 | } | ||
| 67 | ); | ||
| 68 | if ( $res == PE_OK ) { | ||
| 69 | return [ | ||
| 70 | 302, | ||
| 71 | [ | ||
| 72 | Location => $self->p->buildUrl( | ||
| 73 | $req->portal, "2fregisters", { continue => 1 } | ||
| 74 | ) | ||
| 75 | ], | ||
| 76 | [] | ||
| 77 | ]; | ||
| 78 | } | ||
| 79 | else { | ||
| 80 | $self->logger->debug( $self->prefix . "2f: unable to add device" ); | ||
| 81 | return $self->failResponse( $req, "PE$res" ); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | else { | ||
| 85 | return $self->failHtmlResponse( $req, "PE2" ); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | 1; |