blib/lib/Lemonldap/NG/Portal/Plugins/CheckHIBP.pm untested

filter: Statementstmt 0.0% 0 / 95 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 140 CC 14 SCAR 39.0
CC
14
Cov
0%
_checkHIBP
110.0
init
12.0
checkHIBP
12.0
CRAP 49.5
1 package Lemonldap::NG::Portal::Plugins::CheckHIBP;
2  
3 use strict;
4 use warnings;
5 use Mouse;
6 use Digest::SHA qw( sha1_hex );
7 use Lemonldap::NG::Common::UserAgent;
8 use MIME::Base64;
9 use Lemonldap::NG::Portal::Main::Constants qw(
10 PE_OK
11 PE_ERROR
12 PE_PP_INSUFFICIENT_PASSWORD_QUALITY
13 );
14  
15 our $VERSION = '2.21.0';
16  
17 extends 'Lemonldap::NG::Portal::Main::Plugin';
18  
19 has ua => (
20 is => 'rw',
21 lazy => 1,
22 builder => sub {
23 my $ua = Lemonldap::NG::Common::UserAgent->new( $_[0]->{conf} );
24 $ua->env_proxy();
25 return $ua;
26 }
27 );
28  
29 has apiURL => (
30 is => 'rw',
31 isa => 'Str',
32 );
33  
34 has hibpRequired => (
35 is => 'rw',
36 isa => 'Bool',
37 );
38  
39 use constant hook => { passwordBeforeChange => 'checkHIBP', };
40  
41 sub init {
42 my ($self) = @_;
43 $self->logger->debug('checkHIBP: initialization');
44  
45 if ( $self->conf->{checkHIBPURL} ) {
46 $self->apiURL( $self->conf->{checkHIBPURL} );
47 }
48 else {
49 $self->logger->error('checkHIBP: missing checkHIBPURL parameter');
50 return 0;
51 }
52  
53 if ( exists $self->conf->{checkHIBPRequired} ) {
54 $self->hibpRequired( $self->conf->{checkHIBPRequired} );
55 }
56 else {
57 $self->logger->error('checkHIBP: missing checkHIBPRequired parameter');
58 return 0;
59 }
60  
61 # Declare REST route
62 $self->addUnauthRoute(
63 checkhibp => '_checkHIBP',
64 ['POST']
65 );
66 $self->addAuthRoute(
67 checkhibp => '_checkHIBP',
68 ['POST']
69 );
70  
71 my $cacheTag = $self->p->cacheTag;
72 $self->p->addPasswordPolicyDisplay(
73 'ppolicy-checkhibp',
74 {
75 condition => $self->conf->{checkHIBP},
76 label => "passwordCompromised",
77 customHtml =>
78 qq'<script type="text/javascript" src="$self->{p}->{staticPrefix}/common/js/hibp.min.js?v=$cacheTag"></script>',
79 order => 201,
80 }
81 );
82  
83 return 1;
84 }
85  
86 # Check user password against an URL listing compromised passwords
87 # Method called before the password change, blocking if the password is compromised
88 sub checkHIBP {
89 my ( $self, $req, $user, $password, $old ) = @_;
90  
91 if ( $self->hibpRequired ) {
92 my $res = $self->_checkHIBP( $req, $password, $self->hibpRequired );
93 return $res->{code} == 0 ? PE_OK : PE_PP_INSUFFICIENT_PASSWORD_QUALITY;
94 }
95 else {
96 # Do not verify new password if checkHIBPRequired parameter has not been set
97 return PE_OK;
98 }
99 }
100  
101 # Check user password against an URL listing compromised passwords
102 # Input : new user password, flag noJSONResponse
103 # Output: JSON response, including a code.
104 # code = 0 = success
105 # code > 0 = error
106 sub _checkHIBP {
107 my ( $self, $req, $pass, $noJSONResponse ) = @_;
108 my $response_params = {};
109 my $password;
110  
111 # Password already given, so take it directly
112 if ($pass) {
113 $password = $pass;
114 }
115 else {
116 # use password value submitted in form
117 my $password_base64 = $req->param('password');
118  
119 unless ($password_base64) {
120 $response_params->{"code"} = 1;
121 $response_params->{"message"} = "missing parameter password";
122  
123 return $noJSONResponse
124 ? $response_params
125 : $self->sendJSONresponse( $req, $response_params );
126 }
127 $password = decode_base64($password_base64);
128 }
129  
130 my $digestFull = sha1_hex($password); # Compute sha1 hash of new password
131 my $digestPrefix = substr $digestFull, 0,
132 5; # take only 5 first characters of the hash
133  
134 # Prepare connection to blacklist URL
135 my $reqAPI;
136 $reqAPI = HTTP::Request->new( "GET", $self->apiURL . $digestPrefix );
137 $reqAPI->header( 'Content-type' => 'text/html' );
138 $reqAPI->header( 'User-Agent' => 'libwww-perl/6.05 (LemonLDAPNG)' );
139  
140 my $response = $self->ua->request($reqAPI);
141  
142 my $debugstr =
143 'checkHIBP: requesting ' . $reqAPI->as_string . $response->status_line;
144  
145 $self->logger->debug($debugstr);
146  
147 my $digest;
148 if ( $response->is_success || $response->is_info ) {
149  
150 # Parse compromised keys
151 foreach ( split /[\r\n]/, $response->content ) {
152  
153 # Exclude empty lines
154 if ( $_ ne "" ) {
155 $digest = lc("$digestPrefix$_");
156 $self->logger->debug(
157 "checkHIBP: Check if new password matches compromised password $digest"
158 );
159 if ( $digest =~ /$digestFull/i ) {
160 my ( $dig, $num ) = split /:/, $digest;
161 $self->userLogger->warn(
162 "checkHIBP: password $dig compromised $num times");
163  
164 $response_params->{"code"} = 2;
165 $response_params->{"message"} =
166 "password $dig compromised $num times";
167  
168 return $noJSONResponse
169 ? $response_params
170 : $self->sendJSONresponse( $req, $response_params );
171 }
172 }
173 }
174 }
175 else {
176 $self->logger->error(
177 "checkHIBP: error while requesting " . $self->apiURL );
178 $self->logger->error( "checkHIBP: " . $response->status_line );
179  
180 $response_params->{"code"} = 1;
181 $response_params->{"message"} =
182 "error while requesting " . $self->apiURL;
183  
184 return $noJSONResponse
185 ? $response_params
186 : $self->sendJSONresponse( $req, $response_params );
187 }
188  
189 $self->logger->info("checkHIBP: password $digestFull is not compromised");
190  
191 $response_params->{"code"} = 0;
192 $response_params->{"message"} = "password $digestFull not compromised";
193  
194 return $noJSONResponse
195 ? $response_params
196 : $self->sendJSONresponse( $req, $response_params );
197 }
198  
199 1;