Skip to content

Commit

Permalink
feat: update isSessionIdExists method and modified tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Gaurav Goel <[email protected]>
  • Loading branch information
grvgoel81 authored and Gaurav Goel committed Sep 20, 2024
1 parent 16d4b5a commit 7cb23fe
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 13 deletions.
23 changes: 14 additions & 9 deletions app/src/main/java/com/web3auth/sfaexample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion singlefactorauth/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,17 @@ class SingleFactorAuth(
return nodeDetails.torusNodeEndpoints
}

fun isSessionIdExists(): Boolean {
return sessionManager.getSessionId().isNotEmpty()
fun isSessionIdExists(ctx: Context): CompletableFuture<Boolean> {
val authorizeCF = CompletableFuture<Boolean>()
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(
Expand Down Expand Up @@ -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<Boolean> {
val logoutCF = CompletableFuture<Boolean>()
sessionManager.invalidateSession(context).whenComplete { res, err ->
if (err != null) {
logoutCF.completeExceptionally(err)
} else {
logoutCF.complete(res)
}
}
return logoutCF
}
}

0 comments on commit 7cb23fe

Please sign in to comment.