Atlassian 1time TOTP - Java Implementation

491 Views Asked by At

I was attempting to implement this TOTP verification library created by Atlassian using java 1.8:

Atlassian 1time totp library

If I create a DefaultTOTPService, when I try to call any of its methods they are asking for a Continuation parameter. I understand this is a Kotlin continuation but I can't seem to get the syntax correct. I have a verification method which has the code:

    DefaultTOTPService serv = new DefaultTOTPService();
    serv.verify(totp, totpSecret, myContinutation);

Within the same class I have defined myContinuation as

Continuation<? super TOTPVerificationResult> myContinutation = new Continuation<TOTPVerificationResult>() {
    @Override
    public CoroutineContext getContext() {
        // TODO Auto-generated method stub
        return EmptyCoroutineContext.INSTANCE;
    }
    @Override
    public void resumeWith(Object arg0) {
        System.out.println("Result of getName is " + arg0);
        
    }

With this code I get the compilation error

The method verify(TOTP, TOTPSecret, Continuation<? super TOTPVerificationResult>) in the type DefaultTOTPService is not applicable for the arguments (OtpInfo, String, Continuation<capture#1-of ? super TOTPVerificationResult>)

Looking at this post I thought my code would work. Not sure what I am missing.

What do I need to fix to get my code to compile? Thank you.

1

There are 1 best solutions below

0
Diego Ocampo On

Cross-posting answer here:

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!