« blib/lib/Lemonldap/NG/Portal/Plugins/LocationDetect.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/2F/REST.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.23.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 | modify => "modify", | ||
| 33 | delete => "delete", | ||
| 34 | }; | ||
| 35 | |||
| 36 | sub register { | ||
| 37 | |||
| 38 | my ( $self, $req ) = @_; | ||
| 39 | my $user = $req->userData->{ $self->conf->{whatToTrace} }; | ||
| 40 | my $otp = $req->param('otp'); | ||
| 41 | my $UBKName = | ||
| 42 | $self->checkNameSfa( $req, $self->type, $req->param('UBKName') ); | ||
| 43 | return $self->failHtmlResponse( $req, 'badName' ) unless $UBKName; | ||
| 44 | |||
| 45 | if ( $otp | ||
| 46 | and length($otp) > $self->conf->{yubikey2fPublicIDSize} ) | ||
| 47 | { | ||
| 48 | my $key = substr( $otp, 0, $self->conf->{yubikey2fPublicIDSize} ); | ||
| 49 | |||
| 50 | # Read existing 2F device(s) | ||
| 51 | my @alldevices = $self->find2fDevicesByType( $req, $req->userData ); | ||
| 52 | |||
| 53 | # Search if the Yubikey is already registered | ||
| 54 | if ( grep { $_->{_yubikey} eq $key } @alldevices ) { | ||
| 55 | return $self->failHtmlResponse( $req, | ||
| 56 | 'yourKeyIsAlreadyRegistered' ); | ||
| 57 | } | ||
| 58 | |||
| 59 | my $res = $self->registerDevice( | ||
| 60 | $req, | ||
| 61 | $req->userData, | ||
| 62 | { | ||
| 63 | _yubikey => $key, | ||
| 64 | type => $self->type, | ||
| 65 | name => $UBKName, | ||
| 66 | epoch => time() | ||
| 67 | } | ||
| 68 | ); | ||
| 69 | if ( $res == PE_OK ) { | ||
| 70 | return $self->p->sendRedirection( | ||
| 71 | $req, | ||
| 72 | $self->p->buildUrl( | ||
| 73 | $req->portal, "2fregisters", { continue => 1 } | ||
| 74 | ) | ||
| 75 | ); | ||
| 76 | } | ||
| 77 | else { | ||
| 78 | $self->logger->debug( $self->prefix . "2f: unable to add device" ); | ||
| 79 | return $self->failResponse( $req, $res ); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | else { | ||
| 83 | return $self->failHtmlResponse( $req, "PE2" ); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | 1; |