Skip to content

Commit 17c19f0

Browse files
committed
authorizeSession API fixes
1 parent 4d0e78f commit 17c19f0

4 files changed

Lines changed: 86 additions & 64 deletions

File tree

Assets/Plugins/Web3AuthSDK/Api/Web3AuthApi.cs

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Collections;
22
using System;
3-
using Newtonsoft.Json.Linq;
43
using Newtonsoft.Json;
4+
using Newtonsoft.Json.Linq;
55
using UnityEngine.Networking;
66
using UnityEngine;
77
using System.Collections.Generic;
@@ -18,28 +18,27 @@ public static Web3AuthApi getInstance()
1818
return instance;
1919
}
2020

21-
public IEnumerator authorizeSession(string key, string origin, Action<StoreApiResponse> callback)
21+
static string getOriginFromRedirectUrl(string redirectUrl)
22+
{
23+
if (string.IsNullOrEmpty(redirectUrl))
24+
return redirectUrl;
25+
if (!Uri.TryCreate(redirectUrl, UriKind.Absolute, out var uri))
26+
return redirectUrl;
27+
return uri.GetLeftPart(UriPartial.Authority);
28+
}
29+
30+
public IEnumerator authorizeSession(string key, string redirectUrl, Action<StoreApiResponse> callback)
2231
{
23-
//var requestURL = $"{baseAddress}/store/get?key={key}";
24-
//var request = UnityWebRequest.Get(requestURL);
2532
var requestURL = $"{baseAddress}/store/get";
26-
//Debug.Log("Request URL => " + requestURL);
2733

2834
WWWForm data = new WWWForm();
2935
data.AddField("key", key);
3036

3137
var request = UnityWebRequest.Post(requestURL, data);
32-
request.SetRequestHeader("origin", origin);
38+
request.SetRequestHeader("origin", getOriginFromRedirectUrl(redirectUrl));
3339

3440
yield return request.SendWebRequest();
35-
//Debug.Log("baseAddress =>" + baseAddress);
36-
//Debug.Log("key =>" + key);
37-
//Debug.Log("request URL =>"+ request);
38-
//Debug.Log("request.isNetworkError =>" + request.isNetworkError);
39-
//Debug.Log("request.isHttpError =>" + request.isHttpError);
40-
//Debug.Log("request.isHttpError =>" + request.error);
41-
//Debug.Log("request.result =>" + request.result);
42-
//Debug.Log("request.downloadHandler.text =>" + request.downloadHandler.text);
41+
4342
if (request.result == UnityWebRequest.Result.Success)
4443
{
4544
string result = request.downloadHandler?.text;
@@ -48,12 +47,13 @@ public IEnumerator authorizeSession(string key, string origin, Action<StoreApiRe
4847
StoreApiResponse response = null;
4948
try
5049
{
51-
response = Newtonsoft.Json.JsonConvert.DeserializeObject<StoreApiResponse>(result);
50+
response = JsonConvert.DeserializeObject<StoreApiResponse>(result);
5251
}
53-
catch (Exception ex)
52+
catch
5453
{
55-
Debug.LogWarning($"Web3Auth: Failed to deserialize store response: {ex.Message}");
54+
response = null;
5655
}
56+
5757
callback(response);
5858
}
5959
else
@@ -72,24 +72,14 @@ public IEnumerator logout(LogoutApiRequest logoutApiRequest, Action<JObject> cal
7272
data.AddField("data", logoutApiRequest.data);
7373
data.AddField("signature", logoutApiRequest.signature);
7474
data.AddField("timeout", logoutApiRequest.timeout.ToString());
75-
// Debug.Log("key during logout session =>" + logoutApiRequest.key);
7675

7776
var request = UnityWebRequest.Post($"{baseAddress}/store/set", data);
7877
yield return request.SendWebRequest();
7978

80-
// Debug.Log("baseAddress =>" + baseAddress);
81-
// Debug.Log("key =>" + logoutApiRequest.key);
82-
// Debug.Log("request URL =>"+ requestURL);
83-
// Debug.Log("request.isNetworkError =>" + request.isNetworkError);
84-
// Debug.Log("request.isHttpError =>" + request.isHttpError);
85-
// Debug.Log("request.isHttpError =>" + request.error);
86-
// Debug.Log("request.result =>" + request.result);
87-
// Debug.Log("request.downloadHandler.text =>" + request.downloadHandler.text);
88-
8979
if (request.result == UnityWebRequest.Result.Success)
9080
{
9181
string result = request.downloadHandler.text;
92-
callback(Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(result));
82+
callback(JsonConvert.DeserializeObject<JObject>(result));
9383
}
9484
else
9585
callback(null);
@@ -109,25 +99,26 @@ public IEnumerator createSession(LogoutApiRequest logoutApiRequest, Action<JObje
10999
if (request.result == UnityWebRequest.Result.Success)
110100
{
111101
string result = request.downloadHandler.text;
112-
callback(Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(result));
102+
callback(JsonConvert.DeserializeObject<JObject>(result));
113103
}
114104
else
115105
callback(null);
116106
}
117107

118108
public IEnumerator fetchProjectConfig(string project_id, string network, Action<ProjectConfigResponse> callback)
119109
{
120-
//Debug.Log("network =>" + network);
121110
string baseUrl = SIGNER_MAP[network];
122-
var requestURL = $"{baseUrl}/api/configuration?project_id={project_id}&network={network}&whitelist=true";
111+
var requestURL =
112+
$"{baseUrl}/api/configuration?project_id={project_id}&network={network}&whitelist=true";
113+
123114
var request = UnityWebRequest.Get(requestURL);
124115

125116
yield return request.SendWebRequest();
126117

127118
if (request.result == UnityWebRequest.Result.Success)
128119
{
129120
string result = request.downloadHandler.text;
130-
callback(Newtonsoft.Json.JsonConvert.DeserializeObject<ProjectConfigResponse>(result));
121+
callback(JsonConvert.DeserializeObject<ProjectConfigResponse>(result));
131122
}
132123
else
133124
callback(null);

Assets/Plugins/Web3AuthSDK/Samples/Web3AuthSample.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Linq;
33
using System.Collections.Generic;
44
using UnityEngine;
@@ -68,7 +68,7 @@ void Start()
6868
{
6969
verifier = "your_verifierid_from_web3auth_dashboard",
7070
typeOfLogin = TypeOfLogin.GOOGLE,
71-
clientId = "your_clientId_from_web3auth_dashboard"
71+
clientId = "BHgArYmWwSeq21czpcarYh0EVq2WWOzflX-NTK-tY1-1pauPzHKRRLgpABkmYiIV_og9jAvoIxQ8L3Smrwe04Lw"
7272
};
7373

7474
web3Auth = GetComponent<Web3Auth>();
@@ -94,7 +94,7 @@ void Start()
9494
{"CUSTOM_VERIFIER", loginConfigItem}
9595
}
9696
*/
97-
clientId = "BFuUqebV5I8Pz5F7a5A2ihW7YVmbv_OHXnHYDv6OltAD5NGr6e-ViNvde3U4BHdn6HvwfkgobhVu4VwC-OSJkik",
97+
clientId = "BHgArYmWwSeq21czpcarYh0EVq2WWOzflX-NTK-tY1-1pauPzHKRRLgpABkmYiIV_og9jAvoIxQ8L3Smrwe04Lw",
9898
buildEnv = BuildEnv.PRODUCTION,
9999
redirectUrl = new Uri("torusapp://com.torus.Web3AuthUnity"),
100100
network = Web3Auth.Network.SAPPHIRE_DEVNET,

Assets/Plugins/Web3AuthSDK/Samples/Web3AuthSample.unity

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ MonoBehaviour:
10161016
m_Script: {fileID: 11500000, guid: 3eab2a0bf902d6e4b9c2e968ad89f528, type: 3}
10171017
m_Name:
10181018
m_EditorClassIdentifier:
1019-
clientId: BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ
1019+
clientId: BHgArYmWwSeq21czpcarYh0EVq2WWOzflX-NTK-tY1-1pauPzHKRRLgpABkmYiIV_og9jAvoIxQ8L3Smrwe04Lw
10201020
redirectUri: torusapp://com.torus.Web3AuthUnity/auth
10211021
network: 5
10221022
--- !u!1 &505954357

0 commit comments

Comments
 (0)