1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.util.password;
35
36 import fr.paris.lutece.portal.service.admin.AdminUserService;
37 import fr.paris.lutece.portal.service.util.AppPropertiesService;
38 import fr.paris.lutece.util.date.DateUtil;
39
40 import java.sql.Timestamp;
41
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.Random;
45
46
47
48
49
50 public final class PasswordUtil
51 {
52 private static final String PROPERTY_PASSWORD_SIZE = "randomPassword.size";
53 private static final int CONSTANT_NUMBER_LETTERS = 26;
54 private static final int CONSTANT_NUMBER_NUMBERS_BASE10 = 10;
55 private static final int CONSTANT_ASCII_CODE_A_UPPERCASE = 65;
56 private static final int CONSTANT_ASCII_CODE_A_LOWERCASE = 97;
57 private static final int CONSTANT_ASCII_CODE_ZERO = 48;
58 private static final char[] CONSTANT_SPECIAL_CHARACTERS =
59 {
60 '!', ',', ':', '?', '$', '-', '@', '}', '{', '(', ')', '*', '+', '=', '[', ']', '%', '.',
61 };
62 private static final String CONSTANT_PASSWORD_BEGIN_REGEX = "^";
63 private static final String CONSTANT_PASSWORD_REGEX_NUM = "(?=.*[0-9])";
64 private static final String CONSTANT_PASSWORD_REGEX_SPECIAL = "(?=.*[^a-zA-Z0-9])";
65 private static final String CONSTANT_PASSWORD_REGEX_UPPER_LOWER = "(?=.*[a-z])(?=.*[A-Z])";
66 private static final String CONSTANT_PASSWORD_END_REGEX = "(.*)$";
67 private static final String PARAMETER_PASSWORD_MINIMUM_LENGTH = "password_minimum_length";
68
69
70 private PasswordUtil( )
71 {
72 }
73
74
75
76
77
78 public static String makePassword( )
79 {
80
81 int nPasswordSize = AppPropertiesService.getPropertyInt( PROPERTY_PASSWORD_SIZE, 8 );
82 int nMinPasswordSize = AdminUserService.getIntegerSecurityParameter( PARAMETER_PASSWORD_MINIMUM_LENGTH );
83
84 if ( nMinPasswordSize > nPasswordSize )
85 {
86 nPasswordSize = nMinPasswordSize;
87 }
88
89 return makePassword( nPasswordSize, true, true, true );
90 }
91
92
93
94
95
96
97
98
99
100
101
102 public static String makePassword( int nPasswordSize, boolean bUpperAndLowerCase, boolean bNumbers,
103 boolean bSpecialCaracters )
104 {
105
106 Random r = new Random( );
107
108 ArrayList<Character> listCharacters = new ArrayList<Character>( nPasswordSize );
109
110
111 int nNumCapitalLetters = bUpperAndLowerCase ? ( r.nextInt( nPasswordSize - 3 ) + 1 ) : 0;
112
113
114 int nNumSpecial = bSpecialCaracters ? ( r.nextInt( nPasswordSize - 2 - nNumCapitalLetters ) + 1 ) : 0;
115
116
117 int nNumNumbers = bNumbers ? ( r.nextInt( nPasswordSize - 1 - nNumCapitalLetters - nNumSpecial ) + 1 ) : 0;
118
119
120 int nNumSmallLetters = nPasswordSize - nNumCapitalLetters - nNumSpecial - nNumNumbers;
121
122 for ( int j = 0; j < nNumCapitalLetters; j++ )
123 {
124 char c1 = (char) ( r.nextInt( CONSTANT_NUMBER_LETTERS ) + CONSTANT_ASCII_CODE_A_UPPERCASE );
125 listCharacters.add( Character.valueOf( c1 ) );
126 }
127
128 for ( int j = 0; j < nNumSmallLetters; j++ )
129 {
130 char c1 = (char) ( r.nextInt( CONSTANT_NUMBER_LETTERS ) + CONSTANT_ASCII_CODE_A_LOWERCASE );
131 listCharacters.add( Character.valueOf( c1 ) );
132 }
133
134 for ( int j = 0; j < nNumNumbers; j++ )
135 {
136 char c1 = (char) ( r.nextInt( CONSTANT_NUMBER_NUMBERS_BASE10 - 1 ) + CONSTANT_ASCII_CODE_ZERO );
137 listCharacters.add( Character.valueOf( c1 ) );
138 }
139
140 for ( int j = 0; j < nNumSpecial; j++ )
141 {
142 char c1 = CONSTANT_SPECIAL_CHARACTERS[r.nextInt( CONSTANT_SPECIAL_CHARACTERS.length )];
143 listCharacters.add( Character.valueOf( c1 ) );
144 }
145
146 Collections.shuffle( listCharacters );
147
148 StringBuilder sbPassword = new StringBuilder( listCharacters.size( ) );
149
150 for ( Character myChar : listCharacters )
151 {
152 sbPassword.append( myChar );
153 }
154
155 return sbPassword.toString( );
156 }
157
158
159
160
161
162
163
164 public static boolean checkPasswordFormat( String strPassword )
165 {
166 return checkPasswordFormat( strPassword, true, true, true );
167 }
168
169
170
171
172
173
174
175
176
177
178
179 public static boolean checkPasswordFormat( String strPassword, boolean bUpperAndLowerCase, boolean bNumero,
180 boolean bSpecialCaracters )
181 {
182 if ( ( strPassword == null ) || strPassword.isEmpty( ) )
183 {
184 return false;
185 }
186
187 StringBuilder sbRegex = new StringBuilder( CONSTANT_PASSWORD_BEGIN_REGEX );
188
189 if ( bUpperAndLowerCase )
190 {
191 sbRegex.append( CONSTANT_PASSWORD_REGEX_UPPER_LOWER );
192 }
193
194 if ( bNumero )
195 {
196 sbRegex.append( CONSTANT_PASSWORD_REGEX_NUM );
197 }
198
199 if ( bSpecialCaracters )
200 {
201 sbRegex.append( CONSTANT_PASSWORD_REGEX_SPECIAL );
202 }
203
204 sbRegex.append( CONSTANT_PASSWORD_END_REGEX );
205
206 return strPassword.matches( sbRegex.toString( ) );
207 }
208
209
210
211
212
213
214
215 public static Timestamp getPasswordMaxValidDate( int nNumberDay )
216 {
217 if ( nNumberDay <= 0 )
218 {
219 return null;
220 }
221
222 long nMilliSeconds = DateUtil.convertDaysInMiliseconds( nNumberDay );
223 Timestamp maxValidDate = new Timestamp( new java.util.Date( ).getTime( ) + nMilliSeconds );
224
225 return maxValidDate;
226 }
227 }