« blib/lib/Lemonldap/NG/Portal/Main/Plugins.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/UserDB/Facebook.pm »
| 1 | package Lemonldap::NG::Portal::Auth::PAM; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Mouse; | ||
| 5 | use Authen::PAM; | ||
| 6 | use Lemonldap::NG::Portal::Main::Constants qw( | ||
| 7 | PE_BADCREDENTIALS | ||
| 8 | PE_ERROR | ||
| 9 | PE_OK | ||
| 10 | ); | ||
| 11 | |||
| 12 | extends qw(Lemonldap::NG::Portal::Auth::_WebForm); | ||
| 13 | |||
| 14 | our $VERSION = '2.0.12'; | ||
| 15 | |||
| 16 | # INITIALIZATION | ||
| 17 | |||
| 18 | has service => ( | ||
| 19 | is => 'rw', | ||
| 20 | lazy => 1, | ||
| 21 | default => sub { | ||
| 22 | return $_[0]->{conf}->{pamService} || 'login'; | ||
| 23 | } | ||
| 24 | ); | ||
| 25 | |||
| 26 | # RUNNING METHODS | ||
| 27 | |||
| 28 | sub authenticate { | ||
| 29 | my ( $self, $req ) = @_; | ||
| 30 | no strict 'subs'; | ||
| 31 | |||
| 32 | # Handler to dial with Authen::PAM | ||
| 33 | my $handler = sub { | ||
| 34 | my @response = (); | ||
| 35 | |||
| 36 | while (@_) { | ||
| 37 | my $code = shift; | ||
| 38 | my $msg = shift; | ||
| 39 | my $res; | ||
| 40 | |||
| 41 | if ( $code == PAM_PROMPT_ECHO_ON ) { | ||
| 42 | $res = $req->user; | ||
| 43 | } | ||
| 44 | |||
| 45 | elsif ( $code == PAM_PROMPT_ECHO_OFF ) { | ||
| 46 | $res = $req->data->{password}; | ||
| 47 | } | ||
| 48 | |||
| 49 | push( @response, PAM_SUCCESS, $res ); | ||
| 50 | } | ||
| 51 | |||
| 52 | return ( @response, PAM_SUCCESS ); | ||
| 53 | }; | ||
| 54 | |||
| 55 | # Launch PAM service | ||
| 56 | my $pam = Authen::PAM->new( $self->service, $req->user, $handler ); | ||
| 57 | unless ( ref $pam ) { | ||
| 58 | $self->logger->error( | ||
| 59 | 'PAM failed: ' . Authen::PAM->pam_strerror($pam) ); | ||
| 60 | return PE_ERROR; | ||
| 61 | } | ||
| 62 | |||
| 63 | # Check for authentication and authorization | ||
| 64 | foreach my $sub (qw(pam_authenticate pam_acct_mgmt)) { | ||
| 65 | my $res = $pam->$sub; | ||
| 66 | unless ( $res == PAM_SUCCESS ) { | ||
| 67 | $self->userLogger->warn( "PAM failed to authenticate $req->{user}: " | ||
| 68 | . $pam->pam_strerror($res) ); | ||
| 69 | $self->setSecurity($req); | ||
| 70 | return PE_BADCREDENTIALS; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | $self->userLogger->notice("Good PAM authentication for $req->{user}"); | ||
| 74 | return PE_OK; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub setAuthSessionInfo { | ||
| 78 | my ( $self, $req ) = @_; | ||
| 79 | $req->sessionInfo->{authenticationLevel} = $self->conf->{pamAuthnLevel}; | ||
| 80 | return PE_OK; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub authLogout { | ||
| 84 | return PE_OK; | ||
| 85 | } | ||
| 86 | |||
| 87 | 1; |