From 7cb23fec0e7a9e97044886fd7a701645df17805a Mon Sep 17 00:00:00 2001 From: Gaurav Goel Date: Fri, 20 Sep 2024 13:31:50 +0530 Subject: [PATCH] feat: update isSessionIdExists method and modified tests. Signed-off-by: Gaurav Goel --- .../com/web3auth/sfaexample/MainActivity.kt | 23 ++++--- singlefactorauth/build.gradle | 2 +- .../com/web3auth/singlefactorauth/AquaTest.kt | 66 +++++++++++++++++++ .../singlefactorauth/SapphireMainnetTest.kt | 66 +++++++++++++++++++ .../singlefactorauth/SingleFactorAuth.kt | 27 +++++++- 5 files changed, 171 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/web3auth/sfaexample/MainActivity.kt b/app/src/main/java/com/web3auth/sfaexample/MainActivity.kt index 7fe5930..f529ecd 100644 --- a/app/src/main/java/com/web3auth/sfaexample/MainActivity.kt +++ b/app/src/main/java/com/web3auth/sfaexample/MainActivity.kt @@ -35,16 +35,21 @@ class MainActivity : AppCompatActivity() { singleFactorAuth = SingleFactorAuth(sfaParams, this) loginParams = LoginParams(TEST_VERIFIER, TORUS_TEST_EMAIL, idToken) - if (singleFactorAuth.isSessionIdExists()) { - val sfakey = singleFactorAuth.initialize(this.applicationContext) - sfakey.whenComplete { response, error -> - if (error == null) { - val text = - "Public Address: ${response.getPublicAddress()} , Private Key: ${response.getPrivateKey()}" - tv.text = text - } else { - tv.text = error.message + val res = singleFactorAuth.isSessionIdExists(this) + res.whenComplete { res, err -> + if (err == null) { + val sfakey = singleFactorAuth.initialize(this.applicationContext) + sfakey.whenComplete { response, error -> + if (error == null) { + val text = + "Public Address: ${response.getPublicAddress()} , Private Key: ${response.getPrivateKey()}" + tv.text = text + } else { + tv.text = error.message + } } + } else { + tv.text = err.message } } } diff --git a/singlefactorauth/build.gradle b/singlefactorauth/build.gradle index 6acf2a0..0c75d2c 100644 --- a/singlefactorauth/build.gradle +++ b/singlefactorauth/build.gradle @@ -44,7 +44,7 @@ dependencies { implementation 'com.google.code.gson:gson:2.10.1' implementation 'org.slf4j:slf4j-simple:2.0.3' //session-manager-sdk - implementation 'com.github.Web3Auth:session-manager-android:2.4.1' + implementation 'com.github.grvgoel81:session-manager-android:7.1.0' testImplementation 'junit:junit:4.13.2' testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2' diff --git a/singlefactorauth/src/androidTest/java/com/web3auth/singlefactorauth/AquaTest.kt b/singlefactorauth/src/androidTest/java/com/web3auth/singlefactorauth/AquaTest.kt index 791cd5c..66a395f 100644 --- a/singlefactorauth/src/androidTest/java/com/web3auth/singlefactorauth/AquaTest.kt +++ b/singlefactorauth/src/androidTest/java/com/web3auth/singlefactorauth/AquaTest.kt @@ -9,6 +9,7 @@ import com.web3auth.singlefactorauth.types.TorusSubVerifierInfo import com.web3auth.singlefactorauth.utils.JwtUtils.generateIdToken import com.web3auth.singlefactorauth.utils.PemUtils.readPrivateKeyFromReader import com.web3auth.singlefactorauth.utils.WellKnownSecret +import junit.framework.TestCase.assertEquals import junit.framework.TestCase.fail import org.junit.Test import org.junit.runner.RunWith @@ -99,4 +100,69 @@ class AquaTest { fail() } } + + @Test + @Throws(ExecutionException::class, InterruptedException::class) + fun testisSessionIdExistsWithLogoutApiCalled() { + val context = getInstrumentation().context + sfaParams = SFAParams(Web3AuthNetwork.AQUA, "YOUR_CLIENT_ID", 86400, null, 0) + singleFactorAuth = SingleFactorAuth(sfaParams, context) + val privateKey = readPrivateKeyFromReader( + WellKnownSecret.pem(), + "EC" + ) as ECPrivateKey + val publicKey = KeyFactory.getInstance("EC").generatePublic( + ECPublicKeySpec( + privateKey.params.generator, + privateKey.params + ) + ) as ECPublicKey + algorithmRs = Algorithm.ECDSA256(publicKey, privateKey) + val idToken: String = generateIdToken(TORUS_TEST_EMAIL, algorithmRs) + loginParams = LoginParams(TEST_VERIFIER, TORUS_TEST_EMAIL, idToken) + singleFactorAuth.connect(loginParams, context) + val logoutCF = singleFactorAuth.logout(context).get() + if (logoutCF == true) { + val res = singleFactorAuth.isSessionIdExists(context) + res.whenComplete { res, err -> + if (err != null) { + fail() + } else { + assertEquals(res, false) + } + } + } else { + fail() + } + } + + @Test + @Throws(ExecutionException::class, InterruptedException::class) + fun testisSessionIdExistsWithLogoutApiNotCalled() { + val context = getInstrumentation().context + sfaParams = SFAParams(Web3AuthNetwork.AQUA, "YOUR_CLIENT_ID", 86400, null, 0) + singleFactorAuth = SingleFactorAuth(sfaParams, context) + val privateKey = readPrivateKeyFromReader( + WellKnownSecret.pem(), + "EC" + ) as ECPrivateKey + val publicKey = KeyFactory.getInstance("EC").generatePublic( + ECPublicKeySpec( + privateKey.params.generator, + privateKey.params + ) + ) as ECPublicKey + algorithmRs = Algorithm.ECDSA256(publicKey, privateKey) + val idToken: String = generateIdToken(TORUS_TEST_EMAIL, algorithmRs) + loginParams = LoginParams(TEST_VERIFIER, TORUS_TEST_EMAIL, idToken) + singleFactorAuth.connect(loginParams, context) + val res = singleFactorAuth.isSessionIdExists(context) + res.whenComplete { res, err -> + if (err != null) { + fail() + } else { + assertEquals(res, true) + } + } + } } \ No newline at end of file diff --git a/singlefactorauth/src/androidTest/java/com/web3auth/singlefactorauth/SapphireMainnetTest.kt b/singlefactorauth/src/androidTest/java/com/web3auth/singlefactorauth/SapphireMainnetTest.kt index 355b62e..0c338e8 100644 --- a/singlefactorauth/src/androidTest/java/com/web3auth/singlefactorauth/SapphireMainnetTest.kt +++ b/singlefactorauth/src/androidTest/java/com/web3auth/singlefactorauth/SapphireMainnetTest.kt @@ -8,6 +8,7 @@ import com.web3auth.singlefactorauth.types.TorusSubVerifierInfo import com.web3auth.singlefactorauth.utils.JwtUtils.generateIdToken import com.web3auth.singlefactorauth.utils.PemUtils.readPrivateKeyFromReader import com.web3auth.singlefactorauth.utils.WellKnownSecret +import junit.framework.TestCase import junit.framework.TestCase.fail import org.junit.Test import org.torusresearch.fetchnodedetails.types.Web3AuthNetwork @@ -104,4 +105,69 @@ class SapphireMainnetTest { fail() } } + + @Test + @Throws(ExecutionException::class, InterruptedException::class) + fun testisSessionIdExistsWithLogoutApiCalled() { + val context = InstrumentationRegistry.getInstrumentation().context + sfaParams = SFAParams(Web3AuthNetwork.SAPPHIRE_MAINNET, "YOUR_CLIENT_ID", 86400, null, 0) + singleFactorAuth = SingleFactorAuth(sfaParams, context) + val privateKey = readPrivateKeyFromReader( + WellKnownSecret.pem(), + "EC" + ) as ECPrivateKey + val publicKey = KeyFactory.getInstance("EC").generatePublic( + ECPublicKeySpec( + privateKey.params.generator, + privateKey.params + ) + ) as ECPublicKey + algorithmRs = Algorithm.ECDSA256(publicKey, privateKey) + val idToken: String = generateIdToken(TORUS_TEST_EMAIL, algorithmRs) + loginParams = LoginParams(TEST_VERIFIER, TORUS_TEST_EMAIL, idToken) + singleFactorAuth.connect(loginParams, context) + val logoutCF = singleFactorAuth.logout(context).get() + if (logoutCF == true) { + val res = singleFactorAuth.isSessionIdExists(context) + res.whenComplete { res, err -> + if (err != null) { + fail() + } else { + TestCase.assertEquals(res, false) + } + } + } else { + fail() + } + } + + @Test + @Throws(ExecutionException::class, InterruptedException::class) + fun testisSessionIdExistsWithLogoutApiNotCalled() { + val context = InstrumentationRegistry.getInstrumentation().context + sfaParams = SFAParams(Web3AuthNetwork.SAPPHIRE_MAINNET, "YOUR_CLIENT_ID", 86400, null, 0) + singleFactorAuth = SingleFactorAuth(sfaParams, context) + val privateKey = readPrivateKeyFromReader( + WellKnownSecret.pem(), + "EC" + ) as ECPrivateKey + val publicKey = KeyFactory.getInstance("EC").generatePublic( + ECPublicKeySpec( + privateKey.params.generator, + privateKey.params + ) + ) as ECPublicKey + algorithmRs = Algorithm.ECDSA256(publicKey, privateKey) + val idToken: String = generateIdToken(TORUS_TEST_EMAIL, algorithmRs) + loginParams = LoginParams(TEST_VERIFIER, TORUS_TEST_EMAIL, idToken) + singleFactorAuth.connect(loginParams, context) + val res = singleFactorAuth.isSessionIdExists(context) + res.whenComplete { res, err -> + if (err != null) { + fail() + } else { + TestCase.assertEquals(res, true) + } + } + } } \ No newline at end of file diff --git a/singlefactorauth/src/main/java/com/web3auth/singlefactorauth/SingleFactorAuth.kt b/singlefactorauth/src/main/java/com/web3auth/singlefactorauth/SingleFactorAuth.kt index 8df1602..893c67b 100644 --- a/singlefactorauth/src/main/java/com/web3auth/singlefactorauth/SingleFactorAuth.kt +++ b/singlefactorauth/src/main/java/com/web3auth/singlefactorauth/SingleFactorAuth.kt @@ -70,8 +70,17 @@ class SingleFactorAuth( return nodeDetails.torusNodeEndpoints } - fun isSessionIdExists(): Boolean { - return sessionManager.getSessionId().isNotEmpty() + fun isSessionIdExists(ctx: Context): CompletableFuture { + val authorizeCF = CompletableFuture() + val data = sessionManager.authorizeSession(ctx.packageName, ctx) + data.whenComplete { response, error -> + if (error != null) { + authorizeCF.complete(false) + } else { + authorizeCF.complete(true) + } + } + return authorizeCF } fun getTorusKey( @@ -138,7 +147,19 @@ class SingleFactorAuth( json.put("publicAddress", torusSFAKey.getPublicAddress()) } - sessionManager.createSession(json.toString(), ctx).get() + sessionManager.createSession(json.toString(), ctx, true).get() return torusSFAKey } + + fun logout(context: Context): CompletableFuture { + val logoutCF = CompletableFuture() + sessionManager.invalidateSession(context).whenComplete { res, err -> + if (err != null) { + logoutCF.completeExceptionally(err) + } else { + logoutCF.complete(res) + } + } + return logoutCF + } } \ No newline at end of file