-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
3 changed files
with
184 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package AuthTestApp; | ||
use warnings; | ||
use strict; | ||
|
||
use Catalyst qw/ | ||
Authentication | ||
/; | ||
|
||
use Test::More; | ||
use Test::Exception; | ||
|
||
# this info needs to match that in TestWebServer | ||
our $members = { | ||
insecure => { password => '123456' }, | ||
paranoid => { password => 'very_secure_password!' } | ||
}; | ||
|
||
sub testnotworking : Local { | ||
my ( $self, $c ) = @_; | ||
|
||
ok( !$c->user, "no user" ); | ||
while ( my ( $user, $info ) = each %$members ) { | ||
ok( | ||
!$c->authenticate( | ||
{ username => $user, password => $info->{password} }, 'members' | ||
), | ||
"user $user authentication" | ||
); | ||
ok( | ||
!$c->authenticate( | ||
{ username => $user, password => 'wrong password' }, 'members' | ||
), | ||
"user $user authentication - wrong password" | ||
); | ||
} | ||
$c->res->body("ok"); | ||
} | ||
|
||
sub testworking : Local { | ||
my ( $self, $c ) = @_; | ||
|
||
ok( !$c->user, "no user" ); | ||
while ( my ( $user, $info ) = each %$members ) { | ||
ok( | ||
$c->authenticate( | ||
{ username => $user, password => $info->{password} }, 'members' | ||
), | ||
"user $user authentication" | ||
); | ||
ok( | ||
!$c->authenticate( | ||
{ username => $user, password => 'wrong password' }, 'members' | ||
), | ||
"user $user authentication - wrong password" | ||
); | ||
|
||
$c->logout; | ||
|
||
# sanity check | ||
ok( !$c->user, "no more user after logout" ); | ||
|
||
} | ||
$c->res->body("ok"); | ||
} | ||
|
||
__PACKAGE__->config->{'Plugin::Authentication'} = { | ||
default_realm => 'members', | ||
realms => { | ||
members => { | ||
credential => { | ||
class => 'RemoteHTTP', | ||
url => 'http://127.0.0.1:8080/stuff.html', | ||
}, | ||
store => { | ||
class => 'Minimal', | ||
users => $members | ||
} | ||
}, | ||
} | ||
}; | ||
|
||
__PACKAGE__->setup; |
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,72 @@ | ||
#!/usr/bin/perl | ||
# | ||
# This is a test web server used to check http authentication | ||
# It always requires basic authentication, with a fixed user/pass | ||
# All successful request return a short piece of text with details | ||
# of the request embedded.... | ||
# | ||
package TestWebServer; | ||
use base qw(HTTP::Server::Simple::CGI); | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use Carp; | ||
use MIME::Base64; | ||
|
||
# hash of usernames (keys) and passwords (values) | ||
my $user_set = { | ||
insecure => '123456', | ||
paranoid => 'very_secure_password!', | ||
}; | ||
|
||
# This next set of methods re-implements most of HTTP::Server::Simple::Authen | ||
# because that and its dependancies are not typically installed in | ||
# a normal catalyst installation | ||
sub do_authenticate { | ||
my $self = shift; | ||
|
||
if ( ( $ENV{HTTP_AUTHORIZATION} || '' ) =~ /^Basic (.*?)$/ ) { | ||
my ( $user, $pass ) = split /:/, ( MIME::Base64::decode($1) || ':' ); | ||
warn "user = $user, pass = $pass\n"; | ||
if ( exists( $user_set->{$user} ) && ( $user_set->{$user} eq $pass ) ) { | ||
return $user; | ||
} | ||
} | ||
|
||
return; | ||
} | ||
|
||
sub authen_realm { "Authorized area" } | ||
|
||
sub authenticate { | ||
my $self = shift; | ||
|
||
my $user = $self->do_authenticate(); | ||
unless ( defined $user ) { | ||
my $realm = $self->authen_realm(); | ||
print "HTTP/1.0 401\r\n"; | ||
print qq(WWW-Authenticate: Basic realm="$realm"\r\n\r\n); | ||
print "Authentication required."; | ||
return; | ||
} | ||
return $user; | ||
} | ||
|
||
sub handle_request { | ||
my ( $self, $cgi ) = @_; | ||
|
||
my $user = $self->authenticate or return; | ||
|
||
print( | ||
"HTTP/1.0 200 OK\r\n", | ||
$cgi->header, | ||
$cgi->start_html("Response"), | ||
$cgi->h1("Response"), | ||
$cgi->p( sprintf( 'Path is %s', $cgi->path_info() ) ), | ||
$cgi->p( sprintf( 'Authenticated as %s', $user ) ), | ||
$cgi->end_html | ||
); | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use strict; | ||
use warnings; | ||
|
||
use Test::More; | ||
|
||
BEGIN { | ||
plan skip_all => "HTTP::Server::Simple is required for this test" | ||
unless eval { require HTTP::Server::Simple }; | ||
plan skip_all => | ||
"Catalyst::Authentication::Store::Minimal is required for this test" | ||
unless eval { require Catalyst::Authentication::Store::Minimal }; | ||
plan "no_plan"; | ||
} | ||
|
||
use lib 't/lib'; | ||
use TestWebServer; | ||
use Catalyst::Test qw/AuthTestApp/; | ||
|
||
# this test should be run *without* the authenticating server | ||
ok( get("/testnotworking"), "get ok" ); | ||
|
||
my $pid = TestWebServer->new(8080)->background; | ||
ok( $pid, 'Start authenticating web server' ); | ||
sleep(1);# give it time to start | ||
|
||
# this test should be run *with* the authenticating server | ||
ok( get("/testworking"), "get ok" ); | ||
|
||
# and kill off the test web server | ||
kill 9, $pid; |