Skip to main content

TOTP setup

Add a TOTP device

var result = await DynamicSDK.Instance.Mfa.AddDevice("totp");

// result.Secret — base32 secret for manual entry
// result.Uri    — otpauth:// URI for QR code

Debug.Log($"TOTP Secret: {result.Secret}");
Debug.Log($"TOTP URI: {result.Uri}");

Verify TOTP device

After the user enters a code from their authenticator app:
await DynamicSDK.Instance.Mfa.VerifyDevice(code, "totp");

TOTP authentication

Authenticate with a TOTP code:
string mfaToken = await DynamicSDK.Instance.Mfa.AuthenticateDevice(
    new MfaAuthenticateDevice
    {
        Code = "123456",
        Type = "totp",
        DeviceId = deviceId,
        CreateMfaToken = new MfaCreateToken { SingleUse = true },
    });

Recovery codes

Generate recovery codes for account recovery:
List<string> codes = await DynamicSDK.Instance.Mfa.GetNewRecoveryCodes();

foreach (var code in codes)
{
    Debug.Log($"Recovery code: {code}");
}
Store recovery codes securely. They can be used to recover the account if the user loses access to their TOTP device.

Complete example

using DynamicSDK.Core;
using UnityEngine;
using System.Collections.Generic;

public class MfaManager : MonoBehaviour
{
    private string _deviceId;
    
    public async void SetupTotp()
    {
        try
        {
            var result = await DynamicSDK.Instance.Mfa.AddDevice("totp");
            
            Debug.Log("TOTP Setup:");
            Debug.Log($"Secret: {result.Secret}");
            Debug.Log($"URI (for QR code): {result.Uri}");
            
            // Store device ID for later use
            _deviceId = result.DeviceId;
            
            // Display QR code to user (use result.Uri)
            // User should scan with authenticator app
        }
        catch (System.Exception ex)
        {
            Debug.LogError($"TOTP setup failed: {ex.Message}");
        }
    }
    
    public async void VerifyTotp(string code)
    {
        try
        {
            await DynamicSDK.Instance.Mfa.VerifyDevice(code, "totp");
            Debug.Log("TOTP device verified successfully!");
        }
        catch (System.Exception ex)
        {
            Debug.LogError($"Verification failed: {ex.Message}");
        }
    }
    
    public async void AuthenticateWithTotp(string code)
    {
        try
        {
            string mfaToken = await DynamicSDK.Instance.Mfa.AuthenticateDevice(
                new MfaAuthenticateDevice
                {
                    Code = code,
                    Type = "totp",
                    DeviceId = _deviceId,
                    CreateMfaToken = new MfaCreateToken { SingleUse = true },
                });
            
            Debug.Log($"MFA authentication successful");
            Debug.Log($"Token: {mfaToken}");
        }
        catch (System.Exception ex)
        {
            Debug.LogError($"MFA authentication failed: {ex.Message}");
        }
    }
    
    public async void GenerateRecoveryCodes()
    {
        try
        {
            List<string> codes = await DynamicSDK.Instance.Mfa.GetNewRecoveryCodes();
            
            Debug.Log("Recovery codes generated:");
            foreach (var code in codes)
            {
                Debug.Log($"  {code}");
            }
            
            // Display codes to user and prompt them to save securely
        }
        catch (System.Exception ex)
        {
            Debug.LogError($"Failed to generate recovery codes: {ex.Message}");
        }
    }
}

Next steps