@@ -47,6 +47,7 @@ public enum Language
47
47
public event Action < Web3AuthResponse > onLogin ;
48
48
public event Action onLogout ;
49
49
public event Action < bool > onMFASetup ;
50
+ public event Action < bool > onManageMFA ;
50
51
public event Action < SignResponse > onSignResponse ;
51
52
52
53
private static SignResponse signResponse = null ;
@@ -147,6 +148,9 @@ public async void setOptions(Web3AuthOptions web3AuthOptions)
147
148
if ( this . web3AuthOptions . sessionTime != null )
148
149
this . initParams [ "sessionTime" ] = this . web3AuthOptions . sessionTime ;
149
150
151
+ if ( this . web3AuthOptions . dashboardUrl != null )
152
+ this . initParams [ "dashboardUrl" ] = this . web3AuthOptions . dashboardUrl ;
153
+
150
154
}
151
155
}
152
156
@@ -281,23 +285,41 @@ private async void processRequest(string path, LoginParams loginParams = null)
281
285
282
286
loginParams . redirectUrl = loginParams . redirectUrl ?? new Uri ( this . initParams [ "redirectUrl" ] . ToString ( ) ) ;
283
287
//Debug.Log("loginParams.redirectUrl: =>" + loginParams.redirectUrl);
288
+ var sessionId = KeyStoreManagerUtils . generateRandomSessionKey ( ) ;
289
+ if ( path == "manage_mfa" ) {
290
+ loginParams . dappUrl = this . initParams [ "redirectUrl" ] . ToString ( ) ;
291
+ loginParams . redirectUrl = new Uri ( this . initParams [ "dashboardUrl" ] . ToString ( ) ) ;
292
+ this . initParams [ "redirectUrl" ] = new Uri ( this . initParams [ "dashboardUrl" ] . ToString ( ) ) ;
293
+ var loginIdObject = new Dictionary < string , string >
294
+ {
295
+ { "loginId" , sessionId } ,
296
+ { "platform" , "unity" } ,
297
+ } ;
298
+ string loginIdBase64 = Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( JsonConvert . SerializeObject ( loginIdObject , Formatting . None ,
299
+ new JsonSerializerSettings
300
+ {
301
+ NullValueHandling = NullValueHandling . Ignore
302
+ } ) ) ) ;
303
+ loginParams . appState = loginIdBase64 ;
304
+ }
305
+
284
306
Dictionary < string , object > paramMap = new Dictionary < string , object > ( ) ;
285
307
paramMap [ "options" ] = this . initParams ;
286
308
paramMap [ "params" ] = loginParams == null ? ( object ) new Dictionary < string , object > ( ) : ( object ) loginParams ;
287
309
paramMap [ "actionType" ] = path ;
288
310
289
- if ( path == "enable_mfa" )
311
+ if ( path == "enable_mfa" || path == "manage_mfa" )
290
312
{
291
- string sessionId = KeyStoreManagerUtils . getPreferencesData ( KeyStoreManagerUtils . SESSION_ID ) ;
292
- paramMap [ "sessionId" ] = sessionId ;
313
+ string savedSessionId = KeyStoreManagerUtils . getPreferencesData ( KeyStoreManagerUtils . SESSION_ID ) ;
314
+ paramMap [ "sessionId" ] = savedSessionId ;
293
315
}
294
316
295
317
//Debug.Log("paramMap: =>" + JsonConvert.SerializeObject(paramMap));
296
318
string loginId = await createSession ( JsonConvert . SerializeObject ( paramMap , Formatting . None ,
297
319
new JsonSerializerSettings
298
320
{
299
321
NullValueHandling = NullValueHandling . Ignore
300
- } ) , 600 , "*" ) ;
322
+ } ) , 600 , "*" , sessionId ) ;
301
323
302
324
if ( ! string . IsNullOrEmpty ( loginId ) )
303
325
{
@@ -353,11 +375,12 @@ public async void launchWalletServices(ChainConfig chainConfig, string path = "w
353
375
paramMap [ "options" ] = this . initParams ;
354
376
355
377
//Debug.Log("paramMap: =>" + JsonConvert.SerializeObject(paramMap));
378
+ var newSessionId = KeyStoreManagerUtils . generateRandomSessionKey ( ) ;
356
379
string loginId = await createSession ( JsonConvert . SerializeObject ( paramMap , Formatting . None ,
357
380
new JsonSerializerSettings
358
381
{
359
382
NullValueHandling = NullValueHandling . Ignore
360
- } ) , 600 , "*" ) ;
383
+ } ) , 600 , "*" , newSessionId ) ;
361
384
362
385
if ( ! string . IsNullOrEmpty ( loginId ) )
363
386
{
@@ -417,6 +440,15 @@ public void setResultUrl(Uri uri)
417
440
Uri newUri = new Uri ( newUriString ) ;
418
441
string b64Params = getQueryParamValue ( newUri , "b64Params" ) ;
419
442
string decodedString = decodeBase64Params ( b64Params ) ;
443
+ if ( decodedString . Contains ( "actionType" ) )
444
+ {
445
+ RedirectResponse response = JsonUtility . FromJson < RedirectResponse > ( decodedString ) ;
446
+ if ( response . actionType == "manage_mfa" )
447
+ {
448
+ this . Enqueue ( ( ) => this . onManageMFA ? . Invoke ( true ) ) ;
449
+ return ;
450
+ }
451
+ }
420
452
if ( isRequestResponse ) {
421
453
try
422
454
{
@@ -549,6 +581,32 @@ public void enableMFA(LoginParams loginParams)
549
581
}
550
582
}
551
583
584
+ public void manageMFA ( LoginParams loginParams )
585
+ {
586
+ if ( web3AuthResponse . userInfo . isMfaEnabled == false )
587
+ {
588
+ throw new Exception ( "MFA is not enabled. Please enable MFA first." ) ;
589
+ }
590
+ string sessionId = KeyStoreManagerUtils . getPreferencesData ( KeyStoreManagerUtils . SESSION_ID ) ;
591
+ if ( ! string . IsNullOrEmpty ( sessionId ) )
592
+ {
593
+ if ( web3AuthOptions . loginConfig != null )
594
+ {
595
+ var loginConfigItem = web3AuthOptions . loginConfig ? . Values . First ( ) ;
596
+ var share = KeyStoreManagerUtils . getPreferencesData ( loginConfigItem ? . verifier ) ;
597
+ if ( ! string . IsNullOrEmpty ( share ) )
598
+ {
599
+ loginParams . dappShare = share ;
600
+ }
601
+ }
602
+ processRequest ( "manage_mfa" , loginParams ) ;
603
+ }
604
+ else
605
+ {
606
+ throw new Exception ( "SessionId not found. Please login first." ) ;
607
+ }
608
+ }
609
+
552
610
public async void request ( ChainConfig chainConfig , string method , JArray requestParams , string path = "wallet/request" ) {
553
611
string sessionId = KeyStoreManagerUtils . getPreferencesData ( KeyStoreManagerUtils . SESSION_ID ) ;
554
612
if ( ! string . IsNullOrEmpty ( sessionId ) )
@@ -569,11 +627,12 @@ public async void request(ChainConfig chainConfig, string method, JArray request
569
627
Dictionary < string , object > paramMap = new Dictionary < string , object > ( ) ;
570
628
paramMap [ "options" ] = this . initParams ;
571
629
630
+ var newSessionId = KeyStoreManagerUtils . generateRandomSessionKey ( ) ;
572
631
string loginId = await createSession ( JsonConvert . SerializeObject ( paramMap , Formatting . None ,
573
632
new JsonSerializerSettings
574
633
{
575
634
NullValueHandling = NullValueHandling . Ignore
576
- } ) , 600 , "*" ) ;
635
+ } ) , 600 , "*" , newSessionId ) ;
577
636
578
637
if ( ! string . IsNullOrEmpty ( loginId ) )
579
638
{
@@ -627,7 +686,7 @@ private void authorizeSession(string newSessionId, string origin)
627
686
if ( string . IsNullOrEmpty ( newSessionId ) )
628
687
{
629
688
sessionId = KeyStoreManagerUtils . getPreferencesData ( KeyStoreManagerUtils . SESSION_ID ) ;
630
- // Debug.Log("sessionId during authorizeSession in if part =>" + sessionId);
689
+ Debug . Log ( "sessionId during authorizeSession in if part =>" + sessionId ) ;
631
690
}
632
691
else
633
692
{
@@ -750,10 +809,10 @@ private void sessionTimeOutAPI()
750
809
}
751
810
}
752
811
753
- private async Task < string > createSession ( string data , long sessionTime , string allowedOrigin )
812
+ private async Task < string > createSession ( string data , long sessionTime , string allowedOrigin , string sessionId )
754
813
{
755
814
TaskCompletionSource < string > createSessionResponse = new TaskCompletionSource < string > ( ) ;
756
- var newSessionKey = KeyStoreManagerUtils . generateRandomSessionKey ( ) ;
815
+ var newSessionKey = sessionId ;
757
816
// Debug.Log("newSessionKey =>" + newSessionKey);
758
817
var ephemKey = KeyStoreManagerUtils . getPubKey ( newSessionKey ) ;
759
818
var ivKey = KeyStoreManagerUtils . generateRandomBytes ( ) ;
0 commit comments