Authentication.pm

filter: Statementstmt 96.6% 115 / 119 Branchbran 71.8% 23 / 32 Conditioncond 65.2% 15 / 23 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 96.7% 30 / 31 Podpod 100% 6 / 6 totaltotal 86.1% 192 / 223 CC 31 SCAR 11.3
CC
31
Cov
88%
ctr_logout
7.1
__ANON__
6.2
truncate_message
4.0
CRAP 3.1
« Authentication/Model/SAML2.pm ↑ Summary Authentication/Model/Role/Local.pm »
1 package MojoX::Authentication;
2 9 { our $VERSION = '0.099001' }
3  
4 9 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use experimental qw< signatures >;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Try::Catch;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Ouch qw< :trytiny_var >;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use MojoX::Authentication::Model;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use MojoX::Authentication::Util qw< coercer_for >;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 9 ▶use constant DEFAULT_ROUTE_FOR => {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 ok => 'protected_root',
15 not_ok => 'public_root',
16 9 };
17  
18 9 ▶use constant DEFAULT_PROVIDER_NAME => 'saml2';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 23 ▶sub logsafe ($str) {
Subroutine logsafe: called CC=2 SCAR=6.9
21 23 ▶ $str //= '*undef*';
Condition100%3/3
$str //= "*undef*"
l!l
2111
MC/DC0%0/1
$str //= "*undef*"
$str
Truth table50%1/2
A
A= $str
Aresult
11
00
22 23 return ($str =~ s{([^\x20-\x7e])}{sprintf '\\x{%02x}', ord($1)}regmxs);
23 }
24  
25 sub truncate_message ($str, $length = 500) {
26 ▶ return $str if $length <= 3 || length($str) <= $length;
Branch: 1 branch
BranchTrueFalseTotal
if $length <= 3 or length $str <= $length 1 1 2
27 return substr($str, 0, $length - 3) . '...';
28 }
29  
30 9 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 9 has model => (is => 'ro', required => 1,
33 coerce
34 => coercer_for(authentication_model => 'MojoX::Authentication::Model')
35 );
36 9 has provider_name => (is => 'ro', default => undef);
37 9 has user_stash_key => (is => 'ro', default => 'user');
38  
39 10 ▶around BUILDARGS => sub ($orig, $class, @args) {
Subroutine __ANON__: called CC=2 SCAR=7.2
40 10 ▶ my $hash = { @args == 1 ? $args[0]->%* : @args };
Branch: 1 branch
BranchTrueFalseTotal
@args == 1 ? : 0 10 10
41 10 return $hash;
42 9 };
43  
44 10 ▶sub BUILD ($self, $hash) {
Subroutine BUILD: called CC=2 SCAR=8.1
Pod documented
45 10 ▶ if (defined(my $app = $hash->{'-startup'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $app = $hash->{"-startup"})) 0 10 10
46 0 $self->app_startup($app);
47 }
48 10 return $self;
49 };
50  
51 10 ▶sub app_startup ($self, $app) {
Subroutine app_startup: called CC=2 SCAR=6.9
Pod documented
52 10 ▶ ouch 400, 'undefined application' unless defined($app);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $app 0 10 10
53  
54 10 my $authn = $self->model;
55  
56 # add Mojolicious::Plugin::Authentication with the "right" callbacks
57 $app->plugin( Authentication => {
58 27 ▶ load_user => sub (@args) { $authn->load_user(@args) },
Subroutine __ANON__: called CC=1 SCAR=0.0
59 8 ▶ validate_user => sub (@args) { $authn->validate_user(@args) },
Subroutine __ANON__: called CC=1 SCAR=0.0
60 10 });
61  
62 # set a hook to set the user's data in the stash, associated to the
63 # requested user_stash_key. This stash key is always set anyway, to
64 # avoid errors in the templates in case it's missing
65 10 my $key = $self->user_stash_key;
66 $app->hook(
67 26 ▶ before_render => sub ($c, $args) {
Subroutine __ANON__: called CC=2 SCAR=6.9
68 26 ▶ my $user = $c->is_user_authenticated ? $c->current_user : undef;
Branch: 1 branch
BranchTrueFalseTotal
$c->is_user_authenticated ? : 13 13 26
69 26 $c->stash($key => $user);
70 26 return $c;
71 }
72 10 );
73  
74 10 return $self;
75 }
76  
77 # convention over configuration
78 11 ▶sub ctr_credentials_login ($self, $c, $route_for = undef) {
Subroutine ctr_credentials_login: called CC=2 SCAR=7.0
Pod documented
79 11 my $username = $c->param('username');
80 11 ▶ try {
Subroutine __ANON__: called CC=3 SCAR=11.0
81 11 ▶ ouch 403, 'bad CSRF token'
Branch: 1 branch
BranchTrueFalseTotal
if $c->validation->csrf_protect->has_error("csrf_token") 3 8 11
82 if $c->validation->csrf_protect->has_error('csrf_token');
83 8 my $password = $c->param('password');
84 8 ▶ $c->authenticate($username, $password, {}) or die 'whatever';
Branch: 1 branch
BranchTrueFalseTotal
$c->authenticate($username, $password, {}) or die "whatever" 6 0 6
85 6 $c->log->info(logsafe("login successful (credentials): $username"));
86 6 $c->flash(message => [ info => "Welcome, $username" ]);
87 6 return $c->redirect_to($self->_route_for($route_for, 'ok'));
88 }
89 5 ▶ catch {
Subroutine __ANON__: called CC=2 SCAR=7.0
90 5 ▶ $username = defined($username) ? qq{'$username'} : '*undef*';
Branch: 1 branch
BranchTrueFalseTotal
defined $username ? : 5 0 5
91 5 $c->log->error(logsafe("authentication error (credentials): $username")
92 . bleep());
93 5 $c->flash(message => [ error => 'Authentication error' ]);
94 5 $c->redirect_to($self->_route_for($route_for, 'not_ok'));
95 11 };
96 11 return;
97 }
98  
99 5 ▶sub ctr_logout ($self, $c, $redirect_route = undef) {
Subroutine ctr_logout: called CC=7 SCAR=19.6
Pod documented
100 5 my $csrf_error = $c->validation->csrf_protect->has_error('csrf_token');
101 5 ▶ if (! $csrf_error && $c->is_user_authenticated) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $csrf_error and $c->is_user_authenticated) 4 1 5
Condition66%2/3
not $csrf_error and $c->is_user_authenticated
!ll&&!rl&&r
104
MC/DC50%1/2
not $csrf_error and $c->is_user_authenticated
not $csrf_error $c->is_user_authenticated
Truth table66%2/3
not(A) and B
A= not $csrf_errorB= $c->is_user_authenticated
ABresult
0X0
100
111
102 4 my $user = $c->current_user;
103 4 $c->log->info(logsafe("logout: $user->{uid}"));
104  
105 4 my $provider = $self->model->provider_named($user->{provider});
106 4 ▶ if ($provider && $provider->can('logout')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($provider and $provider->can('logout')) { } 2 2 4
Condition66%2/3
$provider and $provider->can("logout")
!ll&&!rl&&r
022
MC/DC50%1/2
$provider and $provider->can("logout")
$provider $provider->can("logout")
Truth table66%2/3
A and B
A= $providerB= $provider->can("logout")
ABresult
0X0
100
111
107 2 $provider->logout($c, $user);
108 }
109 else {
110 2 $c->logout; # just make Mojolicious::Plugin::Authentication happy
111 }
112 }
113 5 ▶ $c->redirect_to($redirect_route) if defined($redirect_route);
Branch: 1 branch
BranchTrueFalseTotal
if defined $redirect_route 5 0 5
114 5 return;
115 }
116  
117 13 ▶sub _get_provider_name ($self, $c) {
Subroutine _get_provider_name: called CC=3 SCAR=11.1
118 13 ▶ return $self->provider_name
Condition80%4/5
($self->provider_name // $c->param("provider")) // "saml2"
l!l
76
$self->provider_name // $c->param("provider")
l!l&&r!l&&!r
706
MC/DC50%1/2
($self->provider_name // $c->param("provider")) // "saml2"
$self->provider_name $c->param("provider")
Truth table66%2/3
A // B
A= $self->provider_nameB= $c->param("provider")
ABresult
1X1
011
000
119 // $c->param('provider')
120 // DEFAULT_PROVIDER_NAME;
121 }
122  
123 7 ▶sub _get_provider ($self, $c) {
Subroutine _get_provider: called CC=1 SCAR=0.0
124 7 return $self->model->provider_named($self->_get_provider_name($c));
125 }
126  
127 17 ▶sub _route_for ($self, $route_for, $outcome) {
Subroutine _route_for: called CC=3 SCAR=13.1
128 17 ▶ $route_for //= {};
Condition50%1/2
$route_for //= {}
l!l
017
MC/DC0%0/1
$route_for //= {}
$route_for
Truth table50%1/2
A
A= $route_for
Aresult
11
00
129 17 ▶ return $route_for->{$outcome} // DEFAULT_ROUTE_FOR->{$outcome};
Condition33%1/3
$route_for->{$outcome} // {"not_ok" => "public_root","ok" => "protected_root"}->{$outcome}
l!l&&r!l&&!r
0170
MC/DC0%0/2
$route_for->{$outcome} // {"not_ok" => "public_root","ok" => "protected_root"}->{$outcome}
$route_for->{$outcome} {"not_ok" => "public_root","ok" => "protected_root"}->{$outcome}
Truth table33%1/3
A // B
A= $route_for->{$outcome}B= {"not_ok" => "public_root","ok" => "protected_root"}->{$outcome}
ABresult
1X1
011
000
130 }
131  
132 7 ▶sub ctr_saml2_login ($self, $c, $route_for) {
Subroutine ctr_saml2_login: called CC=1 SCAR=0.5
Pod documented
133 7 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
134 7 my ($id, $url) = $self->_get_provider($c)->idp_login;
135 7 $c->signed_cookie('saml-id' => $id, { path => '/' });
136 7 $c->redirect_to($url);
137 }
138 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
139 0 $c->log->error('ctr_saml2_login error: ' . bleep());
140 0 $c->flash(message => [ error => 'error... ask the administrator!' ]);
141 0 $c->redirect_to($self->_route_for($route_for, 'not_ok'));
142 7 };
143  
144 7 return;
145 }
146  
147 6 ▶sub ctr_saml2_sso_post ($self, $c, $route_for = undef) {
Subroutine ctr_saml2_sso_post: called CC=2 SCAR=7.0
Pod documented
148 6 ▶ try {
Subroutine __ANON__: called CC=6 SCAR=18.2
149 6 ▶ (my $saml_id = $c->signed_cookie('saml-id'))
Branch: 1 branch
BranchTrueFalseTotal
my $saml_id = $c->signed_cookie("saml-id") or ouch(400, "Not expecting anything SAML-related", "no id") 6 0 6
150 or ouch 400, 'Not expecting anything SAML-related', 'no id';
151 6 ▶ defined(my $saml_response = $c->param('SAMLResponse'))
Branch: 1 branch
BranchTrueFalseTotal
defined(my $saml_response = $c->param("SAMLResponse")) or ouch(400, "No SAMLResponse") 6 0 6
152 or ouch 400, 'No SAMLResponse';
153  
154 6 my $model = $self->model;
155 6 my $provider_name = $self->_get_provider_name($c);
156 6 ▶ my $saml2 = $model->provider_named($provider_name)
Branch: 1 branch
BranchTrueFalseTotal
my $saml2 = $model->provider_named($provider_name) or ouch(401, "unsupported provider") 6 0 6
157 or ouch 401, 'unsupported provider';
158 6 my $user = $saml2->parse_assertion($saml_id, $saml_response);
159 4 my $uid = $user->{$saml2->user_key};
160  
161 # we're not using credentials here, so "username" and "password" do
162 # not make sense. We use "auto_validate" instead and skip
163 # validate_user altogether
164 4 my $euid = $model->euid_join($provider_name, $uid);
165 4 $c->authenticate(undef, undef, { auto_validate => $euid });
166 4 $c->log->info(logsafe("login successful (SAML2): $uid"));
167  
168 # assuming a convention from consumers to set "fullname", we use it
169 # but we fall back to the user identifier, which will always be
170 # available
171 4 my $uname = $user->{fullname};
172 4 ▶ $uname = $uid unless length($uname // '');
Branch: 1 branch
BranchTrueFalseTotal
unless length($uname // "") 3 1 4
Condition50%1/2
$uname // ""
l!l
40
MC/DC0%0/1
$uname // ""
$uname
Truth table50%1/2
A
A= $uname
Aresult
11
00
173 4 $c->flash(message => [ info => "Welcome, $uname" ]);
174  
175 4 $c->redirect_to($self->_route_for($route_for, 'ok'));
176 }
177 2 ▶ catch {
Subroutine __ANON__: called CC=3 SCAR=11.0
178 2 ▶ my $message = bleep() || 'Error: invalid credentials';
Condition50%1/2
bleep() || 'Error: invalid credentials'
l!l
20
MC/DC0%0/1
bleep() || 'Error: invalid credentials'
bleep()
Truth table50%1/2
A
A= bleep()
Aresult
11
00
179 2 $c->log->error('authentication error (SAML2): '
180 . truncate_message(logsafe($message)));
181  
182 2 ▶ $message .= ' ' . Dumper($_->data) if eval { $_->isa('Ouch') };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $_->isa("Ouch") } } 1 1 2
183 2 $c->log->trace('full error: ' . $message);
184  
185 2 $c->flash(message => [ error => 'Authentication error' ]);
186 2 $c->redirect_to($self->_route_for($route_for, 'not_ok'));
187 6 };
188 6 return;
189 }
190  
191 9 1;
« Authentication/Model/SAML2.pm ↑ Summary Authentication/Model/Role/Local.pm »