forked from vti/crafty
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
vti
committed
May 3, 2017
1 parent
fb0b2e5
commit 0a12061
Showing
5 changed files
with
162 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package Crafty::Middleware::User; | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use base 'Plack::Middleware'; | ||
|
||
use Plack::Util::Accessor qw( | ||
config | ||
); | ||
|
||
use MIME::Base64 (); | ||
use Plack::Session; | ||
use Crafty::Password; | ||
|
||
sub call { | ||
my $self = shift; | ||
my ($env) = @_; | ||
|
||
my $route = $env->{'crafty.route'}; | ||
|
||
my $username = $self->_username_from_session($env); | ||
$username //= $self->_username_from_basic_auth($env); | ||
|
||
$env->{'crafty.username'} = $username; | ||
|
||
return $self->app->($env); | ||
} | ||
|
||
sub _username_from_session { | ||
my $self = shift; | ||
my ($env) = @_; | ||
|
||
my $session = Plack::Session->new($env); | ||
return unless $session; | ||
|
||
my $username = $session->get('username'); | ||
return unless $username; | ||
|
||
return unless $self->config->user($username); | ||
|
||
return $username; | ||
} | ||
|
||
sub _username_from_basic_auth { | ||
my $self = shift; | ||
my ($env) = @_; | ||
|
||
return unless my $auth = $env->{HTTP_AUTHORIZATION}; | ||
return unless $auth =~ m/^Basic\s+(.*)$/i; | ||
|
||
my ($username, $password) = split /:/, (MIME::Base64::decode($1) || ":"), 2; | ||
|
||
return unless $username && $password; | ||
|
||
return unless my $user = $self->config->user($username); | ||
|
||
my $checker = Crafty::Password->new(hashing => $user->{hashing}, salt => $user->{salt}); | ||
return unless $checker->equals($username, $password, $user->{password}); | ||
|
||
return $username; | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use strict; | ||
use warnings; | ||
|
||
use Test::More; | ||
use Test::Deep; | ||
use TestSetup; | ||
|
||
use MIME::Base64 (); | ||
|
||
use_ok 'Crafty::Middleware::User'; | ||
|
||
subtest 'loads user from session' => sub { | ||
my $build = _build(); | ||
|
||
my $env = { 'psgix.session' => { username => 'username' } }; | ||
$build->call($env); | ||
|
||
is $env->{'crafty.username'}, 'username'; | ||
}; | ||
|
||
subtest 'does not load basic auth not valid' => sub { | ||
my $build = _build(); | ||
|
||
my $env = { | ||
'psgix.session' => {}, | ||
HTTP_AUTHORIZATION => 'Basic 123' | ||
}; | ||
|
||
$build->call($env); | ||
|
||
ok !$env->{'crafty.username'}; | ||
}; | ||
|
||
subtest 'does not load basic auth with unknown user' => sub { | ||
my $build = _build(); | ||
|
||
my $env = { | ||
'psgix.session' => {}, | ||
HTTP_AUTHORIZATION => 'Basic ' . MIME::Base64::encode_base64('unknown:wrong_password') | ||
}; | ||
|
||
$build->call($env); | ||
|
||
ok !$env->{'crafty.username'}; | ||
}; | ||
|
||
subtest 'does not load basic auth with wrong password' => sub { | ||
my $build = _build(); | ||
|
||
my $env = { | ||
'psgix.session' => {}, | ||
HTTP_AUTHORIZATION => 'Basic ' . MIME::Base64::encode_base64('username:wrong_password') | ||
}; | ||
|
||
$build->call($env); | ||
|
||
ok !$env->{'crafty.username'}; | ||
}; | ||
|
||
subtest 'loads user from basic auth' => sub { | ||
my $build = _build(); | ||
|
||
my $env = | ||
{ 'psgix.session' => {}, HTTP_AUTHORIZATION => 'Basic ' . MIME::Base64::encode_base64('username:password') }; | ||
|
||
$build->call($env); | ||
|
||
is $env->{'crafty.username'}, 'username'; | ||
}; | ||
|
||
done_testing; | ||
|
||
sub _build { | ||
return Crafty::Middleware::User->new( | ||
config => TestSetup->build_config, | ||
app => sub { [ 200, [], ['granted'] ] }, | ||
@_ | ||
); | ||
} |