Ignore:
Timestamp:
01/26/12 16:14:06 (12 years ago)
Author:
nanardon
Message:
  • allow ssl login, match user using email address, this still allow basic authentication
File:
1 edited

Legend:

Unmodified
Added
Removed
  • LATMOS-Accounts-Web/lib/Catalyst/Authentication/Credential/La.pm

    r361 r888  
    33use strict; 
    44use warnings; 
     5use MIME::Base64; 
    56 
    67sub new { 
     
    910} 
    1011 
    11 sub authenticate { 
     12sub _search_user { 
     13    my ($self, $c, $attribute, $value) = @_; 
     14 
     15    my $base = $c->model('Accounts')->db; 
     16    # quick prefilter 
     17    my @res = $base->search_objects( 
     18        'user', 
     19        sprintf('%s~%s', $attribute, $value) 
     20    ); 
     21 
     22    @res = grep { 
     23        my $o = $base->get_object('user', $_); 
     24        $o && grep { lc($_) eq lc($value) } $o->get_attributes($attribute);     
     25    } @res; 
     26 
     27    if (@res == 1) { 
     28        return $res[0]; 
     29    } else { 
     30        $c->log->error(sprintf 
     31            "Can't identify `%s' user from `%s' attributes, multiple user found: %s", 
     32            $value, 
     33            $attribute, 
     34            join(', ', @res), 
     35        ); 
     36        return; 
     37    } 
     38} 
     39 
     40sub _ssl_auth { 
    1241    my ($self, $c, $realm, $authinfo) = @_; 
    13     return $realm->find_user($authinfo) 
     42    if ($ENV{HTTP_SSL_CLIENT_S_DN_EMAIL}) { 
     43        $c->log->debug(sprintf 
     44            'Trying to find user `%s\' from env `%s\'', 
     45            $ENV{HTTP_SSL_CLIENT_S_DN_EMAIL}, 
     46            'HTTP_SSL_CLIENT_S_DN_EMAIL' 
     47        ); 
     48        if (my $u = $self->_search_user($c, 'mail', $ENV{HTTP_SSL_CLIENT_S_DN_EMAIL})) { 
     49            $c->log->info(sprintf 'SSL auth for %s as %s', $ENV{HTTP_SSL_CLIENT_S_DN_EMAIL}, $u); 
     50            $authinfo->{username} = $u; 
     51            return 1; 
     52        } else { 
     53            return; 
     54        } 
     55    } 
     56} 
     57 
     58sub _login_auth { 
     59    my ($self, $c, $realm, $authinfo) = @_; 
     60    my $authheader = $c->req->headers->header('Authorization'); 
     61    # TODO check auth type 
     62    my ($type, $base64) = $authheader =~ /(\w+) (\S+)/; 
     63    ($authinfo->{username}, $authinfo->{password}) 
     64        = decode_base64($base64) =~ /^([^:]+):(.*)/; 
     65     
    1466    if($c->model('Accounts')->db->connect( 
    1567        $authinfo->{username}, 
    1668        $authinfo->{password} 
    17     )); 
    18     return; 
     69    )) { 
     70        $c->log->info(sprintf 'basic auth for %s', $authinfo->{username}); 
     71        return $realm->find_user($authinfo) 
     72    } else { 
     73        return; 
     74        $c->log->error(sprintf 
     75            'Invalid password or user for user %s', 
     76            $authinfo->{username} || 
     77            '(none)' 
     78        ); 
     79    } 
     80} 
     81 
     82sub authenticate { 
     83    my ($self, $c, $realm, $authinfo) = @_; 
     84    if (! ( 
     85        $self->_ssl_auth  ($c, $realm, $authinfo) || 
     86        $self->_login_auth($c, $realm, $authinfo) 
     87        )) { return; } 
     88 
     89    $c->model('Accounts')->db->{_user} = $authinfo->{username}; 
     90    return $realm->find_user($authinfo); 
    1991} 
    2092 
Note: See TracChangeset for help on using the changeset viewer.