Java Implmentation #101
-
Hello, I was interested in getting this running in Java but was having difficulties getting my code to compile. Would you guys be able to add example code for Java to the readme? Not being familiar with Kotlin I was having trouble constructing the Kotlin continuation. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 6 replies
-
Hi @Kachopsticks sure, what exactly are you implementing, the server verifying the OTP or the client generating one? |
Beta Was this translation helpful? Give feedback.
-
Hi @Kachopsticks , please update to version 2.0.0. Should be up on maven central soon. Have a look at this simple example for both client and server using the same secret to generate TOTP and to verify it to later handle the verification result: import com.atlassian.onetime.core.TOTP;
import com.atlassian.onetime.core.TOTPGenerator;
import com.atlassian.onetime.model.TOTPSecret;
import com.atlassian.onetime.service.DefaultTOTPService;
import com.atlassian.onetime.service.TOTPVerificationResult;
public class Example {
public static void main(String[] args) {
//Client generating a TOTP given a secret
TOTPSecret secret = TOTPSecret.Companion.fromBase32EncodedString("ZIQL3WHUAGCS5FQQDKP74HZCFT56TJHR");
TOTPGenerator totpGenerator = new TOTPGenerator();
TOTP totp = totpGenerator.generateCurrent(secret);
// totp for current time window. You can now use this to present to the user in UI
System.out.println(totp);
//Server verifying such TOTP for the same secret:
DefaultTOTPService service = new DefaultTOTPService();
TOTPVerificationResult result = service.verify(totp, secret);
if (result instanceof TOTPVerificationResult.Success){
// Valid totp - handle success
Integer successIndex = ((TOTPVerificationResult.Success) result).getIndex();
System.out.printf("Success at %d\n", successIndex );
} else {
// Invalid TOTP - handle error
System.out.println("Invalid TOTP provided" );
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Thanks Diego. I was able to pull down the library but even with version 2.0.0 the method is still expecting a continuation parameter. I deleted the older library I had to make sure there was no conflict. |
Beta Was this translation helpful? Give feedback.
-
Hi @Kachopsticks, 1time 2.0.1 is released. If using maven, do: <dependency>
<groupId>com.atlassian</groupId>
<artifactId>onetime</artifactId>
<version>2.0.1</version>
</dependency> And then see the full Java example: package com.mycompany.app;
import com.atlassian.onetime.core.TOTP;
import com.atlassian.onetime.core.TOTPGenerator;
import com.atlassian.onetime.model.TOTPSecret;
import com.atlassian.onetime.service.DefaultTOTPService;
import com.atlassian.onetime.service.TOTPVerificationResult;
public class App {
public static void main(String[] args) {
//Client generating a TOTP given a secret
TOTPSecret secret = TOTPSecret.Companion.fromBase32EncodedString("ZIQL3WHUAGCS5FQQDKP74HZCFT56TJHR");
TOTPGenerator totpGenerator = new TOTPGenerator();
TOTP totp = totpGenerator.generateCurrent(secret);
// totp for current time window. You can now use this to present to the user in UI
System.out.println(totp);
//Server verifying such TOTP for the same secret:
DefaultTOTPService service = new DefaultTOTPService();
TOTPVerificationResult result = service.verify(new TOTP("1234"), secret);
if (result.isSuccess()) {
// Valid totp - handle success
Integer successIndex = ((TOTPVerificationResult.Success) result).getIndex();
System.out.printf("Success at %d\n", successIndex);
} else {
// Invalid TOTP - handle error
System.out.println("Invalid TOTP provided");
}
}
} Try this out and let us know if it works for you. Thanks! |
Beta Was this translation helpful? Give feedback.
-
Hi Diego, I pulled down 2.0.1 and saw that the method signature has now changed to accept only the TOTP and TOTPSecret parameters. Thanks so much for your help! |
Beta Was this translation helpful? Give feedback.
Hi @Kachopsticks, 1time 2.0.1 is released. If using maven, do:
And then see the full Java example: