From e5b63bc67245a5877a6f8d9fdaef8c27e0d5b9d2 Mon Sep 17 00:00:00 2001 From: Fabrizio Cannizzo Date: Wed, 14 Feb 2024 14:54:07 +0000 Subject: [PATCH] removed java --- .gitignore | 2 - ffi/java-idsdk/README.md | 69 ------ ffi/java-idsdk/pom.xml | 122 ---------- .../com/iotics/sdk/identity/Identity.java | 53 ----- .../java/com/iotics/sdk/identity/Seeds.java | 28 --- .../iotics/sdk/identity/SimpleIdentity.java | 105 --------- .../sdk/identity/SimpleIdentityException.java | 8 - .../com/iotics/sdk/identity/Validator.java | 21 -- .../iotics/sdk/identity/experimental/JWT.java | 61 ----- .../identity/experimental/ResolverClient.java | 118 ---------- .../iotics/sdk/identity/go/StringResult.java | 34 --- .../identity/jna/JnaSdkApiInitialiser.java | 27 --- .../com/iotics/sdk/identity/jna/SdkApi.java | 94 -------- .../java/com/iotics/sdk/identity/App.java | 79 ------- .../com/iotics/sdk/identity/DataFactory.java | 26 --- .../com/iotics/sdk/identity/SeedsTest.java | 38 --- .../sdk/identity/SimpleIdentityTest.java | 221 ------------------ 17 files changed, 1106 deletions(-) delete mode 100644 ffi/java-idsdk/README.md delete mode 100644 ffi/java-idsdk/pom.xml delete mode 100644 ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/Identity.java delete mode 100644 ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/Seeds.java delete mode 100644 ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/SimpleIdentity.java delete mode 100644 ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/SimpleIdentityException.java delete mode 100644 ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/Validator.java delete mode 100644 ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/experimental/JWT.java delete mode 100644 ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/experimental/ResolverClient.java delete mode 100644 ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/go/StringResult.java delete mode 100644 ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/jna/JnaSdkApiInitialiser.java delete mode 100644 ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/jna/SdkApi.java delete mode 100644 ffi/java-idsdk/src/test/java/com/iotics/sdk/identity/App.java delete mode 100644 ffi/java-idsdk/src/test/java/com/iotics/sdk/identity/DataFactory.java delete mode 100644 ffi/java-idsdk/src/test/java/com/iotics/sdk/identity/SeedsTest.java delete mode 100644 ffi/java-idsdk/src/test/java/com/iotics/sdk/identity/SimpleIdentityTest.java diff --git a/.gitignore b/.gitignore index 985bfe7..bc4fdd5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,4 @@ ffi/lib -ffi/java-idsdk/target -ffi/java-idsdk/**/*.class dist/ ffi/dotnet-idsdk/**/.vs ffi/dotnet-idsdk/**/obj diff --git a/ffi/java-idsdk/README.md b/ffi/java-idsdk/README.md deleted file mode 100644 index 84e20a4..0000000 --- a/ffi/java-idsdk/README.md +++ /dev/null @@ -1,69 +0,0 @@ -# Java wrapper for the ID SDK - -FFI wrapper using JNA - -## Build - -Build the java library with: - -`mvn pakage` - -In order for the library to work, the relevant sdk library must be compile for your operating system. -Follow instructions here `ffi/README.md` for how to build it. - -## Test - -A sample app is in the test/java directory: `com.iotics.sdk.identity.App` - -## Use - -Initialise the API with: - -```java -String libPath = ""; -SdkApi api = new JnaSdkApiInitialiser(libPath).get(); -``` - -Usages: see also `src/test/java/com/iotics/sdk/identity/App.java` - -```java - - // Generate seeds - public static void seeds(SdkApi api) { - - Seeds seeds = new Seeds(api); - - String res; - - res = seeds.CreateDefaultSeed(); - System.out.println("CreateDefaultSeed: " + res); - - res = seeds.SeedBip39ToMnemonic(res); - System.out.println("SeedBip39ToMnemonic: " + res); - - res = seeds.MnemonicBip39ToSeed(res); - System.out.println("MnemonicBip39ToSeed: " + res); - } - - // Generate identities - public static void identities(SdkApi api) { - SimpleIdentity idSdk = new SimpleIdentity(api, resolver, seed); - - Identity agentIdentity = idSdk.CreateAgentIdentity("aKey1", "#app1"); - System.out.println("CreateAgentIdentity: " + agentIdentity ); - - Identity userIdentity = idSdk.CreateUserIdentity("uKey1", "#user1"); - System.out.println("CreateUserIdentity: " + userIdentity); - - Identity twinIdentity = idSdk.CreateTwinIdentityWithControlDelegation(agentIdentity, "tKey1", "#tName"); - System.out.println("CreateTwinDidWithControlDelegation: " + twinIdentity); - } - - // Generate an authentication token - public static void token(SdkApi api) { - SimpleIdentity idSdk = new SimpleIdentity(api, resolver, seed); - String token = idSdk.CreateAgentAuthToken(agentIdentity, uDiD, Duration.ofHours(10)); - System.out.println("CreateAgentAuthToken: " + token); - } - -``` diff --git a/ffi/java-idsdk/pom.xml b/ffi/java-idsdk/pom.xml deleted file mode 100644 index 3437516..0000000 --- a/ffi/java-idsdk/pom.xml +++ /dev/null @@ -1,122 +0,0 @@ - - - - 4.0.0 - - com.iotics.sdk.identity - java-idsdk - 1.0-SNAPSHOT - - java-idsdk - https://github.com/Iotic-Labs/iotics-identity-go - - - UTF-8 - 11 - 11 - - - - - org.junit.jupiter - junit-jupiter-api - 5.4.2 - test - - - org.junit.jupiter - junit-jupiter-engine - 5.4.2 - test - - - org.mockito - mockito-junit-jupiter - 4.0.0 - test - - - - net.java.dev.jna - jna - 4.4.0 - - - com.squareup.okhttp3 - okhttp - 4.9.0 - - - org.json - json - 20180130 - - - org.bitcoinj - bitcoinj-core - 0.16.1 - - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - ${maven.compiler.target} - ${maven.compiler.target} - - - - - - - - maven-clean-plugin - 3.1.0 - - - - maven-resources-plugin - 3.0.2 - - - maven-compiler-plugin - 3.8.0 - - - maven-surefire-plugin - 2.22.2 - - - maven-failsafe-plugin - 2.22.2 - - - maven-jar-plugin - 3.0.2 - - - maven-install-plugin - 2.5.2 - - - maven-deploy-plugin - 2.8.2 - - - - maven-site-plugin - 3.7.1 - - - maven-project-info-reports-plugin - 3.0.0 - - - - - diff --git a/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/Identity.java b/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/Identity.java deleted file mode 100644 index de00a02..0000000 --- a/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/Identity.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.iotics.sdk.identity; - -import java.util.Objects; - -public final class Identity { - - private final String name; - private final String keyName; - private final String did; - - public Identity(String keyName, String name, String did) { - Objects.requireNonNull(name); - Objects.requireNonNull(keyName); - Objects.requireNonNull(did); - this.name = name; - this.keyName = keyName; - this.did = did; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Identity identity = (Identity) o; - return Objects.equals(name, identity.name) && Objects.equals(keyName, identity.keyName) && Objects.equals(did, identity.did); - } - - @Override - public int hashCode() { - return Objects.hash(name, keyName, did); - } - - @Override - public String toString() { - return "Identity{" + - "name='" + name + '\'' + - ", keyName='" + keyName + '\'' + - ", did='" + did + '\'' + - '}'; - } - - public String name() { - return name; - } - - public String keyName() { - return keyName; - } - - public String did() { - return did; - } -} \ No newline at end of file diff --git a/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/Seeds.java b/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/Seeds.java deleted file mode 100644 index 0251e29..0000000 --- a/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/Seeds.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.iotics.sdk.identity; - -import com.iotics.sdk.identity.jna.SdkApi; - -import java.util.Objects; - -import static com.iotics.sdk.identity.Validator.getValueOrThrow; - -public class Seeds { - private final SdkApi api; - - public Seeds(SdkApi api) { - this.api = Objects.requireNonNull(api); - } - - public String CreateDefaultSeed() { - return getValueOrThrow(api.CreateDefaultSeed()); - } - - public String MnemonicBip39ToSeed(String mnemonics) { - return getValueOrThrow(api.MnemonicBip39ToSeed(mnemonics)); - } - - public String SeedBip39ToMnemonic(String seed) { - return getValueOrThrow(api.SeedBip39ToMnemonic(seed)); - } - -} diff --git a/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/SimpleIdentity.java b/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/SimpleIdentity.java deleted file mode 100644 index 832cfdf..0000000 --- a/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/SimpleIdentity.java +++ /dev/null @@ -1,105 +0,0 @@ -package com.iotics.sdk.identity; - -import com.iotics.sdk.identity.jna.SdkApi; - -import java.net.URI; -import java.time.Duration; -import java.util.Objects; - -import static com.iotics.sdk.identity.Validator.getValueOrThrow; -import static com.iotics.sdk.identity.Validator.throwIfNotNull; - -public class SimpleIdentity { - private final SdkApi api; - private final String userSeed; - private final String agentSeed; - private final String resolverAddress; - - public SimpleIdentity(SdkApi api, String resolverAddress) { - this(api, resolverAddress, getValueOrThrow(api.CreateDefaultSeed())); - } - - public SimpleIdentity(SdkApi api, String resolverAddress, String seed) { - this(api, resolverAddress, seed, seed); - } - - public SimpleIdentity(SdkApi api, String resolverAddress, String userSeed, String agentSeed) { - this.api = Objects.requireNonNull(api); - this.userSeed = Objects.requireNonNull(userSeed); - this.agentSeed = Objects.requireNonNull(agentSeed); - this.resolverAddress = URI.create(resolverAddress).toString(); - } - - public Identity CreateAgentIdentity(String keyName, String name) { - String did = getValueOrThrow(api.CreateAgentIdentity(resolverAddress, keyName, name, agentSeed)); - return new Identity(keyName, name, did); - } - - public Identity RecreateAgentIdentity(String keyName, String name) { - String did = getValueOrThrow(api.RecreateAgentIdentity(resolverAddress, keyName, name, agentSeed)); - return new Identity(keyName, name, did); - } - - public Identity CreateUserIdentity(String keyName, String name) { - String did = getValueOrThrow(api.CreateUserIdentity(resolverAddress, keyName, name, userSeed)); - return new Identity(keyName, name, did); - } - - public Identity RecreateUserIdentity(String keyName, String name) { - String did = getValueOrThrow(api.RecreateUserIdentity(resolverAddress, keyName, name, userSeed)); - return new Identity(keyName, name, did); - } - - public Identity CreateTwinIdentityWithControlDelegation(Identity agentIdentity, String twinKeyName, String twinName) { - String did = getValueOrThrow(api.CreateTwinDidWithControlDelegation(resolverAddress, - agentIdentity.did(), agentIdentity.keyName(), agentIdentity.name(), agentSeed, twinKeyName, twinName)); - return new Identity(twinKeyName, twinName, did); - } - - public String CreateAgentAuthToken(Identity agentIdentity, String userDid, String audience, Duration duration) { - return getValueOrThrow(api.CreateAgentAuthToken( - agentIdentity.did(), agentIdentity.keyName(), agentIdentity.name(), agentSeed, userDid, audience, duration.toSeconds())); - } - - public String CreateAgentAuthToken(Identity agentIdentity, String userDid, Duration duration) { - return CreateAgentAuthToken(agentIdentity, userDid, resolverAddress, duration); - } - - public String RecreateAgentAuthToken(Identity agentIdentity, String userDid, String audience, Duration duration) { - return getValueOrThrow(api.CreateAgentAuthToken( - agentIdentity.did(), agentIdentity.keyName(), agentIdentity.name(), agentSeed, userDid, audience, duration.toSeconds())); - } - - public String RecreateAgentAuthToken(Identity agentIdentity, String userDid, Duration duration) { - return CreateAgentAuthToken(agentIdentity, userDid, resolverAddress, duration); - } - - public String IsAllowedFor(String resolverAddress, String token) { - return getValueOrThrow(api.IsAllowedFor(resolverAddress, token)); - } - - public void UserDelegatesAuthenticationToAgent(Identity agentId, Identity userId, String delegationName) { - throwIfNotNull(api.UserDelegatesAuthenticationToAgent(resolverAddress, - agentId.did(), agentId.keyName(), agentId.name(), agentSeed, - userId.did(), userId.keyName(), userId.name(), userSeed, delegationName)); - - } - - public void TwinDelegatesControlToAgent(Identity agentId, Identity twinId, String delegationName) { - throwIfNotNull(api.TwinDelegatesControlToAgent(resolverAddress, - agentId.did(), agentId.keyName(), agentId.name(), agentSeed, - twinId.did(), twinId.keyName(), twinId.name(), agentSeed, delegationName)); - } - - String getAgentSeed() { - return agentSeed; - } - - String getUserSeed() { - return userSeed; - } - - public String getResolverAddress() { - return resolverAddress; - } -} diff --git a/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/SimpleIdentityException.java b/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/SimpleIdentityException.java deleted file mode 100644 index 983d46e..0000000 --- a/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/SimpleIdentityException.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.iotics.sdk.identity; - -public class SimpleIdentityException extends RuntimeException { - public SimpleIdentityException(String message) { - super(message); - } - -} diff --git a/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/Validator.java b/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/Validator.java deleted file mode 100644 index ff1d411..0000000 --- a/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/Validator.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.iotics.sdk.identity; - -import com.iotics.sdk.identity.go.StringResult; - -public final class Validator { - - static String getValueOrThrow(StringResult ret) { - if (ret.err != null) { - throw new SimpleIdentityException(ret.err); - } - return ret.value; - - } - - static void throwIfNotNull(String err) { - if (err != null) { - throw new SimpleIdentityException(err); - } - } - -} diff --git a/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/experimental/JWT.java b/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/experimental/JWT.java deleted file mode 100644 index ab1e8d4..0000000 --- a/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/experimental/JWT.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.iotics.sdk.identity.experimental; - -import org.json.JSONObject; - -import java.text.SimpleDateFormat; -import java.util.Base64; -import java.util.Date; -import java.util.TimeZone; - -public class JWT { - private final String header; - private final String signature; - private final String payload; - private final String token; - - public JWT(String token) { - this.token = token; - String[] chunks = token.split("\\."); - Base64.Decoder decoder = Base64.getDecoder(); - - try { - this.header = new String(decoder.decode(chunks[0])); - this.payload = new String(decoder.decode(chunks[1])); - this.signature = chunks[2]; - } catch (ArrayIndexOutOfBoundsException e) { - throw new IllegalArgumentException("Invalid JWT token"); - } - } - - public String toNiceString() { - JSONObject h = new JSONObject(this.header); - - try { - JSONObject p = new JSONObject(this.payload); - long exp = p.getLong("exp"); - long iat = p.getLong("iat"); - - SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss"); - simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - - String sExp = simpleDateFormat.format(new Date(exp * 1000)); - String sIat = simpleDateFormat.format(new Date(iat * 1000)); - p.put("exp", sExp).put("iat", sIat); - - JSONObject obj = new JSONObject(); - obj.put("header", h).put("payload", p).put("signature", this.signature); - return obj.toString(2); - } catch (Exception e) { - throw new RuntimeException("Invalid token", e); - } - } - - @Override - public String toString() { - return "JWT{" + - "header='" + header + '\'' + - ", payload='" + payload + '\'' + - ", signature='" + signature + '\'' + - '}'; - } -} diff --git a/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/experimental/ResolverClient.java b/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/experimental/ResolverClient.java deleted file mode 100644 index da8de17..0000000 --- a/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/experimental/ResolverClient.java +++ /dev/null @@ -1,118 +0,0 @@ -package com.iotics.sdk.identity.experimental; - -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.Response; -import okhttp3.ResponseBody; -import org.json.JSONObject; - -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URI; -import java.net.URL; -import java.util.Base64; -import java.util.Objects; - -public final class ResolverClient { - private final URL base; - private final OkHttpClient client; - - public ResolverClient(URL base) { - this.base = base; - this.client = new OkHttpClient(); - } - - public final class Result { - private final String content; - private final String contentType; - private final boolean isErr; - - public Result(String content, String contentType, boolean isErr) { - this.content = content; - this.contentType = contentType; - this.isErr = isErr; - } - - @Override - public String toString() { - return "Result{" + - "content='" + content + '\'' + - ", contentType='" + contentType + '\'' + - ", isErr=" + isErr + - '}'; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Result result = (Result) o; - return isErr == result.isErr && Objects.equals(content, result.content) && Objects.equals(contentType, result.contentType); - } - - @Override - public int hashCode() { - return Objects.hash(content, contentType, isErr); - } - - public String content() { - return content; - } - - public String contentType() { - return contentType; - } - - public boolean isErr() { - return isErr; - } - } - - public Result discover(String did) throws IOException { - URL url = null; - try { - url = new URL(base, "/1.0/discover/" + did); - } catch (MalformedURLException e) { - throw new IllegalArgumentException("invalid did"); - } - Request request = new Request.Builder() - .url(url) - .get() - .build(); - try (Response response = client.newCall(request).execute()) { - ResponseBody body = response.body(); - if(response.code() > 299) { - if(response.code() == 404) { - return new Result("DID not found", "application/text", true); - } - if(body != null) { - return new Result(body.string(), "application/xml", true); - } - else { - return new Result("No result found", "application/text", true); - } - } - if(body == null) { - return new Result("invalid response", "application/text", true); - } - JSONObject obj = new JSONObject(body.string()); - String token = obj.getString("token"); - Base64.Decoder decoder = Base64.getDecoder(); - String payload = new String(decoder.decode(token.split("\\.")[1])); - obj = new JSONObject(payload); - return new Result(obj.toString(3), "application/json", false); - } - - } - - public static void main(String[] args) throws Exception { - ResolverClient c = new ResolverClient(URI.create("https://did.stg.iotics.com").toURL()); - Result agent = c.discover("did:iotics:iotJxn2AHBkaFXKkBymbFYcVokGhLShLtUf1"); - Result user = c.discover("did:iotics:iotLUmwHDFtpfLEWTeGAQwyp4Y5FoSTt4jbg"); - - System.out.println("AGENT ------"); - System.out.println(agent); - System.out.println("USER ------"); - System.out.println(user); - } -} diff --git a/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/go/StringResult.java b/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/go/StringResult.java deleted file mode 100644 index 8f57730..0000000 --- a/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/go/StringResult.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.iotics.sdk.identity.go; - -import com.sun.jna.Pointer; -import com.sun.jna.Structure; - -import java.util.Arrays; -import java.util.List; - -public class StringResult extends Structure implements Structure.ByValue { - // need to be public for jra to work - public String value; - public String err; - - // needed for jra to instantiate this class - public StringResult() { - } - - public StringResult(String value, String err) { - this.value = value; - this.err = err; - } - - protected List getFieldOrder() { - return Arrays.asList("value", "err"); - } - - @Override - public String toString() { - return "StringResult{" + - "value='" + value + '\'' + - ", r1='" + err + '\'' + - '}'; - } -} \ No newline at end of file diff --git a/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/jna/JnaSdkApiInitialiser.java b/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/jna/JnaSdkApiInitialiser.java deleted file mode 100644 index 85eb66e..0000000 --- a/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/jna/JnaSdkApiInitialiser.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.iotics.sdk.identity.jna; - -import com.sun.jna.Native; - -import java.io.File; - -public class JnaSdkApiInitialiser { - private static String LIB_PATH = new File("./lib/lib-iotics-id-sdk.so").getAbsolutePath(); - private final String libPath; - - private final SdkApi idProxy; - - public JnaSdkApiInitialiser() { - this(LIB_PATH); - } - - public JnaSdkApiInitialiser(String libPath) { - this.libPath = libPath; - this.idProxy = Native.loadLibrary(this.libPath, SdkApi.class); - } - - // not thread safe - public final SdkApi get() { - return idProxy; - } - -} diff --git a/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/jna/SdkApi.java b/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/jna/SdkApi.java deleted file mode 100644 index 1c068b7..0000000 --- a/ffi/java-idsdk/src/main/java/com/iotics/sdk/identity/jna/SdkApi.java +++ /dev/null @@ -1,94 +0,0 @@ -package com.iotics.sdk.identity.jna; - -import com.iotics.sdk.identity.go.StringResult; -import com.sun.jna.Library; - -public interface SdkApi extends Library { - - StringResult CreateDefaultSeed(); - - StringResult MnemonicBip39ToSeed(String mnemonics); - - StringResult SeedBip39ToMnemonic(String seed); - - StringResult RecreateAgentIdentity( - String resolverAddress, - String keyName, - String name, - String seed); - - StringResult RecreateUserIdentity( - String resolverAddress, - String keyName, - String name, - String seed); - - StringResult CreateAgentIdentity( - String resolverAddress, - String keyName, - String name, - String seed); - - StringResult CreateUserIdentity( - String resolverAddress, - String keyName, - String name, - String seed); - - StringResult CreateTwinDidWithControlDelegation( - String resolverAddress, - String agentDid, - String agentKeyName, - String agentName, - String agentSeed, - String twinKeyName, - String twinName); - - String UserDelegatesAuthenticationToAgent( - String resolverAddress, - - String agentDid, - String agentKeyName, - String agentName, - String agentSeed, - - String userDid, - String userKeyName, - String userName, - String userSeed, - - String delegationName); - - StringResult IsAllowedFor( - String resolverAddress, - String token); - - String TwinDelegatesControlToAgent( - String resolverAddress, - - String agentDid, - String agentKeyName, - String agentName, - String agentSeed, - - String twinDid, - String twinKeyName, - String twinName, - String twinSeed, - - String delegationName); - - StringResult CreateAgentAuthToken( - String agentDid, - String agentKeyName, - String agentName, - String agentSeed, - - String userDid, - - String audience, - - long durationInSeconds); - - -} \ No newline at end of file diff --git a/ffi/java-idsdk/src/test/java/com/iotics/sdk/identity/App.java b/ffi/java-idsdk/src/test/java/com/iotics/sdk/identity/App.java deleted file mode 100644 index 4941317..0000000 --- a/ffi/java-idsdk/src/test/java/com/iotics/sdk/identity/App.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.iotics.sdk.identity; - -import com.iotics.sdk.identity.experimental.JWT; -import com.iotics.sdk.identity.jna.JnaSdkApiInitialiser; -import com.iotics.sdk.identity.jna.SdkApi; - -import java.nio.file.Paths; -import java.time.Duration; - -public class App { - static String resolver = "https://did.stg.iotics.com"; - static String seed = "f25a09c9d21ad5f7535fac4c30afe1a9f2ca025a192db549044b1b0130d1e945"; - static String uDiD = "did:iotics:iotEBuXp2wHMREZmwYAyPhFzPYfWtt9Ka2R2"; - static Identity agentIdentity = new Identity("aKey1", "#app1", "did:iotics:iotJxn2AHBkaFXKkBymbFYcVokGhLShLtUf1"); - - public static void main(String[] args) { - String os = System.getProperty("os.name").toLowerCase(); - String libPath = Paths.get(os.contains("win") ? "../lib/lib-iotics-id-sdk.dll" : "../lib/lib-iotics-id-sdk.so") - .toAbsolutePath() - .toString(); - SdkApi api = new JnaSdkApiInitialiser(libPath).get(); - token(api); - // delegation(api); - } - - public static void delegation(SdkApi api) { - SimpleIdentity idSdk = new SimpleIdentity(api, resolver, seed); - - Identity userIdentity = idSdk.CreateUserIdentity("uKey1", "#user1"); - System.out.println("CreateUserIdentity: " + userIdentity); - System.out.println("Agent identity: " + agentIdentity); - idSdk.UserDelegatesAuthenticationToAgent(agentIdentity, userIdentity, "delegation1"); - - Identity twinIdentity = idSdk.CreateTwinIdentityWithControlDelegation(agentIdentity, "tKey1", "#tName"); - System.out.println("CreateTwinDidWithControlDelegation: " + twinIdentity); - - Identity anotherAgentIdentity = idSdk.CreateAgentIdentity("aKey1", "#app2"); - System.out.println("CreateAgentIdentity: " + anotherAgentIdentity); - - idSdk.TwinDelegatesControlToAgent(anotherAgentIdentity, twinIdentity, "delegation2"); - } - - public static void token(SdkApi api) { - SimpleIdentity idSdk = new SimpleIdentity(api, resolver, seed); - String token = idSdk.CreateAgentAuthToken(agentIdentity, uDiD, Duration.ofSeconds(11)); - System.out.println("CreateAgentAuthToken: " + new JWT(token).toNiceString()); - token = idSdk.CreateAgentAuthToken(agentIdentity, uDiD, "random", Duration.ofSeconds(9)); - System.out.println("CreateAgentAuthToken: " + new JWT(token).toNiceString()); - } - - public static void seeds(SdkApi api) { - - Seeds seeds = new Seeds(api); - - String res; - - res = seeds.CreateDefaultSeed(); - System.out.println("CreateDefaultSeed: " + res); - - res = seeds.SeedBip39ToMnemonic(res); - System.out.println("SeedBip39ToMnemonic: " + res); - - res = seeds.MnemonicBip39ToSeed(res); - System.out.println("MnemonicBip39ToSeed: " + res); - - SimpleIdentity idSdk = new SimpleIdentity(api, resolver); - - Identity agentIdentity = idSdk.CreateAgentIdentity("aKey1", "#app1"); - System.out.println("CreateAgentIdentity: " + agentIdentity); - - Identity userIdentity = idSdk.CreateUserIdentity("uKey1", "#user1"); - System.out.println("CreateUserIdentity: " + userIdentity); - - Identity twinIdentity = idSdk.CreateTwinIdentityWithControlDelegation(agentIdentity, "tKey1", "#tName"); - System.out.println("CreateTwinDidWithControlDelegation: " + twinIdentity); - - } - -} diff --git a/ffi/java-idsdk/src/test/java/com/iotics/sdk/identity/DataFactory.java b/ffi/java-idsdk/src/test/java/com/iotics/sdk/identity/DataFactory.java deleted file mode 100644 index 3d2ec37..0000000 --- a/ffi/java-idsdk/src/test/java/com/iotics/sdk/identity/DataFactory.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.iotics.sdk.identity; - -import com.iotics.sdk.identity.go.StringResult; - -public final class DataFactory { - public static String validUrl() { - return "http://localhost:2020"; - } - - public static StringResult validResult(String value) { - return new StringResult(value, null); - } - - public static StringResult errorResult(String err) { - return new StringResult(null, err); - } - - public static Identity aValidAgentIdentity() { - return new Identity("aKeyName", "aName", "did:iotics:123"); - } - - public static Identity aValidUserIdentity() { - return new Identity("uKeyName", "uName", "did:iotics:abc"); - } - -} diff --git a/ffi/java-idsdk/src/test/java/com/iotics/sdk/identity/SeedsTest.java b/ffi/java-idsdk/src/test/java/com/iotics/sdk/identity/SeedsTest.java deleted file mode 100644 index 3031269..0000000 --- a/ffi/java-idsdk/src/test/java/com/iotics/sdk/identity/SeedsTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.iotics.sdk.identity; - -import com.iotics.sdk.identity.jna.SdkApi; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -import static com.iotics.sdk.identity.DataFactory.validResult; -import static org.mockito.Mockito.*; - -@ExtendWith(MockitoExtension.class) -public class SeedsTest { - @Mock - SdkApi sdkApi; - - @Test - void whenCreatingDefaultSeed_thenDelegatesToApi() { - when(sdkApi.CreateDefaultSeed()).thenReturn(validResult("some seed")); - new Seeds(sdkApi).CreateDefaultSeed(); - verify(sdkApi).CreateDefaultSeed(); - } - - @Test - void whenMnemonicBip39ToSeed_thenDelegatesToApi() { - when(sdkApi.MnemonicBip39ToSeed(any())).thenReturn(validResult("some seed")); - new Seeds(sdkApi).MnemonicBip39ToSeed ("1 2 3"); - verify(sdkApi).MnemonicBip39ToSeed("1 2 3"); - } - - @Test - void whenSeedBip39ToMnemonic_thenDelegatesToApi() { - when(sdkApi.SeedBip39ToMnemonic(any())).thenReturn(validResult("1 2 3")); - new Seeds(sdkApi).SeedBip39ToMnemonic("some seed"); - verify(sdkApi).SeedBip39ToMnemonic("some seed"); - } - -} diff --git a/ffi/java-idsdk/src/test/java/com/iotics/sdk/identity/SimpleIdentityTest.java b/ffi/java-idsdk/src/test/java/com/iotics/sdk/identity/SimpleIdentityTest.java deleted file mode 100644 index 8f7953b..0000000 --- a/ffi/java-idsdk/src/test/java/com/iotics/sdk/identity/SimpleIdentityTest.java +++ /dev/null @@ -1,221 +0,0 @@ -package com.iotics.sdk.identity; - -import com.iotics.sdk.identity.jna.SdkApi; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -import java.time.Duration; - -import static com.iotics.sdk.identity.DataFactory.*; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.Mockito.*; - -@ExtendWith(MockitoExtension.class) -public class SimpleIdentityTest { - - @Mock - SdkApi sdkApi; - - @Test - void validApiConstruction() { - assertThrows(NullPointerException.class, () -> { - new SimpleIdentity(null, ""); - }); - assertThrows(IllegalArgumentException.class, () -> { - new SimpleIdentity(sdkApi, "invalid url", "some seed"); - }); - } - - @Test - void whenConstructedWithoutSeed_thenGeneratesNewOne() { - when(sdkApi.CreateDefaultSeed()).thenReturn(validResult("some seed")); - new SimpleIdentity(sdkApi, validUrl()); - - verify(sdkApi).CreateDefaultSeed(); - } - - @Test - void whenConstructedWithOneSeed_thenUsesItForBothAgentAndUser() { - SimpleIdentity si = new SimpleIdentity(sdkApi, validUrl(), "some seed"); - - assertEquals("some seed", si.getAgentSeed()); - assertEquals("some seed", si.getUserSeed()); - - verifyNoInteractions(sdkApi); - } - - @Test - void whenConstructedWithTwoSeed_thenUsesOneForUserAndOneForAgent() { - SimpleIdentity si = new SimpleIdentity(sdkApi, validUrl(), "user seed", "agent seed"); - - assertEquals("agent seed", si.getAgentSeed()); - assertEquals("user seed", si.getUserSeed()); - - verifyNoInteractions(sdkApi); - } - - @Test - void whenCreateTwinDidWithControlDelegation_thenMapsParametersAndDelegatesToApi() { - String res = validUrl(); - - SimpleIdentity si = new SimpleIdentity(sdkApi, res, "some seed"); - Identity id = aValidAgentIdentity(); - when(sdkApi.CreateTwinDidWithControlDelegation(any(), any(), any(), any(), any(), any(), any())).thenReturn(validResult("twin did")); - - Identity twinId = si.CreateTwinIdentityWithControlDelegation(id, "twinKeyName", "twinName"); - - assertEquals(twinId.did(), "twin did"); - assertEquals(twinId.keyName(), "twinKeyName"); - assertEquals(twinId.name(), "twinName"); - verify(sdkApi).CreateTwinDidWithControlDelegation(res, id.did(), id.keyName(), id.name(), si.getAgentSeed(), "twinKeyName", "twinName"); - } - - @Test - void whenRecreateAgentIdentity_thenMapsParametersAndDelegatesToApi() { - String res = validUrl(); - - SimpleIdentity si = new SimpleIdentity(sdkApi, res, "some seed"); - when(sdkApi.RecreateAgentIdentity(any(), any(), any(), any())).thenReturn(validResult("agent did")); - - Identity agentIdentity = si.RecreateAgentIdentity("agentKeyName", "agentName"); - - assertEquals(agentIdentity.did(), "agent did"); - assertEquals(agentIdentity.keyName(), "agentKeyName"); - assertEquals(agentIdentity.name(), "agentName"); - verify(sdkApi).RecreateAgentIdentity(res, "agentKeyName", "agentName", "some seed"); - } - - @Test - void whenCreateAgentIdentity_thenMapsParametersAndDelegatesToApi() { - String res = validUrl(); - - SimpleIdentity si = new SimpleIdentity(sdkApi, res, "some seed"); - when(sdkApi.CreateAgentIdentity(any(), any(), any(), any())).thenReturn(validResult("agent did")); - - Identity agentIdentity = si.CreateAgentIdentity("agentKeyName", "agentName"); - - assertEquals(agentIdentity.did(), "agent did"); - assertEquals(agentIdentity.keyName(), "agentKeyName"); - assertEquals(agentIdentity.name(), "agentName"); - verify(sdkApi).CreateAgentIdentity(res, "agentKeyName", "agentName", "some seed"); - } - - @Test - void whenIsAllowedFor_thenDelegatesToApi() { - String res = validUrl(); - - SimpleIdentity si = new SimpleIdentity(sdkApi, res, "some seed"); - when(sdkApi.IsAllowedFor(any(), any())).thenReturn(validResult("true")); - - String allowed = si.IsAllowedFor("resolver", "token"); - - assertEquals(allowed, "true"); - verify(sdkApi).IsAllowedFor("resolver", "token"); - } - - @Test - void whenRecreateUserIdentity_thenMapsParametersAndDelegatesToApi() { - String res = validUrl(); - - SimpleIdentity si = new SimpleIdentity(sdkApi, res, "some seed"); - when(sdkApi.RecreateUserIdentity(any(), any(), any(), any())).thenReturn(validResult("user did")); - - Identity userIdentity = si.RecreateUserIdentity("userKeyName", "userName"); - - assertEquals(userIdentity.did(), "user did"); - assertEquals(userIdentity.keyName(), "userKeyName"); - assertEquals(userIdentity.name(), "userName"); - verify(sdkApi).RecreateUserIdentity(res, "userKeyName", "userName", "some seed"); - } - - @Test - void whenCreateUserIdentity_thenMapsParametersAndDelegatesToApi() { - String res = validUrl(); - - SimpleIdentity si = new SimpleIdentity(sdkApi, res, "some seed"); - when(sdkApi.CreateUserIdentity(any(), any(), any(), any())).thenReturn(validResult("user did")); - - Identity userIdentity = si.CreateUserIdentity("userKeyName", "userName"); - - assertEquals(userIdentity.did(), "user did"); - assertEquals(userIdentity.keyName(), "userKeyName"); - assertEquals(userIdentity.name(), "userName"); - verify(sdkApi).CreateUserIdentity(res, "userKeyName", "userName", "some seed"); - } - - @Test - void whenCreateUserIdentityFails_thenThrows() { - SimpleIdentity si = new SimpleIdentity(sdkApi, validUrl(), "some seed"); - when(sdkApi.CreateUserIdentity(any(), any(), any(), any())).thenReturn(errorResult("some error")); - - assertThrows(SimpleIdentityException.class, () -> { - si.CreateUserIdentity("userKeyName", "userName"); - }); - } - - @Test - void whenCreateAgentAuthToken_thenMapsParametersAndDelegatesToApi() { - String res = validUrl(); - - SimpleIdentity si = new SimpleIdentity(sdkApi, res, "some seed"); - when(sdkApi.CreateAgentAuthToken(any(), any(), any(), any(), any(), any(), anyLong())).thenReturn(validResult("some token")); - - Identity i = aValidAgentIdentity(); - String token = si.CreateAgentAuthToken(i, "did:iotics:user", "aud", Duration.ofSeconds(123)); - - assertEquals(token, "some token"); - verify(sdkApi).CreateAgentAuthToken(i.did(), i.keyName(), i.name(), si.getAgentSeed(), "did:iotics:user", "aud", Integer.valueOf(123)); - } - - @Test - void whenCreateAgentAuthToken_thenMapsParametersAndDelegatesToApiWithDefaultAudience() { - String res = validUrl(); - - SimpleIdentity si = new SimpleIdentity(sdkApi, res, "some seed"); - when(sdkApi.CreateAgentAuthToken(any(), any(), any(), any(), any(), any(), anyLong())).thenReturn(validResult("some token")); - - Identity i = aValidAgentIdentity(); - String token = si.CreateAgentAuthToken(i, "did:iotics:user", Duration.ofSeconds(123)); - - assertEquals(token, "some token"); - verify(sdkApi).CreateAgentAuthToken(i.did(), i.keyName(), i.name(), si.getAgentSeed(), "did:iotics:user", res, Integer.valueOf(123)); - } - - @Test - void whenUserDelegatesAuthenticationToAgent_thenMapsParametersAndDelegatesToApi() { - String res = validUrl(); - String as = "agentSeed"; - String us = "userSeed"; - SimpleIdentity si = new SimpleIdentity(sdkApi, res, us, as); - - Identity i = aValidAgentIdentity(); - Identity u = aValidUserIdentity(); - si.UserDelegatesAuthenticationToAgent(i, u, "#foobar"); - - verify(sdkApi).UserDelegatesAuthenticationToAgent(res, - i.did(), i.keyName(), i.name(), si.getAgentSeed(), - u.did(), u.keyName(), u.name(), si.getUserSeed(), - "#foobar"); - } - - @Test - void whenTwinDelegatesControlToAgent_thenMapsParametersAndDelegatesToApi() { - String res = validUrl(); - String as = "agentSeed"; - String us = "userSeed"; - SimpleIdentity si = new SimpleIdentity(sdkApi, res, us, as); - - Identity i = aValidAgentIdentity(); - Identity u = aValidUserIdentity(); - si.TwinDelegatesControlToAgent(i, u, "#foobar"); - - verify(sdkApi).TwinDelegatesControlToAgent(res, - i.did(), i.keyName(), i.name(), si.getAgentSeed(), - u.did(), u.keyName(), u.name(), si.getAgentSeed(), - "#foobar"); - } - -}