Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Assets/Plugins/Web3AuthSDK/Samples/Web3AuthSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ void Start()
web3Auth.onLogout += onLogout;
web3Auth.onMFASetup += onMFASetup;
web3Auth.onSignResponse += onSignResponse;
web3Auth.onManageMFA += onManageMFA;

emailAddressField.gameObject.SetActive(false);
logoutButton.gameObject.SetActive(false);
Expand All @@ -117,6 +118,7 @@ void Start()
mfaSetupButton.onClick.AddListener(enableMFA);
launchWalletServicesButton.onClick.AddListener(launchWalletServices);
signMessageButton.onClick.AddListener(request);
signResponseButton.onClick.AddListener(manageMFA);

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

private void onManageMFA(bool response) {
Debug.Log("Manage MFA: " + response);
}

private void onVerifierDropDownChange(int selectedIndex)
{
if (verifierList[selectedIndex].loginProvider == Provider.EMAIL_PASSWORDLESS)
Expand Down Expand Up @@ -220,6 +226,26 @@ private void enableMFA()
web3Auth.enableMFA(options);
}

private void manageMFA()
{
var selectedProvider = verifierList[verifierDropdown.value].loginProvider;

var options = new LoginParams()
{
loginProvider = selectedProvider,
mfaLevel = MFALevel.MANDATORY
};

if (selectedProvider == Provider.EMAIL_PASSWORDLESS)
{
options.extraLoginOptions = new ExtraLoginOptions()
{
login_hint = emailAddressField.text
};
}
web3Auth.manageMFA(options);
}

private void launchWalletServices() {
var selectedProvider = verifierList[verifierDropdown.value].loginProvider;

Expand Down
4 changes: 2 additions & 2 deletions Assets/Plugins/Web3AuthSDK/Samples/Web3AuthSample.unity
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ GameObject:
- component: {fileID: 311891818}
- component: {fileID: 311891817}
m_Layer: 5
m_Name: Sign Response
m_Name: Mange MFA
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
Expand Down Expand Up @@ -1389,7 +1389,7 @@ MonoBehaviour:
m_HorizontalOverflow: 0
m_VerticalOverflow: 0
m_LineSpacing: 1
m_Text: Get Sign response
m_Text: Manage MFA
--- !u!222 &537563201
CanvasRenderer:
m_ObjectHideFlags: 0
Expand Down
4 changes: 4 additions & 0 deletions Assets/Plugins/Web3AuthSDK/Types/RedirectResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public class RedirectResponse
{
public string actionType;
}
11 changes: 11 additions & 0 deletions Assets/Plugins/Web3AuthSDK/Types/RedirectResponse.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions Assets/Plugins/Web3AuthSDK/Types/Web3AuthOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,21 @@ public string walletSdkUrl {
public int sessionTime { get; set; } = 86400;
public ChainConfig? chainConfig { get; set; }
public Dictionary<string, string> originData { get; set; } = null;

public string dashboardUrl
{
get
{
return buildEnv switch
{
Web3Auth.BuildEnv.STAGING => $"https://staging-account.web3auth.io/{authDashboardVersion}/{walletAccountConstant}",
Web3Auth.BuildEnv.TESTING => $"https://develop-account.web3auth.io/{walletAccountConstant}",
_ => $"https://account.web3auth.io/{authDashboardVersion}/{walletAccountConstant}"
};
}
set { }
}

private const string authDashboardVersion = "v9";
private const string walletAccountConstant = "wallet/account";
}
77 changes: 68 additions & 9 deletions Assets/Plugins/Web3AuthSDK/Web3Auth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public enum Language
public event Action<Web3AuthResponse> onLogin;
public event Action onLogout;
public event Action<bool> onMFASetup;
public event Action<bool> onManageMFA;
public event Action<SignResponse> onSignResponse;

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

if (this.web3AuthOptions.dashboardUrl != null)
this.initParams["dashboardUrl"] = this.web3AuthOptions.dashboardUrl;

}
}

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

loginParams.redirectUrl = loginParams.redirectUrl ?? new Uri(this.initParams["redirectUrl"].ToString());
//Debug.Log("loginParams.redirectUrl: =>" + loginParams.redirectUrl);
var sessionId = KeyStoreManagerUtils.generateRandomSessionKey();
if(path == "manage_mfa") {
loginParams.dappUrl = this.initParams["redirectUrl"].ToString();
loginParams.redirectUrl = new Uri(this.initParams["dashboardUrl"].ToString());
this.initParams["redirectUrl"] = new Uri(this.initParams["dashboardUrl"].ToString());
var loginIdObject = new Dictionary<string, string>
{
{ "loginId", sessionId },
{ "platform", "unity" },
};
string loginIdBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(loginIdObject, Formatting.None,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
})));
loginParams.appState = loginIdBase64;
}

Dictionary<string, object> paramMap = new Dictionary<string, object>();
paramMap["options"] = this.initParams;
paramMap["params"] = loginParams == null ? (object)new Dictionary<string, object>() : (object)loginParams;
paramMap["actionType"] = path;

if (path == "enable_mfa")
if (path == "enable_mfa" || path == "manage_mfa")
{
string sessionId = KeyStoreManagerUtils.getPreferencesData(KeyStoreManagerUtils.SESSION_ID);
paramMap["sessionId"] = sessionId;
string savedSessionId = KeyStoreManagerUtils.getPreferencesData(KeyStoreManagerUtils.SESSION_ID);
paramMap["sessionId"] = savedSessionId;
}

//Debug.Log("paramMap: =>" + JsonConvert.SerializeObject(paramMap));
string loginId = await createSession(JsonConvert.SerializeObject(paramMap, Formatting.None,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
}), 600, "*");
}), 600, "*", sessionId);

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

//Debug.Log("paramMap: =>" + JsonConvert.SerializeObject(paramMap));
var newSessionId = KeyStoreManagerUtils.generateRandomSessionKey();
string loginId = await createSession(JsonConvert.SerializeObject(paramMap, Formatting.None,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
}), 600, "*");
}), 600, "*", newSessionId);

if (!string.IsNullOrEmpty(loginId))
{
Expand Down Expand Up @@ -417,6 +440,15 @@ public void setResultUrl(Uri uri)
Uri newUri = new Uri(newUriString);
string b64Params = getQueryParamValue(newUri, "b64Params");
string decodedString = decodeBase64Params(b64Params);
if (decodedString.Contains("actionType"))
{
RedirectResponse response = JsonUtility.FromJson<RedirectResponse>(decodedString);
if (response.actionType == "manage_mfa")
{
this.Enqueue(() => this.onManageMFA?.Invoke(true));
return;
}
}
if(isRequestResponse) {
try
{
Expand Down Expand Up @@ -549,6 +581,32 @@ public void enableMFA(LoginParams loginParams)
}
}

public void manageMFA(LoginParams loginParams)
{
if(web3AuthResponse.userInfo.isMfaEnabled == false)
{
throw new Exception("MFA is not enabled. Please enable MFA first.");
}
string sessionId = KeyStoreManagerUtils.getPreferencesData(KeyStoreManagerUtils.SESSION_ID);
if (!string.IsNullOrEmpty(sessionId))
{
if (web3AuthOptions.loginConfig != null)
{
var loginConfigItem = web3AuthOptions.loginConfig?.Values.First();
var share = KeyStoreManagerUtils.getPreferencesData(loginConfigItem?.verifier);
if (!string.IsNullOrEmpty(share))
{
loginParams.dappShare = share;
}
}
processRequest("manage_mfa", loginParams);
}
else
{
throw new Exception("SessionId not found. Please login first.");
}
}

public async void request(ChainConfig chainConfig, string method, JArray requestParams, string path = "wallet/request") {
string sessionId = KeyStoreManagerUtils.getPreferencesData(KeyStoreManagerUtils.SESSION_ID);
if (!string.IsNullOrEmpty(sessionId))
Expand All @@ -569,11 +627,12 @@ public async void request(ChainConfig chainConfig, string method, JArray request
Dictionary<string, object> paramMap = new Dictionary<string, object>();
paramMap["options"] = this.initParams;

var newSessionId = KeyStoreManagerUtils.generateRandomSessionKey();
string loginId = await createSession(JsonConvert.SerializeObject(paramMap, Formatting.None,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
}), 600, "*");
}), 600, "*", newSessionId);

if (!string.IsNullOrEmpty(loginId))
{
Expand Down Expand Up @@ -627,7 +686,7 @@ private void authorizeSession(string newSessionId, string origin)
if (string.IsNullOrEmpty(newSessionId))
{
sessionId = KeyStoreManagerUtils.getPreferencesData(KeyStoreManagerUtils.SESSION_ID);
// Debug.Log("sessionId during authorizeSession in if part =>" + sessionId);
Debug.Log("sessionId during authorizeSession in if part =>" + sessionId);
}
else
{
Expand Down Expand Up @@ -750,10 +809,10 @@ private void sessionTimeOutAPI()
}
}

private async Task<string> createSession(string data, long sessionTime, string allowedOrigin)
private async Task<string> createSession(string data, long sessionTime, string allowedOrigin, string sessionId)
{
TaskCompletionSource<string> createSessionResponse = new TaskCompletionSource<string>();
var newSessionKey = KeyStoreManagerUtils.generateRandomSessionKey();
var newSessionKey = sessionId;
// Debug.Log("newSessionKey =>" + newSessionKey);
var ephemKey = KeyStoreManagerUtils.getPubKey(newSessionKey);
var ivKey = KeyStoreManagerUtils.generateRandomBytes();
Expand Down