Skip to content

Commit d710f21

Browse files
Merge pull request #39 from Web3Auth/feat/auth-service-v9-updates
feat: auth-services v9 updates
2 parents 9826e01 + c306d06 commit d710f21

File tree

6 files changed

+61
-29
lines changed

6 files changed

+61
-29
lines changed

Assets/Plugins/Web3AuthSDK/Api/Models/LogoutApiRequest.cs

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ public class LogoutApiRequest
88
public string data { get; set; }
99
public string signature { get; set; }
1010
public long timeout { get; set; }
11+
public string allowedOrigin { get; set; } = "*";
1112
}

Assets/Plugins/Web3AuthSDK/Api/Web3AuthApi.cs

+15-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
public class Web3AuthApi
1010
{
1111
static Web3AuthApi instance;
12-
static string baseAddress = "https://session.web3auth.io";
12+
static string baseAddress = "https://session.web3auth.io/v2";
1313

1414
public static Web3AuthApi getInstance()
1515
{
@@ -18,24 +18,28 @@ public static Web3AuthApi getInstance()
1818
return instance;
1919
}
2020

21-
public IEnumerator authorizeSession(string key, Action<StoreApiResponse> callback)
21+
public IEnumerator authorizeSession(string key, string origin, Action<StoreApiResponse> callback)
2222
{
2323
//var requestURL = $"{baseAddress}/store/get?key={key}";
2424
//var request = UnityWebRequest.Get(requestURL);
25+
var requestURL = $"{baseAddress}/store/get";
26+
//Debug.Log("Request URL => " + requestURL);
27+
2528
WWWForm data = new WWWForm();
2629
data.AddField("key", key);
2730

28-
var request = UnityWebRequest.Post($"{baseAddress}/store/get", data);
31+
var request = UnityWebRequest.Post(requestURL, data);
32+
request.SetRequestHeader("origin", origin);
2933

3034
yield return request.SendWebRequest();
31-
// Debug.Log("baseAddress =>" + baseAddress);
32-
// Debug.Log("key =>" + key);
33-
// Debug.Log("request URL =>"+ requestURL);
34-
// Debug.Log("request.isNetworkError =>" + request.isNetworkError);
35-
// Debug.Log("request.isHttpError =>" + request.isHttpError);
36-
// Debug.Log("request.isHttpError =>" + request.error);
37-
// Debug.Log("request.result =>" + request.result);
38-
// Debug.Log("request.downloadHandler.text =>" + request.downloadHandler.text);
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);
3943
if (request.result == UnityWebRequest.Result.Success)
4044
{
4145
string result = request.downloadHandler.text;

Assets/Plugins/Web3AuthSDK/Keystore/KeyStoreManagerUtils.cs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class KeyStoreManagerUtils
2828
public static string IV_KEY = "ivKey";
2929
public static string EPHEM_PUBLIC_Key = "ephemPublicKey";
3030
public static string MAC = "mac";
31+
public static string REDIRECT_URL = "redirectUrl";
3132

3233
public static string getPubKey(string sessionId)
3334
{

Assets/Plugins/Web3AuthSDK/Samples/Web3AuthSample.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ void Start()
9595
}
9696
*/
9797
clientId = "BFuUqebV5I8Pz5F7a5A2ihW7YVmbv_OHXnHYDv6OltAD5NGr6e-ViNvde3U4BHdn6HvwfkgobhVu4VwC-OSJkik",
98-
buildEnv = BuildEnv.TESTING,
99-
redirectUrl = new Uri("torusapp://com.torus.Web3AuthUnity/auth"),
98+
buildEnv = BuildEnv.PRODUCTION,
99+
redirectUrl = new Uri("torusapp://com.torus.Web3AuthUnity"),
100100
network = Web3Auth.Network.SAPPHIRE_DEVNET,
101101
sessionTime = 86400
102102
});

Assets/Plugins/Web3AuthSDK/Types/Web3AuthOptions.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ public class Web3AuthOptions {
1111
public string sdkUrl {
1212
get {
1313
if (buildEnv == Web3Auth.BuildEnv.STAGING)
14-
return "https://staging-auth.web3auth.io/v8";
14+
return "https://staging-auth.web3auth.io/v9";
1515
else if (buildEnv == Web3Auth.BuildEnv.TESTING)
1616
return "https://develop-auth.web3auth.io";
1717
else
18-
return "https://auth.web3auth.io/v8";
18+
return "https://auth.web3auth.io/v9";
1919
}
2020
set { }
2121
}
2222

2323
public string walletSdkUrl {
2424
get {
2525
if (buildEnv == Web3Auth.BuildEnv.STAGING)
26-
return "https://staging-wallet.web3auth.io/v2";
26+
return "https://staging-wallet.web3auth.io/v3";
2727
else if (buildEnv == Web3Auth.BuildEnv.TESTING)
2828
return "https://develop-wallet.web3auth.io";
2929
else
30-
return "https://wallet.web3auth.io/v2";
30+
return "https://wallet.web3auth.io/v3";
3131
}
3232
set { }
3333
}

Assets/Plugins/Web3AuthSDK/Web3Auth.cs

+38-12
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public static void setSignResponse(SignResponse _response)
6464

6565
[SerializeField]
6666
private Web3Auth.Network network;
67+
private string redirectUrl;
6768

6869
private static readonly Queue<Action> _executionQueue = new Queue<Action>();
6970

@@ -108,7 +109,8 @@ public async void setOptions(Web3AuthOptions web3AuthOptions)
108109
{
109110
throw new Exception("Failed to fetch project config. Please try again later.");
110111
} else {
111-
authorizeSession("");
112+
var redirectUrl = KeyStoreManagerUtils.getPreferencesData(KeyStoreManagerUtils.REDIRECT_URL);
113+
authorizeSession("", redirectUrl);
112114

113115
JsonSerializerSettings settings = new JsonSerializerSettings
114116
{
@@ -264,13 +266,21 @@ private void IncomingHttpRequest(IAsyncResult result)
264266

265267
private async void processRequest(string path, LoginParams loginParams = null)
266268
{
269+
redirectUrl = this.initParams["redirectUrl"].ToString();
270+
if (redirectUrl.EndsWith("/"))
271+
{
272+
redirectUrl = redirectUrl.TrimEnd('/');
273+
}
274+
267275
#if UNITY_STANDALONE || UNITY_EDITOR
268276
this.initParams["redirectUrl"] = StartLocalWebserver();
277+
redirectUrl = this.initParams["redirectUrl"].ToString().Replace("/complete/", "");
269278
#elif UNITY_WEBGL
270279
this.initParams["redirectUrl"] = Utils.GetCurrentURL();
271280
#endif
272281

273282
loginParams.redirectUrl = loginParams.redirectUrl ?? new Uri(this.initParams["redirectUrl"].ToString());
283+
//Debug.Log("loginParams.redirectUrl: =>" + loginParams.redirectUrl);
274284
Dictionary<string, object> paramMap = new Dictionary<string, object>();
275285
paramMap["options"] = this.initParams;
276286
paramMap["params"] = loginParams == null ? (object)new Dictionary<string, object>() : (object)loginParams;
@@ -287,7 +297,7 @@ private async void processRequest(string path, LoginParams loginParams = null)
287297
new JsonSerializerSettings
288298
{
289299
NullValueHandling = NullValueHandling.Ignore
290-
}), 600);
300+
}), 600, "*");
291301

292302
if (!string.IsNullOrEmpty(loginId))
293303
{
@@ -326,8 +336,14 @@ public async void launchWalletServices(ChainConfig chainConfig, string path = "w
326336
string sessionId = KeyStoreManagerUtils.getPreferencesData(KeyStoreManagerUtils.SESSION_ID);
327337
if (!string.IsNullOrEmpty(sessionId))
328338
{
339+
redirectUrl = this.initParams["redirectUrl"].ToString();
340+
if (redirectUrl.EndsWith("/"))
341+
{
342+
redirectUrl = redirectUrl.TrimEnd('/');
343+
}
329344
#if UNITY_STANDALONE || UNITY_EDITOR
330345
this.initParams["redirectUrl"] = StartLocalWebserver();
346+
redirectUrl = this.initParams["redirectUrl"].ToString().Replace("/complete/", "");
331347
#elif UNITY_WEBGL
332348
this.initParams["redirectUrl"] = Utils.GetCurrentURL();
333349
#endif
@@ -341,7 +357,7 @@ public async void launchWalletServices(ChainConfig chainConfig, string path = "w
341357
new JsonSerializerSettings
342358
{
343359
NullValueHandling = NullValueHandling.Ignore
344-
}), 600);
360+
}), 600, "*");
345361

346362
if (!string.IsNullOrEmpty(loginId))
347363
{
@@ -425,9 +441,10 @@ public void setResultUrl(Uri uri)
425441
}
426442
string sessionId = sessionResponse.sessionId;
427443
this.Enqueue(() => KeyStoreManagerUtils.savePreferenceData(KeyStoreManagerUtils.SESSION_ID, sessionId));
444+
this.Enqueue(() => KeyStoreManagerUtils.savePreferenceData(KeyStoreManagerUtils.REDIRECT_URL, redirectUrl));
428445

429446
//call authorize session API
430-
this.Enqueue(() => authorizeSession(sessionId));
447+
this.Enqueue(() => authorizeSession(sessionId, redirectUrl));
431448

432449
#if !UNITY_EDITOR && UNITY_WEBGL
433450
if (this.web3AuthResponse != null)
@@ -536,8 +553,14 @@ public async void request(ChainConfig chainConfig, string method, JArray request
536553
string sessionId = KeyStoreManagerUtils.getPreferencesData(KeyStoreManagerUtils.SESSION_ID);
537554
if (!string.IsNullOrEmpty(sessionId))
538555
{
556+
redirectUrl = this.initParams["redirectUrl"].ToString();
557+
if (redirectUrl.EndsWith("/"))
558+
{
559+
redirectUrl = redirectUrl.TrimEnd('/');
560+
}
539561
#if UNITY_STANDALONE || UNITY_EDITOR
540562
this.initParams["redirectUrl"] = StartLocalWebserver();
563+
redirectUrl = this.initParams["redirectUrl"].ToString().Replace("/complete/", "");
541564
#elif UNITY_WEBGL
542565
this.initParams["redirectUrl"] = Utils.GetCurrentURL();
543566
#endif
@@ -550,7 +573,7 @@ public async void request(ChainConfig chainConfig, string method, JArray request
550573
new JsonSerializerSettings
551574
{
552575
NullValueHandling = NullValueHandling.Ignore
553-
}), 60000);
576+
}), 600, "*");
554577

555578
if (!string.IsNullOrEmpty(loginId))
556579
{
@@ -598,7 +621,7 @@ public async void request(ChainConfig chainConfig, string method, JArray request
598621
}
599622
}
600623

601-
private void authorizeSession(string newSessionId)
624+
private void authorizeSession(string newSessionId, string origin)
602625
{
603626
string sessionId = "";
604627
if (string.IsNullOrEmpty(newSessionId))
@@ -614,7 +637,8 @@ private void authorizeSession(string newSessionId)
614637
if (!string.IsNullOrEmpty(sessionId))
615638
{
616639
var pubKey = KeyStoreManagerUtils.getPubKey(sessionId);
617-
StartCoroutine(Web3AuthApi.getInstance().authorizeSession(pubKey, (response =>
640+
//Debug.Log("origin: =>" + origin);
641+
StartCoroutine(Web3AuthApi.getInstance().authorizeSession(pubKey, origin, (response =>
618642
{
619643
if (response != null)
620644
{
@@ -641,6 +665,7 @@ private void authorizeSession(string newSessionId)
641665
if (!string.IsNullOrEmpty(this.web3AuthResponse.sessionId))
642666
{
643667
KeyStoreManagerUtils.savePreferenceData(KeyStoreManagerUtils.SESSION_ID, this.web3AuthResponse.sessionId);
668+
//Debug.Log("redirectUrl: =>" + redirectUrl);
644669
}
645670

646671
if (!string.IsNullOrEmpty(web3AuthResponse.userInfo?.dappShare))
@@ -665,10 +690,11 @@ private void authorizeSession(string newSessionId)
665690
private void sessionTimeOutAPI()
666691
{
667692
string sessionId = KeyStoreManagerUtils.getPreferencesData(KeyStoreManagerUtils.SESSION_ID);
693+
string redirectUrl = KeyStoreManagerUtils.getPreferencesData(KeyStoreManagerUtils.REDIRECT_URL);
668694
if (!string.IsNullOrEmpty(sessionId))
669695
{
670696
var pubKey = KeyStoreManagerUtils.getPubKey(sessionId);
671-
StartCoroutine(Web3AuthApi.getInstance().authorizeSession(pubKey, (response =>
697+
StartCoroutine(Web3AuthApi.getInstance().authorizeSession(pubKey, redirectUrl, (response =>
672698
{
673699
if (response != null)
674700
{
@@ -724,7 +750,7 @@ private void sessionTimeOutAPI()
724750
}
725751
}
726752

727-
private async Task<string> createSession(string data, long sessionTime)
753+
private async Task<string> createSession(string data, long sessionTime, string allowedOrigin)
728754
{
729755
TaskCompletionSource<string> createSessionResponse = new TaskCompletionSource<string>();
730756
var newSessionKey = KeyStoreManagerUtils.generateRandomSessionKey();
@@ -756,15 +782,15 @@ private async Task<string> createSession(string data, long sessionTime)
756782
newSessionKey,
757783
jsonData
758784
),
759-
timeout = Math.Min(sessionTime, 30 * 86400)
785+
timeout = Math.Min(sessionTime, 30 * 86400),
786+
allowedOrigin = allowedOrigin
760787
}, result =>
761788
{
762789
if (result != null)
763790
{
764791
try
765792
{
766-
// Debug.Log("newSessionKey before saving into keystore =>" + newSessionKey);
767-
this.Enqueue(() => KeyStoreManagerUtils.savePreferenceData(KeyStoreManagerUtils.SESSION_ID, newSessionKey));
793+
// Debug.Log("newSessionKey before saving into keystore =>" + newSessionKey)
768794
createSessionResponse.SetResult(newSessionKey);
769795
}
770796
catch (Exception ex)

0 commit comments

Comments
 (0)