Updated TotpGenerator tool to read secret from System.in instead of arg

This commit is contained in:
Stian Thorgersen 2013-11-28 18:07:55 +00:00
parent 64396a16b8
commit 46f7e1f7fc
3 changed files with 35 additions and 19 deletions

View file

@ -30,11 +30,11 @@ TOTP codes
To generate totp codes without Google authenticator run: To generate totp codes without Google authenticator run:
mvn exec:java -Ptotp -Dsecret='PJBX GURY NZIT C2JX I44T S3D2 JBKD G6SB' mvn exec:java -Ptotp
or run org.keycloak.testutils.TotpGenerator from your favourite IDE! or run org.keycloak.testutils.TotpGenerator from your favourite IDE!
Replace value of -Dsecret with the secret from the totp configuration page (remember quotes!) Once started copy/paste the totp secret and press enter. To use a new secret just copy/paste and press enter again.
Mail server Mail server
----------- -----------

View file

@ -268,9 +268,6 @@
<artifactId>exec-maven-plugin</artifactId> <artifactId>exec-maven-plugin</artifactId>
<configuration> <configuration>
<mainClass>org.keycloak.testutils.TotpGenerator</mainClass> <mainClass>org.keycloak.testutils.TotpGenerator</mainClass>
<arguments>
<argument>${secret}</argument>
</arguments>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>

View file

@ -3,6 +3,10 @@ package org.keycloak.testutils;
import org.keycloak.models.utils.Base32; import org.keycloak.models.utils.Base32;
import org.keycloak.models.utils.TimeBasedOTP; import org.keycloak.models.utils.TimeBasedOTP;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -10,23 +14,38 @@ import java.util.concurrent.TimeUnit;
public class TotpGenerator { public class TotpGenerator {
public static void main(String[] args) { public static void main(String[] args) throws IOException {
String totp = "";
for (String a : args) { Timer timer = new Timer();
totp += a.trim(); TotpTask task = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Insert secret: ");
for (String l = br.readLine(); true; l = br.readLine()) {
if (task != null) {
task.cancel();
} }
totp = totp.replace(" ", "");
final String google = new String(Base32.decode(totp)); System.out.println("Secret: " + l);
final TimeBasedOTP otp = new TimeBasedOTP(); task = new TotpTask(l);
timer.schedule(task, 0, TimeUnit.SECONDS.toMillis(TimeBasedOTP.DEFAULT_INTERVAL_SECONDS));
}
}
private static class TotpTask extends TimerTask {
private String secret;
private TotpTask(String secret) {
this.secret = secret;
}
Timer t = new Timer();
t.schedule(new TimerTask() {
@Override @Override
public void run() { public void run() {
String google = new String(Base32.decode(secret));
TimeBasedOTP otp = new TimeBasedOTP();
System.out.println(otp.generate(google)); System.out.println(otp.generate(google));
} }
}, 0, TimeUnit.SECONDS.toMillis(TimeBasedOTP.DEFAULT_INTERVAL_SECONDS));
} }
} }