| 1 | package Punk::Command::SAML; | ||||||||||||
| 2 | |||||||||||||
| 3 | 2 | ▶ | use 5.010; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 2 | ▶ | use Punk::SAML (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | sub run { | ||||||||||||
| 10 | 15 | ▶ | my ($class, @argv) = @_; | ||||||||||
|
Subroutine
run: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 11 | 15 | ▶ | my $sub = shift(@argv) // ''; | ||||||||||
|
Condition100%2/2
shift @argv // ""
MC/DC100%1/1
shift @argv // ""
shift @argv
Truth table100%2/2
A
A= shift @argv
| |||||||||||||
| 12 | 15 | my $m = $class->can("cmd_$sub"); | |||||||||||
| 13 | 15 | ▶ | return $class->usage unless $m; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 14 | 12 | return $class->$m(@argv); | |||||||||||
| 15 | } | ||||||||||||
| 16 | |||||||||||||
| 17 | sub usage { | ||||||||||||
| 18 | 5 | ▶ | print <<'USAGE'; | ||||||||||
|
Subroutine
usage: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 19 | punk saml key a secret for the flow cookie | ||||||||||||
| 20 | punk saml metadata this application's SP metadata | ||||||||||||
| 21 | punk saml idp <file-or-url> what this plugin reads from metadata | ||||||||||||
| 22 | punk saml verify <file> [--at N] run the checks over a saved Response | ||||||||||||
| 23 | USAGE | ||||||||||||
| 24 | 5 | return 1; | |||||||||||
| 25 | } | ||||||||||||
| 26 | |||||||||||||
| 27 | sub cmd_key { | ||||||||||||
| 28 | 2 | ▶ | require Crypt::JWS; | ||||||||||
|
Subroutine
cmd_key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 29 | 2 | print Crypt::JWS::b64url(Crypt::JWS::random_bytes(32)), "\n"; | |||||||||||
| 30 | 2 | return 0; | |||||||||||
| 31 | } | ||||||||||||
| 32 | |||||||||||||
| 33 | sub cmd_metadata { | ||||||||||||
| 34 | 1 | ▶ | my ($class, %o) = @_; | ||||||||||
|
Subroutine
cmd_metadata: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 35 | 1 | require Punk::SAML::Metadata; | |||||||||||
| 36 | 1 | print Punk::SAML::Metadata->build(%o), "\n"; | |||||||||||
| 37 | 1 | return 0; | |||||||||||
| 38 | } | ||||||||||||
| 39 | |||||||||||||
| 40 | sub cmd_idp { | ||||||||||||
| 41 | 4 | ▶ | my ($class, $src, @rest) = @_; | ||||||||||
|
Subroutine
cmd_idp: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||
| 42 | 4 | ▶ | return $class->usage unless defined $src; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 43 | 3 | require Punk::SAML::IdP; | |||||||||||
| 44 | 3 | my $bytes = _slurp($src); | |||||||||||
| 45 | 3 | my %o = @rest; | |||||||||||
| 46 | 3 | my $idp = eval { Punk::SAML::IdP->read($bytes, %o) }; | |||||||||||
| 47 | 3 | ▶ | if (my $e = $@) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 48 | 1 | ▶ | print ref $e ? "refused: $e->{code}: $e->{message}\n" : "refused: $e"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 49 | 1 | return 1; | |||||||||||
| 50 | } | ||||||||||||
| 51 | 2 | printf "entity_id %s\n", $idp->{entity_id}; | |||||||||||
| 52 | 2 | printf "sso_url %s\n", $idp->{sso_url}; | |||||||||||
| 53 | printf "signed WantAuthnRequestsSigned=%s\n", | ||||||||||||
| 54 | 2 | ▶ | $idp->{want_authn_requests_signed} ? 'true' : 'false'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 55 | 2 | for my $i (0 .. $#{ $idp->{certs} }) { | |||||||||||
| 56 | 2 | printf "cert %-2d sha256:%s\n", $i, $idp->{fingerprints}[$i]; | |||||||||||
| 57 | } | ||||||||||||
| 58 | 2 | printf "nameid %s\n", $_ for @{ $idp->{name_id_formats} }; | |||||||||||
| 59 | 2 | return 0; | |||||||||||
| 60 | } | ||||||||||||
| 61 | |||||||||||||
| 62 | sub cmd_verify { | ||||||||||||
| 63 | 5 | ▶ | my ($class, $file, %o) = @_; | ||||||||||
|
Subroutine
cmd_verify: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||
| 64 | 5 | ▶ | return $class->usage unless defined $file; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 65 | 4 | require Punk::SAML::Response; | |||||||||||
| 66 | 4 | my $bytes = _slurp($file); | |||||||||||
| 67 | # the base64 as it came, or the XML | ||||||||||||
| 68 | 4 | ▶ | $bytes = Punk::SAML::_decode_field($bytes, 0) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 69 | unless $bytes =~ /\A\s*</; | ||||||||||||
| 70 | 4 | my $at = delete $o{'--at'}; | |||||||||||
| 71 | 4 | my $id = eval { | |||||||||||
| 72 | 4 | ▶ | Punk::SAML::Response->verify($bytes, %o, | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 73 | ($at ? (now => $at) : ())); | ||||||||||||
| 74 | }; | ||||||||||||
| 75 | 4 | ▶ | if (my $e = $@) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 76 | 2 | ▶ | print ref $e ? "refused: $e->{code}\n $e->{message}\n" : "refused: $e"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 77 | 2 | return 1; | |||||||||||
| 78 | } | ||||||||||||
| 79 | printf "accepted\n name_id %s\n idp %s\n", $id->{name_id}, | ||||||||||||
| 80 | 2 | ▶ | $id->{idp} // ''; | ||||||||||
|
Condition50%1/2
$id->{"idp"} // ""
MC/DC0%0/1
$id->{"idp"} // ""
$id->{"idp"}
Truth table50%1/2
A
A= $id->{"idp"}
| |||||||||||||
| 81 | 2 | for my $k (sort keys %{ $id->{attributes} }) { | |||||||||||
| 82 | 4 | printf " %-8s %s\n", $k, join ', ', @{ $id->{attributes}{$k} }; | |||||||||||
| 83 | } | ||||||||||||
| 84 | 2 | return 0; | |||||||||||
| 85 | } | ||||||||||||
| 86 | |||||||||||||
| 87 | sub _slurp { | ||||||||||||
| 88 | 7 | ▶ | my ($src) = @_; | ||||||||||
|
Subroutine
_slurp: called CC=2 SCAR=7.0
| |||||||||||||
| 89 | 7 | $src =~ s{^file:}{}; | |||||||||||
| 90 | 7 | ▶ | open my $fh, '<:raw', $src or die "Punk::SAML: $src: $!\n"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 91 | 7 | local $/; | |||||||||||
| 92 | 7 | return <$fh>; | |||||||||||
| 93 | } | ||||||||||||
| 94 | |||||||||||||
| 95 | 2 | 1; | |||||||||||
| 96 | |||||||||||||
| 97 | __END__ | ||||||||||||