| 1 | #+############################################################################## | ||||||||||||||||||||||||||||
| 2 | # # | ||||||||||||||||||||||||||||
| 3 | # File: Net/STOMP/Client/Auth.pm # | ||||||||||||||||||||||||||||
| 4 | # # | ||||||||||||||||||||||||||||
| 5 | # Description: Authentication support for Net::STOMP::Client # | ||||||||||||||||||||||||||||
| 6 | # # | ||||||||||||||||||||||||||||
| 7 | #-############################################################################## | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | # module definition | ||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | package Net::STOMP::Client::Auth; | ||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 2 | our $VERSION = "2.4"; | |||||||||||||||||||||||||||
| 17 | 2 | our $REVISION = sprintf("%d.%02d", q$Revision: 2.1 $ =~ /(\d+)\.(\d+)/); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # | ||||||||||||||||||||||||||||
| 20 | # used modules | ||||||||||||||||||||||||||||
| 21 | # | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use No::Worries::Die qw(dief); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 2 | ▶ | use Params::Validate qw(validate_pos :types); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # | ||||||||||||||||||||||||||||
| 27 | # Authen::Credential is optional | ||||||||||||||||||||||||||||
| 28 | # | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 2 | eval { require Authen::Credential }; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # | ||||||||||||||||||||||||||||
| 33 | # check a single authentication | ||||||||||||||||||||||||||||
| 34 | # | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | sub _chkauth ($) { | ||||||||||||||||||||||||||||
| 37 | 0 | ▶ | my($auth) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_chkauth: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | return(Authen::Credential->parse($auth)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | if ref($auth) eq ""; | ||||||||||||||||||||||||||||
| 41 | 0 | ▶ | return($auth) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $auth and $auth->isa("Authen::Credential")
MC/DC0%0/2
ref $auth and $auth->isa("Authen::Credential")
ref $auth
$auth->isa("Authen::Credential")
Truth table0%0/3
A and B
A= ref $authB= $auth->isa("Authen::Credential")
| |||||||||||||||||||||||||||||
| 42 | if ref($auth) and $auth->isa("Authen::Credential"); | ||||||||||||||||||||||||||||
| 43 | 0 | dief("unexpected authentication: %s", $auth); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # | ||||||||||||||||||||||||||||
| 47 | # setup | ||||||||||||||||||||||||||||
| 48 | # | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub _setup ($) { | ||||||||||||||||||||||||||||
| 51 | 2 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_setup: called CC=15 SCAR=52.0
| |||||||||||||||||||||||||||||
| 52 | 2 | my(@list, $scheme, $sslopts); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # no additional options if Authen::Credential is not available! | ||||||||||||||||||||||||||||
| 55 | 2 | ▶ | return() unless $self or $Authen::Credential::VERSION; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self or $Authen::Credential::VERSION
MC/DC0%0/2
$self or $Authen::Credential::VERSION
$self
$Authen::Credential::VERSION
Truth table33%1/3
A or B
A= $selfB= $Authen::Credential::VERSION
| |||||||||||||||||||||||||||||
| 56 | # additional options for new() | ||||||||||||||||||||||||||||
| 57 | return( | ||||||||||||||||||||||||||||
| 58 | 0 | ▶ | "auth" => { optional => 1, type => SCALAR|ARRAYREF|OBJECT }, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | ) unless $self; | ||||||||||||||||||||||||||||
| 60 | # check the given authentication | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | return() unless $self->{"auth"}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | if (ref($self->{"auth"}) eq "ARRAY") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | @list = map(_chkauth($_), @{ $self->{"auth"} }); | |||||||||||||||||||||||||||
| 64 | } else { | ||||||||||||||||||||||||||||
| 65 | 0 | @list = (_chkauth($self->{"auth"})); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | # make sure we have at most one X.509 and one plain|none | ||||||||||||||||||||||||||||
| 68 | 0 | foreach my $auth (@list) { | |||||||||||||||||||||||||||
| 69 | 0 | $auth->check(); | |||||||||||||||||||||||||||
| 70 | 0 | $scheme = $auth->scheme(); | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | if ($scheme eq "x509") { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 72 | dief("duplicate authentication: %s", $auth->string()) | ||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if exists($self->{"x509_auth"}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | $self->{"x509_auth"} = $auth; | |||||||||||||||||||||||||||
| 75 | } elsif ($scheme eq "none") { | ||||||||||||||||||||||||||||
| 76 | dief("duplicate authentication: %s", $auth->string()) | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | if exists($self->{"plain_auth"}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | $self->{"plain_auth"} = ""; # special case... | |||||||||||||||||||||||||||
| 79 | } elsif ($scheme eq "plain") { | ||||||||||||||||||||||||||||
| 80 | dief("duplicate authentication: %s", $auth->string()) | ||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if exists($self->{"plain_auth"}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | $self->{"plain_auth"} = $auth; | |||||||||||||||||||||||||||
| 83 | } else { | ||||||||||||||||||||||||||||
| 84 | 0 | dief("unsupported authentication scheme: %s", $scheme); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | # use the X.509 authentication via the socket options | ||||||||||||||||||||||||||||
| 88 | 0 | ▶ | if ($self->{"x509_auth"}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 0 | $sslopts = $self->{"x509_auth"}->prepare("IO::Socket::SSL"); | |||||||||||||||||||||||||||
| 90 | 0 | ▶ | while (my($name, $value) = each(%{ $sslopts })) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | $self->{"sockopts"}{$name} = $value; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # | ||||||||||||||||||||||||||||
| 97 | # hook for the CONNECT frame | ||||||||||||||||||||||||||||
| 98 | # | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | sub _connect_hook ($$) { | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | my($self, $frame) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_connect_hook: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | return unless $self->{"plain_auth"}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | # do not override what the user did put in the frame | ||||||||||||||||||||||||||||
| 105 | 0 | ▶ | $frame->header("login", $self->{"plain_auth"}->name()) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | unless defined($frame->header("login")); | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | $frame->header("passcode", $self->{"plain_auth"}->pass()) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | unless defined($frame->header("passcode")); | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # | ||||||||||||||||||||||||||||
| 112 | # register the setup and hook | ||||||||||||||||||||||||||||
| 113 | # | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | { | ||||||||||||||||||||||||||||
| 116 | 2 | ▶ | no warnings qw(once); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 117 | 2 | $Net::STOMP::Client::Setup{"auth"} = \&_setup; | |||||||||||||||||||||||||||
| 118 | 2 | $Net::STOMP::Client::Hook{"CONNECT"}{"auth"} = \&_connect_hook; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 2 | 1; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | __END__ | ||||||||||||||||||||||||||||