Skip to content

Commit d6a4f2b

Browse files
Merge pull request #42 from Web3Auth/feat/manage_mfa
feat: manageMFA functionality
2 parents d710f21 + 372e558 commit d6a4f2b

File tree

6 files changed

+128
-11
lines changed

6 files changed

+128
-11
lines changed

Assets/Plugins/Web3AuthSDK/Samples/Web3AuthSample.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ void Start()
104104
web3Auth.onLogout += onLogout;
105105
web3Auth.onMFASetup += onMFASetup;
106106
web3Auth.onSignResponse += onSignResponse;
107+
web3Auth.onManageMFA += onManageMFA;
107108

108109
emailAddressField.gameObject.SetActive(false);
109110
logoutButton.gameObject.SetActive(false);
@@ -117,6 +118,7 @@ void Start()
117118
mfaSetupButton.onClick.AddListener(enableMFA);
118119
launchWalletServicesButton.onClick.AddListener(launchWalletServices);
119120
signMessageButton.onClick.AddListener(request);
121+
signResponseButton.onClick.AddListener(manageMFA);
120122

121123
verifierDropdown.AddOptions(verifierList.Select(x => x.name).ToList());
122124
verifierDropdown.onValueChanged.AddListener(onVerifierDropDownChange);
@@ -160,6 +162,10 @@ private void onSignResponse(SignResponse signResponse)
160162
Debug.Log("Retrieved SignResponse: " + signResponse);
161163
}
162164

165+
private void onManageMFA(bool response) {
166+
Debug.Log("Manage MFA: " + response);
167+
}
168+
163169
private void onVerifierDropDownChange(int selectedIndex)
164170
{
165171
if (verifierList[selectedIndex].loginProvider == Provider.EMAIL_PASSWORDLESS)
@@ -220,6 +226,26 @@ private void enableMFA()
220226
web3Auth.enableMFA(options);
221227
}
222228

229+
private void manageMFA()
230+
{
231+
var selectedProvider = verifierList[verifierDropdown.value].loginProvider;
232+
233+
var options = new LoginParams()
234+
{
235+
loginProvider = selectedProvider,
236+
mfaLevel = MFALevel.MANDATORY
237+
};
238+
239+
if (selectedProvider == Provider.EMAIL_PASSWORDLESS)
240+
{
241+
options.extraLoginOptions = new ExtraLoginOptions()
242+
{
243+
login_hint = emailAddressField.text
244+
};
245+
}
246+
web3Auth.manageMFA(options);
247+
}
248+
223249
private void launchWalletServices() {
224250
var selectedProvider = verifierList[verifierDropdown.value].loginProvider;
225251

Assets/Plugins/Web3AuthSDK/Samples/Web3AuthSample.unity

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ GameObject:
642642
- component: {fileID: 311891818}
643643
- component: {fileID: 311891817}
644644
m_Layer: 5
645-
m_Name: Sign Response
645+
m_Name: Mange MFA
646646
m_TagString: Untagged
647647
m_Icon: {fileID: 0}
648648
m_NavMeshLayer: 0
@@ -1389,7 +1389,7 @@ MonoBehaviour:
13891389
m_HorizontalOverflow: 0
13901390
m_VerticalOverflow: 0
13911391
m_LineSpacing: 1
1392-
m_Text: Get Sign response
1392+
m_Text: Manage MFA
13931393
--- !u!222 &537563201
13941394
CanvasRenderer:
13951395
m_ObjectHideFlags: 0
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public class RedirectResponse
2+
{
3+
public string actionType;
4+
}

Assets/Plugins/Web3AuthSDK/Types/RedirectResponse.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Plugins/Web3AuthSDK/Types/Web3AuthOptions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,21 @@ public string walletSdkUrl {
3939
public int sessionTime { get; set; } = 86400;
4040
public ChainConfig? chainConfig { get; set; }
4141
public Dictionary<string, string> originData { get; set; } = null;
42+
43+
public string dashboardUrl
44+
{
45+
get
46+
{
47+
return buildEnv switch
48+
{
49+
Web3Auth.BuildEnv.STAGING => $"https://staging-account.web3auth.io/{authDashboardVersion}/{walletAccountConstant}",
50+
Web3Auth.BuildEnv.TESTING => $"https://develop-account.web3auth.io/{walletAccountConstant}",
51+
_ => $"https://account.web3auth.io/{authDashboardVersion}/{walletAccountConstant}"
52+
};
53+
}
54+
set { }
55+
}
56+
57+
private const string authDashboardVersion = "v9";
58+
private const string walletAccountConstant = "wallet/account";
4259
}

Assets/Plugins/Web3AuthSDK/Web3Auth.cs

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public enum Language
4747
public event Action<Web3AuthResponse> onLogin;
4848
public event Action onLogout;
4949
public event Action<bool> onMFASetup;
50+
public event Action<bool> onManageMFA;
5051
public event Action<SignResponse> onSignResponse;
5152

5253
private static SignResponse signResponse = null;
@@ -147,6 +148,9 @@ public async void setOptions(Web3AuthOptions web3AuthOptions)
147148
if (this.web3AuthOptions.sessionTime != null)
148149
this.initParams["sessionTime"] = this.web3AuthOptions.sessionTime;
149150

151+
if (this.web3AuthOptions.dashboardUrl != null)
152+
this.initParams["dashboardUrl"] = this.web3AuthOptions.dashboardUrl;
153+
150154
}
151155
}
152156

@@ -281,23 +285,41 @@ private async void processRequest(string path, LoginParams loginParams = null)
281285

282286
loginParams.redirectUrl = loginParams.redirectUrl ?? new Uri(this.initParams["redirectUrl"].ToString());
283287
//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+
284306
Dictionary<string, object> paramMap = new Dictionary<string, object>();
285307
paramMap["options"] = this.initParams;
286308
paramMap["params"] = loginParams == null ? (object)new Dictionary<string, object>() : (object)loginParams;
287309
paramMap["actionType"] = path;
288310

289-
if (path == "enable_mfa")
311+
if (path == "enable_mfa" || path == "manage_mfa")
290312
{
291-
string sessionId = KeyStoreManagerUtils.getPreferencesData(KeyStoreManagerUtils.SESSION_ID);
292-
paramMap["sessionId"] = sessionId;
313+
string savedSessionId = KeyStoreManagerUtils.getPreferencesData(KeyStoreManagerUtils.SESSION_ID);
314+
paramMap["sessionId"] = savedSessionId;
293315
}
294316

295317
//Debug.Log("paramMap: =>" + JsonConvert.SerializeObject(paramMap));
296318
string loginId = await createSession(JsonConvert.SerializeObject(paramMap, Formatting.None,
297319
new JsonSerializerSettings
298320
{
299321
NullValueHandling = NullValueHandling.Ignore
300-
}), 600, "*");
322+
}), 600, "*", sessionId);
301323

302324
if (!string.IsNullOrEmpty(loginId))
303325
{
@@ -353,11 +375,12 @@ public async void launchWalletServices(ChainConfig chainConfig, string path = "w
353375
paramMap["options"] = this.initParams;
354376

355377
//Debug.Log("paramMap: =>" + JsonConvert.SerializeObject(paramMap));
378+
var newSessionId = KeyStoreManagerUtils.generateRandomSessionKey();
356379
string loginId = await createSession(JsonConvert.SerializeObject(paramMap, Formatting.None,
357380
new JsonSerializerSettings
358381
{
359382
NullValueHandling = NullValueHandling.Ignore
360-
}), 600, "*");
383+
}), 600, "*", newSessionId);
361384

362385
if (!string.IsNullOrEmpty(loginId))
363386
{
@@ -417,6 +440,15 @@ public void setResultUrl(Uri uri)
417440
Uri newUri = new Uri(newUriString);
418441
string b64Params = getQueryParamValue(newUri, "b64Params");
419442
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+
}
420452
if(isRequestResponse) {
421453
try
422454
{
@@ -549,6 +581,32 @@ public void enableMFA(LoginParams loginParams)
549581
}
550582
}
551583

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+
552610
public async void request(ChainConfig chainConfig, string method, JArray requestParams, string path = "wallet/request") {
553611
string sessionId = KeyStoreManagerUtils.getPreferencesData(KeyStoreManagerUtils.SESSION_ID);
554612
if (!string.IsNullOrEmpty(sessionId))
@@ -569,11 +627,12 @@ public async void request(ChainConfig chainConfig, string method, JArray request
569627
Dictionary<string, object> paramMap = new Dictionary<string, object>();
570628
paramMap["options"] = this.initParams;
571629

630+
var newSessionId = KeyStoreManagerUtils.generateRandomSessionKey();
572631
string loginId = await createSession(JsonConvert.SerializeObject(paramMap, Formatting.None,
573632
new JsonSerializerSettings
574633
{
575634
NullValueHandling = NullValueHandling.Ignore
576-
}), 600, "*");
635+
}), 600, "*", newSessionId);
577636

578637
if (!string.IsNullOrEmpty(loginId))
579638
{
@@ -627,7 +686,7 @@ private void authorizeSession(string newSessionId, string origin)
627686
if (string.IsNullOrEmpty(newSessionId))
628687
{
629688
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);
631690
}
632691
else
633692
{
@@ -750,10 +809,10 @@ private void sessionTimeOutAPI()
750809
}
751810
}
752811

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)
754813
{
755814
TaskCompletionSource<string> createSessionResponse = new TaskCompletionSource<string>();
756-
var newSessionKey = KeyStoreManagerUtils.generateRandomSessionKey();
815+
var newSessionKey = sessionId;
757816
// Debug.Log("newSessionKey =>" + newSessionKey);
758817
var ephemKey = KeyStoreManagerUtils.getPubKey(newSessionKey);
759818
var ivKey = KeyStoreManagerUtils.generateRandomBytes();

0 commit comments

Comments
 (0)