| 1 | package Clustericious::Plugin::PlugAuth; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use Clustericious::Log; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use Mojo::ByteStream qw/b/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use Mojo::URL; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 8 | ▶ | use JSON::MaybeXS qw( encode_json ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # ABSTRACT: Plugin for clustericious to use PlugAuth. | ||||||||||||||||||||||||||||
| 12 | 8 | our $VERSION = '1.29'; # VERSION | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 8 | has 'config_url'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 7 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 7 | my ($self, $app, $conf) = @_; | |||||||||||||||||||||||||||
| 19 | 7 | my $url = eval { $conf->{plug_auth}->url(default => '') }; | |||||||||||||||||||||||||||
| 20 | 7 | ▶ | $url =~ s{/$}{} if defined $url; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 7 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 0 | WARN "unable to determine PlugAuth URL: $@"; | |||||||||||||||||||||||||||
| 23 | 0 | return $self; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | 7 | $self->config_url($url); | |||||||||||||||||||||||||||
| 26 | 7 | $self; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | sub authenticate { | ||||||||||||||||||||||||||
|
Subroutine
authenticate: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 32 | 0 | my $c = shift; | |||||||||||||||||||||||||||
| 33 | 0 | my $realm = shift; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | TRACE ("Authenticating for realm $realm"); | |||||||||||||||||||||||||||
| 36 | # Everyone needs to send an authorization header | ||||||||||||||||||||||||||||
| 37 | 0 | ▶ | my $auth = $c->req->headers->authorization or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | $c->res->headers->www_authenticate(qq[Basic realm="$realm"]); | |||||||||||||||||||||||||||
| 39 | 0 | $c->render(text => "auth required", layout => "", status => 401); | |||||||||||||||||||||||||||
| 40 | 0 | return; | |||||||||||||||||||||||||||
| 41 | }; | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | my $config_url = $self->config_url; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | my ($method,$str) = split / /,$auth; | |||||||||||||||||||||||||||
| 46 | 0 | my $userinfo = b($str)->b64_decode; | |||||||||||||||||||||||||||
| 47 | 0 | my ($user,$pw) = split /:/, $userinfo; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | my $self_plug_auth = 0; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # VIP treatment for some hosts | ||||||||||||||||||||||||||||
| 52 | 0 | my $ip = $c->tx->remote_address; | |||||||||||||||||||||||||||
| 53 | 0 | ▶ | $c->ua->get("$config_url/host/$ip/trusted", sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 54 | 0 | my $ua = shift; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | do { | |||||||||||||||||||||||||||
| 57 | 0 | my $tx = shift; | |||||||||||||||||||||||||||
| 58 | 0 | ▶ | if ( my $res = $tx->success ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | if ( $res->code == 200 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 0 | TRACE "Host $ip is trusted, not authenticating"; | |||||||||||||||||||||||||||
| 61 | 0 | $c->stash( user => $user ); | |||||||||||||||||||||||||||
| 62 | 0 | $c->continue; | |||||||||||||||||||||||||||
| 63 | 0 | return; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | else { | ||||||||||||||||||||||||||||
| 66 | 0 | WARN "PlugAuth returned code " . $res->code; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | } else { | ||||||||||||||||||||||||||||
| 69 | 0 | my $error = $tx->error; | |||||||||||||||||||||||||||
| 70 | 0 | ▶ | if ($error->{code}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 0 | TRACE "Not VIP $config_url/host/$ip/trusted : @{[ $error->{code} ]} @{[ $error->{message} ]}"; | |||||||||||||||||||||||||||
| 72 | } else { | ||||||||||||||||||||||||||||
| 73 | 0 | WARN "Error connecting to PlugAuth at $config_url/host/$ip/trusted : @{[ encode_json $error ]}"; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | }; | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # Everyone else get in line | ||||||||||||||||||||||||||||
| 79 | 0 | my $auth_url = Mojo::URL->new("$config_url/auth"); | |||||||||||||||||||||||||||
| 80 | 0 | $auth_url->userinfo($userinfo); | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | my $check; | |||||||||||||||||||||||||||
| 83 | my $res; | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | $ua->head($auth_url, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | my($ua, $tx) = @_; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | $res = $tx->res; | |||||||||||||||||||||||||||
| 90 | 0 | $check = $res->code(); | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | if(!defined $check || $check == 503) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $check or $check == 503
MC/DC0%0/2
not defined $check or $check == 503
not defined $check
$check == 503
Truth table0%0/3
not(A) or B
A= not defined $checkB= $check == 503
| |||||||||||||||||||||||||||||
| 93 | 0 | $c->res->headers->www_authenticate(qq[Basic realm="$realm"]); | |||||||||||||||||||||||||||
| 94 | 0 | my $error = $tx->error; | |||||||||||||||||||||||||||
| 95 | 0 | ▶ | if ($error->{code}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | WARN "Error connecting to PlugAuth at $auth_url : @{[ $error->{code} ]} @{[ $error->{message} ]}"; | |||||||||||||||||||||||||||
| 97 | } else { | ||||||||||||||||||||||||||||
| 98 | 0 | WARN "Error connecting to PlugAuth at $auth_url : @{[ encode_json $error ]}"; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 0 | $c->render(text => "auth server down", status => 503); # "Service unavailable" | |||||||||||||||||||||||||||
| 101 | 0 | return; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | 0 | ▶ | if ($check == 200) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | $c->stash(user => $user); | |||||||||||||||||||||||||||
| 105 | 0 | $c->continue; | |||||||||||||||||||||||||||
| 106 | 0 | return; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 0 | INFO "Authentication denied for $user, status code : ".$check; | |||||||||||||||||||||||||||
| 109 | 0 | ▶ | TRACE "Response was ".$res->to_string if defined $res; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 0 | $c->res->headers->www_authenticate(qq[Basic realm="$realm"]); | |||||||||||||||||||||||||||
| 111 | 0 | $c->render(text => "authentication failure", status => 401); | |||||||||||||||||||||||||||
| 112 | 0 | }); | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | }); | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | return undef; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | sub authorize { | ||||||||||||||||||||||||||
|
Subroutine
authorize: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 122 | 0 | my $c = shift; | |||||||||||||||||||||||||||
| 123 | 0 | my ($action,$resource) = @_; | |||||||||||||||||||||||||||
| 124 | 0 | ▶ | my $user = $c->stash("user") or LOGDIE "missing user in authorize()"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | LOGDIE "missing action or resource in authorize()" unless @_==2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | TRACE "Authorizing user $user, action $action, resource $resource"; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | $resource =~ s[^/][]; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | my $url = Mojo::URL->new( join '/', $self->config_url, "authz/user", $user, $action, $resource ); | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | my $tx = $c->ua->build_tx(HEAD => $url); | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | $c->ua->start($tx, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 135 | 0 | my($ua, $tx) = @_; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | my $code = $tx->res->code; | |||||||||||||||||||||||||||
| 138 | 0 | ▶ | if($code && $code == 200) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$code and $code == 200
MC/DC0%0/2
$code and $code == 200
$code
$code == 200
Truth table0%0/3
A and B
A= $codeB= $code == 200
| |||||||||||||||||||||||||||||
| 139 | 0 | $c->continue; | |||||||||||||||||||||||||||
| 140 | 0 | return; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | INFO "Unauthorized access by $user to $action $resource"; | |||||||||||||||||||||||||||
| 144 | 0 | ▶ | if($code == 503) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 0 | $c->render(text => "auth server down", status => 503); # "Service unavailable" | |||||||||||||||||||||||||||
| 146 | } else { | ||||||||||||||||||||||||||||
| 147 | 0 | $c->render(text => "unauthorized", status => 403); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | 0 | }); | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | return undef; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 8 | 1; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | __END__ | ||||||||||||||||||||||||||||