| 1 | package Net::SAML2::Binding::Redirect; | ||||||||||||||||||||||||||||
| 2 | 32 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 32 | our $VERSION = '0.89'; # VERSION | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 32 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 32 | ▶ | use Crypt::PK::RSA; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 32 | ▶ | use Crypt::OpenSSL::X509; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 32 | ▶ | use File::Slurper qw/ read_text /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 32 | ▶ | use IO::Compress::RawDeflate qw/ rawdeflate /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 32 | ▶ | use IO::Uncompress::RawInflate qw/ rawinflate /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 32 | ▶ | use MIME::Base64 qw/ encode_base64 decode_base64 /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 32 | ▶ | use MooseX::Types::URI qw/ Uri /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 32 | ▶ | use Net::SAML2::Types qw(signingAlgorithm SAMLRequestType); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 32 | ▶ | use URI::Encode qw/uri_decode/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 32 | ▶ | use URI::Escape qw(uri_unescape); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 32 | ▶ | use URI::QueryParam; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 32 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # ABSTRACT: HTTP Redirect binding for SAML | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 32 | has 'cert' => (isa => 'ArrayRef[Str]', is => 'ro', required => 0, predicate => 'has_cert'); | |||||||||||||||||||||||||||
| 25 | 32 | has 'url' => (isa => Uri, is => 'ro', required => 0, coerce => 1, predicate => 'has_url'); | |||||||||||||||||||||||||||
| 26 | 32 | has 'key' => (isa => 'Str', is => 'ro', required => 0, predicate => 'has_key'); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 32 | has 'insecure' => (isa => 'Bool', is => 'ro', default => 0 ); | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 32 | has 'param' => ( | |||||||||||||||||||||||||||
| 31 | isa => SAMLRequestType, | ||||||||||||||||||||||||||||
| 32 | is => 'ro', | ||||||||||||||||||||||||||||
| 33 | required => 0, | ||||||||||||||||||||||||||||
| 34 | default => 'SAMLRequest' | ||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 32 | has 'sig_hash' => ( | |||||||||||||||||||||||||||
| 38 | isa => signingAlgorithm, | ||||||||||||||||||||||||||||
| 39 | is => 'ro', | ||||||||||||||||||||||||||||
| 40 | required => 0, | ||||||||||||||||||||||||||||
| 41 | default => 'sha256' | ||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 32 | has debug => ( | |||||||||||||||||||||||||||
| 45 | is => 'ro', | ||||||||||||||||||||||||||||
| 46 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 47 | required => 0, | ||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 14 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=7 SCAR=19.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | 14 | my $self = shift; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 14 | ▶ | if ($self->param eq 'SAMLRequest') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 55 | 9 | ▶ | croak("Need to have an URL specified") unless $self->has_url; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 8 | ▶ | croak("Need to have a key specified") unless $self->has_key || $self->insecure; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->has_key or $self->insecure
MC/DC100%2/2
$self->has_key or $self->insecure
$self->has_key
$self->insecure
Truth table100%3/3
A or B
A= $self->has_keyB= $self->insecure
| |||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | elsif ($self->param eq 'SAMLResponse') { | ||||||||||||||||||||||||||||
| 59 | 5 | ▶ | croak("Need to have a cert specified") unless $self->has_cert; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # BUILDARGS | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # Earlier versions expected the cert to be a string. However, metadata | ||||||||||||||||||||||||||||
| 66 | # can include multiple signing certificates so the $idp->cert is now | ||||||||||||||||||||||||||||
| 67 | # expected to be an arrayref to the certificates. To avoid breaking existing | ||||||||||||||||||||||||||||
| 68 | # applications this changes the the cert to an arrayref if it is not | ||||||||||||||||||||||||||||
| 69 | # already an array ref. | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 14 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 72 | 14 | my $orig = shift; | |||||||||||||||||||||||||||
| 73 | 14 | my $self = shift; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 14 | my %params = @_; | |||||||||||||||||||||||||||
| 76 | 14 | ▶ | if ($params{cert} && ref($params{cert}) ne 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$params{"cert"} and ref $params{"cert"} ne "ARRAY"
MC/DC100%2/2
$params{"cert"} and ref $params{"cert"} ne "ARRAY"
$params{"cert"}
ref $params{"cert"} ne "ARRAY"
Truth table100%3/3
A and B
A= $params{"cert"}B= ref $params{"cert"} ne "ARRAY"
| |||||||||||||||||||||||||||||
| 77 | 5 | $params{cert} = [$params{cert}]; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 14 | return $self->$orig(%params); | |||||||||||||||||||||||||||
| 81 | 32 | }; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 4 | ▶ | sub get_redirect_uri { | ||||||||||||||||||||||||||
|
Subroutine
get_redirect_uri: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 86 | 4 | my $request = shift; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 4 | ▶ | if (!defined $request) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 0 | croak("Unable to create redirect URI without a request"); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 4 | my $relaystate = shift; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 4 | my $input = "$request"; | |||||||||||||||||||||||||||
| 95 | 4 | my $output = ''; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 4 | rawdeflate \$input => \$output; | |||||||||||||||||||||||||||
| 98 | 4 | my $req = encode_base64($output, ''); | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 4 | my $uri = URI->new($self->url); | |||||||||||||||||||||||||||
| 101 | 4 | $uri->query_param($self->param, $req); | |||||||||||||||||||||||||||
| 102 | 4 | ▶ | $uri->query_param('RelayState', $relaystate) if defined $relaystate; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 4 | ▶ | return $uri->as_string if $self->insecure; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 2 | return $self->_sign_redirect_uri($uri); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 2 | ▶ | sub _sign_redirect_uri { | ||||||||||||||||||||||||||
|
Subroutine
_sign_redirect_uri: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 109 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 110 | 2 | my $uri = shift; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 2 | my $key_string = read_text($self->key); | |||||||||||||||||||||||||||
| 113 | 2 | my $pk = Crypt::PK::RSA->new(); | |||||||||||||||||||||||||||
| 114 | 2 | my $rsa_priv = $pk->import_key(\$key_string); | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 2 | ▶ | $uri->query_param('SigAlg', | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | $self->sig_hash eq 'sha1' | ||||||||||||||||||||||||||||
| 118 | ? 'http://www.w3.org/2000/09/xmldsig#rsa-sha1' | ||||||||||||||||||||||||||||
| 119 | : 'http://www.w3.org/2001/04/xmldsig-more#rsa-' . $self->sig_hash); | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 2 | my $to_sign = $uri->query; | |||||||||||||||||||||||||||
| 122 | 2 | my $sig = encode_base64($rsa_priv->sign_message($to_sign, uc($self->sig_hash), 'v1.5'), ''); | |||||||||||||||||||||||||||
| 123 | 2 | $uri->query_param('Signature', $sig); | |||||||||||||||||||||||||||
| 124 | 2 | return $uri->as_string; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 3 | ▶ | sub sign { | ||||||||||||||||||||||||||
|
Subroutine
sign: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 3 | ▶ | if ($self->insecure) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 1 | croak("Cannot sign an insecure request!"); | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 2 | return $self->get_redirect_uri(@_); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 7 | ▶ | sub verify { | ||||||||||||||||||||||||||
|
Subroutine
verify: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 140 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 141 | 7 | my $query_string = shift; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 7 | $query_string =~ s#^.*\?##; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 7 | my %params = map { split(/=/, $_, 2) } split(/&/, $query_string); | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 7 | my $sigalg = uri_unescape($params{SigAlg}); | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 7 | my $encoded_sig = uri_unescape($params{Signature}); | |||||||||||||||||||||||||||
| 150 | 7 | my $sig = decode_base64($encoded_sig); | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 7 | my @signed_parts; | |||||||||||||||||||||||||||
| 153 | 7 | for my $p ($self->param, qw(RelayState SigAlg)) { | |||||||||||||||||||||||||||
| 154 | 21 | ▶ | push @signed_parts, join('=', $p, $params{$p}) if exists $params{$p}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | 7 | my $signed = join('&', @signed_parts); | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 7 | $self->_verify($sigalg, $signed, $sig); | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | # unpack the SAML request | ||||||||||||||||||||||||||||
| 161 | 7 | my $deflated = decode_base64(uri_unescape($params{$self->param})); | |||||||||||||||||||||||||||
| 162 | 7 | my $request = ''; | |||||||||||||||||||||||||||
| 163 | 7 | rawinflate \$deflated => \$request; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # return the relaystate if it is defined | ||||||||||||||||||||||||||||
| 166 | 7 | ▶ | return ($request, defined $params{RelayState} ? uri_unescape($params{RelayState}) : undef); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 7 | ▶ | sub _verify { | ||||||||||||||||||||||||||
|
Subroutine
_verify: called CC=10 SCAR=29.6
| |||||||||||||||||||||||||||||
| 170 | 7 | my ($self, $sigalg, $signed, $sig) = @_; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 7 | foreach my $crt (@{$self->cert}) { | |||||||||||||||||||||||||||
| 173 | 7 | my $cert = Crypt::OpenSSL::X509->new_from_string($crt); | |||||||||||||||||||||||||||
| 174 | 7 | my $pk = Crypt::PK::RSA->new(); | |||||||||||||||||||||||||||
| 175 | 7 | my $rsa_pub = $pk->import_key(\$cert->pubkey); | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 7 | my $hash_name; | |||||||||||||||||||||||||||
| 178 | 7 | ▶ | if ($sigalg eq 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha256') { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 179 | 2 | $hash_name = 'SHA256'; | |||||||||||||||||||||||||||
| 180 | } elsif ($sigalg eq 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha224') { | ||||||||||||||||||||||||||||
| 181 | 0 | $hash_name = 'SHA224'; | |||||||||||||||||||||||||||
| 182 | } elsif ($sigalg eq 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha384') { | ||||||||||||||||||||||||||||
| 183 | 0 | $hash_name = 'SHA384'; | |||||||||||||||||||||||||||
| 184 | } elsif ($sigalg eq 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha512') { | ||||||||||||||||||||||||||||
| 185 | 0 | $hash_name = 'SHA512'; | |||||||||||||||||||||||||||
| 186 | } elsif ($sigalg eq 'http://www.w3.org/2000/09/xmldsig#rsa-sha1') { | ||||||||||||||||||||||||||||
| 187 | 5 | $hash_name = 'SHA1'; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | else { | ||||||||||||||||||||||||||||
| 190 | 0 | ▶ | warn "Unsupported Signature Algorithim: $sigalg, defaulting to sha256" if $self->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 7 | ▶ | return 1 if $rsa_pub->verify_message($sig, $signed, $hash_name, 'v1.5'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 0 | ▶ | warn "Unable to verify with " . $cert->subject if $self->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 0 | croak("Unable to verify the XML signature"); | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 32 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | __END__ | ||||||||||||||||||||||||||||