forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordSpec.java
More file actions
369 lines (336 loc) · 10.8 KB
/
PasswordSpec.java
File metadata and controls
369 lines (336 loc) · 10.8 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package act.validation;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2018 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
/**
* `PasswordSpec` provides a simple way to define {@link Password.Validator}.
*
* It supports validating the following trait of a password:
* * check if the password contains a lowercase letter
* * check if the password contains an uppercase letter
* * check if the password contains a digit letter
* * check if the password contains a special character
* * check if the password length fall in specified range
*
* A `PasswordSpec` can be build using `PasswordSpec.Builder`:
*
* ```
* PasswordSpec spec = PasswordSpec.builder()
* .requireLowercase()
* .requireUppercase()
* .minLength(5)
* .toPasswordSpec();
* boolean isValid = spec.isValid("abcde"); // false as no uppercase letter found
* isValid = spec.isValid("aBcde"); // true
* isValid = spec.isValid("aBcd"); // false as length less than min length
* ```
*
* It can also e created by parsing a password spec string:
*
* ```
* PasswordSpec spec = PasswordSpec.parse("aA[5,])");
* ```
*
* @see #parse(String)
*/
public class PasswordSpec implements Password.Validator {
/**
* Default minimum length of a password is `3`
*/
public static final int DEF_MIN_LEN = 3;
/**
* Default maximum length of a password is unlimited
*/
public static final int DEF_MAX_LEN = Integer.MAX_VALUE;
/**
* The following chars are considered to be special characters:
*
* <code>`~!@#$%^&*()[]{}'?</code>
*/
public static final String SPECIAL_CHARS = "`~!@#$%^&*()[]{}'?";
public static final char SPEC_LOWERCASE = 'a';
public static final char SPEC_UPPERCASE = 'A';
public static final char SPEC_DIGIT = '0';
public static final char SPEC_SPECIAL_CHAR = '#';
public static final char SPEC_LENSPEC_START = '[';
public static final char SPEC_LENSPEC_CLOSE = ']';
public static final char SPEC_LENSPEC_SEP = ',';
private static final int BIT_LOWERCASE = 0x00001000;
private static final int BIT_UPPERCASE = 0x00002000;
private static final int BIT_DIGIT = 0x00003000;
private static final int BIT_SPECIAL_CHAR = 0x00004000;
private int minLength = DEF_MIN_LEN;
private int maxLength = DEF_MAX_LEN;
private int trait;
private PasswordSpec() {}
private PasswordSpec(PasswordSpec copy) {
trait = copy.trait;
minLength = copy.minLength;
maxLength = copy.maxLength;
}
public boolean lowercaseRequired() {
return (trait & BIT_LOWERCASE) != 0;
}
public boolean upppercaseRequired() {
return (trait & BIT_UPPERCASE) != 0;
}
public boolean digitRequired() {
return (trait & BIT_DIGIT) != 0;
}
public boolean specialCharRequired() {
return (trait & BIT_SPECIAL_CHAR) != 0;
}
public int minLength() {
return minLength;
}
public int maxLength() {
return maxLength;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
if (lowercaseRequired()) {
sb.append(SPEC_LOWERCASE);
}
if (upppercaseRequired()) {
sb.append(SPEC_UPPERCASE);
}
if (digitRequired()) {
sb.append(SPEC_DIGIT);
}
if (specialCharRequired()) {
sb.append(SPEC_SPECIAL_CHAR);
}
sb.append("[").append(minLength).append(",");
if (maxLength != DEF_MAX_LEN) {
sb.append(maxLength);
}
sb.append("]");
return sb.toString();
}
@Override
public boolean isValid(char[] password) {
int len = password.length;
if (len < minLength() || len > maxLength()) {
return false;
}
int trait = this.trait;
for (int i = len - 1; i >= 0; --i) {
char c = password[i];
if (isLowercase(c)) {
trait &= ~BIT_LOWERCASE;
} else if (isUppercase(c)) {
trait &= ~BIT_UPPERCASE;
} else if (isDigit(c)) {
trait &= ~BIT_DIGIT;
} else if (isSpecialChar(c)) {
trait &= ~BIT_SPECIAL_CHAR;
}
}
return 0 == trait;
}
/**
* A `PasswordSpec.Builder` is used to build a `PasswordSpec`.
*
* @see PasswordSpec
*/
public static class Builder extends PasswordSpec {
/**
* Specify lowercase letter is required.
*
* @return the builder instance
*/
public Builder requireLowercase() {
super.trait |= BIT_LOWERCASE;
return this;
}
/**
* Specify uppercase letter is required.
*
* @return the builder instance
*/
public Builder requireUppercase() {
super.trait |= BIT_UPPERCASE;
return this;
}
/**
* Specify digit letter is required.
*
* @return the builder instance
*/
public Builder requireDigit() {
super.trait |= BIT_DIGIT;
return this;
}
/**
* Specify {@link #SPECIAL_CHARS special character} is required.
*
* @return the builder instance
*/
public Builder requireSpecialChar() {
super.trait |= BIT_SPECIAL_CHAR;
return this;
}
/**
* Specify minimum length of the password
* @param len the minimum length
* @return the builder instance
*/
public Builder minLength(int len) {
super.minLength = len;
return this;
}
/**
* Specify maximum length of the password
* @param len the maximum length
* @return the builder instance
*/
public Builder maxLength(int len) {
super.maxLength = len;
return this;
}
/**
* Return a {@link PasswordSpec} instance from this builder.
*
* The builder can be used to keep building other `PasswordSpec`
* instance after calling this method
*
* @return the password spec built from this builder.
*/
public PasswordSpec toPasswordSpec() {
return new PasswordSpec(this);
}
}
/**
* Create a `PasswordSpec.Builder` instance.
* @return a password spec builder
*/
public static Builder builder() {
return new Builder();
}
/**
* Parse a string representation of password spec.
*
* A password spec string should be `<trait spec><length spec>`.
*
* Where "trait spec" should be a composition of
*
* * `a` - indicate lowercase letter required
* * `A` - indicate uppercase letter required
* * `0` - indicate digit letter required
* * `#` - indicate special character required
*
* "length spec" should be `[min,max]` where `max` can be omitted.
*
* Here are examples of valid "length spec":
*
* * `[6,20]` // min length: 6, max length: 20
* * `[8,]` // min length: 8, max length: unlimited
*
* And examples of invalid "length spec":
*
* * `[8]` // "," required after min part
* * `[a,f]` // min and max part needs to be decimal digit(s)
* * `[3,9)` // length spec must be started with `[` and end with `]`
*
* @param spec a string representation of password spec
* @return a {@link PasswordSpec} instance
*/
public static PasswordSpec parse(String spec) {
char[] ca = spec.toCharArray();
int len = ca.length;
illegalIf(0 == len, spec);
Builder builder = new Builder();
StringBuilder minBuf = new StringBuilder();
StringBuilder maxBuf = new StringBuilder();
boolean lenSpecStart = false;
boolean minPart = false;
for (int i = 0; i < len; ++i) {
char c = ca[i];
switch (c) {
case SPEC_LOWERCASE:
illegalIf(lenSpecStart, spec);
builder.requireLowercase();
break;
case SPEC_UPPERCASE:
illegalIf(lenSpecStart, spec);
builder.requireUppercase();
break;
case SPEC_SPECIAL_CHAR:
illegalIf(lenSpecStart, spec);
builder.requireSpecialChar();
break;
case SPEC_LENSPEC_START:
lenSpecStart = true;
minPart = true;
break;
case SPEC_LENSPEC_CLOSE:
illegalIf(minPart, spec);
lenSpecStart = false;
break;
case SPEC_LENSPEC_SEP:
minPart = false;
break;
case SPEC_DIGIT:
if (!lenSpecStart) {
builder.requireDigit();
} else {
if (minPart) {
minBuf.append(c);
} else {
maxBuf.append(c);
}
}
break;
default:
illegalIf(!lenSpecStart || !isDigit(c), spec);
if (minPart) {
minBuf.append(c);
} else {
maxBuf.append(c);
}
}
}
illegalIf(lenSpecStart, spec);
if (minBuf.length() != 0) {
builder.minLength(Integer.parseInt(minBuf.toString()));
}
if (maxBuf.length() != 0) {
builder.maxLength(Integer.parseInt(maxBuf.toString()));
}
return builder;
}
private static boolean isDigit(char c) {
return c >= '0' && c <= '9';
}
private static boolean isLowercase(char c) {
return c >= 'a' && c <= 'z';
}
private static boolean isUppercase(char c) {
return c >= 'A' && c <= 'Z';
}
private static boolean isSpecialChar(char c) {
return -1 < SPECIAL_CHARS.indexOf((int)c);
}
private static void illegalIf(boolean test, String spec) {
if (test) {
throw new IllegalArgumentException("Invalid password requirement spec:" + spec);
}
}
}