Changeset 114 in bookmarks


Ignore:
Timestamp:
02/18/16 17:16:31 (8 years ago)
Author:
peter
Message:

Store user credentials in an external htdigest file.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/BookmarksApp.pm

    r112 r114  
    116116    }; 
    117117 
     118    # if configured for auth, read in the htdigest database file 
     119    # and store the password hashes keyed by username 
     120    my %password_hash_for; 
     121    if ($config->{auth}) { 
     122        $config->{realm} ||= 'Bookmarks'; 
     123        $config->{htdigest} or die "No htdigest configured for authentication\n"; 
     124        open my $htdigest, '<', $config->{htdigest} or die "Can't open $$config{htdigest}\n"; 
     125        while (my $credentials = <$htdigest>) { 
     126            chomp $credentials; 
     127            my ($username, $realm, $password_hash) = split /:/, $credentials; 
     128            # only add password digests for the configured realm 
     129            if ($realm eq $config->{realm}) { 
     130                $password_hash_for{$username} = $password_hash; 
     131            } 
     132        } 
     133        close $htdigest; 
     134    } 
     135 
    118136    $self->_app( 
    119137        builder { 
     
    121139                if $config->{proxy_ip}; 
    122140            enable_if { $_[0]->{REQUEST_METHOD} ne 'GET' } 'Auth::Digest', ( 
    123                 realm           => 'Bookmarks', 
     141                realm           => $config->{realm}, 
    124142                secret          => $config->{digest_key}, 
    125143                password_hashed => 1, 
    126                 authenticator   => sub { $config->{digest_password} } 
     144                authenticator   => sub { 
     145                    my ($username, $env) = @_; 
     146                    return $password_hash_for{$username}; 
     147                }, 
    127148            ) if $config->{auth}; 
    128149            sub { $router->dispatch(shift); }; 
Note: See TracChangeset for help on using the changeset viewer.