/
Assertion.pm
278 lines (202 loc) · 7.99 KB
/
Assertion.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use strict;
use warnings;
package Net::SAML2::Protocol::Assertion;
# VERSION
use Moose;
use MooseX::Types::DateTime qw/ DateTime /;
use MooseX::Types::Common::String qw/ NonEmptySimpleStr /;
use DateTime;
use DateTime::HiRes;
use DateTime::Format::XSD;
use Net::SAML2::XML::Util qw/ no_comments /;
use Net::SAML2::XML::Sig;
use XML::Enc;
use XML::LibXML;
with 'Net::SAML2::Role::ProtocolMessage';
# ABSTRACT: Net::SAML2::Protocol::Assertion - SAML2 assertion object
=head1 NAME
Net::SAML2::Protocol::Assertion - SAML2 assertion object
=head1 SYNOPSIS
my $assertion = Net::SAML2::Protocol::Assertion->new_from_xml(
xml => decode_base64($SAMLResponse)
);
=cut
has 'attributes' => (isa => 'HashRef[ArrayRef]', is => 'ro', required => 1);
has 'audience' => (isa => NonEmptySimpleStr, is => 'ro', required => 1);
has 'not_after' => (isa => DateTime, is => 'ro', required => 1);
has 'not_before' => (isa => DateTime, is => 'ro', required => 1);
has 'session' => (isa => 'Str', is => 'ro', required => 1);
has 'in_response_to' => (isa => 'Str', is => 'ro', required => 1);
has 'response_status' => (isa => 'Str', is => 'ro', required => 1);
has 'xpath' => (isa => 'XML::LibXML::XPathContext', is => 'ro', required => 1);
has 'nameid_object' => (
isa => 'XML::LibXML::Element',
is => 'ro',
required => 1,
init_arg => 'nameid'
);
=head1 METHODS
=cut
=head2 new_from_xml( ... )
Constructor. Creates an instance of the Assertion object, parsing the
given XML to find the attributes, session and nameid.
Arguments:
=over
=item B<xml>
XML data
=item B<key_file>
Optional but Required handling Encrypted Assertions.
path to the SP's private key file that matches the SP's public certificate
used by the IdP to Encrypt the response (or parts of the response)
=item B<cacert>
path to the CA certificate for verification. Optional: This is only used for
validating the certificate provided for a signed Assertion that was found
when the EncryptedAssertion is decrypted.
While optional it is recommended for ensuring that the Assertion in an
EncryptedAssertion is properly validated.
=back
=cut
sub new_from_xml {
my($class, %args) = @_;
my $dom = no_comments($args{xml});
my $key_file = $args{key_file};
my $cacert = $args{cacert};
my $xpath = XML::LibXML::XPathContext->new($dom);
$xpath->registerNs('saml', 'urn:oasis:names:tc:SAML:2.0:assertion');
$xpath->registerNs('samlp', 'urn:oasis:names:tc:SAML:2.0:protocol');
$xpath->registerNs('xenc', 'http://www.w3.org/2001/04/xmlenc#');
my $attributes = {};
if ($xpath->findnodes('//saml:EncryptedAssertion')) {
if ( ! defined $key_file) {
die "Encrypted Assertions require key_file";
}
my $decrypted;
my $enc = XML::Enc->new(
{ key => $key_file , no_xml_declaration => 1 }, );
$decrypted = $enc->decrypt($dom->toString());
$dom = XML::LibXML->load_xml(string => $decrypted);
$xpath = XML::LibXML::XPathContext->new($dom);
$xpath->registerNs('saml', 'urn:oasis:names:tc:SAML:2.0:assertion');
$xpath->registerNs('samlp', 'urn:oasis:names:tc:SAML:2.0:protocol');
$xpath->registerNs('dsig', 'http://www.w3.org/2000/09/xmldsig#');
$xpath->registerNs('xenc', 'http://www.w3.org/2001/04/xmlenc#');
my $xml_opts->{ no_xml_declaration } = 1;
my $assert = $xpath->findnodes('//saml:Assertion')->[0];
my @signedinfo = $xpath->findnodes('dsig:Signature', $assert);
if (defined $assert && (scalar @signedinfo ne 0)) {
my $x = Net::SAML2::XML::Sig->new($xml_opts);
my $ret = $x->verify($assert->serialize);
die "Decrypted Assertion signature check failed" unless $ret;
if ($cacert) {
my $cert = $x->signer_cert
or die "Certificate not provided and not in SAML Response, cannot validate";
my $ca = Crypt::OpenSSL::Verify->new($cacert, { strict_certs => 0, });
if (! $ca->verify($cert)) {
die "Decrypted Assertion - Unable to verify signer cert with cacert: $cert->subject";
}
}
}
}
for my $node (
$xpath->findnodes('//saml:Assertion/saml:AttributeStatement/saml:Attribute'))
{
# We can't select by saml:AttributeValue
# because of https://rt.cpan.org/Public/Bug/Display.html?id=8784
my @values = $node->findnodes("*[local-name()='AttributeValue']");
$attributes->{$node->getAttribute('Name')} = [map $_->string_value, @values];
}
my $not_before;
if($xpath->findvalue('//saml:Conditions/@NotBefore')) {
$not_before = DateTime::Format::XSD->parse_datetime(
$xpath->findvalue('//saml:Conditions/@NotBefore'));
}
else {
$not_before = DateTime::HiRes->now();
}
my $not_after;
if($xpath->findvalue('//saml:Conditions/@NotOnOrAfter')) {
$not_after = DateTime::Format::XSD->parse_datetime(
$xpath->findvalue('//saml:Conditions/@NotOnOrAfter'));
}
else {
$not_after = DateTime->from_epoch(epoch => time() + 1000);
}
my $self = $class->new(
issuer => $xpath->findvalue('//saml:Assertion/saml:Issuer'),
destination => $xpath->findvalue('/samlp:Response/@Destination'),
attributes => $attributes,
session => $xpath->findvalue('//saml:AuthnStatement/@SessionIndex'),
nameid => $xpath->findnodes('//saml:Subject/saml:NameID')->get_node(1),
audience => $xpath->findvalue('//saml:Conditions/saml:AudienceRestriction/saml:Audience'),
not_before => $not_before,
not_after => $not_after,
xpath => $xpath,
in_response_to => $xpath->findvalue('//saml:Subject/saml:SubjectConfirmation/saml:SubjectConfirmationData/@InResponseTo'),
response_status => $xpath->findvalue('//samlp:Response/samlp:Status/samlp:StatusCode/@Value'),
);
return $self;
}
=head2 name( )
Returns the CN attribute, if provided.
=cut
sub name {
my($self) = @_;
return $self->attributes->{CN}->[0];
}
=head2 nameid
Returns the NameID
=cut
sub nameid {
my $self = shift;
return $self->nameid_object->textContent;
}
=head2 nameid_format
Returns the NameID Format
=cut
sub nameid_format {
my $self = shift;
return $self->nameid_object->getAttribute('Format');
}
=head2 nameid_name_qualifier
Returns the NameID NameQualifier
=cut
sub nameid_name_qualifier {
my $self = shift;
return $self->nameid_object->getAttribute('NameQualifier');
}
=head2 nameid_sp_name_qualifier
Returns the NameID SPNameQualifier
=cut
sub nameid_sp_name_qualifier {
my $self = shift;
return $self->nameid_object->getAttribute('SPNameQualifier');
}
=head2 nameid_sp_provided_id
Returns the NameID SPProvidedID
=cut
sub nameid_sp_provided_id {
my $self = shift;
return $self->nameid_object->getAttribute('SPProvidedID');
}
=head2 valid( $audience, $in_response_to )
Returns true if this Assertion is currently valid for the given audience.
Also accepts $in_response_to which it checks against the returned
Assertion. This is very important for security as it helps ensure
that the assertion that was received was for the request that was made.
Checks the audience matches, and that the current time is within the
Assertions validity period as specified in its Conditions element.
=cut
sub valid {
my ($self, $audience, $in_response_to) = @_;
return 0 unless defined $audience;
return 0 unless($audience eq $self->audience);
return 0 unless !defined $in_response_to
or $in_response_to eq $self->in_response_to;
my $now = DateTime::HiRes->now;
# not_before is "NotBefore" element - exact match is ok
# not_after is "NotOnOrAfter" element - exact match is *not* ok
return 0 unless DateTime::->compare($now, $self->not_before) > -1;
return 0 unless DateTime::->compare($self->not_after, $now) > 0;
return 1;
}
1;