blib/lib/Lemonldap/NG/Portal/Auth/OpenID.pm

filter: Statementstmt 28.5% 28 / 98 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 41.1% 7 / 17 Podpod 0.0% 0 / 6 totaltotal 19.8% 35 / 176 CC 22 SCAR 54.0
CC
22
Cov
19%
extractFormInfo
420.0
__ANON__
6.0
init
6.0
CRAP 222.2
« blib/lib/Lemonldap/NG/Portal/Auth/AD.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/CheckState.pm »
1 package Lemonldap::NG::Portal::Auth::OpenID;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Lemonldap::NG::Common::FormEncode;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Lemonldap::NG::Common::Regexp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Lemonldap::NG::Common::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Cache::FileCache;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Lemonldap::NG::Portal::Main::Constants qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
10 PE_BADCREDENTIALS
11 PE_BADPARTNER
12 PE_ERROR
13 PE_FIRSTACCESS
14 PE_OK
15 PE_REDIRECT
16 1 );
17  
18 1 our $VERSION = '2.23.0';
19  
20 1 extends 'Lemonldap::NG::Portal::Main::Auth';
21  
22 # PROPERTIES
23  
24 has secret => (
25 is => 'rw',
26 lazy => 1,
27 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
28 return $_[0]->conf->{openIdSecret}
29 0 ▶ || $_[0]->conf->{cipher}->encrypt(0);
Condition0%0/3
$_[0]->conf->{'openIdSecret'} || $_[0]->conf->{'cipher'}->encrypt(0)
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]->conf->{'openIdSecret'} || $_[0]->conf->{'cipher'}->encrypt(0)
$_[0]->conf->{'openIdSecret'} $_[0]->conf->{'cipher'}->encrypt(0)
Truth table0%0/3
A || B
A= $_[0]->conf->{'openIdSecret'}B= $_[0]->conf->{'cipher'}->encrypt(0)
ABresult
1X1
011
000
30 }
31 1 );
32  
33 has listIsWhite => (
34 is => 'rw',
35 lazy => 1,
36 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 0 ( $_[0]->conf->{openIdIDPList} =~ /^(\d);/ )[0] + 0;
38 }
39 1 );
40  
41 has idpList => (
42 is => 'rw',
43 lazy => 1,
44 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 Lemonldap::NG::Common::Regexp::reDomainsToHost(
46 0 ( $_[0]->conf->{openIdIDPList} =~ /^\d;(.*)$/ )[0] );
47 }
48 1 );
49  
50 has ua => (
51 is => 'rw',
52 lazy => 1,
53 0 ▶ builder => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
54  
55 # TODO : LWP options to use a proxy for example
56 0 my $ua = Lemonldap::NG::Common::UserAgent->new( $_[0]->{conf} );
57 0 return $ua;
58 }
59 1 );
60  
61 # INITIALIZATION
62  
63 0 ▶sub init {
Subroutine init: not called CC=2 SCAR=17.9
Pod undocumented
64 0 my $self = shift;
65  
66 0 eval { require Net::OpenID::Consumer };
67 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
68 0 $self->error("Unable to load Net::OpenID::Server: $@");
69 0 return 0;
70 }
71 0 return 1;
72 }
73  
74 # RUNNING METHODS
75  
76 0 ▶sub extractFormInfo {
Subroutine extractFormInfo: not called CC=20 SCAR=60.4
Pod undocumented
77 0 my ( $self, $req ) = @_;
78 $req->data->{csr} = Net::OpenID::Consumer->new(
79 ua => $self->ua(),
80 cache => $self->p->HANDLER->tsv->{refLocalStorage}
81 || Cache::FileCache->new,
82 args => $req,
83 consumer_secret => $self->conf->{openIdSecret},
84 0 ▶ required_root => $req->portal
Condition0%0/3
$self->p->HANDLER->tsv->{'refLocalStorage'} || 'Cache::FileCache'->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->p->HANDLER->tsv->{'refLocalStorage'} || 'Cache::FileCache'->new
$self->p->HANDLER->tsv->{'refLocalStorage'} 'Cache::FileCache'->new
Truth table0%0/3
A || B
A= $self->p->HANDLER->tsv->{'refLocalStorage'}B= 'Cache::FileCache'->new
ABresult
1X1
011
000
85 );
86  
87 0 my ( $url, $openid );
88  
89 # 1. If no openid element has been detected
90 0 $openid = $req->param('openid');
91 0 ▶ return PE_FIRSTACCESS
Branch: 1 branch
BranchTrueFalseTotal
unless $url = $req->param("openid_identifier") or $openid 0 0 0
Condition0%0/3
$url = $req->param("openid_identifier") or $openid
l!l&&r!l&&!r
000
MC/DC0%0/2
$url = $req->param("openid_identifier") or $openid
$url = $req->param("openid_identifier") $openid
Truth table0%0/3
A or B
A= $url = $req->param("openid_identifier")B= $openid
ABresult
1X1
011
000
92 unless ( $url = $req->param('openid_identifier') or $openid );
93  
94 # 2. Check OpenID responses
95 0 ▶ if ($openid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($openid) { } 0 0 0
96 0 my $csr = $req->data->{csr};
97  
98 # Remote error
99 0 ▶ unless ( $csr->is_server_response() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($csr->is_server_response) 0 0 0
100 0 $self->userLogger->info('No OpenID valid message found');
101 0 return PE_BADCREDENTIALS;
102 }
103  
104 # If confirmation is needed
105 0 ▶ if ( my $setup_url = $csr->user_setup_url ) {
Branch: 3 branches
BranchTrueFalseTotal
if (my $setup_url = $csr->user_setup_url) { } 0 0 0
elsif ($csr->user_cancel) { } 0 0 0
elsif ($req->data->{'vident'} = $csr->verified_identity) { } 0 0 0
106 0 $self->userLogger->info('OpenID confirmation needed');
107 0 $req->urldc($setup_url);
108 0 return PE_REDIRECT;
109 }
110  
111 # Check if user has refused to share his authentication
112 elsif ( $csr->user_cancel() ) {
113 0 $self->userLogger->info('OpenID request cancelled by user');
114 0 return PE_FIRSTACCESS;
115 }
116  
117 # TODO: check verified identity
118 elsif ( $req->data->{vident} = $csr->verified_identity ) {
119 0 $req->user( $req->data->{vident}->url() );
120 0 $self->userLogger->notice(
121 "OpenID good authentication for $req->{user}");
122 0 $req->{mustRedirect} = 1;
123 0 return PE_OK;
124 }
125  
126 # Other errors
127 else {
128 0 $self->logger->error( 'OpenID error: ' . $csr->err );
129 0 return PE_ERROR;
130 }
131 }
132  
133 # 3. Check if an OpenID url has been submitted
134 else {
135 0 my $tmp = $url;
136 0 $tmp =~ m#^https?://(.*?)/#;
137 0 ▶ if ( $tmp =~ $self->idpList xor $self->listIsWhite ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp =~ $self->idpList xor $self->listIsWhite) 0 0 0
Condition0%0/4
$tmp =~ $self->idpList xor $self->listIsWhite
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
$tmp =~ $self->idpList xor $self->listIsWhite
$tmp =~ $self->idpList $self->listIsWhite
Truth table0%0/4
A xor B
A= $tmp =~ $self->idpListB= $self->listIsWhite
ABresult
110
101
011
000
138 0 $self->userLogger->warn("$url is forbidden for openID exchange");
139 0 return PE_BADPARTNER;
140 }
141 0 my $claimed_identity = $req->data->{csr}->claimed_identity($url);
142  
143 # Check if url is valid
144 0 ▶ unless ($claimed_identity) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($claimed_identity) 0 0 0
145 0 $self->userLogger->warn( 'OpenID error : ' . $req->{csr}->err() );
146 0 return PE_BADCREDENTIALS;
147 }
148  
149 # Build the redirection
150 0 $self->logger->debug("OpenID redirection to $url");
151 0 my $req_url = $req->data->{_url};
152 0 ▶ my $return_to = $self->p->buildUrl(
Branch: 1 branch
BranchTrueFalseTotal
$req_url ? : 0 0 0
153 $req->portal,
154 {
155 openid => 1,
156 ( $req_url ? ( url => $req_url ) : () )
157 }
158 );
159 0 my $check_url = $claimed_identity->check_url(
160 return_to => $return_to,
161 trust_root => $req->portal,
162 delayed_return => 1,
163 );
164  
165 # If UserDB uses OpenID, add "OpenID Simple Registration Extension"
166 # compatible fields
167 0 ▶ if ( $self->p->getModule( $req, 'user' ) eq 'OpenID' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->p->getModule($req, "user") eq "OpenID") 0 0 0
168 0 my ( @r, @o );
169 my %vars = (
170 0 %{ $self->conf->{exportedVars} },
171 0 %{ $self->conf->{openIdExportedVars} }
172 );
173 0 ▶ while ( my ( $v, $k ) = each %vars ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %vars) 0 0 0
174 0 ▶ if ( $k =~ Lemonldap::NG::Common::Regexp::OPENIDSREGATTR() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ Lemonldap::NG::Common::Regexp::OPENIDSREGATTR()) { } 0 0 0
175 0 ▶ if ( $v =~ s/^!// ) { push @r, $k }
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ s/^!//) { } 0 0 0
176 0 else { push @o, $k }
177 }
178 else {
179 0 $self->logger->warn(
180 qq'Unknown "OpenID Simple Registration Extension" field name: $k'
181 );
182 }
183 }
184 0 my @tmp;
185 0 ▶ push @tmp, 'openid.sreg.required' => join( ',', @r ) if (@r);
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
186 0 ▶ push @tmp, 'openid.sreg.optional' => join( ',', @o ) if (@o);
Branch: 1 branch
BranchTrueFalseTotal
if @o 0 0 0
187 0 ▶ OpenID::util::push_url_arg( \$check_url, @tmp ) if (@tmp);
Branch: 1 branch
BranchTrueFalseTotal
if @tmp 0 0 0
188 }
189 0 $req->urldc($check_url);
190 0 return PE_REDIRECT;
191 }
192 }
193  
194 0 ▶sub authenticate {
Subroutine authenticate: not called CC=1 SCAR=6.9
Pod undocumented
195 0 return PE_OK;
196 }
197  
198 0 ▶sub setAuthSessionInfo {
Subroutine setAuthSessionInfo: not called CC=1 SCAR=6.9
Pod undocumented
199 0 my ( $self, $req ) = @_;
200 $req->{sessionInfo}->{authenticationLevel} =
201 0 $self->conf->{openIdAuthnLevel};
202 0 return PE_OK;
203 }
204  
205 0 ▶sub authLogout {
Subroutine authLogout: not called CC=1 SCAR=6.9
Pod undocumented
206 0 return PE_OK;
207 }
208  
209 0 ▶sub getDisplayType {
Subroutine getDisplayType: not called CC=1 SCAR=6.9
Pod undocumented
210 0 return "openidform";
211 }
212  
213 1 1;
« blib/lib/Lemonldap/NG/Portal/Auth/AD.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/CheckState.pm »