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

filter: Statementstmt 18.1% 23 / 127 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 23.8% 5 / 21 Podpod 50.0% 2 / 4 totaltotal 11.9% 30 / 252 CC 39 SCAR 39.6
CC
39
Cov
11%
_openIDResponse
132.0
__ANON__
110.0
init
30.0
CRAP 52.3
« blib/lib/Lemonldap/NG/Portal/UserDB/Demo.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Password/Base.pm »
1 package Lemonldap::NG::Portal::Issuer::OpenID;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Mouse;
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::Portal::Main::Constants qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
8 PE_OK
9 PE_ERROR
10 PE_CONFIRM
11 PE_REDIRECT
12 PE_BADPARTNER
13 PE_OPENID_BADID
14 PE_OPENID_EMPTY
15 PE_SENDRESPONSE
16 PE_OID_SERVICE_NOT_ALLOWED
17 1 );
18  
19 1 our $VERSION = '2.23.0';
20  
21 1 extends 'Lemonldap::NG::Portal::Main::Issuer';
22  
23 # PROPERTIES
24  
25 has secret => (
26 is => 'rw',
27 lazy => 1,
28 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
29 return $_[0]->conf->{openIdIssuerSecret}
30 0 ▶ || $_[0]->conf->{cipher}->encrypt(0);
Condition0%0/3
$_[0]->conf->{'openIdIssuerSecret'} || $_[0]->conf->{'cipher'}->encrypt(0)
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]->conf->{'openIdIssuerSecret'} || $_[0]->conf->{'cipher'}->encrypt(0)
$_[0]->conf->{'openIdIssuerSecret'} $_[0]->conf->{'cipher'}->encrypt(0)
Truth table0%0/3
A || B
A= $_[0]->conf->{'openIdIssuerSecret'}B= $_[0]->conf->{'cipher'}->encrypt(0)
ABresult
1X1
011
000
31 }
32 1 );
33  
34 has listIsWhite => (
35 is => 'rw',
36 lazy => 1,
37 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
38 0 ( $_[0]->conf->{openIdSPList} =~ /^(\d);/ )[0] + 0;
39 }
40 1 );
41  
42 has spList => (
43 is => 'rw',
44 lazy => 1,
45 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 Lemonldap::NG::Common::Regexp::reDomainsToHost(
47 0 ( $_[0]->conf->{openIdSPList} =~ /^\d;(.*)$/ )[0] );
48 }
49 1 );
50  
51 1 has openidPortal => ( is => 'rw' );
52  
53 1 has rule => ( is => 'rw' );
54  
55 # INITIALIZATION
56  
57 0 ▶sub init {
Subroutine init: not called CC=5 SCAR=34.0
Pod undocumented
58 0 my ($self) = @_;
59  
60 # Parse activation rule
61 0 my $hd = $self->p->HANDLER;
62 $self->logger->debug(
63 0 "OpenID rule -> " . $self->conf->{issuerDBOpenIDRule} );
64 my $rule =
65 0 $hd->buildSub( $hd->substitute( $self->conf->{issuerDBOpenIDRule} ) );
66 0 ▶ unless ($rule) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rule) 0 0 0
67 0 ▶ my $error = $hd->tsv->{jail}->error || '???';
Condition0%0/2
$hd->tsv->{'jail'}->error || '???'
l!l
00
MC/DC0%0/1
$hd->tsv->{'jail'}->error || '???'
$hd->tsv->{'jail'}->error
Truth table0%0/2
A
A= $hd->tsv->{'jail'}->error
Aresult
11
00
68 0 $self->error("Bad OpenID activation rule -> $error");
69 0 return 0;
70 }
71 0 $self->{rule} = $rule;
72  
73 0 eval { require Lemonldap::NG::Portal::Lib::OpenID::Server };
74 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
75 0 $self->error("Unable to load Net::OpenID::Server: $@");
76 0 return 0;
77 }
78 0 ▶ return 0 unless ( $self->SUPER::init() );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->SUPER::init 0 0 0
79 0 $self->openidPortal( $self->p->buildUrl( $self->path ) . '/' );
80  
81 #$openidPortal =~ s#(?<!:)//#/#g;
82 0 return 1;
83 }
84  
85 # RUNNING METHOD
86  
87 # Overwrite _redirect to handle server-to-server queries
88 0 ▶sub _redirect {
Subroutine _redirect: not called CC=5 SCAR=34.0
89 0 my ( $self, $req ) = @_;
90 0 ▶ unless ( $req->pdata->{issuerRequestopenidserver} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($req->pdata->{"issuerRequestopenidserver"}) 0 0 0
91 0 my $mode = $req->param('openid.mode');
92 0 ▶ unless ($mode) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mode) 0 0 0
93 0 $self->logger->debug('OpenID SP test');
94 0 return $self->p->doPE( $req, PE_OPENID_EMPTY );
95 }
96 0 ▶ if ( $mode eq 'associate' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq 'associate') { } 0 0 0
elsif ($mode eq 'check_authentication') { } 0 0 0
97 0 return $self->_openIDResponse( $req,
98 $self->openIDServer($req)->_mode_associate() );
99 }
100 elsif ( $mode eq 'check_authentication' ) {
101 0 return $self->_openIDResponse( $req,
102 $self->openIDServer($req)->_mode_check_authentication() );
103 }
104 }
105 0 return $self->SUPER::_redirect($req);
106 }
107  
108 0 ▶sub run {
Subroutine run: not called CC=4 SCAR=30.0
Pod documented
109 0 my ( $self, $req ) = @_;
110  
111 # Check activation rule
112 0 ▶ unless ( $self->rule->( $req, $req->sessionInfo ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->rule->($req, $req->sessionInfo)) 0 0 0
113 0 $self->userLogger->error('OpenID service not authorized');
114 0 return PE_OID_SERVICE_NOT_ALLOWED;
115 }
116  
117 0 my $mode = $req->param('openid.mode');
118 0 ▶ unless ($mode) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mode) 0 0 0
119 0 $self->logger->debug('OpenID SP test');
120 0 return PE_OPENID_EMPTY;
121 }
122  
123 0 ▶ unless ( $mode =~ /^checkid_(?:immediate|setup)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mode =~ /^checkid_(?:immediate|setup)/) 0 0 0
124 0 $self->logger->error(
125 "OpenID error : $mode is not known at this step (issuerForAuthUser)"
126 );
127 0 return PE_ERROR;
128 }
129 0 my @r = $self->openIDServer($req)->_mode_checkid($mode);
130 0 return $self->_openIDResponse( $req, @r );
131 }
132  
133 0 ▶sub logout {
Subroutine logout: not called CC=1 SCAR=6.9
Pod documented
134 0 return PE_OK;
135 }
136  
137 # INTERNAL METHODS
138  
139 # Create if not done a new Lemonldap::NG::Portal::Lib::OpenID::Server objet
140 0 ▶sub openIDServer {
Subroutine openIDServer: not called CC=2 SCAR=17.9
Pod undocumented
141 0 my ( $self, $req ) = @_;
142 0 ▶ return $req->data->{_openidserver} if ( $req->data->{_openidserver} );
Branch: 1 branch
BranchTrueFalseTotal
if $req->data->{"_openidserver"} 0 0 0
143  
144 $req->data->{_openidserver} =
145 Lemonldap::NG::Portal::Lib::OpenID::Server->new(
146 0 ▶ server_secret => sub { return $self->secret },
Subroutine __ANON__: not called CC=1 SCAR=6.9
147 0 ▶ args => sub { $req->param(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
148 endpoint_url => $self->openidPortal,
149 setup_url => $self->openidPortal,
150 0 ▶ get_user => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
151 return $req->{sessionInfo}
152 0 ▶ ->{ $self->conf->{openIdAttr} || $self->conf->{whatToTrace} };
Condition0%0/3
$self->conf->{'openIdAttr'} || $self->conf->{'whatToTrace'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->conf->{'openIdAttr'} || $self->conf->{'whatToTrace'}
$self->conf->{'openIdAttr'} $self->conf->{'whatToTrace'}
Truth table0%0/3
A || B
A= $self->conf->{'openIdAttr'}B= $self->conf->{'whatToTrace'}
ABresult
1X1
011
000
153 },
154 0 ▶ get_identity => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
155 0 my ( $u, $identity ) = @_;
156 0 ▶ return $identity unless $u;
Branch: 1 branch
BranchTrueFalseTotal
unless $u 0 0 0
157 0 return $self->openidPortal . $u;
158 },
159 0 ▶ is_identity => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
160 0 my ( $u, $identity ) = @_;
161 0 ▶ return 0 unless ( $u and $identity );
Branch: 1 branch
BranchTrueFalseTotal
unless $u and $identity 0 0 0
Condition0%0/3
$u and $identity
!ll&&!rl&&r
000
MC/DC0%0/2
$u and $identity
$u $identity
Truth table0%0/3
A and B
A= $uB= $identity
ABresult
0X0
100
111
162 0 ▶ if ( $u eq ( split '/', $identity )[-1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($u eq (split(m[/], $identity, 0))[-1]) { } 0 0 0
163 0 return 1;
164 }
165 else {
166 0 $self->{_badOpenIdentity} = 1;
167 0 return 0;
168 }
169 },
170 0 ▶ is_trusted => sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
171 0 my ( $u, $trust_root, $is_identity ) = @_;
172 0 ▶ return 0 unless ( $u and $is_identity );
Branch: 1 branch
BranchTrueFalseTotal
unless $u and $is_identity 0 0 0
Condition0%0/3
$u and $is_identity
!ll&&!rl&&r
000
MC/DC0%0/2
$u and $is_identity
$u $is_identity
Truth table0%0/3
A and B
A= $uB= $is_identity
ABresult
0X0
100
111
173 0 my $tmp = $trust_root;
174 0 $tmp =~ s#^http://(.*?)/#$1#;
175 0 ▶ if ( $tmp =~ $self->spList xor $self->listIsWhite ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($tmp =~ $self->spList xor $self->listIsWhite) { } 0 0 0
elsif ($req->{'sessionInfo'}{"_openidTrust$trust_root"}) { } 0 0 0
elsif ($req->param('confirm') and $req->param('confirm') == 1) { } 0 0 0
elsif ($req->param('confirm') and $req->param('confirm') == -1) { } 0 0 0
Condition0%0/4
$tmp =~ $self->spList xor $self->listIsWhite
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
$tmp =~ $self->spList xor $self->listIsWhite
$tmp =~ $self->spList $self->listIsWhite
Truth table0%0/4
A xor B
A= $tmp =~ $self->spListB= $self->listIsWhite
ABresult
110
101
011
000
176 0 $self->userLogger->warn(
177 "$trust_root is forbidden for openID exchange");
178 0 $req->data->{_openIdForbidden} = 1;
179 0 return 0;
180 }
181 elsif ( $req->{sessionInfo}->{"_openidTrust$trust_root"} ) {
182 0 $self->logger->debug('OpenID request already trusted');
183 0 return 1;
184 }
185 ▶ elsif ( $req->param("confirm") and $req->param("confirm") == 1 ) {
Condition0%0/3
$req->param("confirm") and $req->param("confirm") == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$req->param("confirm") and $req->param("confirm") == 1
$req->param("confirm") $req->param("confirm") == 1
Truth table0%0/3
A and B
A= $req->param("confirm")B= $req->param("confirm") == 1
ABresult
0X0
100
111
186 0 $self->p->updatePersistentSession( $req,
187 { "_openidTrust$trust_root" => 1 } );
188 0 return 1;
189 }
190 ▶ elsif ( $req->param("confirm") and $req->param("confirm") == -1 ) {
Condition0%0/3
$req->param("confirm") and $req->param("confirm") == -1
!ll&&!rl&&r
000
MC/DC0%0/2
$req->param("confirm") and $req->param("confirm") == -1
$req->param("confirm") $req->param("confirm") == -1
Truth table0%0/3
A and B
A= $req->param("confirm")B= $req->param("confirm") == -1
ABresult
0X0
100
111
191 0 $self->p->updatePersistentSession( $req,
192 { "_openidTrust$trust_root" => 0 } );
193 0 return 0;
194 }
195 else {
196 0 $self->logger->debug('OpenID request not trusted');
197 0 $req->data->{_openIdTrustRequired} = 1;
198 0 return 0;
199 }
200 },
201 extensions => {
202 0 ▶ sreg => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
203 0 ▶ return ( 1, {} ) unless (@_);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
204 0 require Lemonldap::NG::Portal::Lib::OpenID::SREG;
205 return
206 0 $self->Lemonldap::NG::Portal::Lib::OpenID::SREG::sregHook(
207 $req, @_ );
208 },
209 },
210 0 );
211 0 return $req->data->{_openidserver};
212 }
213  
214 # Manage Lemonldap::NG::Portal::OpenID::Server responses
215 # @return Lemonldap::NG::Portal error code
216 0 ▶sub _openIDResponse {
Subroutine _openIDResponse: not called CC=11 SCAR=48.8
217 0 my ( $self, $req, $type, $data ) = @_;
218  
219 # Redirect
220 0 ▶ if ( $type eq 'redirect' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'redirect') { } 0 0 0
elsif ($type eq 'setup') { } 0 0 0
elsif ($type) { } 0 0 0
221 0 $self->logger->debug("OpenID redirection to $data");
222 0 $req->{urldc} = $data;
223 0 return PE_REDIRECT;
224 }
225  
226 # Setup
227 elsif ( $type eq 'setup' ) {
228 0 ▶ if ( $req->data->{_openIdTrustRequired}
Branch: 3 branches
BranchTrueFalseTotal
if ($req->data->{'_openIdTrustRequired'} or $req->data->{'_openIdTrustExtMsg'}) { } 0 0 0
elsif ($req->data->{'_badOpenIdentity'}) { } 0 0 0
elsif ($req->data->{'_openIdForbidden'}) { } 0 0 0
Condition0%0/3
$req->data->{"_openIdTrustRequired"} or $req->data->{"_openIdTrustExtMsg"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$req->data->{"_openIdTrustRequired"} or $req->data->{"_openIdTrustExtMsg"}
$req->data->{"_openIdTrustRequired"} $req->data->{"_openIdTrustExtMsg"}
Truth table0%0/3
A or B
A= $req->data->{"_openIdTrustRequired"}B= $req->data->{"_openIdTrustExtMsg"}
ABresult
1X1
011
000
229 or $req->data->{_openIdTrustExtMsg} )
230 {
231  
232 # TODO
233 0 $req->info(
234 $self->loadTemplate(
235 $req,
236 'simpleInfo',
237 params => { trspan => "openidExchange,$data->{trust_root}" }
238 )
239 );
240 $req->info( $req->data->{_openIdTrustExtMsg} )
241 0 ▶ if ( $req->data->{_openIdTrustExtMsg} );
Branch: 1 branch
BranchTrueFalseTotal
if $req->data->{"_openIdTrustExtMsg"} 0 0 0
242 0 $self->logger->debug('OpenID confirmation');
243 0 foreach ( keys %{ $req->parameters } ) {
244 0 ▶ if (/^(?:openid.*|url)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(?:openid.*|url)$/) 0 0 0
245 0 $self->p->setHiddenFormValue( $req, $_, $req->param($_),
246 '', 0 );
247 }
248 }
249  
250 # TODO: understand why this is needed here and not in OIDC
251 0 delete $req->data->{_url};
252 0 return PE_CONFIRM;
253 }
254 elsif ( $req->data->{_badOpenIdentity} ) {
255 0 $self->userLogger->warn(
256 "The user $req->{sessionInfo}->{_user} tries to use the id \"$data->{identity}\" on $data->{trust_root}"
257 );
258 0 return PE_OPENID_BADID;
259 }
260 elsif ( $req->data->{_openIdForbidden} ) {
261 0 return PE_BADPARTNER;
262 }
263  
264 # User has refused sharing its data
265 else {
266 $self->userLogger->notice(
267 $req->{sessionInfo}->{ $self->conf->{whatToTrace} }
268 0 . ' refused to share its OpenIdentity' );
269 0 return PE_OK;
270 }
271 }
272 elsif ($type) {
273 0 $self->logger->debug("OpenID generated page ($type)");
274 0 $req->response(
275 $self->p->sendBinaryResponse( $req, $data, type => $type ) );
276 }
277 else {
278 0 $req->response(
279 $self->p->sendError(
280 $req, 'OpenID error ' . $self->openIDServer($req)->err()
281 )
282 );
283 }
284 0 return $req->response;
285 }
286 1 1;
« blib/lib/Lemonldap/NG/Portal/UserDB/Demo.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Password/Base.pm »