| 1 | package Dancer::Plugin::Auth::CAS; | ||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||
| 3 | 1 | $Dancer::Plugin::Auth::CAS::VERSION = '1.128'; | |||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||
| 8 | Dancer::Plugin::Auth::CAS - CAS sso authentication for Dancer | ||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||
| 10 | =cut | ||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||
| 12 | 1 | ▶ | use warnings; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 13 | 1 | ▶ | use strict; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||
| 15 | 1 | ▶ | use Dancer ':syntax'; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 16 | 1 | ▶ | use Dancer::Plugin; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 17 | 1 | ▶ | use Dancer::Response; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 18 | 1 | ▶ | use Dancer::Exception ':all'; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 19 | 1 | ▶ | use HTTP::Headers; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 20 | 1 | ▶ | use Authen::CAS::Client; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 21 | 1 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||
| 23 | 1 | our $VERSION; | |||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||
| 25 | 1 | register_exception('InvalidConfig', message_pattern => "Invalid or missing configuration: %s"); | |||||||||||||||||||||||
| 26 | 1 | register_exception('CasError', message_pattern => "Unable to auth with CAS backend: %s"); | |||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||
| 28 | 1 | my $settings = plugin_setting; | |||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||
| 30 | 0 | ▶ | sub _auth_cas { | ||||||||||||||||||||||
|
Subroutine
_auth_cas: not called CC=21 SCAR=61.4
| |||||||||||||||||||||||||
| 31 | 0 | my (%options) = @_; | |||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||
| 33 | 0 | ▶ | my $base_url = $settings->{cas_url} // raise( InvalidConfig => "cas_url is unset" ); | ||||||||||||||||||||||
|
Condition0%0/3
$settings->{"cas_url"} // &raise("InvalidConfig", "cas_url is unset")
MC/DC0%0/2
$settings->{"cas_url"} // &raise("InvalidConfig", "cas_url is unset")
$settings->{"cas_url"}
&raise("InvalidConfig", "cas_url is unset")
Truth table0%0/3
A // B
A= $settings->{"cas_url"}B= &raise("InvalidConfig", "cas_url is unset")
| |||||||||||||||||||||||||
| 34 | 0 | ▶ | my $cas_version = $settings->{cas_version} || raise( InvalidConfig => "cas_version is unset"); | ||||||||||||||||||||||
|
Condition0%0/3
$settings->{'cas_version'} || &raise('InvalidConfig', 'cas_version is unset')
MC/DC0%0/2
$settings->{'cas_version'} || &raise('InvalidConfig', 'cas_version is unset')
$settings->{'cas_version'}
&raise('InvalidConfig', 'cas_version is unset')
Truth table0%0/3
A || B
A= $settings->{'cas_version'}B= &raise('InvalidConfig', 'cas_version is unset')
| |||||||||||||||||||||||||
| 35 | 0 | ▶ | my $cas_user_map = $options{cas_user_map} || $settings->{cas_user_map} || 'cas_user'; | ||||||||||||||||||||||
|
Condition0%0/5
$options{'cas_user_map'} || $settings->{'cas_user_map'} || 'cas_user'
$options{"cas_user_map"} or $settings->{"cas_user_map"}
MC/DC0%0/2
$options{'cas_user_map'} || $settings->{'cas_user_map'} || 'cas_user'
$options{"cas_user_map"}
$settings->{"cas_user_map"}
Truth table0%0/3
A or B
A= $options{"cas_user_map"}B= $settings->{"cas_user_map"}
| |||||||||||||||||||||||||
| 36 | 0 | ▶ | my $cas_denied_url = $options{cas_denied_path} || $settings->{cas_denied_path} || '/denied'; | ||||||||||||||||||||||
|
Condition0%0/5
$options{'cas_denied_path'} || $settings->{'cas_denied_path'} || '/denied'
$options{"cas_denied_path"} or $settings->{"cas_denied_path"}
MC/DC0%0/2
$options{'cas_denied_path'} || $settings->{'cas_denied_path'} || '/denied'
$options{"cas_denied_path"}
$settings->{"cas_denied_path"}
Truth table0%0/3
A or B
A= $options{"cas_denied_path"}B= $settings->{"cas_denied_path"}
| |||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||
| 38 | 0 | my $ssl_verify_hostname = $settings->{ssl_verify_hostname}; | |||||||||||||||||||||||
| 39 | 0 | ▶ | $ENV{"PERL_LWP_SSL_VERIFY_HOSTNAME"} = defined( $ssl_verify_hostname ) ? $ssl_verify_hostname : 1; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||
| 41 | # check supported versions | ||||||||||||||||||||||||
| 42 | 0 | ▶ | unless( grep(/$cas_version/, qw( 2.0 1.0 )) ) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 43 | 0 | raise( InvalidConfig => "cas_version '$cas_version' not supported"); | |||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||
| 46 | 0 | ▶ | my $mapping = $settings->{cas_attr_map} || {}; | ||||||||||||||||||||||
|
Condition0%0/2
$settings->{'cas_attr_map'} || {}
MC/DC0%0/1
$settings->{'cas_attr_map'} || {}
$settings->{'cas_attr_map'}
Truth table0%0/2
A
A= $settings->{'cas_attr_map'}
| |||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||
| 48 | 0 | my $ticket = $options{ticket}; | |||||||||||||||||||||||
| 49 | 0 | my $params = request->params; | |||||||||||||||||||||||
| 50 | 0 | ▶ | unless( $ticket ) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 51 | 0 | my $tickets = $params->{ticket}; | |||||||||||||||||||||||
| 52 | # For the case when application also uses 'ticket' parameters | ||||||||||||||||||||||||
| 53 | # we only remove the real cas service ticket | ||||||||||||||||||||||||
| 54 | 0 | ▶ | if( ref($tickets) eq "ARRAY" ) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 55 | 0 | ▶ | while( my ($index, $value) = each @$tickets ) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 56 | # The 'ST-' is specified in CAS-protocol | ||||||||||||||||||||||||
| 57 | 0 | ▶ | if( $value =~ m/^ST\-/ ) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 58 | 0 | $ticket = delete $tickets->[$index]; | |||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||
| 61 | } else { | ||||||||||||||||||||||||
| 62 | 0 | $ticket = delete $params->{ticket}; | |||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||
| 65 | 0 | my $service = uri_for( request->path_info, $params ); | |||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||
| 67 | 0 | my $cas = Authen::CAS::Client->new( $base_url ); | |||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||
| 69 | 0 | my $user = session($cas_user_map); | |||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||
| 71 | 0 | ▶ | unless( $user ) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||
| 73 | 0 | my $response = Dancer::Response->new( status => 302 ); | |||||||||||||||||||||||
| 74 | 0 | my $redirect_url; | |||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||
| 76 | 0 | ▶ | if( $ticket) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 77 | 0 | debug "Trying to validate via CAS '$cas_version' with ticket=$ticket"; | |||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||
| 79 | 0 | my $r; | |||||||||||||||||||||||
| 80 | 0 | ▶ | if( $cas_version eq "1.0" ) { | ||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||
| 81 | 0 | $r = $cas->validate( $service, $ticket ); | |||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||
| 83 | elsif( $cas_version eq "2.0" ) { | ||||||||||||||||||||||||
| 84 | 0 | $r = $cas->service_validate( $service, $ticket ); | |||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||
| 86 | else { | ||||||||||||||||||||||||
| 87 | 0 | raise( InvalidConfig => "cas_version '$cas_version' not supported"); | |||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||
| 90 | 0 | ▶ | if( $r->is_success ) { | ||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||
| 92 | # Redirect to given path | ||||||||||||||||||||||||
| 93 | 0 | info "Authenticated as: ".$r->user; | |||||||||||||||||||||||
| 94 | 0 | ▶ | if( $cas_version eq "1.0" ) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 95 | 0 | session $cas_user_map => $r->user; | |||||||||||||||||||||||
| 96 | } else { | ||||||||||||||||||||||||
| 97 | 0 | session $cas_user_map => _map_attributes( $r->doc, $mapping ); | |||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||
| 99 | 0 | $redirect_url = $service; | |||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||
| 101 | } elsif( $r->is_failure ) { | ||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||
| 103 | # Redirect to denied | ||||||||||||||||||||||||
| 104 | 0 | debug "Failed to authenticate: ".$r->code." / ".$r->message; | |||||||||||||||||||||||
| 105 | 0 | $redirect_url = uri_for( $cas_denied_url ); | |||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||
| 107 | } else { | ||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||
| 109 | # Raise hard error, backend has errors | ||||||||||||||||||||||||
| 110 | 0 | error "Unable to authenticate: ".$r->error; | |||||||||||||||||||||||
| 111 | 0 | raise( CasError => $r->error ); | |||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||
| 114 | } else { | ||||||||||||||||||||||||
| 115 | # Has no ticket, needs one | ||||||||||||||||||||||||
| 116 | 0 | debug "Redirecting to CAS: ".$cas->login_url( $service ); | |||||||||||||||||||||||
| 117 | 0 | $redirect_url = $cas->login_url( $service ); | |||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||
| 120 | # General redir response | ||||||||||||||||||||||||
| 121 | 0 | $response->header( Location => $redirect_url ); | |||||||||||||||||||||||
| 122 | 0 | halt( $response ); | |||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||
| 127 | 0 | ▶ | sub _map_attributes { | ||||||||||||||||||||||
|
Subroutine
_map_attributes: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||
| 128 | 0 | my ( $doc, $mapping ) = @_; | |||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||
| 130 | 0 | my $attrs = {}; | |||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||
| 132 | 0 | my $result = $doc->find( '/cas:serviceResponse/cas:authenticationSuccess' ); | |||||||||||||||||||||||
| 133 | 0 | ▶ | if( $result ) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 134 | 0 | my $node = $result->get_node(1); | |||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||
| 136 | # extra all attributes | ||||||||||||||||||||||||
| 137 | 0 | my @attributes = $node->findnodes( "./cas:attributes/*" ); | |||||||||||||||||||||||
| 138 | 0 | foreach my $a (@attributes) { | |||||||||||||||||||||||
| 139 | 0 | my $name = (split(/:/, $a->nodeName, 2))[1]; | |||||||||||||||||||||||
| 140 | 0 | my $val = $a->textContent; | |||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||
| 142 | 0 | ▶ | my $mapped_name = $mapping->{ $name } // $name; | ||||||||||||||||||||||
|
Condition0%0/3
$mapping->{$name} // $name
MC/DC0%0/2
$mapping->{$name} // $name
$mapping->{$name}
$name
Truth table0%0/3
A // B
A= $mapping->{$name}B= $name
| |||||||||||||||||||||||||
| 143 | 0 | $attrs->{ $mapped_name } = $val; | |||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||
| 147 | 0 | debug "Mapped attributes: ".to_dumper( $attrs ); | |||||||||||||||||||||||
| 148 | 0 | return $attrs; | |||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||
| 152 | 1 | register auth_cas => \&_auth_cas; | |||||||||||||||||||||||
| 153 | 1 | register_plugin; | |||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||
| 155 | 1 | 1; # End of Dancer::Plugin::Auth::CAS | |||||||||||||||||||||||
| 156 | __END__ | ||||||||||||||||||||||||