| 1 | package Catalyst::Authentication::Credential::RemoteHTTP; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Authenticate against remote HTTP server | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use MooseX::Types::Moose qw/Object/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use 5.008005; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Catalyst::Exception (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Catalyst::Authentication::Credential::RemoteHTTP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 3 | our $VERSION = '0.05'; # VERSION | |||||||||||||||||||||||||||
| 15 | 3 | our $AUTHORITY = 'cpan:NIGELM'; # AUTHORITY | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 3 | has realm => ( isa => Object, is => 'ro', required => 1 ); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 3 | has [qw/http_keep_alive defer_find_user/] => ( is => 'ro', default => 0 ); | |||||||||||||||||||||||||||
| 20 | 3 | has username_field => ( is => 'ro', default => 'username' ); | |||||||||||||||||||||||||||
| 21 | 3 | has password_field => ( is => 'ro', default => 'password' ); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 3 | has url => ( is => 'ro', required => 1 ); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 3 | has [qw/ user_prefix user_suffix /] => ( is => 'ro', default => '' ); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | sub BUILDARGS { | ||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 1 | my ( $class, $config, $app, $realm ) = @_; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 1 | $config->{realm} = $realm; | |||||||||||||||||||||||||||
| 31 | 1 | $config->{app} = $app; | |||||||||||||||||||||||||||
| 32 | 1 | $config->{class} = $class; | |||||||||||||||||||||||||||
| 33 | 1 | return $config; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 8 | ▶ | sub authenticate { | ||||||||||||||||||||||||||
|
Subroutine
authenticate: called CC=13 SCAR=30.4
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | 8 | my ( $self, $c, $realm, $authinfo ) = @_; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 8 | my $username = $authinfo->{ $self->username_field }; | |||||||||||||||||||||||||||
| 40 | 8 | ▶ | unless ( defined($username) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | $c->log->debug("No username supplied") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | if $c->debug; | ||||||||||||||||||||||||||||
| 43 | 0 | return; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | ## we remove the password_field before we pass it to the user | ||||||||||||||||||||||||||||
| 46 | ## routine, as some store modules use all data passed to them | ||||||||||||||||||||||||||||
| 47 | ## to find a matching user... | ||||||||||||||||||||||||||||
| 48 | 8 | my $userfindauthinfo = { %{$authinfo} }; | |||||||||||||||||||||||||||
| 49 | 8 | delete( $userfindauthinfo->{ $self->password_field } ); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 8 | my $user_obj; | |||||||||||||||||||||||||||
| 52 | 8 | ▶ | $user_obj = $realm->find_user( $userfindauthinfo, $c ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | unless ( $self->defer_find_user ); | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 8 | ▶ | if ( ref($user_obj) || $self->defer_find_user ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $user_obj or $self->defer_find_user
MC/DC0%0/2
ref $user_obj or $self->defer_find_user
ref $user_obj
$self->defer_find_user
Truth table33%1/3
A or B
A= ref $user_objB= $self->defer_find_user
| |||||||||||||||||||||||||||||
| 56 | 8 | ▶ | my $ua = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | Catalyst::Authentication::Credential::RemoteHTTP::UserAgent->new( | ||||||||||||||||||||||||||||
| 58 | keep_alive => $self->http_keep_alive ? 1 : 0 ); | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # add prefix/suffix to user data to make auth_user, get password | ||||||||||||||||||||||||||||
| 61 | 8 | my $auth_user = sprintf( '%s%s%s', $self->user_prefix, $username, $self->user_suffix ); | |||||||||||||||||||||||||||
| 62 | 8 | my $password = $authinfo->{ $self->password_field }; | |||||||||||||||||||||||||||
| 63 | 8 | $ua->set_credentials( $auth_user, $password ); | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # do the request | ||||||||||||||||||||||||||||
| 66 | 8 | my $res = $ua->head( $self->url ); | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # did it succeed | ||||||||||||||||||||||||||||
| 69 | 8 | ▶ | if ( $res->is_success ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # TODO: should we check here that it was actually authenticated? | ||||||||||||||||||||||||||||
| 72 | # this could be done by ensuring there is a request chain... | ||||||||||||||||||||||||||||
| 73 | 2 | ▶ | $c->log->debug( "remote http auth succeeded for user " . $auth_user ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | if $c->debug; | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | else { | ||||||||||||||||||||||||||||
| 77 | 6 | ▶ | $c->log->debug( "remote http auth FAILED for user " . $auth_user ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | if $c->debug; | ||||||||||||||||||||||||||||
| 79 | 6 | return; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # get the user object now, if deferred before | ||||||||||||||||||||||||||||
| 84 | 2 | ▶ | $user_obj = $realm->find_user( $userfindauthinfo, $c ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | if ( $self->defer_find_user ); | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # deal with no-such-user in store | ||||||||||||||||||||||||||||
| 88 | 2 | ▶ | unless ( ref($user_obj) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | $c->log->debug("Unable to locate user matching user info provided") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | if $c->debug; | ||||||||||||||||||||||||||||
| 91 | 0 | return; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | 2 | return $user_obj; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 3 | 1; # End of Catalyst::Authentication::Credential::RemoteHTTP | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | __END__ | ||||||||||||||||||||||||||||