Skip to content

Commit a97543f

Browse files
made classes final and removed otp constructors, using builders is now obligated
1 parent 8c555ee commit a97543f

File tree

5 files changed

+40
-53
lines changed

5 files changed

+40
-53
lines changed

src/main/java/com/bastiaanjansen/otp/HOTPGenerator.java

+2-12
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,11 @@
1313
* @author Bastiaan Jansen
1414
* @see OTPGenerator
1515
*/
16-
public class HOTPGenerator extends OTPGenerator {
16+
public final class HOTPGenerator extends OTPGenerator {
1717
private final static String OTP_TYPE = "hotp";
1818

19-
/**
20-
* Constructs generator with custom password length
21-
* @param passwordLength number of digits for generated code in range 6...8
22-
* @param algorithm HMAC hash algorithm used to hash data
23-
* @param secret used to generate hash
24-
*/
25-
public HOTPGenerator(final int passwordLength, final HMACAlgorithm algorithm, final byte[] secret) {
26-
super(passwordLength, algorithm, secret);
27-
}
28-
2919
private HOTPGenerator(final HOTPGenerator.Builder builder) {
30-
super(builder.getPasswordLength(), builder.getAlgorithm(), builder.getSecret());
20+
super(builder);
3121
}
3222

3323
/**

src/main/java/com/bastiaanjansen/otp/OTPGenerator.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
* @author Bastiaan Jansen
2222
*/
23-
public class OTPGenerator {
23+
class OTPGenerator {
2424
private final static String URL_SCHEME = "otpauth";
2525

2626
/**
@@ -57,6 +57,10 @@ protected OTPGenerator(final int passwordLength, final HMACAlgorithm algorithm,
5757
this.secret = secret;
5858
}
5959

60+
protected OTPGenerator(final Builder<?, ?> builder) {
61+
this(builder.getPasswordLength(), builder.getAlgorithm(), builder.getSecret());
62+
}
63+
6064
public int getPasswordLength() {
6165
return passwordLength;
6266
}
@@ -248,27 +252,27 @@ protected abstract static class Builder<B, G> {
248252
/**
249253
* Number of digits for generated code in range 6...8, defaults to 6
250254
*/
251-
protected int passwordLength;
255+
private int passwordLength;
252256

253257
/**
254258
* Hashing algorithm used to generate code, defaults to SHA1
255259
*/
256-
protected HMACAlgorithm algorithm;
260+
private HMACAlgorithm algorithm;
257261

258262
/**
259263
* Secret key used to generate the code, this should be a base32 string
260264
*/
261-
protected byte[] secret;
265+
private final byte[] secret;
262266

263267
/**
264268
* Default value for password length
265269
*/
266-
public static final int DEFAULT_PASSWORD_LENGTH = 6;
270+
private static final int DEFAULT_PASSWORD_LENGTH = 6;
267271

268272
/**
269273
* Default value for HMAC Algorithm
270274
*/
271-
public static final HMACAlgorithm DEFAULT_HMAC_ALGORITHM = HMACAlgorithm.SHA1;
275+
private static final HMACAlgorithm DEFAULT_HMAC_ALGORITHM = HMACAlgorithm.SHA1;
272276

273277
public Builder(final byte[] secret) {
274278
this.secret = secret;

src/main/java/com/bastiaanjansen/otp/TOTPGenerator.java

+3-19
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,16 @@
1818
* @author Bastiaan Jansen
1919
* @see OTPGenerator
2020
*/
21-
public class TOTPGenerator extends OTPGenerator {
21+
public final class TOTPGenerator extends OTPGenerator {
2222
private final static String OTP_TYPE = "totp";
2323

2424
/**
2525
* Time interval between new codes
2626
*/
2727
private final Duration period;
2828

29-
/**
30-
* Constructs generator with custom password length, time interval and hashing algorithm
31-
*
32-
* @param passwordLength number of digits for generated code in range 6...8
33-
* @param period time interval between new codes
34-
* @param algorithm HMAC hash algorithm used to hash data
35-
* @param secret used to generate hash
36-
*/
37-
public TOTPGenerator(final int passwordLength, final Duration period, final HMACAlgorithm algorithm, final byte[] secret) {
38-
super(passwordLength, algorithm, secret);
39-
40-
if (period.getSeconds() < 1) throw new IllegalArgumentException("Period must be at least 1 second");
41-
42-
this.period = period;
43-
}
44-
4529
private TOTPGenerator(final TOTPGenerator.Builder builder) {
46-
super(builder.getPasswordLength(), builder.getAlgorithm(), builder.getSecret());
30+
super(builder);
4731
this.period = builder.getPeriod();
4832
}
4933

@@ -194,7 +178,7 @@ public static class Builder extends OTPGenerator.Builder<Builder, TOTPGenerator>
194178
/**
195179
* Default time interval for a time-based one-time password
196180
*/
197-
public static final Duration DEFAULT_PERIOD = Duration.ofSeconds(30);
181+
private static final Duration DEFAULT_PERIOD = Duration.ofSeconds(30);
198182

199183
/**
200184
* Constructs a TOTPGenerator builder

src/test/java/com/bastiaanjansen/otp/HOTPGeneratorTest.java

+11-11
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ class HOTPGeneratorTest {
1515

1616
private final String secret = "vv3kox7uqj4kyakohmzpph3us4cjimh6f3zknb5c2oobq6v2kiyhm27q";
1717

18-
@Test
19-
void constructor_instanceOfHOTPGenerator() {
20-
HOTPGenerator generator = new HOTPGenerator(6, HMACAlgorithm.SHA1, secret.getBytes());
21-
22-
assertThat(generator, instanceOf(HOTPGenerator.class));
23-
}
24-
25-
@Test
26-
void constructorWithInvalidPasswordLength_throwsIllegalArgumentException() {
27-
assertThrows(IllegalArgumentException.class, () -> new HOTPGenerator(5, HMACAlgorithm.SHA1, secret.getBytes()));
28-
}
18+
// @Test
19+
// void constructor_instanceOfHOTPGenerator() {
20+
// HOTPGenerator generator = new HOTPGenerator(6, HMACAlgorithm.SHA1, secret.getBytes());
21+
//
22+
// assertThat(generator, instanceOf(HOTPGenerator.class));
23+
// }
24+
//
25+
// @Test
26+
// void constructorWithInvalidPasswordLength_throwsIllegalArgumentException() {
27+
// assertThrows(IllegalArgumentException.class, () -> new HOTPGenerator(5, HMACAlgorithm.SHA1, secret.getBytes()));
28+
// }
2929

3030
@Test
3131
void generateWithSixDigits() {

src/test/java/com/bastiaanjansen/otp/TOTPGeneratorTest.java

+14-5
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,24 @@ class TOTPGeneratorTest {
2020
private final String secret = "vv3kox7uqj4kyakohmzpph3us4cjimh6f3zknb5c2oobq6v2kiyhm27q";
2121

2222
@Test
23-
void constructor_instanceOfTOTPGenerator() {
24-
TOTPGenerator generator = new TOTPGenerator(6, Duration.ofSeconds(30), HMACAlgorithm.SHA1, secret.getBytes());
23+
void builderWithInvalidPasswordLength_throwsIllegalArgumentException() {
24+
assertThrows(IllegalArgumentException.class, () -> new TOTPGenerator.Builder(secret.getBytes()).withPasswordLength(5).build());
25+
}
26+
27+
@Test
28+
void builderWithoutPeriod_defaultPeriod() {
29+
TOTPGenerator generator = new TOTPGenerator.Builder(secret.getBytes()).build();
30+
Duration expected = Duration.ofSeconds(30);
2531

26-
assertThat(generator, instanceOf(TOTPGenerator.class));
32+
assertThat(generator.getPeriod(), is(expected));
2733
}
2834

2935
@Test
30-
void constructorWithInvalidPasswordLength_throwsIllegalArgumentException() {
31-
assertThrows(IllegalArgumentException.class, () -> new TOTPGenerator(5, Duration.ofSeconds(30), HMACAlgorithm.SHA1, secret.getBytes()));
36+
void builderWithoutAlgorithm_defaultAlgorithm() {
37+
TOTPGenerator generator = new TOTPGenerator.Builder(secret.getBytes()).build();
38+
HMACAlgorithm expected = HMACAlgorithm.SHA1;
39+
40+
assertThat(generator.getAlgorithm(), is(expected));
3241
}
3342

3443
@Test

0 commit comments

Comments
 (0)