Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Session manager constructor Updates #14

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class SessionManagerTest {
@Throws(ExecutionException::class, InterruptedException::class)
fun test_createSession() {
val context = InstrumentationRegistry.getInstrumentation().context
sessionManager = SessionManager(context)
sessionManager = SessionManager(context, 86400, context.packageName)
val json = JSONObject()
json.put(
"privateKey",
Expand All @@ -29,7 +29,6 @@ class SessionManagerTest {
json.put("publicAddress", "0x93475c78dv0jt80f2b6715a5c53838eC4aC96EF7")
val sessionKey = sessionManager.createSession(
json.toString(),
86400,
context
).get()
assert(sessionKey != null)
Expand All @@ -39,7 +38,7 @@ class SessionManagerTest {
@Throws(ExecutionException::class, InterruptedException::class)
fun test_authorizeSession() {
val context = InstrumentationRegistry.getInstrumentation().context
sessionManager = SessionManager(context)
sessionManager = SessionManager(context, 86400, context.packageName)
val json = JSONObject()
json.put(
"privateKey",
Expand All @@ -48,12 +47,11 @@ class SessionManagerTest {
json.put("publicAddress", "0x93475c78dv0jt80f2b6715a5c53838eC4aC96EF7")
sessionManager.createSession(
json.toString(),
86400,
context,
).get()
sessionManager = SessionManager(context)
sessionManager = SessionManager(context, 86400, "*")
val authResponse = sessionManager.authorizeSession(
"",
context.packageName,
context
).get()
val resp = JSONObject(authResponse)
Expand All @@ -65,7 +63,7 @@ class SessionManagerTest {
@Throws(ExecutionException::class, InterruptedException::class)
fun test_invalidateSession() {
val context = InstrumentationRegistry.getInstrumentation().context
sessionManager = SessionManager(context)
sessionManager = SessionManager(context, 86400, context.packageName)
val json = JSONObject()
json.put(
"privateKey",
Expand All @@ -74,10 +72,9 @@ class SessionManagerTest {
json.put("publicAddress", "0x93475c78dv0jt80f2b6715a5c53838eC4aC96EF7")
sessionManager.createSession(
json.toString(),
86400,
context
).get()
sessionManager = SessionManager(context)
sessionManager = SessionManager(context, 86400, context.packageName)
val invalidateRes = sessionManager.invalidateSession(context).get()
assertEquals(invalidateRes, true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import java.nio.charset.StandardCharsets
import java.util.concurrent.CompletableFuture
import kotlin.math.min

class SessionManager(context: Context) {
class SessionManager(context: Context, sessionTime: Int = 86400, allowedOrigin: String = "*") {

private val gson = GsonBuilder().disableHtmlEscaping().create()
private val web3AuthApi = ApiHelper.getInstance().create(Web3AuthApi::class.java)
private var sessionTime: Int
private var allowedOrigin: String

companion object {
fun generateRandomSessionKey(): String {
Expand All @@ -37,6 +39,8 @@ class SessionManager(context: Context) {
init {
KeyStoreManager.initializePreferences(context.applicationContext)
initiateKeyStoreManager()
this.sessionTime = sessionTime
this.allowedOrigin = allowedOrigin
}

private fun initiateKeyStoreManager() {
Expand Down Expand Up @@ -189,9 +193,7 @@ class SessionManager(context: Context) {

fun createSession(
data: String,
sessionTime: Long,
context: Context,
allowedOrigin: String = "*",
): CompletableFuture<String> {
return CompletableFuture.supplyAsync {
val newSessionKey = generateRandomSessionKey()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ data class SessionRequestBody(
val key: String,
val data: String,
val signature: String,
val timeout: Long = 0L,
val timeout: Int = 0,
val allowedOrigin: String? = null
)