Skip to content

Commit

Permalink
Merge pull request #14 from Web3Auth/feat/constructor-updates
Browse files Browse the repository at this point in the history
feat: Session manager constructor Updates
  • Loading branch information
chaitanyapotti authored Sep 17, 2024
2 parents e140448 + 1480c9c commit c77fde3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
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
)

0 comments on commit c77fde3

Please sign in to comment.