Changeset 888


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
Location:
LATMOS-Accounts-Web
Files:
2 added
3 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 
  • LATMOS-Accounts-Web/lib/LATMOS/Accounts/Web/Controller/Root.pm

    r886 r888  
    44use warnings; 
    55use base 'Catalyst::Controller'; 
    6 use MIME::Base64; 
    76 
    87# 
     
    3736    my ( $self, $c ) = @_; 
    3837 
     38    require Data::Dumper; 
    3939    if ($c->user_exists) { 
    4040        # Set login user: 
    41         warn $c->model('Accounts')->db->{_user} = $c->user->{username}; 
     41        $c->model('Accounts')->db->{_user} = $c->user->{username}; 
    4242    } else { 
    4343        # No need to login for About section 
    44         if ($c->namespace ne 'about') { 
    45             if ($c->authenticate({}, 'remote')) { 
    46                 # Set login user: 
    47                 $c->model('Accounts')->db->{_user} = $c->user->{username}; 
    48             } elsif (my $authheader = $c->req->headers->header('Authorization')) { 
    49                 # TODO check auth type 
    50                 my ($type, $base64) = $authheader =~ /(\w+) (\S+)/; 
    51                 my ($login, $pass) = decode_base64($base64) =~ /^([^:]+):(.*)/; 
    52                 if (!$c->authenticate({  
    53                             username => $login, 
    54                             password => $pass }, 'la')) { 
    55                     $c->forward('auth_required'); 
    56                     return; 
    57                 } 
    58             } else { 
    59                 $c->forward('auth_required'); 
    60                 return; 
    61             } 
     44        if (!$c->authenticate({}, 'la')) { 
     45            $c->forward('auth_required'); 
     46            return; 
    6247        } 
    6348    } 
  • LATMOS-Accounts-Web/root/html/includes/header.tt

    r886 r888  
    178178        <span> 
    179179            [% luri = c.uri_for('/logout') %] 
    180             [% luri.userinfo('none:x') %] 
     180            [% luri.userinfo(c.user.username _ ':x') %] 
    181181            <a href="[% luri %]" 
    182182            onMouseOver="document.getElementById('menui').innerHTML='Se déconnecter';" 
Note: See TracChangeset for help on using the changeset viewer.