Skip to content

Commit b807f35

Browse files
committed
Add XsdID as a type for id attribute type checking
Signed-off-by: Wesley Schwengle <waterkip@cpan.org>
1 parent d03bb23 commit b807f35

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

lib/Net/SAML2/Role/ProtocolMessage.pm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use namespace::autoclean;
1212
use DateTime;
1313
use MooseX::Types::URI qw/ Uri /;
1414
use Net::SAML2::Util qw(generate_id);
15+
use Net::SAML2::Types qw(XsdID);
1516

1617
=head1 NAME
1718
@@ -28,7 +29,7 @@ implementation.
2829
=cut
2930

3031
has id => (
31-
isa => 'Str',
32+
isa => XsdID,
3233
is => 'ro',
3334
builder => "_build_id"
3435
);

lib/Net/SAML2/Types.pm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,24 @@ use strict;
99
use Types::Serialiser;
1010
use MooseX::Types -declare => [
1111
qw(
12+
XsdID
1213
SAMLRequestType
1314
signingAlgorithm
1415
)
1516
];
1617

1718
use MooseX::Types::Moose qw(Str Int Num Bool ArrayRef HashRef Item);
1819

20+
=head2 XsdID
21+
22+
The type xsd:ID is used for an attribute that uniquely identifies an element in an XML document. An xsd:ID value must be an NCName. This means that it must start with a letter or underscore, and can only contain letters, digits, underscores, hyphens, and periods.
23+
24+
=cut
25+
26+
subtype XsdID, as Str,
27+
where { return $_ =~ /^[a-zA-Z_]/ },
28+
message { "'$_' is not a valid xsd:ID" };
29+
1930
=head2 SAMLRequestType
2031
2132
Enum which consists of two options: SAMLRequest and SAMLResponse

t/22-types.t

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use strict;
2+
use warnings;
3+
use Test::Lib;
4+
use Test::Net::SAML2;
5+
use Net::SAML2::Types qw(XsdID SAMLRequestType signingAlgorithm);
6+
7+
my @xsdidok = qw(thisiscorrect _so_it_this THISTOO _YES);
8+
foreach (@xsdidok) {
9+
ok(XsdID->check($_), "$_ is correct as an xsd:ID");
10+
}
11+
12+
ok(!XsdID->check("1abc"), "... and this is not a correct xsd:ID");
13+
like(
14+
XsdID->get_message("1abc"),
15+
qr/is not a valid xsd:ID/,
16+
".. with the correct error message"
17+
);
18+
19+
foreach (qw(SAMLRequest SAMLResponse)) {
20+
ok(SAMLRequestType->check($_), "$_ is correct SAMLRequestType");
21+
}
22+
ok(!SAMLRequestType->check("foo"), ".. and this is not");
23+
like(
24+
SAMLRequestType->get_message("foo"),
25+
qr/is not a SAML Request type/,
26+
".. with the correct error message"
27+
);
28+
29+
foreach (qw(sha244 sha256 sha384 sha512 sha1)) {
30+
ok(signingAlgorithm->check($_), "$_ is correct signingAlgorithm");
31+
}
32+
33+
ok(!signingAlgorithm->check("shafake"), ".. and this is not");
34+
like(
35+
signingAlgorithm->get_message("shafake"),
36+
qr/is not a supported signingAlgorithm/,
37+
".. with the correct error message"
38+
);
39+
40+
done_testing;

0 commit comments

Comments
 (0)