SecondFactor.pm 6.55 KB
Newer Older
Yadd's avatar
Yadd committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
package Lemonldap::NG::Portal::Main::SecondFactor;

use strict;
use Mouse;
use Lemonldap::NG::Portal::Main::Constants qw(
  PE_OK
  PE_NOTOKEN
  PE_TOKENEXPIRED
);

our $VERSION = '2.0.0';

extends 'Lemonldap::NG::Portal::Main::Plugin';

# INTERFACE

sub afterDatas { '_run' }

# INITIALIZATION

has ott => (
    is      => 'rw',
    default => sub {
        my $ott =
          $_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken');
        $ott->timeout( $_[0]->{conf}->{formTimeout} );
        return $ott;
    }
);

has rule => ( is => 'rw' );

has prefix => ( is => 'rw' );

sub init {
    my ($self) = @_;
Yadd's avatar
Yadd committed
37 38
    $self->addUnauthRoute( $self->prefix . '2fcheck' => '_verify',   ['POST'] );
    $self->addUnauthRoute( $self->prefix . '2fcheck' => '_redirect', ['GET'] );
Yadd's avatar
Yadd committed
39 40 41 42 43 44 45 46 47 48 49
    my $rule = $self->conf->{ $self->prefix . '2fActivation' };
    $rule = $self->p->HANDLER->substitute($rule);
    unless ( $rule = $self->p->HANDLER->buildSub($rule) ) {
        $self->error( 'External 2F rule error: '
              . $self->p->HANDLER->tsv->{jail}->error );
        return 0;
    }
    $self->rule($rule);
    1;
}

Yadd's avatar
Yadd committed
50 51 52 53 54 55 56 57
sub _redirect {
    my ( $self, $req ) = @_;
    my $arg = $req->env->{QUERY_STRING};
    return [
        302, [ Location => $self->conf->{portal} . ( $arg ? "?$arg" : '' ) ], []
    ];
}

Yadd's avatar
Yadd committed
58 59
sub _run {
    my ( $self, $req ) = @_;
60
    return PE_OK unless ( $self->rule->( $req, $req->sessionInfo ) );
Yadd's avatar
Yadd committed
61 62 63 64 65
    $self->userLogger->info( 'Second factor required ('
          . $self->prefix
          . ') for '
          . $req->sessionInfo->{ $self->conf->{whatToTrace} } );
    $req->sessionInfo->{_2fRealSession} = $req->id;
Yadd's avatar
Yadd committed
66
    $req->sessionInfo->{_2fUrldc}       = $req->urldc;
Yadd's avatar
Yadd committed
67 68 69 70
    my $token = $self->ott->createToken( $req->sessionInfo );
    $req->id(0);
    $self->p->rebuildCookies($req);
    my $res = $self->run( $req, $token );
71
    delete $req->{authResult} if ($res);
Yadd's avatar
Yadd committed
72 73 74 75 76 77 78 79 80 81
    return $res;
}

sub _verify {
    my ( $self, $req ) = @_;

    # Check token
    my $token;
    unless ( $token = $req->param('token') ) {
        $self->userLogger->error( $self->prefix . ' 2F access without token' );
Yadd's avatar
Yadd committed
82
        $req->mustRedirect(1);
Yadd's avatar
Yadd committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96
        return $self->p->do( $req, [ sub { PE_NOTOKEN } ] );
    }

    my $session;
    unless ( $session = $self->ott->getToken($token) ) {
        $self->userLogger->info('Token expired');
        return $self->p->do( $req, [ sub { PE_TOKENEXPIRED } ] );
    }

    # Launch second factor verification
    my $res = $self->verify( $req, $session );

    # Case error
    if ($res) {
Yadd's avatar
Yadd committed
97
        $req->noLoginDisplay(1);
Yadd's avatar
Yadd committed
98 99 100 101 102 103
        return $self->p->do( $req, [ sub { $res } ] );
    }

    # Else restore session
    $req->sessionInfo($session);
    $req->id( delete $req->sessionInfo->{_2fRealSession} );
Yadd's avatar
Yadd committed
104
    $req->urldc( delete $req->sessionInfo->{_2fUrldc} );
Yadd's avatar
Yadd committed
105 106 107 108 109 110 111 112 113 114 115 116
    $self->p->rebuildCookies($req);
    $req->mustRedirect(1);
    $self->userLogger->notice( $self->prefix
          . '2F verification for '
          . $req->sessionInfo->{ $self->conf->{whatToTrace} } );
    if ( my $l = $self->conf->{ $self->prefix . '2fAuthnLevel' } ) {
        $self->p->updateSession( $req, { authenticationLevel => $l } );
    }
    return $self->p->do( $req, [ sub { PE_OK } ] );
}

1;
Yadd's avatar
Yadd committed
117 118 119 120 121 122 123 124 125 126 127 128 129
__END__

=pod

=encoding utf8

=head1 NAME

Lemonldap::NG::Portal::Main::SecondFactor - Base class for
L<Lemonldap::NG::Portal> second factor plugins.

=head1 SYNOPSIS

Yadd's avatar
Yadd committed
130
  package Lemonldap::NG::Portal::2F::MySecondFactor;
Yadd's avatar
Yadd committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
  use Mouse;
  use Lemonldap::NG::Portal::Main::Constants qw(
    PE_OK
    PE_BADCREDENTIALS
    PE_SENDRESPONSE
  );
  extends 'Lemonldap::NG::Portal::Main::SecondFactor';
  
  # Prefix that will be used in parameter names. The form used to enter the
  # second factor must post its result to "/my2fcheck" (if "my" is the prefix).
  has prefix => ( is => 'ro', default => 'my' );
  
  sub init {
      my ($self) = @_;
      # Insert here initialization process
      return $self->SUPER::init();
  }
  # Required 2nd factor send method
  sub run {
      my ( $self, $req, $token ) = @_;
      # $token must be inserted in a hidden input in your form with the name
      # "token"
      ...
      # A LLNG constant must be returned. Example
      $req->response($my_html_form_page)
      return PE_SENDRESPONSE;
  }
  sub verify {
      my ( $self, $req, $session ) = @_;
      # Use $req->param('field') to get POST responses
      ...
      if($req->param('result') eq $goodResult) {
        return PE_OK;
      }
      else {
        return PE_BADCREDENTIALS
      }
  }

Enable your plugin in lemonldap-ng.ini, section [portal]:

=over

=item <prefix>2fActivation (required): 1, 0 or a rule

=item <prefix>2fAuthnLevel (optional): change authentication level for users
authenticated by this plugin

=back

Example:

  [portal]
Yadd's avatar
Yadd committed
184
  customPlugins = Lemonldap::NG::Portal::2F::MyPlugin
Yadd's avatar
Yadd committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
  my2fActivation = 1
  my2fAuthnLevel = 4

=head1 DESCRIPTION

Lemonldap::NG::Portal::Main::SecondFactor provides a simple framework to build
Lemonldap::NG second authentication factor plugin.

See Lemonldap::NG::Portal::Plugins::External2F or
Lemonldap::NG::Portal::Plugins::U2F for examples.

=head1 SEE ALSO

L<http://lemonldap-ng.org>

=head2 OTHER POD FILES

=over

Yadd's avatar
Yadd committed
204 205 206 207 208
=item Writing an authentication module: L<Lemonldap::NG::Portal::Auth>

=item Writing a UserDB module: L<Lemonldap::NG::Portal::UserDB>

=item Writing a second factor module: L<Lemonldap::NG::Portal::Main::SecondFactor>
Yadd's avatar
Yadd committed
209 210 211

=item Writing an issuer module: L<Lemonldap::NG::Portal::Main::Issuer>

Yadd's avatar
Yadd committed
212 213
=item Writing an other plugin: L<Lemonldap::NG::Portal::Main::Plugin>

Yadd's avatar
Yadd committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
=item Request object: L<Lemonldap::NG::Portal::Main::Request>

=item Adding parameters in the manager: L<Lemonldap::NG::Manager::Build>

=back

=head1 AUTHORS

=over

=item LemonLDAP::NG team L<http://lemonldap-ng.org/team>

=back

=head1 BUG REPORT

Use OW2 system to report bug or ask for features:
Yadd's avatar
Yadd committed
231
L<https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues>
Yadd's avatar
Yadd committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

=head1 DOWNLOAD

Lemonldap::NG is available at
L<http://forge.objectweb.org/project/showfiles.php?group_id=274>

=head1 COPYRIGHT AND LICENSE

See COPYING file for details.

This library is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see L<http://www.gnu.org/licenses/>.

=cut