blib/lib/Lemonldap/NG/Portal/Captcha/CaptchEtat.pm

filter: Statementstmt 85.3% 134 / 157 Branchbran 63.4% 33 / 52 Conditioncond 51.6% 16 / 31 MC/DCmcdc 23.5% 4 / 17 Subroutinesub 100% 24 / 24 Podpod 0.0% 0 / 6 totaltotal 73.5% 211 / 287 CC 38 SCAR 16.7
CC
38
Cov
76%
captchaProxy
11.0
_isInvalidTokenResponse
10.7
_getNewToken
7.2
CRAP 5.3
« blib/lib/Lemonldap/NG/Portal/Auth/Choice.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Lib/Wrapper.pm »
1 package Lemonldap::NG::Portal::Captcha::CaptchEtat;
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 JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Lemonldap::NG::Common::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # Add constants used by this module
11  
12 1 our $VERSION = '2.23.0';
13  
14 1 extends 'Lemonldap::NG::Portal::Main::Plugin';
15  
16 has ua => (
17 is => 'rw',
18 lazy => 1,
19 2 ▶ builder => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
20 2 my $ua = Lemonldap::NG::Common::UserAgent->new( $_[0]->{conf} );
21 2 $ua->env_proxy();
22 2 return $ua;
23 }
24 1 );
25  
26 has client_id => (
27 is => 'ro',
28 lazy => 1,
29 3 ▶ builder => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
30 3 $_[0]->conf->{captchaOptions}->{clientId};
31 }
32 1 );
33  
34 has client_secret => (
35 is => 'ro',
36 lazy => 1,
37 3 ▶ builder => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
38 3 $_[0]->conf->{captchaOptions}->{clientSecret};
39 }
40 1 );
41  
42 has captcha_style_name => (
43 is => 'ro',
44 lazy => 1,
45 1 ▶ builder => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
46 1 ▶ $_[0]->conf->{captchaOptions}->{captchaStyleName} || 'captchaFR';
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->conf->{"captchaOptions"}{"captchaStyleName"} or "captchaFR" 1 0 1
47 }
48 1 );
49  
50 has oauth_token_endpoint => (
51 is => 'ro',
52 lazy => 1,
53 2 ▶ builder => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
54 $_[0]->conf->{captchaOptions}->{oauthTokenEndpoint}
55 2 ▶ || 'https://oauth.piste.gouv.fr/api/oauth/token';
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->conf->{"captchaOptions"}{"oauthTokenEndpoint"} or "https://oauth.piste.gouv.fr/api/oauth/token" 2 0 2
56 }
57 1 );
58  
59 has oauth_scope => (
60 is => 'ro',
61 lazy => 1,
62 2 ▶ builder => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
63 2 ▶ $_[0]->conf->{captchaOptions}->{oauthScope} || 'piste.captchetat';
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->conf->{"captchaOptions"}{"oauthScope"} or "piste.captchetat" 2 0 2
64 }
65 1 );
66  
67 has captcha_api_base => (
68 is => 'ro',
69 lazy => 1,
70 2 ▶ builder => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
71 $_[0]->conf->{captchaOptions}->{captchaApiBase}
72 2 ▶ || 'https://api.piste.gouv.fr/piste/captchetat/v2';
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->conf->{"captchaOptions"}{"captchaApiBase"} or "https://api.piste.gouv.fr/piste/captchetat/v2" 2 0 2
73 }
74 1 );
75  
76 has access_token_cache => (
77 is => 'rw',
78 3 ▶ builder => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
79 {
80 3 expires_at => 0,
81 value => 0,
82 }
83 },
84 1 );
85  
86 3 ▶sub init {
Subroutine init: called CC=3 SCAR=13.0
Pod undocumented
87 3 my ($self) = @_;
88 3 ▶ unless ( $self->client_id && $self->client_secret ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->client_id and $self->client_secret) 0 3 3
Condition33%1/3
$self->client_id and $self->client_secret
!ll&&!rl&&r
003
MC/DC0%0/2
$self->client_id and $self->client_secret
$self->client_id $self->client_secret
Truth table33%1/3
A and B
A= $self->client_idB= $self->client_secret
ABresult
0X0
100
111
89 0 $self->logger->error(
90 'Missing required options for CaptchEtat: clientId, clientSecret');
91 0 return 0;
92 }
93  
94 $self->addAuthRoute(
95 3 'simple-captcha-endpoint' => 'captchaProxy',
96 [qw(GET)]
97 );
98 3 $self->addUnauthRoute(
99 'simple-captcha-endpoint' => 'captchaProxy',
100 [qw(GET)]
101 );
102 3 return 1;
103 }
104  
105 10 ▶sub getToken {
Subroutine getToken: called CC=4 SCAR=13.9
Pod undocumented
106 10 my ($self) = @_;
107  
108 10 my $cache = $self->access_token_cache;
109  
110 10 ▶ if ( $cache->{expires_at}
Branch: 1 branch
BranchTrueFalseTotal
if ($cache->{"expires_at"} and $cache->{"expires_at"} > &CORE::GLOBAL::time() and $cache->{"value"}) 5 5 10
Condition83%5/6
$cache->{"expires_at"} and $cache->{"expires_at"} > &CORE::GLOBAL::time() and $cache->{"value"}
!ll&&!rl&&r
505
$cache->{"expires_at"} and $cache->{"expires_at"} > &CORE::GLOBAL::time()
!ll&&!rl&&r
415
MC/DC66%2/3
$cache->{"expires_at"} and $cache->{"expires_at"} > &CORE::GLOBAL::time() and $cache->{"value"}
$cache->{"expires_at"} $cache->{"expires_at"} > &CORE::GLOBAL::time() $cache->{"value"}
Truth table75%3/4
A and B and C
A= $cache->{"expires_at"}B= $cache->{"expires_at"} > &CORE::GLOBAL::time()C= $cache->{"value"}
ABCresult
0XX0
10X0
1100
1111
111 and $cache->{expires_at} > time
112 and $cache->{value} )
113 {
114 5 return $cache->{value};
115 }
116  
117 5 return $self->_getNewToken;
118 }
119  
120 5 ▶sub _getNewToken {
Subroutine _getNewToken: called CC=5 SCAR=19.7
121 5 my ($self) = @_;
122  
123 5 my $token_url = URI->new( $self->oauth_token_endpoint );
124 5 $self->logger->debug("Calling CaptchEtat token endpoint $token_url");
125 5 my $response = $self->ua->post(
126 $token_url,
127 Content => {
128 grant_type => 'client_credentials',
129 client_id => $self->client_id,
130 client_secret => $self->client_secret,
131 scope => $self->oauth_scope,
132 }
133 );
134  
135 5 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) 5 0 5
136 5 my $res = eval { JSON::from_json( $response->decoded_content ) };
137 5 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 5 5
138 0 $self->logger->error(
139 "Could not decode JSON response from CaptchEtat token endpoint: $@"
140 );
141 0 $self->logger->debug( $response->dump );
142 0 return;
143 }
144  
145 # Cache token if we can
146 5 ▶ if ( $res->{expires_in} and $res->{access_token} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{'expires_in'} and $res->{'access_token'}) { } 5 0 5
Condition33%1/3
$res->{"expires_in"} and $res->{"access_token"}
!ll&&!rl&&r
005
MC/DC0%0/2
$res->{"expires_in"} and $res->{"access_token"}
$res->{"expires_in"} $res->{"access_token"}
Truth table33%1/3
A and B
A= $res->{"expires_in"}B= $res->{"access_token"}
ABresult
0X0
100
111
147 $self->access_token_cache( {
148 expires_at => ( time + $res->{expires_in} ),
149 value => $res->{access_token},
150 }
151 5 );
152 }
153 else {
154 0 $self->access_token_cache( {} );
155 }
156 5 return ( $res->{access_token} );
157 }
158 $self->logger->error(
159 0 'CaptchEtat token endpoint error: ' . $response->status_line );
160 0 $self->logger->debug( $response->dump );
161 0 return;
162 }
163  
164 7 ▶sub captchaProxy {
Subroutine captchaProxy: called CC=9 SCAR=24.0
Pod undocumented
165 7 my ( $self, $req ) = @_;
166  
167 7 my $param_get = $self->_validateProxyParam( $req, 'get' );
168  
169 7 ▶ if ( !$param_get ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($param_get) 1 6 7
170 1 $self->logger->error("CaptchEtat proxy: missing 'get' parameter");
171 1 return $self->p->sendError( $req, 'invalid parameters', 400 );
172 }
173  
174 6 my $param_c = $self->_validateProxyParam( $req, 'c' );
175 6 ▶ if ( !$param_c ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($param_c) 0 6 6
176 0 $self->logger->error("CaptchEtat proxy: missing 'c' parameter");
177 0 return $self->p->sendError( $req, 'invalid parameters', 400 );
178 }
179  
180 6 my $param_t = $self->_validateProxyParam( $req, 't' );
181 6 ▶ if ( $param_get eq "sound" and !$param_t ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param_get eq "sound" and not $param_t) 0 6 6
Condition33%1/3
$param_get eq "sound" and not $param_t
!ll&&!rl&&r
600
MC/DC0%0/2
$param_get eq "sound" and not $param_t
$param_get eq "sound" not $param_t
Truth table33%1/3
A and not(B)
A= $param_get eq "sound"B= not $param_t
ABresult
0X0
100
111
182 0 $self->logger->error("CaptchEtat proxy: missing 't' parameter");
183 0 return $self->p->sendError( $req, 'invalid parameters', 400 );
184 }
185  
186 6 my $backend_uri = URI->new( $self->captcha_api_base );
187 6 $backend_uri->path_segments( $backend_uri->path_segments,
188 "simple-captcha-endpoint" );
189 6 ▶ $backend_uri->query_form(
Branch: 1 branch
BranchTrueFalseTotal
$param_t ? : 0 6 6
190 get => $param_get,
191 c => $param_c,
192 ( $param_t ? ( t => $param_t ) : () )
193 );
194  
195 6 $self->logger->debug("Sending CaptchEtat request to $backend_uri");
196 6 my $http_request =
197 GET( $backend_uri, "Content-Type" => "application/json", );
198  
199 6 my $response = $self->request_authenticated_service($http_request);
200 6 ▶ if ($response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response) 6 0 6
201 6 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 5 1 6
202 5 my $res = eval { JSON::from_json( $response->decoded_content ) };
203 5 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 5 5
204 0 $self->logger->error(
205 "Could not decode JSON response from CaptchEtat: $@");
206 0 $self->logger->debug( $response->dump );
207 0 return $self->p->sendError( $req, 'CaptchEtat backend failed',
208 500 );
209 }
210 5 return $self->p->sendJSONresponse( $req, $res );
211 }
212 else {
213 1 $self->logger->error(
214 'CaptchEtat error: ' . $response->status_line );
215 1 $self->logger->debug( $response->dump );
216 }
217 }
218 1 return $self->p->sendError( $req, 'CaptchEtat backend failed', 500 );
219 }
220  
221 19 ▶sub _validateProxyParam {
Subroutine _validateProxyParam: called CC=3 SCAR=11.0
222 19 my ( $self, $req, $param_name ) = @_;
223  
224 19 ▶ my $value = $req->param($param_name) || "";
Condition100%2/2
$req->param($param_name) || ''
l!l
127
MC/DC100%1/1
$req->param($param_name) || ''
$req->param($param_name)
Truth table100%2/2
A
A= $req->param($param_name)
Aresult
11
00
225  
226 19 ▶ if ( $value =~ /^[\w-]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^[\w-]+$/) 12 7 19
227 12 return $value;
228 }
229 7 return;
230 }
231  
232 6 ▶sub init_captcha {
Subroutine init_captcha: called CC=2 SCAR=6.9
Pod undocumented
233 6 my ( $self, $req ) = @_;
234  
235 6 my $cacheTag = $self->p->cacheTag;
236 6 $req->data->{customScript} .= <<"EOF";
237 <script type="text/javascript" src="$self->{p}->{staticPrefix}/common/js/captchetat-js.min.js?v=$cacheTag"></script>
238 EOF
239  
240 6 my $nomCaptcha = $self->captcha_style_name;
241  
242 6 ▶ my $sc = $req->script_name // "";
Condition50%1/2
$req->script_name // ""
l!l
60
MC/DC0%0/1
$req->script_name // ""
$req->script_name
Truth table50%1/2
A
A= $req->script_name
Aresult
11
00
243 6 $sc =~ s#/*$#/#;
244 6 my $endpoint = "${sc}simple-captcha-endpoint";
245  
246 # Read option from the manager configuration
247 6 my $html = <<EOF;
248 <div id="captchetat" captchaStyleName="$nomCaptcha" altImage="alternativeImage" urlBackend="$endpoint"></div>
249 <input type="text" id="captchaCode" name="captchaCode"/>
250 EOF
251 6 $req->captchaHtml($html);
252 }
253  
254 8 ▶sub request_authenticated_service {
Subroutine request_authenticated_service: called CC=2 SCAR=6.9
Pod undocumented
255 8 my ( $self, $http_request ) = @_;
256  
257 8 my $response = $self->_request_with_token($http_request);
258  
259 # If we got an invalid token response, get new token and retry once
260 8 ▶ if ( $self->_isInvalidTokenResponse($response) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isInvalidTokenResponse($response)) 2 6 8
261 2 $self->logger->debug( 'Current access token rejected,'
262 . ' retrying request with a new Access Token' );
263  
264 # Invalidate token cache to make sure it will be refreshed
265 2 $self->access_token_cache( {} );
266  
267 2 $response = $self->_request_with_token($http_request);
268 }
269 8 return $response;
270 }
271  
272 10 ▶sub _request_with_token {
Subroutine _request_with_token: called CC=2 SCAR=7.6
273 10 my ( $self, $http_request ) = @_;
274  
275 10 my $bearer_token = $self->getToken;
276  
277 10 ▶ unless ($bearer_token) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bearer_token) 0 10 10
278 0 $self->logger->info('Could not get bearer token for CaptchEtat');
279 0 return;
280 }
281  
282 10 $http_request->header( Authorization => "Bearer $bearer_token" );
283  
284 10 return $self->ua->request($http_request);
285 }
286  
287 8 ▶sub _isInvalidTokenResponse {
Subroutine _isInvalidTokenResponse: called CC=8 SCAR=23.7
288 8 my ( $self, $r ) = @_;
289  
290 8 ▶ return unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 0 8 8
291  
292 8 ▶ if ( $r->code and $r->code == 401 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->code and $r->code == 401) 2 6 8
Condition66%2/3
$r->code and $r->code == 401
!ll&&!rl&&r
062
MC/DC50%1/2
$r->code and $r->code == 401
$r->code $r->code == 401
Truth table66%2/3
A and B
A= $r->codeB= $r->code == 401
ABresult
0X0
100
111
293  
294 # This dependency improves parsing of WWW-Authenticate headers
295 2 require HTTP::Headers::Auth;
296  
297 2 my ( $auth_scheme, $params ) = $r->www_authenticate;
298  
299 2 ▶ if ( lc($auth_scheme) eq "bearer"
Branch: 1 branch
BranchTrueFalseTotal
if (lc $auth_scheme eq "bearer" and ref $params eq "HASH" and $params->{"error"} and $params->{"error"} eq "invalid_token") 2 0 2
Condition33%3/9
lc $auth_scheme eq "bearer" and ref $params eq "HASH" and $params->{"error"} and $params->{"error"} eq "invalid_token"
!ll&&!rl&&r
002
lc $auth_scheme eq "bearer" and ref $params eq "HASH" and $params->{"error"}
!ll&&!rl&&r
002
lc $auth_scheme eq "bearer" and ref $params eq "HASH"
!ll&&!rl&&r
002
MC/DC0%0/4
lc $auth_scheme eq "bearer" and ref $params eq "HASH" and $params->{"error"} and $params->{"error"} eq "invalid_token"
lc $auth_scheme eq "bearer" ref $params eq "HASH" $params->{"error"} $params->{"error"} eq "invalid_token"
Truth table20%1/5
A and B and C and D
A= lc $auth_scheme eq "bearer"B= ref $params eq "HASH"C= $params->{"error"}D= $params->{"error"} eq "invalid_token"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
300 and ref($params) eq "HASH"
301 and $params->{error}
302 and $params->{error} eq "invalid_token" )
303 {
304 2 return 1;
305 }
306 }
307 6 return 0;
308 }
309  
310 3 ▶sub check_captcha {
Subroutine check_captcha: called CC=5 SCAR=17.1
Pod undocumented
311 3 my ( $self, $req ) = @_;
312  
313 3 my $captcha_uuid = $req->param('captchetat-uuid');
314 3 my $captcha_input = $req->param('captchaCode');
315  
316 3 ▶ unless ($captcha_input) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($captcha_input) 1 2 3
317 1 $self->logger->info('No captcha value submitted');
318 1 return 0;
319 }
320  
321 2 ▶ unless ($captcha_uuid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($captcha_uuid) 0 2 2
322 0 $self->logger->info('No captcha UUID received');
323 0 return 0;
324 }
325  
326 2 my $validation_url = URI->new( $self->captcha_api_base );
327 2 $validation_url->path_segments( $validation_url->path_segments,
328 "valider-captcha" );
329 2 $self->logger->debug(
330 "Sending CaptchEtat validation request to $validation_url");
331  
332 2 my $http_request = POST(
333 $validation_url,
334 "Content-Type" => "application/json",
335 Content => to_json( {
336 uuid => $captcha_uuid,
337 code => $captcha_input,
338 }
339 )
340 );
341 2 my $response = $self->request_authenticated_service($http_request);
342  
343 2 ▶ if ($response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response) 2 0 2
344 2 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 2 0 2
345 2 my $res = $response->decoded_content;
346 2 return ( $res eq "true" );
347 }
348 else {
349 0 $self->logger->error(
350 'CaptchEtat error: ' . $response->status_line );
351 0 $self->logger->debug( $response->dump );
352 }
353 }
354 0 return 0;
355 }
356  
357 1 1;
358  
« blib/lib/Lemonldap/NG/Portal/Auth/Choice.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Lib/Wrapper.pm »