From 0fb00726de07376f09c72c8c883149d1956301f1 Mon Sep 17 00:00:00 2001 From: Gaurav Goel Date: Fri, 20 Sep 2024 09:34:33 +0530 Subject: [PATCH 01/13] Added saveSessionFlag in createSession API. Signed-off-by: Gaurav Goel --- .../com/web3auth/session_manager_android/SessionManager.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt b/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt index 9beb408..fc36a97 100644 --- a/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt +++ b/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt @@ -209,6 +209,7 @@ class SessionManager(context: Context, sessionTime: Int = 86400, allowedOrigin: fun createSession( data: String, context: Context, + saveSession: Boolean ): CompletableFuture { return CompletableFuture.supplyAsync { val newSessionKey = generateRandomSessionKey() @@ -254,7 +255,7 @@ class SessionManager(context: Context, sessionTime: Int = 86400, allowedOrigin: } } - if (result.isSuccessful) { + if (result.isSuccessful && saveSession) { KeyStoreManager.savePreferenceData( KeyStoreManager.SESSION_ID_TAG, newSessionKey ) From aecceb0e070497c95256f9c313029857f71d9826 Mon Sep 17 00:00:00 2001 From: Gaurav Goel Date: Fri, 20 Sep 2024 09:43:14 +0530 Subject: [PATCH 02/13] feat: update test cases and documentation Signed-off-by: Gaurav Goel --- .../SessionManagerTest.kt | 6 +-- .../session_manager_android/SessionManager.kt | 40 ++++++++++++++++++- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/session-manager-android/src/androidTest/java/com/web3auth/session_manager_android/SessionManagerTest.kt b/session-manager-android/src/androidTest/java/com/web3auth/session_manager_android/SessionManagerTest.kt index 0931776..cf368d2 100644 --- a/session-manager-android/src/androidTest/java/com/web3auth/session_manager_android/SessionManagerTest.kt +++ b/session-manager-android/src/androidTest/java/com/web3auth/session_manager_android/SessionManagerTest.kt @@ -29,7 +29,7 @@ class SessionManagerTest { json.put("publicAddress", "0x93475c78dv0jt80f2b6715a5c53838eC4aC96EF7") val sessionKey = sessionManager.createSession( json.toString(), - context + context, true ).get() assert(sessionKey != null) } @@ -47,7 +47,7 @@ class SessionManagerTest { json.put("publicAddress", "0x93475c78dv0jt80f2b6715a5c53838eC4aC96EF7") sessionManager.createSession( json.toString(), - context, + context, true ).get() sessionManager = SessionManager(context, 86400, "*") val authResponse = sessionManager.authorizeSession( @@ -72,7 +72,7 @@ class SessionManagerTest { json.put("publicAddress", "0x93475c78dv0jt80f2b6715a5c53838eC4aC96EF7") sessionManager.createSession( json.toString(), - context + context, true ).get() sessionManager = SessionManager(context, 86400, context.packageName) val invalidateRes = sessionManager.invalidateSession(context).get() diff --git a/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt b/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt index fc36a97..5c5e205 100644 --- a/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt +++ b/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt @@ -59,10 +59,21 @@ class SessionManager(context: Context, sessionTime: Int = 86400, allowedOrigin: return KeyStoreManager.getPreferencesData(KeyStoreManager.SESSION_ID_TAG).toString() } + /** - * Authorize User session in order to avoid re-login + * Authorizes a session for a given origin, performing any necessary authentication or token generation. + * This method operates asynchronously and returns a `CompletableFuture` that holds the result of the authorization. + * + * @param origin A string representing the origin or source of the session. This can be the app's package name or a specific domain. + * @param context The context in which the session authorization occurs. Typically used to access resources or perform operations within the application. + * + * @return A `CompletableFuture` that will contain the result of the session authorization. This will usually be a token or session ID upon successful authorization. + * + * Usage example: + * ``` + * authorizeSession("com.example.app", context) + * ``` */ - fun authorizeSession(origin: String, context: Context): CompletableFuture { return CompletableFuture.supplyAsync { if (!ApiHelper.isNetworkAvailable(context)) { @@ -140,6 +151,17 @@ class SessionManager(context: Context, sessionTime: Int = 86400, allowedOrigin: }.exceptionally { throw it } } + /** + * Invalidates the current session, effectively logging the user out or clearing session-related data. + * + * @param context The context in which the session invalidation occurs. Typically used to access resources + * or perform operations within the application (e.g., clearing shared preferences or cache). + * + * Usage example: + * ``` + * invalidateSession(context) + * ``` + */ fun invalidateSession( context: Context, ): CompletableFuture { @@ -206,6 +228,20 @@ class SessionManager(context: Context, sessionTime: Int = 86400, allowedOrigin: }.exceptionally { throw it } } + /** + * Creates a new session with the provided data. + * + * @param data The session data as a string. This can include information such as tokens or user-specific identifiers. + * @param context The context in which the session is being created. Typically used to access resources or perform operations within the application. + * @param saveSession A boolean flag that determines whether the session should be persisted. If `true`, the session will be saved for future access. + * + * @return This function can be extended to return a result, such as a success or failure message. + * + * Usage example: + * ``` + * createSession("sessionData", context, ) + * ``` + */ fun createSession( data: String, context: Context, From e398feca8be3b9e4b466ba8c699a6a39bc84fe4a Mon Sep 17 00:00:00 2001 From: Gaurav Goel Date: Fri, 20 Sep 2024 09:45:27 +0530 Subject: [PATCH 03/13] feat: update test cases and documentation Signed-off-by: Gaurav Goel --- .../java/com/web3auth/session_manager_android/SessionManager.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt b/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt index 5c5e205..42a6d89 100644 --- a/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt +++ b/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt @@ -234,6 +234,7 @@ class SessionManager(context: Context, sessionTime: Int = 86400, allowedOrigin: * @param data The session data as a string. This can include information such as tokens or user-specific identifiers. * @param context The context in which the session is being created. Typically used to access resources or perform operations within the application. * @param saveSession A boolean flag that determines whether the session should be persisted. If `true`, the session will be saved for future access. + * (Should be true for SFA sdk and false for PNP sdk). * * @return This function can be extended to return a result, such as a success or failure message. * From 2f18c7cea5bf1db0b7826cabd3fa436ac71bc4b3 Mon Sep 17 00:00:00 2001 From: Gaurav Goel Date: Fri, 20 Sep 2024 10:00:37 +0530 Subject: [PATCH 04/13] feat: update test cases and documentation Signed-off-by: Gaurav Goel --- .../web3auth/session_manager_android/SessionManager.kt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt b/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt index 42a6d89..125b8aa 100644 --- a/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt +++ b/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt @@ -292,10 +292,12 @@ class SessionManager(context: Context, sessionTime: Int = 86400, allowedOrigin: } } - if (result.isSuccessful && saveSession) { - KeyStoreManager.savePreferenceData( - KeyStoreManager.SESSION_ID_TAG, newSessionKey - ) + if (result.isSuccessful) { + if (saveSession) { + KeyStoreManager.savePreferenceData( + KeyStoreManager.SESSION_ID_TAG, newSessionKey + ) + } } else { throw Exception( SessionManagerError.getError( From e22f388b4a5c0f7f1a9faddff9b616979a4d3f29 Mon Sep 17 00:00:00 2001 From: Gaurav Goel Date: Fri, 20 Sep 2024 11:26:45 +0530 Subject: [PATCH 05/13] feat: update test cases Signed-off-by: Gaurav Goel --- .../com/web3auth/session_manager_android/SessionManagerTest.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/session-manager-android/src/androidTest/java/com/web3auth/session_manager_android/SessionManagerTest.kt b/session-manager-android/src/androidTest/java/com/web3auth/session_manager_android/SessionManagerTest.kt index cf368d2..7870120 100644 --- a/session-manager-android/src/androidTest/java/com/web3auth/session_manager_android/SessionManagerTest.kt +++ b/session-manager-android/src/androidTest/java/com/web3auth/session_manager_android/SessionManagerTest.kt @@ -77,6 +77,8 @@ class SessionManagerTest { sessionManager = SessionManager(context, 86400, context.packageName) val invalidateRes = sessionManager.invalidateSession(context).get() assertEquals(invalidateRes, true) + val res = sessionManager.getSessionId().isNotEmpty() + assertEquals(res, false) } @Test From eb88acb43f9fbb8c266d04cc764e71798f63933b Mon Sep 17 00:00:00 2001 From: Gaurav Goel Date: Mon, 23 Sep 2024 09:10:53 +0530 Subject: [PATCH 06/13] feat: handling errors rather than throw them. Signed-off-by: Gaurav Goel --- .../session_manager_android/SessionManager.kt | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt b/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt index 125b8aa..4a2ecb8 100644 --- a/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt +++ b/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt @@ -148,7 +148,12 @@ class SessionManager(context: Context, sessionTime: Int = 86400, allowedOrigin: Hex.decode(ecies.ephemPublicKey) ) String(share, Charsets.UTF_8) - }.exceptionally { throw it } + }.handle { result, exception -> + if (exception != null) { + return@handle "Error: ${exception.message}" + } + result + } } /** @@ -225,7 +230,14 @@ class SessionManager(context: Context, sessionTime: Int = 86400, allowedOrigin: ) } } - }.exceptionally { throw it } + }.handle { result, exception -> + if (exception != null) { + println("Error: ${exception.message}") + false + } else { + result + } + } } /** @@ -233,20 +245,17 @@ class SessionManager(context: Context, sessionTime: Int = 86400, allowedOrigin: * * @param data The session data as a string. This can include information such as tokens or user-specific identifiers. * @param context The context in which the session is being created. Typically used to access resources or perform operations within the application. - * @param saveSession A boolean flag that determines whether the session should be persisted. If `true`, the session will be saved for future access. - * (Should be true for SFA sdk and false for PNP sdk). * * @return This function can be extended to return a result, such as a success or failure message. * * Usage example: * ``` - * createSession("sessionData", context, ) + * createSession("sessionData", context) * ``` */ fun createSession( data: String, context: Context, - saveSession: Boolean ): CompletableFuture { return CompletableFuture.supplyAsync { val newSessionKey = generateRandomSessionKey() @@ -293,11 +302,7 @@ class SessionManager(context: Context, sessionTime: Int = 86400, allowedOrigin: } if (result.isSuccessful) { - if (saveSession) { - KeyStoreManager.savePreferenceData( - KeyStoreManager.SESSION_ID_TAG, newSessionKey - ) - } + // do nothing } else { throw Exception( SessionManagerError.getError( @@ -306,6 +311,11 @@ class SessionManager(context: Context, sessionTime: Int = 86400, allowedOrigin: ) } newSessionKey - }.exceptionally { throw it } + }.handle { result, exception -> + if (exception != null) { + return@handle "Error: ${exception.message}" + } + result + } } } \ No newline at end of file From 51eb14d614a85a23d39e4be7f28895ab6fe7447b Mon Sep 17 00:00:00 2001 From: Gaurav Goel Date: Mon, 23 Sep 2024 10:06:16 +0530 Subject: [PATCH 07/13] feat: update tests Signed-off-by: Gaurav Goel --- .../session_manager_android/SessionManagerTest.kt | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/session-manager-android/src/androidTest/java/com/web3auth/session_manager_android/SessionManagerTest.kt b/session-manager-android/src/androidTest/java/com/web3auth/session_manager_android/SessionManagerTest.kt index 7870120..1108f34 100644 --- a/session-manager-android/src/androidTest/java/com/web3auth/session_manager_android/SessionManagerTest.kt +++ b/session-manager-android/src/androidTest/java/com/web3auth/session_manager_android/SessionManagerTest.kt @@ -29,7 +29,7 @@ class SessionManagerTest { json.put("publicAddress", "0x93475c78dv0jt80f2b6715a5c53838eC4aC96EF7") val sessionKey = sessionManager.createSession( json.toString(), - context, true + context ).get() assert(sessionKey != null) } @@ -45,10 +45,11 @@ class SessionManagerTest { "91714924788458331086143283967892938475657483928374623640418082526960471979197446884" ) json.put("publicAddress", "0x93475c78dv0jt80f2b6715a5c53838eC4aC96EF7") - sessionManager.createSession( + val sessionId = sessionManager.createSession( json.toString(), - context, true + context ).get() + sessionManager.saveSessionId(sessionId) sessionManager = SessionManager(context, 86400, "*") val authResponse = sessionManager.authorizeSession( context.packageName, @@ -70,10 +71,11 @@ class SessionManagerTest { "91714924788458331086143283967892938475657483928374623640418082526960471979197446884" ) json.put("publicAddress", "0x93475c78dv0jt80f2b6715a5c53838eC4aC96EF7") - sessionManager.createSession( + val sessionId = sessionManager.createSession( json.toString(), - context, true + context ).get() + sessionManager.saveSessionId(sessionId) sessionManager = SessionManager(context, 86400, context.packageName) val invalidateRes = sessionManager.invalidateSession(context).get() assertEquals(invalidateRes, true) From db95ff57fd20b2915f64772014b04f6549a83b47 Mon Sep 17 00:00:00 2001 From: Gaurav Goel Date: Tue, 24 Sep 2024 08:21:55 +0530 Subject: [PATCH 08/13] feat: Code cleanup Signed-off-by: Gaurav Goel --- .../java/com/web3auth/session_manager_android/SessionManager.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt b/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt index 4a2ecb8..c3400ca 100644 --- a/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt +++ b/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt @@ -232,7 +232,6 @@ class SessionManager(context: Context, sessionTime: Int = 86400, allowedOrigin: } }.handle { result, exception -> if (exception != null) { - println("Error: ${exception.message}") false } else { result From a9d7df5f98a0bc1fba646a9e7efb599eabfa1c95 Mon Sep 17 00:00:00 2001 From: Gaurav Goel Date: Tue, 24 Sep 2024 12:57:14 +0530 Subject: [PATCH 09/13] feat: Code cleanup Signed-off-by: Gaurav Goel --- .../session_manager_android/SessionManager.kt | 22 +++---------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt b/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt index c3400ca..c94b03a 100644 --- a/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt +++ b/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt @@ -148,12 +148,7 @@ class SessionManager(context: Context, sessionTime: Int = 86400, allowedOrigin: Hex.decode(ecies.ephemPublicKey) ) String(share, Charsets.UTF_8) - }.handle { result, exception -> - if (exception != null) { - return@handle "Error: ${exception.message}" - } - result - } + }.exceptionally { throw it } } /** @@ -230,13 +225,7 @@ class SessionManager(context: Context, sessionTime: Int = 86400, allowedOrigin: ) } } - }.handle { result, exception -> - if (exception != null) { - false - } else { - result - } - } + }.exceptionally { throw it } } /** @@ -310,11 +299,6 @@ class SessionManager(context: Context, sessionTime: Int = 86400, allowedOrigin: ) } newSessionKey - }.handle { result, exception -> - if (exception != null) { - return@handle "Error: ${exception.message}" - } - result - } + }.exceptionally { throw it } } } \ No newline at end of file From e71428b1b8c08b03cac1c21b0842fda1ee166ab4 Mon Sep 17 00:00:00 2001 From: Gaurav Goel Date: Tue, 24 Sep 2024 13:33:05 +0530 Subject: [PATCH 10/13] feat: Code cleanup Signed-off-by: Gaurav Goel --- .../com/web3auth/session_manager_android/SessionManager.kt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt b/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt index c94b03a..3ad188b 100644 --- a/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt +++ b/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt @@ -289,9 +289,7 @@ class SessionManager(context: Context, sessionTime: Int = 86400, allowedOrigin: } } - if (result.isSuccessful) { - // do nothing - } else { + if (!result.isSuccessful) { throw Exception( SessionManagerError.getError( ErrorCode.SOMETHING_WENT_WRONG From 9efb615f15ff4c1628ee895bf317c595a629f7cd Mon Sep 17 00:00:00 2001 From: Gaurav Goel Date: Wed, 30 Oct 2024 09:22:46 +0530 Subject: [PATCH 11/13] feat: Update sessionManager sdk as per web and swift Signed-off-by: Gaurav Goel --- .../session_manager_android/SessionManager.kt | 64 ++++++++++++++----- 1 file changed, 48 insertions(+), 16 deletions(-) diff --git a/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt b/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt index 3ad188b..ce66905 100644 --- a/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt +++ b/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt @@ -23,40 +23,63 @@ import java.nio.charset.StandardCharsets import java.util.concurrent.CompletableFuture import kotlin.math.min -class SessionManager(context: Context, sessionTime: Int = 86400, allowedOrigin: String = "*") { +class SessionManager( + context: Context, + sessionTime: Int = 86400, + allowedOrigin: String = "*", + sessionId: String? = null +) { private val gson = GsonBuilder().disableHtmlEscaping().create() private val web3AuthApi = ApiHelper.getInstance().create(Web3AuthApi::class.java) private var sessionTime: Int private var allowedOrigin: String + private lateinit var sessionId: String companion object { fun generateRandomSessionKey(): String { return KeyStoreManager.generateRandomSessionKey() } + + fun getSessionIdFromStorage(): String { + return KeyStoreManager.getPreferencesData(KeyStoreManager.SESSION_ID_TAG).toString() + } + + fun deleteSessionIdFromStorage() { + KeyStoreManager.deletePreferencesData(KeyStoreManager.SESSION_ID_TAG) + } + + fun saveSessionIdToStorage(sessionId: String) { + if (sessionId.isNotEmpty() && sessionId.isNotBlank()) { + KeyStoreManager.savePreferenceData(KeyStoreManager.SESSION_ID_TAG, sessionId) + } + } } init { KeyStoreManager.initializePreferences(context.applicationContext) initiateKeyStoreManager() + if (sessionId != null) { + if (sessionId.isNotEmpty()) { + this.sessionId = sessionId + } + } this.sessionTime = sessionTime this.allowedOrigin = allowedOrigin } - private fun initiateKeyStoreManager() { - KeyStoreManager.getKeyGenerator() - } - - fun saveSessionId(sessionId: String) { + fun setSessionId(sessionId: String) { if (sessionId.isNotEmpty()) { - KeyStoreManager.savePreferenceData( - KeyStoreManager.SESSION_ID_TAG, sessionId - ) + this.sessionId = sessionId } } fun getSessionId(): String { - return KeyStoreManager.getPreferencesData(KeyStoreManager.SESSION_ID_TAG).toString() + return this.sessionId + } + + private fun initiateKeyStoreManager() { + KeyStoreManager.getKeyGenerator() } @@ -84,10 +107,9 @@ class SessionManager(context: Context, sessionTime: Int = 86400, allowedOrigin: ) } - val sessionId = - getSessionId() + val sessionId = this.sessionId - if (sessionId.isEmpty()) { + if (sessionId.isNullOrEmpty()) { throw Exception( SessionManagerError.getError( ErrorCode.SESSIONID_NOT_FOUND @@ -174,7 +196,14 @@ class SessionManager(context: Context, sessionTime: Int = 86400, allowedOrigin: ) } - val sessionId = getSessionId() + val sessionId = this.sessionId + if (sessionId.isNullOrEmpty()) { + throw Exception( + SessionManagerError.getError( + ErrorCode.SESSIONID_NOT_FOUND + ) + ) + } val ephemKey = "04" + KeyStoreManager.getPubKey(sessionId).padStart(128, '0') val ivKey = KeyStoreManager.randomBytes(16) @@ -215,7 +244,6 @@ class SessionManager(context: Context, sessionTime: Int = 86400, allowedOrigin: } if (result.isSuccessful) { - KeyStoreManager.deletePreferencesData(KeyStoreManager.SESSION_ID_TAG) true } else { throw Exception( @@ -246,7 +274,11 @@ class SessionManager(context: Context, sessionTime: Int = 86400, allowedOrigin: context: Context, ): CompletableFuture { return CompletableFuture.supplyAsync { - val newSessionKey = generateRandomSessionKey() + val newSessionKey = this.sessionId + + if (newSessionKey.isNullOrEmpty()) { + throw Exception(SessionManagerError.getError(ErrorCode.SESSIONID_NOT_FOUND)) + } if (!ApiHelper.isNetworkAvailable(context)) { throw Exception( SessionManagerError.getError(ErrorCode.RUNTIME_ERROR) From d1db5b32bae82557d7fb029142f0d44d55e89e52 Mon Sep 17 00:00:00 2001 From: Gaurav Goel Date: Wed, 30 Oct 2024 09:23:05 +0530 Subject: [PATCH 12/13] feat: refine test Signed-off-by: Gaurav Goel --- .../SessionManagerTest.kt | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/session-manager-android/src/androidTest/java/com/web3auth/session_manager_android/SessionManagerTest.kt b/session-manager-android/src/androidTest/java/com/web3auth/session_manager_android/SessionManagerTest.kt index 1108f34..0586f1e 100644 --- a/session-manager-android/src/androidTest/java/com/web3auth/session_manager_android/SessionManagerTest.kt +++ b/session-manager-android/src/androidTest/java/com/web3auth/session_manager_android/SessionManagerTest.kt @@ -5,6 +5,7 @@ import androidx.test.platform.app.InstrumentationRegistry import com.web3auth.session_manager_android.keystore.KeyStoreManager import com.web3auth.session_manager_android.types.AES256CBC import junit.framework.TestCase.assertEquals +import junit.framework.TestCase.assertTrue import org.bouncycastle.util.encoders.Hex import org.json.JSONObject import org.junit.Test @@ -20,7 +21,8 @@ class SessionManagerTest { @Throws(ExecutionException::class, InterruptedException::class) fun test_createSession() { val context = InstrumentationRegistry.getInstrumentation().context - sessionManager = SessionManager(context, 86400, context.packageName) + val sessionId = SessionManager.generateRandomSessionKey() + sessionManager = SessionManager(context, 86400, context.packageName, sessionId) val json = JSONObject() json.put( "privateKey", @@ -38,19 +40,20 @@ class SessionManagerTest { @Throws(ExecutionException::class, InterruptedException::class) fun test_authorizeSession() { val context = InstrumentationRegistry.getInstrumentation().context - sessionManager = SessionManager(context, 86400, context.packageName) + val sessionId = SessionManager.generateRandomSessionKey() + sessionManager = SessionManager(context, 86400, context.packageName, sessionId) val json = JSONObject() json.put( "privateKey", "91714924788458331086143283967892938475657483928374623640418082526960471979197446884" ) json.put("publicAddress", "0x93475c78dv0jt80f2b6715a5c53838eC4aC96EF7") - val sessionId = sessionManager.createSession( + val created = sessionManager.createSession( json.toString(), context ).get() - sessionManager.saveSessionId(sessionId) - sessionManager = SessionManager(context, 86400, "*") + SessionManager.saveSessionIdToStorage(created) + assertTrue(created.isNotEmpty()) val authResponse = sessionManager.authorizeSession( context.packageName, context @@ -64,22 +67,23 @@ class SessionManagerTest { @Throws(ExecutionException::class, InterruptedException::class) fun test_invalidateSession() { val context = InstrumentationRegistry.getInstrumentation().context - sessionManager = SessionManager(context, 86400, context.packageName) + val sessionId = SessionManager.generateRandomSessionKey() + sessionManager = SessionManager(context, 86400, context.packageName, sessionId) val json = JSONObject() json.put( "privateKey", "91714924788458331086143283967892938475657483928374623640418082526960471979197446884" ) json.put("publicAddress", "0x93475c78dv0jt80f2b6715a5c53838eC4aC96EF7") - val sessionId = sessionManager.createSession( + val created = sessionManager.createSession( json.toString(), context ).get() - sessionManager.saveSessionId(sessionId) - sessionManager = SessionManager(context, 86400, context.packageName) + SessionManager.saveSessionIdToStorage(created) val invalidateRes = sessionManager.invalidateSession(context).get() assertEquals(invalidateRes, true) - val res = sessionManager.getSessionId().isNotEmpty() + SessionManager.deleteSessionIdFromStorage() + val res = SessionManager.getSessionIdFromStorage().isNotEmpty() assertEquals(res, false) } From dcb58967061de5d035e494cb1b041f4e762f4249 Mon Sep 17 00:00:00 2001 From: Chaitanya Potti Date: Wed, 6 Nov 2024 12:32:59 +0800 Subject: [PATCH 13/13] minor refactor --- .../com/web3auth/session_manager_android/SessionManager.kt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt b/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt index ce66905..4be27c7 100644 --- a/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt +++ b/session-manager-android/src/main/java/com/web3auth/session_manager_android/SessionManager.kt @@ -59,10 +59,8 @@ class SessionManager( init { KeyStoreManager.initializePreferences(context.applicationContext) initiateKeyStoreManager() - if (sessionId != null) { - if (sessionId.isNotEmpty()) { - this.sessionId = sessionId - } + if (sessionId != null && sessionId.isNotEmpty()) { + this.sessionId = sessionId } this.sessionTime = sessionTime this.allowedOrigin = allowedOrigin