Authentication/Model/SAML2.pm

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