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

filter: Statementstmt 0.0% 0 / 86 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 123 CC 10 SCAR 24.8
CC
10
Cov
0%
init
30.0
checkEntropy
12.0
_checkEntropyJSON
12.0
CRAP 11.9
1 package Lemonldap::NG::Portal::Plugins::CheckEntropy;
2  
3 use strict;
4 use warnings;
5 use Mouse;
6 use Lemonldap::NG::Common::UserAgent;
7 use MIME::Base64;
8 use Lemonldap::NG::Portal::Main::Constants qw(
9 PE_OK
10 PE_ERROR
11 PE_PP_INSUFFICIENT_PASSWORD_QUALITY
12 );
13  
14 our $VERSION = '2.21.0';
15  
16 extends 'Lemonldap::NG::Portal::Main::Plugin';
17  
18 has entropyRequired => (
19 is => 'rw',
20 isa => 'Bool',
21 );
22  
23 has entropyRequiredLevel => (
24 is => 'rw',
25 isa => 'Int',
26 );
27  
28 use constant hook => { passwordBeforeChange => 'checkEntropy', };
29  
30 sub init {
31 my ($self) = @_;
32 $self->logger->debug('checkEntropy: initialization');
33  
34 eval { use Data::Password::zxcvbn qw(password_strength); };
35 if ($@) {
36 $self->logger->error("Can't load zxcvbn library: $@");
37 $self->error("Can't load zxcvbn library: $@");
38 return 0;
39 }
40  
41 # check checkEntropyRequired param
42 if ( exists $self->conf->{checkEntropyRequired} ) {
43 $self->entropyRequired( $self->conf->{checkEntropyRequired} );
44  
45 # check checkEntropyRequiredLevel param
46 if ( exists $self->conf->{checkEntropyRequiredLevel} ) {
47 $self->entropyRequiredLevel(
48 $self->conf->{checkEntropyRequiredLevel} );
49 }
50 else {
51 # If entropy is required to pass, then checkEntropyRequiredLevel is required too, this is an error
52 if ( $self->entropyRequired ) {
53 $self->logger->error(
54 'checkEntropy: missing checkEntropyRequiredLevel parameter'
55 );
56 return 0;
57 }
58 else {
59 $self->logger->warn(
60 'checkEntropy: missing checkEntropyRequiredLevel parameter, but entropy is not required to pass. Set checkEntropyRequiredLevel to 0'
61 );
62 $self->entropyRequiredLevel(0);
63 }
64 }
65  
66 }
67 else {
68 $self->logger->error(
69 'checkEntropy: missing checkEntropyRequired parameter');
70 return 0;
71 }
72  
73 # Declare REST route
74 $self->addUnauthRoute(
75 checkentropy => '_checkEntropyJSON',
76 ['POST']
77 );
78 $self->addAuthRoute(
79 checkentropy => '_checkEntropyJSON',
80 ['POST']
81 );
82  
83 my $cacheTag = $self->p->cacheTag;
84 $self->p->addPasswordPolicyDisplay(
85 'ppolicy-checkentropy',
86 {
87 condition => $self->conf->{checkEntropy},
88 label => "checkentropyLabel",
89 data => {
90 "CHECKENTROPY_REQUIRED" => $self->entropyRequired,
91 "CHECKENTROPY_REQUIRED_LEVEL" => $self->entropyRequiredLevel,
92 },
93 customHtml =>
94 qq'<script type="text/javascript" src="$self->{p}->{staticPrefix}/common/js/entropy.min.js?v=$cacheTag"></script>\n
95 <link rel="stylesheet" type="text/css" href="$self->{p}->{staticPrefix}/common/css/entropy.min.css?v=$cacheTag">',
96 customHtmlAfter =>
97 qq'<div id="entropybar" class="progress">\n
98 <div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>\n
99 </div>\n
100 <div id="entropybar-msg" class="alert alert-warning entropyHidden"></div>',
101 order => 202,
102 }
103 );
104  
105 return 1;
106 }
107  
108 # Check user password against zxcvbn library
109 # Method called before the password change, blocking if the password is compromised
110 sub checkEntropy {
111 my ( $self, $req, $user, $password, $old ) = @_;
112  
113 if ( $self->entropyRequired ) {
114 my $entropy =
115 $self->_checkEntropy( $req, $password, $self->entropyRequired );
116 if ( $entropy->{level}
117 and $entropy->{level} >= $self->entropyRequiredLevel )
118 {
119 return PE_OK;
120 }
121 else {
122 $self->userLogger->warn( "checkEntropy: insufficient entropy: "
123 . "level = "
124 . $entropy->{level}
125 . " but minimal required = "
126 . $self->entropyRequiredLevel );
127 return PE_PP_INSUFFICIENT_PASSWORD_QUALITY;
128 }
129 }
130 else {
131 # Do not verify new password if checkEntropyRequired parameter has not been set
132 return PE_OK;
133 }
134 }
135  
136 # Check user password against zxcvbn library
137 # Input : request, new user base64-encoded password
138 # Output: JSON response: { "level" => int, "message" => "msg" }
139 sub _checkEntropyJSON {
140 my ( $self, $req, $pass ) = @_;
141 my $response_params = {};
142 my $password;
143  
144 # use password value submitted in form
145 # this happens when frontend is testing a new password while user is typing
146 my $password_base64 = $req->param('password');
147 $self->logger->debug( 'checkEntropy: password taken from submitted form');
148  
149 unless ($password_base64) {
150 $response_params->{"level"} = -1;
151 $response_params->{"message"} = "missing parameter password";
152  
153 $self->userLogger->warn("checkEntropy: missing parameter password");
154  
155 return $self->sendJSONresponse( $req, $response_params );
156 }
157 $password = decode_base64($password_base64);
158  
159 my $entropy = password_strength($password);
160  
161 $response_params->{"level"} = $entropy->{score};
162 $response_params->{"message"} =
163 $entropy->{feedback}->{warning} ? $entropy->{feedback}->{warning} : "";
164  
165 $self->userLogger->debug( "checkEntropy: level "
166 . $response_params->{"level"}
167 . " msg: "
168 . $response_params->{"message"} );
169  
170 return $self->sendJSONresponse( $req, $response_params );
171 }
172  
173 # Check user password against zxcvbn library
174 # Input : request, new user password
175 # Output: hash response: { "level" => int, "message" => "msg" }
176 sub _checkEntropy {
177 my ( $self, $req, $pass ) = @_;
178 my $response_params = {};
179  
180 # Password already given, so take it directly
181 # this happens at backend side, when really changing the password
182 $self->logger->debug( 'checkEntropy: password taken directly');
183  
184 my $entropy = password_strength($pass);
185  
186 $response_params->{"level"} = $entropy->{score};
187 $response_params->{"message"} =
188 $entropy->{feedback}->{warning} ? $entropy->{feedback}->{warning} : "";
189  
190 $self->userLogger->debug( "checkEntropy: level "
191 . $response_params->{"level"}
192 . " msg: "
193 . $response_params->{"message"} );
194  
195 return $response_params;
196 }
197  
198 1;