Skip to content

Don't extend HmacOneTimePasswordGenerator in TimeBasedOneTimePasswordGenerator #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
*
* @author <a href="https://github.com/jchambers">Jon Chambers</a>
*/
public class TimeBasedOneTimePasswordGenerator extends HmacOneTimePasswordGenerator {
public class TimeBasedOneTimePasswordGenerator {
private final HmacOneTimePasswordGenerator hotp;
private final Duration timeStep;

/**
Expand Down Expand Up @@ -115,7 +116,7 @@ public TimeBasedOneTimePasswordGenerator(final Duration timeStep, final int pass
public TimeBasedOneTimePasswordGenerator(final Duration timeStep, final int passwordLength, final String algorithm)
throws NoSuchAlgorithmRuntimeException {

super(passwordLength, algorithm);
this.hotp = new HmacOneTimePasswordGenerator(passwordLength, algorithm);
this.timeStep = timeStep;
}

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

/**
Expand Down Expand Up @@ -163,7 +164,7 @@ public String generateOneTimePasswordString(final Key key, final Instant timesta
* @throws InvalidKeyException if the given key is inappropriate for initializing the {@link Mac} for this generator
*/
public String generateOneTimePasswordString(final Key key, final Instant timestamp, final Locale locale) throws InvalidKeyException {
return this.formatOneTimePassword(this.generateOneTimePassword(key, timestamp), locale);
return this.hotp.formatOneTimePassword(this.generateOneTimePassword(key, timestamp), locale);
}

/**
Expand All @@ -174,4 +175,22 @@ public String generateOneTimePasswordString(final Key key, final Instant timesta
public Duration getTimeStep() {
return this.timeStep;
}

/**
* Returns the length, in decimal digits, of passwords produced by this generator.
*
* @return the length, in decimal digits, of passwords produced by this generator
*/
public int getPasswordLength() {
return this.hotp.getPasswordLength();
}

/**
* Returns the name of the HMAC algorithm used by this generator.
*
* @return the name of the HMAC algorithm used by this generator
*/
public String getAlgorithm() {
return this.hotp.getAlgorithm();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void testGetAlgorithm() {
@ParameterizedTest
@MethodSource("argumentsForTestGenerateOneTimePasswordHotp")
void testGenerateOneTimePassword(final int counter, final int expectedOneTimePassword) throws Exception {
assertEquals(expectedOneTimePassword, this.getDefaultGenerator().generateOneTimePassword(HOTP_KEY, counter));
assertEquals(expectedOneTimePassword, new HmacOneTimePasswordGenerator().generateOneTimePassword(HOTP_KEY, counter));
}

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

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

private static Stream<Arguments> argumentsForTestGenerateOneTimePasswordStringLocaleHotp() {
Expand All @@ -147,8 +147,4 @@ private static Stream<Arguments> argumentsForTestGenerateOneTimePasswordStringLo
arguments(9, locale, "५२०४८९")
);
}

protected HmacOneTimePasswordGenerator getDefaultGenerator() {
return new HmacOneTimePasswordGenerator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.params.provider.Arguments.arguments;

public class TimeBasedOneTimePasswordGeneratorTest extends HmacOneTimePasswordGeneratorTest {
public class TimeBasedOneTimePasswordGeneratorTest {

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

@Override
protected HmacOneTimePasswordGenerator getDefaultGenerator() {
return new TimeBasedOneTimePasswordGenerator();
@Test
void testGetPasswordLength() {
final int passwordLength = 7;
assertEquals(passwordLength, new TimeBasedOneTimePasswordGenerator(Duration.ofSeconds(30), passwordLength).getPasswordLength());
}

@Test
void testGetAlgorithm() {
final String algorithm = TimeBasedOneTimePasswordGenerator.TOTP_ALGORITHM_HMAC_SHA256;
assertEquals(algorithm, new TimeBasedOneTimePasswordGenerator(Duration.ofSeconds(30), 6, algorithm).getAlgorithm());
}

@Test
void testGetTimeStep() {
final Duration timeStep = Duration.ofSeconds(97);
final TimeBasedOneTimePasswordGenerator totp = new TimeBasedOneTimePasswordGenerator(timeStep);

assertEquals(timeStep, totp.getTimeStep());
assertEquals(timeStep, new TimeBasedOneTimePasswordGenerator(timeStep).getTimeStep());
}

/**
Expand Down