uport-project / kotlin-did-jwt

Create and verify uPort and DID compliant JWTs in Kotlin
https://developer.uport.me
Apache License 2.0
8 stars 6 forks source link

[Do not merge][Support] 166983459 java calling conventions #8

Closed mirceanis closed 5 years ago

mirceanis commented 5 years ago

This is a test of a wrapper for did-jwt usable in a JAVA 8 app.

This is a java compatible did-jwt library built by wrapping the kotlin variant.

in your build.gradle:

dependencies {
    implementation "com.github.uport-project.kotlin-did-jwt:jwt-java:eb45524e15"
}

The wrapper exposes the same signature as the kotlin variant so they are not usable in the same project. This is not set in stone so if needed, they can be differentiated, because it's likely there need to be different calling conventions for Java (builder pattern since there are no default params).

As opposed to the coroutines of the kotlin variant, the wrapper methods result in CompletableFuture<*> so they are usable in a Java 8 context.

create a token

HashMap<String, Object> payload = new HashMap<>();
put.put("claim", "something something");

KPSigner signer = new KPSigner("0x65fc670d9351cb87d1f56702fb56a7832ae2aab3427be944ab8c9f2a0ab87960");
String issuer = "did:ethr:" + signer.getAddress();

CompletableFuture<String> jwtFuture = new JWTTools().createJWT(
        payload,
        issuer,
        signer,
        -1,
        JwtHeader.ES256K);

//...sometime later
String jwtToken = jwtFuture.join();

verify a token

JWTTools tools = new JWTTools();
CompletableFuture<JwtPayload> jwtPayloadCompletableFuture =
        tools.verify(
"eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NksifQ.eyJoZWxsbyI6IndvcmxkIiwiaWF0IjoxNTYxOTcxMTE5LCJpc3MiOiJkaWQ6ZXRocjoweGNmMDNkZDBhODk0ZWY3OWNiNWI2MDFhNDNjNGIyNWUzYWU0YzY3ZWQifQ.t5o1vzZExArlrrTVHmwtti7fnicXqvWrX6SS3F-Lu3budH7p6zQHjG8X7EvUTRUxhvr-eENCbXeteSE4rgF7MA",
                false,
                null);

//...sometime later
JwtPayload result = jwtPayloadCompletableFuture.join();

Also, there's a test artifact that can be used to mock some EthrDID doc calls. testImplementation "com.github.uport-project.kotlin-did-jwt:jwt-test:eb45524e15"

ajunge commented 5 years ago

To verify a token the method "verifyAsync" needs to be called not "verify"