| 1 | package Catalyst::Plugin::Authentication::Credential::PAM; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 0 | ▶ | use Authen::PAM qw/:constants/; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use Scalar::Util qw/blessed/; | ||
| 7 | |||
| 8 | our $VERSION = '0.01'; | ||
| 9 | |||
| 10 | sub login { | ||
| 11 | my ($c, $user, $password) = @_; | ||
| 12 | unless ($user && defined $password) { | ||
| 13 | $c->log->debug("Can't login a user without a username and a password") if $c->debug; | ||
| 14 | return 0; | ||
| 15 | } | ||
| 16 | unless (blessed($user) && $user->isa("Catalyst::Plugin::Authentication::User")) { | ||
| 17 | if (my $user_obj = $c->get_user($user, $password)) { | ||
| 18 | $user = $user_obj; | ||
| 19 | } | ||
| 20 | else { | ||
| 21 | $c->log->debug("User '$user' doesn't exist in the default store") if $c->debug; | ||
| 22 | return 0; | ||
| 23 | } | ||
| 24 | } | ||
| 25 | if ($c->_check_password($user->id, $password)) { | ||
| 26 | $c->set_authenticated($user); | ||
| 27 | return 1; | ||
| 28 | } | ||
| 29 | else { | ||
| 30 | return 0; | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | sub _check_password { | ||
| 35 | my ($c, $username, $password) = @_; | ||
| 36 | my $service = $c->config->{authentication}{pam}{service} || 'login'; | ||
| 37 | my $handler = sub { | ||
| 38 | my @response = (); | ||
| 39 | while (@_) { | ||
| 40 | my $code = shift; | ||
| 41 | my $message = shift; | ||
| 42 | my $answer; | ||
| 43 | if ($code == PAM_PROMPT_ECHO_ON) { | ||
| 44 | $answer = $username; | ||
| 45 | } | ||
| 46 | elsif ($code == PAM_PROMPT_ECHO_OFF) { | ||
| 47 | $answer = $password; | ||
| 48 | } | ||
| 49 | push(@response, PAM_SUCCESS, $answer); | ||
| 50 | } | ||
| 51 | return (@response, PAM_SUCCESS); | ||
| 52 | }; | ||
| 53 | my $pam = Authen::PAM->new($service, $username, $handler); | ||
| 54 | $pam or do { | ||
| 55 | $c->_log_debug($username, $service, Authen::PAM->pam_strerror($pam)); | ||
| 56 | return 0; | ||
| 57 | }; | ||
| 58 | my $result = $pam->pam_authenticate; | ||
| 59 | unless ($result == PAM_SUCCESS) { | ||
| 60 | $c->_log_debug($username, $service, $pam->pam_strerror($result)); | ||
| 61 | return 0; | ||
| 62 | } | ||
| 63 | $result = $pam->pam_acct_mgmt; | ||
| 64 | unless ($result == PAM_SUCCESS) { | ||
| 65 | $c->_log_debug($username, $service, $pam->pam_strerror($result)); | ||
| 66 | return 0; | ||
| 67 | } | ||
| 68 | $c->log->debug(qq/Successfully authenticated user '$username' using service '$service'./) if $c->debug; | ||
| 69 | return 1; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub _log_debug { | ||
| 73 | my ($c, $username, $service, $error) = @_; | ||
| 74 | $c->log->debug(qq/Failed to authenticate user '$username' using service '$service'. Reason: '$error'/) if $c->debug; | ||
| 75 | } | ||
| 76 | |||
| 77 | 1; | ||
| 78 | |||
| 79 | __END__ | ||