@@ -27,58 +27,72 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
2727 }
2828
2929 private fun request (
30- path : String , params : LoginParams ? = null , extraParams : Map <String , Any >? = null
30+ path : String , params : LoginParams , extraParams : Map <String , Any >? = null
3131 ) {
3232 val sdkUrl = Uri .parse(web3AuthOption.sdkUrl)
3333 val context = web3AuthOption.context
3434
35- val initOptions = mutableMapOf (
36- " clientId" to web3AuthOption.clientId,
37- " network" to web3AuthOption.network.name.lowercase(Locale .ROOT )
35+ val initOptions = JSONObject ()
36+ initOptions.put(" clientId" , web3AuthOption.clientId)
37+ initOptions.put(" network" , web3AuthOption.network.name.lowercase(Locale .ROOT ))
38+ if (web3AuthOption.redirectUrl != null ) initOptions.put(
39+ " redirectUrl" , web3AuthOption.redirectUrl.toString()
3840 )
39- if (web3AuthOption.redirectUrl != null ) initOptions[" redirectUrl" ] =
40- web3AuthOption.redirectUrl.toString()
41- if (web3AuthOption.whiteLabel != null ) initOptions[" whiteLabel" ] =
42- gson.toJson(web3AuthOption.whiteLabel)
43- if (web3AuthOption.loginConfig != null ) initOptions[" loginConfig" ] =
44- gson.toJson(web3AuthOption.loginConfig)
45- if (web3AuthOption.buildEnv != null ) initOptions[" buildEnv" ] =
46- web3AuthOption.buildEnv.toString().lowercase()
47- if (web3AuthOption.mfaSettings != null ) initOptions[" mfaSettings" ] =
48- gson.toJson(web3AuthOption.mfaSettings)
49- if (web3AuthOption.sessionTime != null ) initOptions[" sessionTime" ] =
50- web3AuthOption.sessionTime.toString()
51-
52- val initParams = mutableMapOf (
53- " loginProvider" to params?.loginProvider,
54- " extraLoginOptions" to params?.extraLoginOptions,
55- " redirectUrl" to if (params?.redirectUrl != null ) params.redirectUrl.toString() else initOptions[" redirectUrl" ].toString(),
56- " mfaLevel" to params?.mfaLevel,
57- " curve" to params?.curve,
58- " dappShare" to params?.dappShare
41+ if (web3AuthOption.whiteLabel != null ) initOptions.put(
42+ " whiteLabel" , gson.toJson(web3AuthOption.whiteLabel)
5943 )
44+ if (web3AuthOption.loginConfig != null ) initOptions.put(
45+ " loginConfig" , gson.toJson(web3AuthOption.loginConfig)
46+ )
47+ if (web3AuthOption.buildEnv != null ) initOptions.put(
48+ " buildEnv" , web3AuthOption.buildEnv?.name?.lowercase(Locale .ROOT )
49+ )
50+ if (web3AuthOption.mfaSettings != null ) initOptions.put(
51+ " mfaSettings" , gson.toJson(web3AuthOption.mfaSettings)
52+ )
53+ if (web3AuthOption.sessionTime != null ) initOptions.put(
54+ " sessionTime" , web3AuthOption.sessionTime
55+ )
56+
57+ val initParams = JSONObject ()
58+ initParams.put(" loginProvider" , params.loginProvider.name.lowercase(Locale .ROOT ))
59+ if (params.extraLoginOptions != null ) initParams.put(
60+ " extraLoginOptions" ,
61+ gson.toJson(params.extraLoginOptions)
62+ )
63+ initParams.put(
64+ " redirectUrl" ,
65+ if (params.redirectUrl != null ) params.redirectUrl.toString() else initOptions[" redirectUrl" ].toString()
66+ )
67+ if (params.mfaLevel != null ) initParams.put(
68+ " mfaLevel" ,
69+ params.mfaLevel.name.lowercase(Locale .ROOT )
70+ )
71+ if (params.curve != null ) initParams.put(" curve" , params.curve.name.lowercase(Locale .ROOT ))
72+ if (params.dappShare != null ) initParams.put(" dappShare" , params.dappShare)
73+
6074
61- val paramMap = mapOf (
62- " options" to initOptions, " params" to initParams, " actionType" to path
75+ val paramMap = JSONObject ()
76+ paramMap.put(
77+ " options" , initOptions
6378 )
79+ paramMap.put(" params" , initParams)
80+ paramMap.put(" actionType" , path)
6481
65- extraParams?.let { paramMap.plus(" params" to extraParams) }
66- val validParams = paramMap.filterValues { it != null }
82+ extraParams?.let { paramMap.put(" params" , extraParams) }
6783
68- val loginIdCf = getLoginId(validParams )
84+ val loginIdCf = getLoginId(paramMap )
6985
7086 loginIdCf.whenComplete { loginId, error ->
7187 if (error == null ) {
72- val jsonObject = mapOf (
73- " loginId" to loginId
74- )
88+ val jsonObject = mapOf (" loginId" to loginId)
7589 val hash = " b64Params=" + gson.toJson(jsonObject).toByteArray(Charsets .UTF_8 )
7690 .toBase64URLString()
7791
7892 val url =
7993 Uri .Builder ().scheme(sdkUrl.scheme).encodedAuthority(sdkUrl.encodedAuthority)
8094 .encodedPath(sdkUrl.encodedPath).appendPath(" start" ).fragment(hash).build()
81-
95+ print ( " url: => $url " )
8296 val defaultBrowser = context.getDefaultBrowser()
8397 val customTabsBrowsers = context.getCustomTabsBrowsers()
8498
@@ -126,14 +140,15 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
126140 loginCompletableFuture.completeExceptionally(UserCancelledException ())
127141 return
128142 }
143+ val hashUri = Uri .parse(uri.host + " ?" + uri.fragment)
129144 val error = uri.getQueryParameter(" error" )
130145 if (error != null ) {
131146 loginCompletableFuture.completeExceptionally(UnKnownException (error))
132147 }
133148
134- val sessionId = hash.split( " & " )[ 0 ].split( " = " )[ 1 ]
149+ val sessionId = hashUri.getQueryParameter( " sessionId " )
135150
136- if (sessionId != null ) {
151+ if (! sessionId.isNullOrBlank() && sessionId.isNotEmpty() ) {
137152 sessionManager.saveSessionId(sessionId)
138153
139154 // Rehydrate Session
@@ -223,8 +238,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
223238 sessionResponse.whenComplete { response, error ->
224239 if (error == null ) {
225240 val tempJson = JSONObject (response)
226- web3AuthResponse =
227- gson.fromJson(tempJson.toString(), Web3AuthResponse ::class .java)
241+ web3AuthResponse = gson.fromJson(tempJson.toString(), Web3AuthResponse ::class .java)
228242 if (web3AuthResponse?.error?.isNotBlank() == true ) {
229243 sessionCompletableFuture.completeExceptionally(
230244 UnKnownException (
@@ -255,13 +269,10 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
255269 return sessionCompletableFuture
256270 }
257271
258- private fun getLoginId (jsonObject : Map < String , Any ?> ): CompletableFuture <String > {
272+ private fun getLoginId (jsonObject : JSONObject ): CompletableFuture <String > {
259273 val createSessionCompletableFuture: CompletableFuture <String > = CompletableFuture ()
260- if (this .sessionManager == null ) {
261- createSessionCompletableFuture.completeExceptionally(Exception (" Session Manager is not initialized" ))
262- }
263274 val sessionResponse: CompletableFuture <String > =
264- sessionManager.createSession(gson.toJson(jsonObject ), 600 )
275+ sessionManager.createSession(jsonObject.toString( ), 600 )
265276 sessionResponse.whenComplete { response, error ->
266277 if (error == null ) {
267278 createSessionCompletableFuture.complete(response)
0 commit comments