| 1 | package WebAPI::DBIC::Resource::Role::DBICAuth; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 25 | $WebAPI::DBIC::Resource::Role::DBICAuth::VERSION = '0.004002'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 25 | ▶ | use Carp qw(confess); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 25 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 25 | ▶ | use WebAPI::DBIC::Util qw(create_header); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 25 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 25 | requires 'set'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 25 | requires 'http_auth_type'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 0 | ▶ | sub connect_schema_as { # XXX sub rather than method? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
connect_schema_as: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | my ($self, $user, $pass) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | $_[2] = '...'; # hide password from stack trace | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | my $schema = $self->set->result_source->schema; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | my $ci = $schema->storage->connect_info; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | my ($ci_dsn, $ci_user, $ci_pass, $ci_attr) = @$ci; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | # ok if we're currently using the right auth | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | ▶ | return 1 if defined $ci_user and $user eq $ci_user | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/9
defined $ci_user and $user eq $ci_user and defined $ci_pass and $pass eq $ci_pass
defined $ci_user and $user eq $ci_user and defined $ci_pass
defined $ci_user and $user eq $ci_user
MC/DC0%0/4
defined $ci_user and $user eq $ci_user and defined $ci_pass and $pass eq $ci_pass
defined $ci_user
$user eq $ci_user
defined $ci_pass
$pass eq $ci_pass
Truth table0%0/5
A and B and C and D
A= defined $ci_userB= $user eq $ci_userC= defined $ci_passD= $pass eq $ci_pass
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | and defined $ci_pass and $pass eq $ci_pass; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | # try to connect with the user supplied credentials | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | my $newschema = $schema->clone->connect($ci_dsn, $user, $pass, $ci_attr); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | my $err; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | try { $newschema->storage->dbh } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | catch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # XXX we need to differentiate between auth errors and other problems | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | warn "Error connecting to $ci_dsn: $_\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | $err = $_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | return 0 if $err; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | # we connected ok, so update resultset to use new connection | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # XXX Is this sane and safe? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | $self->set->result_source->schema($newschema); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 146 | ▶ | sub is_authorized { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_authorized: called CC=11 SCAR=40.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 146 | my ($self, $auth_header) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 146 | ▶ | my $http_auth_type = $self->http_auth_type || ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->http_auth_type || ''
MC/DC0%0/1
$self->http_auth_type || ''
$self->http_auth_type
Truth table50%1/2
A
A= $self->http_auth_type
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 146 | ▶ | if ($http_auth_type =~ /^(none|disabled)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # This role was included in the resource, so auth was desired, yet auth | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | # has been specified. That seems worthy of a warning. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # 'none' gives a warning, but 'disabled' is silent. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 146 | (my $name = $self->request->path) =~ s:/\d+$::; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | warn "HTTP authentication configured but not enabled for $name\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | if $http_auth_type ne 'disabled' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 146 | ▶ | and not our $warn_once->{"http_auth_type $name"}++; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$http_auth_type ne "disabled" and not $our $warn_once{"http_auth_type $name"}++
MC/DC0%0/2
$http_auth_type ne "disabled" and not $our $warn_once{"http_auth_type $name"}++
$http_auth_type ne "disabled"
not $our $warn_once{"http_auth_type $name"}++
Truth table33%1/3
A and not(B)
A= $http_auth_type ne "disabled"B= not $our $warn_once{"http_auth_type $name"}++
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 146 | return 1 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | elsif ($http_auth_type eq 'Basic') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | # https://metacpan.org/pod/DBIx::Class::Storage::DBI#connect_info | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | my $ci = $self->set->result_source->schema->storage->connect_info; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # extract the dsn (doesn't handle $ci->[0] being a code ref) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | my $dsn = (ref $ci->[0]) ? $ci->[0]->{dsn} : $ci->[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | confess "Can't determine DSN to use as auth realm from @$ci" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $dsn or ref $dsn
MC/DC0%0/2
not $dsn or ref $dsn
not $dsn
ref $dsn
Truth table0%0/3
not(A) or B
A= not $dsnB= ref $dsn
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | if !$dsn or ref $dsn; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | my $auth_realm = "Insecure unless https! - $dsn"; # XXX get via a method | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | if ( $auth_header ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | return 1 if $self->connect_schema_as($auth_header->username, $auth_header->password); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | return create_header( 'WWWAuthenticate' => [ 'Basic' => ( realm => $auth_realm ) ] ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | die "Unsupported value for http_auth_type: $http_auth_type"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 25 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||