REST.pm 1.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
package Lemonldap::NG::Portal::Auth::REST;

use strict;
use Mouse;
use Lemonldap::NG::Portal::Main::Constants qw(
  PE_ERROR
  PE_BADCREDENTIALS
  PE_OK
);

Christophe Maudoux's avatar
Christophe Maudoux committed
11
our $VERSION = '2.0.3';
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

extends 'Lemonldap::NG::Portal::Auth::_WebForm',
  'Lemonldap::NG::Portal::Lib::REST';

# INITIALIZATION

sub init {
    my $self = shift;

    # Add warning in log
    unless ( $self->conf->{restAuthUrl} ) {
        $self->logger->error('No Auth REST URL given');
        return 0;
    }

    return $self->Lemonldap::NG::Portal::Auth::_WebForm::init();
}

sub authenticate {
    my ( $self, $req ) = @_;
    my $res = eval {
        $self->restCall( $self->conf->{restAuthUrl},
Xavier Guimard's avatar
Xavier Guimard committed
34
            { user => $req->user, password => $req->data->{password} } );
35 36 37
    };
    if ($@) {
        $self->logger("Auth error: $@");
38
        $self->setSecurity($req);
39 40
        return PE_ERROR;
    }
Xavier Guimard's avatar
Xavier Guimard committed
41 42 43 44 45 46 47 48
    $self->logger->debug( "REST result:" . ( $res->{result} || 'undef' ) );
    if ( $res->{info} ) {
        eval {
            $self->logger->debug(" $_ => $res->{info}->{$_}")
              foreach ( keys %{ $res->{info} } );
        };
    }
    $self->logger->error( 'No "info": ' . $@ ) if ($@);
49 50 51
    unless ( $res->{result} ) {
        $self->userLogger->warn(
            "Bad credentials for " . $req->user . ' (' . $req->address . ')' );
52
        $self->setSecurity($req);
53 54
        return PE_BADCREDENTIALS;
    }
Xavier Guimard's avatar
Xavier Guimard committed
55
    $req->data->{restAuthInfo} = $res->{info} || {};
56 57 58 59 60 61
    return PE_OK;
}

sub setAuthSessionInfo {
    my ( $self, $req ) = @_;
    $self->SUPER::setAuthSessionInfo($req);
Xavier Guimard's avatar
Xavier Guimard committed
62 63
    $req->sessionInfo->{$_} = $req->data->{restAuthInfo}->{$_}
      foreach ( keys %{ $req->data->{restAuthInfo} } );
Christophe Maudoux's avatar
Christophe Maudoux committed
64
    $req->sessionInfo->{authenticationLevel} = $self->conf->{restAuthnLevel};
65 66 67 68 69 70 71 72
    return PE_OK;
}

sub authLogout {
    PE_OK;
}

1;