Authentication/Model/SAML2.pm

filter: Statementstmt 99.3% 143 / 144 Branchbran 63.6% 28 / 44 Conditioncond 53.8% 7 / 13 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 33 / 33 Podpod 100% 8 / 8 totaltotal 88.3% 220 / 249 CC 31 SCAR 14.0
CC
31
Cov
89%
parse_assertion
10.1
coerce_cache
7.8
coerce_idp
5.4
CRAP 4.0
« Authentication/Model/Hash.pm ↑ Summary Authentication.pm »
1 package MojoX::Authentication::Model::SAML2;
2 12 { our $VERSION = '0.100' }
3  
4 12 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use experimental qw< signatures >;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Net::SAML2 0.86; # refuse anything lower, they changed interface!
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Net::SAML2::Protocol::AuthnRequest;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use Net::SAML2::Object::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use Net::SAML2::Binding::Redirect;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use Net::SAML2::Binding::POST;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use Net::SAML2::Protocol::Assertion;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use Ouch qw< :trytiny_var >;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 ▶use Scalar::Util qw< blessed >;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 12 ▶use Module::Runtime qw< use_module >;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 12 ▶use MojoX::Authentication::Util qw< encode_name >;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 12 ▶use constant DEFAULT_FOR => {
Subroutine BEGIN: called CC=1 SCAR=0.0
20 name => 'saml2',
21 request_ttl => 30,
22 user_key => 'key',
23 user_ttl => 5 * 60,
24 sig_hash => undef,
25  
26 # we don't need to worry about backwards mappings here
27 remaps => [
28 {
29 okey => 'givenname',
30 ikey => 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname',
31 op => \&join_if_array,
32 },
33 {
34 okey => 'surname',
35 ikey => 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname',
36 op => \&join_if_array,
37 },
38 17 ▶ sub ($user, $is_backwards) {
Subroutine __ANON__: called CC=2 SCAR=7.2
39 $user->{fullname} = join(' ',
40 17 ▶ grep { defined } $user->@{qw< givenname surname >}) // '';
Condition50%1/2
join(" ", grep({defined $_;} @$user{"givenname", "surname"})) // ""
l!l
170
MC/DC0%0/1
join(" ", grep({defined $_;} @$user{"givenname", "surname"})) // ""
join(" ", grep({defined $_;} @$user{"givenname", "surname"}))
Truth table50%1/2
A
A= join(" ", grep({defined $_;} @$user{"givenname", "surname"}))
Aresult
11
00
41 },
42 12 ],
43 12 };
44  
45 sub join_if_array ($x) {
46 return $x unless ref($x) eq 'ARRAY';
47 return join(', ', $x->@*);
48 }
49  
50 27 ▶sub coerce_cache ($x) {
Subroutine coerce_cache: called CC=7 SCAR=20.5
51 # as a shortcut, if we're passed a MojoX::MojoDbWrap instance, we assume
52 # that we want a ::SAML2::Db cache using $x as its wmdb
53 $x = {
54 class => 'MojoX::Authentication::Model::SAML2::Db',
55 args => { wmdb => $x },
56 27 ▶ } if eval { $x->isa('MojoX::MojoDbWrap') };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $x->isa("MojoX::MojoDbWrap") } } 0 27 27
57  
58 # anything still blessed at this point is assumed to be a valid cache
59 25 ▶ return $x if blessed($x);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($x) 12 13 25
60  
61 # set defaults and normalize
62 13 ▶ $x //= 'MojoX::Authentication::Model::SAML2::Hash';
Condition50%1/2
$x //= "MojoX::Authentication::Model::SAML2::Hash"
l!l
013
MC/DC0%0/1
$x //= "MojoX::Authentication::Model::SAML2::Hash"
$x
Truth table50%1/2
A
A= $x
Aresult
11
00
63 13 ▶ $x = { class => $x } unless ref($x);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $x 13 0 13
64  
65 # extract (optional) args, normalizing them too
66 13 ▶ my $args = $x->{args} // [];
Condition50%1/2
$x->{"args"} // []
l!l
013
MC/DC0%0/1
$x->{"args"} // []
$x->{"args"}
Truth table50%1/2
A
A= $x->{"args"}
Aresult
11
00
67 13 ▶ my @args = ref($args) eq 'ARRAY' ? $args->@* : $args->%*;
Branch: 1 branch
BranchTrueFalseTotal
ref $args eq 'ARRAY' ? : 13 0 13
68  
69 # time to create something
70 13 return use_module($x->{class})->new(@args);
71 }
72  
73 25 ▶sub coerce_idp ($x) {
Subroutine coerce_idp: called CC=5 SCAR=16.8
74 25 ▶ return $x if blessed($x);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($x) 0 25 25
75 25 require Net::SAML2::IdP;
76 25 ▶ return Net::SAML2::IdP->new_from_url(url => $x)
Branch: 1 branch
BranchTrueFalseTotal
if $x =~ m[\A (?: \w+) :// ]imsux 0 25 25
77 if $x =~ m{\A (?: \w+) :// }imxs;
78 25 ▶ if ($x !~ m{\A \s* < }mxs) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x =~ /\A \s* < /msux) 25 0 25
79 25 ▶ open my $fh, '<:raw', $x or ouch 400, 'cannot read file', $x;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $x or ouch(400, "cannot read file", $x) 25 0 25
80 25 local $/;
81 25 $x = <$fh>;
82 }
83 25 return Net::SAML2::IdP->new_from_xml(xml => $x);
84 }
85  
86 12 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 12 with 'MojoX::Authentication::Model::Role::Creator';
89 12 with 'MojoX::Authentication::Model::Role::Remap';
90  
91 12 has cache => (is => 'ro', coerce => \&coerce_cache, default => undef);
92 12 has idp => (is => 'ro', required => 1, coerce => \&coerce_idp);
93 12 has name => (is => 'rw', default => DEFAULT_FOR->{name});
94 25 ▶has remaps => (is => 'ro', default => sub { DEFAULT_FOR->{remaps} });
Subroutine __ANON__: called CC=1 SCAR=0.0
95 12 has request_ttl => (is => 'ro', default => DEFAULT_FOR->{request_ttl});
96 12 has sig_hash => (is => 'ro', default => DEFAULT_FOR->{sig_hash});
97 12 has sp_configuration => (is => 'ro', required => 1);
98 12 has user_key => (is => 'ro', default => DEFAULT_FOR->{user_key});
99 12 has user_ttl => (is => 'ro', default => DEFAULT_FOR->{user_ttl});
100 12 has username_validator => (is => 'lazy');
101  
102 4 ▶sub _build_username_validator ($self) {
Subroutine _build_username_validator: called CC=1 SCAR=0.0
103 4 ▶ return sub ($name) { return 'by all means' };
Subroutine __ANON__: called CC=1 SCAR=0.0
104 };
105  
106 114 ▶sub _key ($self, $prefix, $x) {
Subroutine _key: called CC=2 SCAR=6.9
107 114 ▶ my $bare = ref($x) eq 'HASH' ? $x->{$self->user_key} : $x;
Branch: 1 branch
BranchTrueFalseTotal
ref $x eq 'HASH' ? : 20 94 114
108 114 my $ename = encode_name($self->name);
109 114 return "$prefix-$ename/$bare";
110 }
111  
112 20 ▶sub _cache_user ($self, $user, $ttl = undef) {
Subroutine _cache_user: called CC=3 SCAR=11.4
113 20 my $key = $self->_key(user => $user);
114 20 ▶ my $expire = time() + ($ttl //= $self->user_ttl);
Condition33%1/3
$ttl //= $self->user_ttl
l!l&&r!l&&!r
0200
MC/DC0%0/2
$ttl //= $self->user_ttl
$ttl $self->user_ttl
Truth table33%1/3
A //= B
A= $ttlB= $self->user_ttl
ABresult
1X1
011
000
115 20 $self->cache->set($user, $key, $expire);
116 20 return $self;
117 }
118  
119 4 ▶sub create ($class, $config, %args) {
Subroutine create: called CC=3 SCAR=11.4
Pod documented
120 4 %args = $class->_create_args(DEFAULT_FOR->{name}, $config, %args);
121 4 ▶ ouch 404, 'missing argument for idp' unless defined($args{idp});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"idp"} 0 4 4
122 ouch 404, 'missing argument for sp_configuration'
123 4 ▶ unless defined($args{sp_configuration});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"sp_configuration"} 0 4 4
124 4 return $class->new(%args);
125 }
126  
127 7 ▶sub handles_username ($self, $controller, $name) {
Subroutine handles_username: called CC=2 SCAR=9.2
Pod documented
128 7 ▶ return 'sure' if $self->username_validator->($name);
Branch: 1 branch
BranchTrueFalseTotal
if $self->username_validator->($name) 7 0 7
129 0 return undef;
130 }
131  
132 # returns id of request and redirect URL
133 6 ▶sub idp_login ($self) {
Subroutine idp_login: called CC=4 SCAR=14.0
Pod documented
134 6 my $sp_conf = $self->sp_configuration;
135 6 my $idp = $self->idp;
136  
137 6 my $sso_url =
138 $idp->sso_url('urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect');
139  
140 6 ▶ my $assertion_url = $sp_conf->{'sso-post-url-override'} // undef;
Condition50%1/2
$sp_conf->{"sso-post-url-override"} // undef
l!l
06
MC/DC0%0/1
$sp_conf->{"sso-post-url-override"} // undef
$sp_conf->{"sso-post-url-override"}
Truth table50%1/2
A
A= $sp_conf->{"sso-post-url-override"}
Aresult
11
00
141 my $authnreq = Net::SAML2::Protocol::AuthnRequest->new(
142 issuer => $sp_conf->{identifier},
143 6 ▶ destination => $sso_url,
Branch: 1 branch
BranchTrueFalseTotal
defined $assertion_url ? : 0 6 6
144 (defined($assertion_url) ? (assertion_url => $assertion_url) : ()),
145 );
146  
147 # Store the request's id for later verification
148 6 my $id = $authnreq->id;
149 6 my $key = $self->_key(authnreq_id => $id);
150 6 my $ts = time();
151 6 my $expire = $ts + $self->request_ttl;
152 6 $self->cache->set({ ts => $ts }, $key, $expire);
153  
154 6 my $sig_hash = $self->sig_hash;
155 my $redirect = Net::SAML2::Binding::Redirect->new(
156 key => $sp_conf->{key}, # to sign the redirect
157 6 ▶ param => 'SAMLRequest', # what's this about
Branch: 1 branch
BranchTrueFalseTotal
defined $sig_hash ? : 0 6 6
158 url => $sso_url, # where it's heading to
159 (defined($sig_hash) ? (sig_hash => $sig_hash) : ()),
160 );
161 6 my $url = $redirect->sign($authnreq->as_xml);
162  
163 6 return ($id, $url);
164 }
165  
166 50 ▶sub load_user ($self, $A, $key) {
Subroutine load_user: called CC=1 SCAR=0.0
Pod documented
167 50 return $self->cache->get($self->_key(user => $key));
168 }
169  
170 27 ▶sub tamed_user_record ($self, $user) {
Subroutine tamed_user_record: called CC=1 SCAR=0.0
Pod documented
171 # the SAML2 user record does not have any secret material inside, so
172 # we can pass $user through
173 27 return $user;
174 }
175  
176 # TODO FIXME this is an occasion to implement some clever redirection in
177 # the Controller to implement the logout properly (i.e. forwarding to the
178 # server, not just "consider this user gone here"). But we're not there
179 # yet, sorry!
180 8 ▶sub logout ($self, $controller, $user) {
Subroutine logout: called CC=1 SCAR=0.0
Pod documented
181 8 $self->wipe_user($user->{uid});
182 8 $controller->logout;
183 8 return;
184 }
185  
186 17 ▶sub _normalize_user ($s, $user) { return $s->remap($user, $s->remaps) }
Subroutine _normalize_user: called CC=1 SCAR=0.0
187  
188 15 ▶sub parse_assertion ($self, $id, $response) {
Subroutine parse_assertion: called CC=10 SCAR=23.1
Pod documented
189 15 ▶ defined($response) or ouch 400, 'No SAMLResponse';
Branch: 1 branch
BranchTrueFalseTotal
defined $response or ouch(400, "No SAMLResponse") 15 0 15
190 15 ▶ defined($id)
Branch: 1 branch
BranchTrueFalseTotal
defined $id or ouch(400, "Not expecting anything SAML-related", "undefined id") 15 0 15
191 or ouch 400, 'Not expecting anything SAML-related', 'undefined id';
192 15 my $req_key = $self->_key(authnreq_id => $id);
193 15 ▶ my $reqts = $self->cache->get($req_key)
Branch: 1 branch
BranchTrueFalseTotal
my $reqts = $self->cache->get($req_key) or ouch(400, "Not expecting anything SAML-related", "unknown id") 15 0 15
194 or ouch 400, 'Not expecting anything SAML-related', 'unknown id';
195 15 $self->cache->wipe($req_key); # single use: no replays
196 15 ▶ time() <= $reqts->{ts} + $self->request_ttl
Branch: 1 branch
BranchTrueFalseTotal
time <= $reqts->{"ts"} + $self->request_ttl or ouch(400, "Not expecting anything SAML-related", "expired id") 15 0 15
197 or ouch 400, 'Not expecting anything SAML-related', 'expired id';
198  
199 15 my $idp = $self->idp;
200  
201 # the module for Assertion insists on a file-based CA certificate so we
202 # save it in a temporary file. It is also useful for the POST module
203 # since Net::SAML 0.88
204 15 my $tmp = File::Temp->new;
205 15 print {$tmp} join "\n\n", $idp->cert('signing')->@*;
206 15 close $tmp;
207  
208 15 my $xml_response = Net::SAML2::Binding::POST
209 ->new(cacert => $tmp->filename)
210 ->handle_response($response);
211  
212 11 my $saml_response = Net::SAML2::Object::Response->new_from_xml(
213 xml => $xml_response,
214 cacert => $tmp->filename,
215 );
216 10 ▶ if (! $saml_response->success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($saml_response->success) 3 7 10
217 3 my $status = $saml_response->status;
218 3 my $substatus = $saml_response->substatus;
219 3 ▶ $status .= '/' . $substatus if defined($substatus);
Branch: 1 branch
BranchTrueFalseTotal
if defined $substatus 1 2 3
220 3 ouch 403, "IdP refused with $status";
221 }
222  
223 7 my $sp_conf = $self->sp_configuration;
224 my $assertion = Net::SAML2::Protocol::Assertion->new_from_xml(
225 xml => $xml_response,
226 key_file => $sp_conf->{key},
227 7 cacert => $tmp->filename,
228 issuer => $idp->entityid,
229 );
230  
231 6 ▶ $assertion->valid($sp_conf->{identifier}, $id, $idp->entityid)
Branch: 1 branch
BranchTrueFalseTotal
$assertion->valid($sp_conf->{"identifier"}, $id, $idp->entityid) or ouch(400, "Invalid SAMLResponse") 3 3 6
232 or ouch 400, 'Invalid SAMLResponse';
233  
234 3 my $uid = $assertion->nameid;
235 3 ▶ ouch 400, 'no NameID in assertion' unless length($uid // '') > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless length($uid // "") > 0 2 1 3
Condition100%2/2
$uid // ""
l!l
21
MC/DC100%1/1
$uid // ""
$uid
Truth table100%2/2
A
A= $uid
Aresult
11
00
236 1 my $user = $self->_normalize_user(
237 { $assertion->attributes->%*, $self->user_key => $uid, });
238 1 $self->_cache_user($user);
239  
240 1 return $user;
241 }
242  
243 10 ▶sub wipe_user ($self, $user) { $self->cache->wipe($self->_key(user => $user)) }
Subroutine wipe_user: called CC=1 SCAR=0.0
Pod documented
244  
245 12 1;
« Authentication/Model/Hash.pm ↑ Summary Authentication.pm »