Skip to content

Commit

Permalink
feat: added origin header in authorize session API.
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 16, 2024
1 parent 62f6a17 commit c52ec3d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 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: Long = 86400, allowedOrigin: String = "*") {

private val gson = GsonBuilder().disableHtmlEscaping().create()
private val web3AuthApi = ApiHelper.getInstance().create(Web3AuthApi::class.java)
private var sessionTime: Long
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

0 comments on commit c52ec3d

Please sign in to comment.