Skip to content

Commit 891aaba

Browse files
committed
Don't extend HmacOneTimePasswordGenerator in TimeBasedOneTimePasswordGenerator
1 parent f9e0fcb commit 891aaba

File tree

3 files changed

+39
-21
lines changed

3 files changed

+39
-21
lines changed

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

Lines changed: 24 additions & 7 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
/**
@@ -112,10 +113,8 @@ public TimeBasedOneTimePasswordGenerator(final Duration timeStep, final int pass
112113
* @see #TOTP_ALGORITHM_HMAC_SHA256
113114
* @see #TOTP_ALGORITHM_HMAC_SHA512
114115
*/
115-
public TimeBasedOneTimePasswordGenerator(final Duration timeStep, final int passwordLength, final String algorithm)
116-
throws NoSuchAlgorithmRuntimeException {
117-
118-
super(passwordLength, algorithm);
116+
public TimeBasedOneTimePasswordGenerator(final Duration timeStep, final int passwordLength, final String algorithm) {
117+
this.hotp = new HmacOneTimePasswordGenerator(passwordLength, algorithm);
119118
this.timeStep = timeStep;
120119
}
121120

@@ -131,7 +130,7 @@ public TimeBasedOneTimePasswordGenerator(final Duration timeStep, final int pass
131130
* @throws InvalidKeyException if the given key is inappropriate for initializing the {@link Mac} for this generator
132131
*/
133132
public int generateOneTimePassword(final Key key, final Instant timestamp) throws InvalidKeyException {
134-
return this.generateOneTimePassword(key, timestamp.toEpochMilli() / this.timeStep.toMillis());
133+
return this.hotp.generateOneTimePassword(key, timestamp.toEpochMilli() / this.timeStep.toMillis());
135134
}
136135

137136
/**
@@ -163,7 +162,7 @@ public String generateOneTimePasswordString(final Key key, final Instant timesta
163162
* @throws InvalidKeyException if the given key is inappropriate for initializing the {@link Mac} for this generator
164163
*/
165164
public String generateOneTimePasswordString(final Key key, final Instant timestamp, final Locale locale) throws InvalidKeyException {
166-
return this.formatOneTimePassword(this.generateOneTimePassword(key, timestamp), locale);
165+
return this.hotp.formatOneTimePassword(this.generateOneTimePassword(key, timestamp), locale);
167166
}
168167

169168
/**
@@ -174,4 +173,22 @@ public String generateOneTimePasswordString(final Key key, final Instant timesta
174173
public Duration getTimeStep() {
175174
return this.timeStep;
176175
}
176+
177+
/**
178+
* Returns the length, in decimal digits, of passwords produced by this generator.
179+
*
180+
* @return the length, in decimal digits, of passwords produced by this generator
181+
*/
182+
public int getPasswordLength() {
183+
return this.hotp.getPasswordLength();
184+
}
185+
186+
/**
187+
* Returns the name of the HMAC algorithm used by this generator.
188+
*
189+
* @return the name of the HMAC algorithm used by this generator
190+
*/
191+
public String getAlgorithm() {
192+
return this.hotp.getAlgorithm();
193+
}
177194
}

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)