Skip to content

Navigation Menu

Sign in
Sign up

Java Implmentation #101

Closed Answered by diegoocampoh
Kachopsticks asked this question in Q&A
Discussion options

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!

You must be logged in to vote

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(

Replies: 5 comments 6 replies

Comment options

Hi @Kachopsticks sure, what exactly are you implementing, the server verifying the OTP or the client generating one?

You must be logged in to vote
0 replies
Comment options

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" );
 }
 }
}
You must be logged in to vote
0 replies
Comment options

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.

image

You must be logged in to vote
5 replies
Comment options

Hi @Kachopsticks please fill in this form https://forms.gle/iAtMVmtaZoszUH7EA so we can add you to the slack channel to further discuss. Looks like the publishing to maven central hasn't reached there yet. Give it another go when 2.0.0 show up here: https://mvnrepository.com/artifact/com.atlassian/onetime
Otherwise, happy to discuss further on slack. Thanks!

Comment options

Thanks Diego. I did notice that on maven central the 2.0.0 version was not listed but when I added it to my pom it still seemed to pull down that version. I will delete what it pulled and redo it once it shows up on the maven central website. If I still have issues I will join the slack.

Thanks!

Comment options

@Kachopsticks https://mvnrepository.com/artifact/com.atlassian/onetime/2.0.0 is up now;

If using maven do:

<dependency>
 <groupId>com.atlassian</groupId>
 <artifactId>onetime</artifactId>
 <version>2.0.0</version>
</dependency>

Make sure to clean and delete build folder and reload the project.
The new version does not take a continuation. Let me know how it goes.
Any feedback is appreciated, thanks!

Comment options

Hi Diego, thanks for all of your help. Unfortunately I am still getting the same issue. I deleted my entire onetime folder in my .m2 repository. I did a maven clean/install and then maven update. all of the methods still look for the continuation. Not sure if it is something on my end or something else.

Comment options

Hi @Kachopsticks, I'll fix this problem when realising 2.0.1, which also contains the isSuccess and isFailure on TOTPVerificationResult.
Download this zip with the jars and add them manually to your codebase to test it out. Do not use maven for 1time for now until 2.0.1 is released. Hope this helps!

2.0.1-pre.zip

Comment options

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!

You must be logged in to vote
0 replies
Answer selected by diegoocampoh
Comment options

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!

You must be logged in to vote
1 reply
Comment options

Awesome thanks, let us know if you need further help, I'll close the discussion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /