Skip to content

Commit 9914c74

Browse files
committed
Don't extend HmacOneTimePasswordGenerator in TimeBasedOneTimePasswordGenerator
1 parent f9e0fcb commit 9914c74

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

src/main/java/com/eatthepath/otp/TimeBasedOneTimePasswordGenerator.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
*
3636
* @author <a href="https://github.com/jchambers">Jon Chambers</a>
3737
*/
38-
public class TimeBasedOneTimePasswordGenerator extends HmacOneTimePasswordGenerator {
38+
public class TimeBasedOneTimePasswordGenerator {
39+
private final HmacOneTimePasswordGenerator hotp;
3940
private final Duration timeStep;
4041

4142
/**
@@ -115,7 +116,7 @@ public TimeBasedOneTimePasswordGenerator(final Duration timeStep, final int pass
115116
public TimeBasedOneTimePasswordGenerator(final Duration timeStep, final int passwordLength, final String algorithm)
116117
throws NoSuchAlgorithmRuntimeException {
117118

118-
super(passwordLength, algorithm);
119+
this.hotp = new HmacOneTimePasswordGenerator(passwordLength, algorithm);
119120
this.timeStep = timeStep;
120121
}
121122

@@ -131,7 +132,7 @@ public TimeBasedOneTimePasswordGenerator(final Duration timeStep, final int pass
131132
* @throws InvalidKeyException if the given key is inappropriate for initializing the {@link Mac} for this generator
132133
*/
133134
public int generateOneTimePassword(final Key key, final Instant timestamp) throws InvalidKeyException {
134-
return this.generateOneTimePassword(key, timestamp.toEpochMilli() / this.timeStep.toMillis());
135+
return this.hotp.generateOneTimePassword(key, timestamp.toEpochMilli() / this.timeStep.toMillis());
135136
}
136137

137138
/**
@@ -163,7 +164,7 @@ public String generateOneTimePasswordString(final Key key, final Instant timesta
163164
* @throws InvalidKeyException if the given key is inappropriate for initializing the {@link Mac} for this generator
164165
*/
165166
public String generateOneTimePasswordString(final Key key, final Instant timestamp, final Locale locale) throws InvalidKeyException {
166-
return this.formatOneTimePassword(this.generateOneTimePassword(key, timestamp), locale);
167+
return this.hotp.formatOneTimePassword(this.generateOneTimePassword(key, timestamp), locale);
167168
}
168169

169170
/**
@@ -174,4 +175,22 @@ public String generateOneTimePasswordString(final Key key, final Instant timesta
174175
public Duration getTimeStep() {
175176
return this.timeStep;
176177
}
178+
179+
/**
180+
* Returns the length, in decimal digits, of passwords produced by this generator.
181+
*
182+
* @return the length, in decimal digits, of passwords produced by this generator
183+
*/
184+
public int getPasswordLength() {
185+
return this.hotp.getPasswordLength();
186+
}
187+
188+
/**
189+
* Returns the name of the HMAC algorithm used by this generator.
190+
*
191+
* @return the name of the HMAC algorithm used by this generator
192+
*/
193+
public String getAlgorithm() {
194+
return this.hotp.getAlgorithm();
195+
}
177196
}

src/test/java/com/eatthepath/otp/HmacOneTimePasswordGeneratorTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void testGetAlgorithm() {
7676
@ParameterizedTest
7777
@MethodSource("argumentsForTestGenerateOneTimePasswordHotp")
7878
void testGenerateOneTimePassword(final int counter, final int expectedOneTimePassword) throws Exception {
79-
assertEquals(expectedOneTimePassword, this.getDefaultGenerator().generateOneTimePassword(HOTP_KEY, counter));
79+
assertEquals(expectedOneTimePassword, new HmacOneTimePasswordGenerator().generateOneTimePassword(HOTP_KEY, counter));
8080
}
8181

8282
@Test
@@ -106,7 +106,7 @@ private static Stream<Arguments> argumentsForTestGenerateOneTimePasswordHotp() {
106106
@MethodSource("argumentsForTestGenerateOneTimePasswordStringHotp")
107107
void testGenerateOneTimePasswordString(final int counter, final String expectedOneTimePassword) throws Exception {
108108
Locale.setDefault(Locale.US);
109-
assertEquals(expectedOneTimePassword, this.getDefaultGenerator().generateOneTimePasswordString(HOTP_KEY, counter));
109+
assertEquals(expectedOneTimePassword, new HmacOneTimePasswordGenerator().generateOneTimePasswordString(HOTP_KEY, counter));
110110
}
111111

112112
private static Stream<Arguments> argumentsForTestGenerateOneTimePasswordStringHotp() {
@@ -128,7 +128,7 @@ private static Stream<Arguments> argumentsForTestGenerateOneTimePasswordStringHo
128128
@MethodSource("argumentsForTestGenerateOneTimePasswordStringLocaleHotp")
129129
void testGenerateOneTimePasswordStringLocale(final int counter, final Locale locale, final String expectedOneTimePassword) throws Exception {
130130
Locale.setDefault(Locale.US);
131-
assertEquals(expectedOneTimePassword, this.getDefaultGenerator().generateOneTimePasswordString(HOTP_KEY, counter, locale));
131+
assertEquals(expectedOneTimePassword, new HmacOneTimePasswordGenerator().generateOneTimePasswordString(HOTP_KEY, counter, locale));
132132
}
133133

134134
private static Stream<Arguments> argumentsForTestGenerateOneTimePasswordStringLocaleHotp() {
@@ -147,8 +147,4 @@ private static Stream<Arguments> argumentsForTestGenerateOneTimePasswordStringLo
147147
arguments(9, locale, "५२०४८९")
148148
);
149149
}
150-
151-
protected HmacOneTimePasswordGenerator getDefaultGenerator() {
152-
return new HmacOneTimePasswordGenerator();
153-
}
154150
}

src/test/java/com/eatthepath/otp/TimeBasedOneTimePasswordGeneratorTest.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import static org.junit.jupiter.api.Assertions.assertEquals;
3737
import static org.junit.jupiter.params.provider.Arguments.arguments;
3838

39-
public class TimeBasedOneTimePasswordGeneratorTest extends HmacOneTimePasswordGeneratorTest {
39+
public class TimeBasedOneTimePasswordGeneratorTest {
4040

4141
private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
4242
private static final String HMAC_SHA256_ALGORITHM = "HmacSHA256";
@@ -54,17 +54,22 @@ public class TimeBasedOneTimePasswordGeneratorTest extends HmacOneTimePasswordGe
5454
new SecretKeySpec("1234567890123456789012345678901234567890123456789012345678901234".getBytes(StandardCharsets.US_ASCII),
5555
TimeBasedOneTimePasswordGenerator.TOTP_ALGORITHM_HMAC_SHA512);
5656

57-
@Override
58-
protected HmacOneTimePasswordGenerator getDefaultGenerator() {
59-
return new TimeBasedOneTimePasswordGenerator();
57+
@Test
58+
void testGetPasswordLength() {
59+
final int passwordLength = 7;
60+
assertEquals(passwordLength, new TimeBasedOneTimePasswordGenerator(Duration.ofSeconds(30), passwordLength).getPasswordLength());
61+
}
62+
63+
@Test
64+
void testGetAlgorithm() {
65+
final String algorithm = TimeBasedOneTimePasswordGenerator.TOTP_ALGORITHM_HMAC_SHA256;
66+
assertEquals(algorithm, new TimeBasedOneTimePasswordGenerator(Duration.ofSeconds(30), 6, algorithm).getAlgorithm());
6067
}
6168

6269
@Test
6370
void testGetTimeStep() {
6471
final Duration timeStep = Duration.ofSeconds(97);
65-
final TimeBasedOneTimePasswordGenerator totp = new TimeBasedOneTimePasswordGenerator(timeStep);
66-
67-
assertEquals(timeStep, totp.getTimeStep());
72+
assertEquals(timeStep, new TimeBasedOneTimePasswordGenerator(timeStep).getTimeStep());
6873
}
6974

7075
/**

0 commit comments

Comments
 (0)