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 0.0% 0 / 2 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 125 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.23.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 => qq'<div id="entropybar" class="progress">\n
97 <div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>\n
98 </div>\n
99 <div id="entropybar-msg" class="alert alert-warning entropyHidden"></div>',
100 order => 202,
101 }
102 );
103  
104 return 1;
105 }
106  
107 # Check user password against zxcvbn library
108 # Method called before the password change, blocking if the password is compromised
109 sub checkEntropy {
110 my ( $self, $req, $user, $password, $old ) = @_;
111  
112 if ( $self->entropyRequired ) {
113 my $entropy =
114 $self->_checkEntropy( $req, $password, $self->entropyRequired );
115 if ( $entropy->{level}
116 and $entropy->{level} >= $self->entropyRequiredLevel )
117 {
118 return PE_OK;
119 }
120 else {
121 $self->userLogger->warn( "checkEntropy: insufficient entropy: "
122 . "level = "
123 . $entropy->{level}
124 . " but minimal required = "
125 . $self->entropyRequiredLevel );
126 return PE_PP_INSUFFICIENT_PASSWORD_QUALITY;
127 }
128 }
129 else {
130 # Do not verify new password if checkEntropyRequired parameter has not been set
131 return PE_OK;
132 }
133 }
134  
135 # Check user password against zxcvbn library
136 # Input : request, new user base64-encoded password
137 # Output: JSON response: { "level" => int, "message" => "msg" }
138 sub _checkEntropyJSON {
139 my ( $self, $req, $pass ) = @_;
140 my $response_params = {};
141 my $password;
142  
143 # use password value submitted in form
144 # this happens when frontend is testing a new password while user is typing
145 my $password_base64 = $req->param('password');
146 $self->logger->debug('checkEntropy: password taken from submitted form');
147  
148 unless ($password_base64) {
149 $response_params->{"level"} = -1;
150 $response_params->{"message"} = "missing parameter password";
151  
152 $self->userLogger->warn("checkEntropy: missing parameter password");
153  
154 return $self->sendJSONresponse( $req, $response_params );
155 }
156 $password = decode_base64($password_base64);
157  
158 my $entropy = password_strength($password);
159  
160 $response_params->{"level"} = $entropy->{score};
161 $response_params->{"message"} =
162 $entropy->{feedback}->{warning} ? $entropy->{feedback}->{warning} : "";
163  
164 $self->userLogger->debug( "checkEntropy: level "
165 . $response_params->{"level"}
166 . " msg: "
167 . $response_params->{"message"} );
168  
169 return $self->sendJSONresponse( $req, $response_params );
170 }
171  
172 # Check user password against zxcvbn library
173 # Input : request, new user password
174 # Output: hash response: { "level" => int, "message" => "msg" }
175 sub _checkEntropy {
176 my ( $self, $req, $pass ) = @_;
177 my $response_params = {};
178  
179 # Password already given, so take it directly
180 # this happens at backend side, when really changing the password
181 $self->logger->debug('checkEntropy: password taken directly');
182  
183 my $entropy = password_strength($pass);
184  
185 $response_params->{"level"} = $entropy->{score};
186 $response_params->{"message"} =
187 $entropy->{feedback}->{warning} ? $entropy->{feedback}->{warning} : "";
188  
189 $self->userLogger->debug( "checkEntropy: level "
190 . $response_params->{"level"}
191 . " msg: "
192 . $response_params->{"message"} );
193  
194 return $response_params;
195 }
196  
197 1;