Skip to content

Commit bd7766e

Browse files
authored
Merge pull request #158 from waterkip/attribute-id-typechecking
Add XsdID as a type for id attribute type checking
2 parents d03bb23 + 1688e1c commit bd7766e

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,28 @@ 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 {
28+
return 0 unless $_ =~ /^[a-zA-Z_]/;
29+
return 0 if $_ =~ /[^a-zA-Z0-9_\.\-]/;
30+
return 1;
31+
},
32+
message { "'$_' is not a valid xsd:ID" };
33+
1934
=head2 SAMLRequestType
2035
2136
Enum which consists of two options: SAMLRequest and SAMLResponse

t/22-types.t

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
subtest 'XsdID' => sub {
8+
my @xsdidok = qw(thisiscorrect _so_it_this THISTOO _YES this.-123.correct);
9+
foreach (@xsdidok) {
10+
ok(XsdID->check($_), "$_ is correct as an xsd:ID");
11+
}
12+
13+
ok(!XsdID->check("1abc"), "... and this is not a correct xsd:ID");
14+
like(
15+
XsdID->get_message("1abc"),
16+
qr/is not a valid xsd:ID/,
17+
".. with the correct error message"
18+
);
19+
20+
ok(!XsdID->check('asb#'), "... not an allowed character");
21+
22+
};
23+
24+
subtest 'SAMLRequestType' => sub {
25+
26+
foreach (qw(SAMLRequest SAMLResponse)) {
27+
ok(SAMLRequestType->check($_), "$_ is correct SAMLRequestType");
28+
}
29+
ok(!SAMLRequestType->check("foo"), ".. and this is not");
30+
like(
31+
SAMLRequestType->get_message("foo"),
32+
qr/is not a SAML Request type/,
33+
".. with the correct error message"
34+
);
35+
};
36+
37+
subtest 'signingAlgorithm' => sub {
38+
39+
foreach (qw(sha244 sha256 sha384 sha512 sha1)) {
40+
ok(signingAlgorithm->check($_), "$_ is correct signingAlgorithm");
41+
}
42+
43+
ok(!signingAlgorithm->check("shafake"), ".. and this is not");
44+
like(
45+
signingAlgorithm->get_message("shafake"),
46+
qr/is not a supported signingAlgorithm/,
47+
".. with the correct error message"
48+
);
49+
};
50+
51+
done_testing;

0 commit comments

Comments
 (0)