Skip to content

Commit 38557b2

Browse files
authored
[EWS] Integrate non-OIDC authentication, enforce encryptionKey (#136)
1 parent 2fb1f4f commit 38557b2

File tree

5 files changed

+66400
-64467
lines changed

5 files changed

+66400
-64467
lines changed

Assets/Thirdweb/Core/Scripts/Wallet.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ public WalletConnection(
708708
this.password = password;
709709
this.email = email;
710710
this.personalWallet = personalWallet;
711-
this.authOptions = authOptions ?? new AuthOptions(authProvider: AuthProvider.EmailOTP, authToken: null);
711+
this.authOptions = authOptions ?? new AuthOptions(authProvider: AuthProvider.EmailOTP, jwtOrPayload: null, encryptionKey: null);
712712
this.smartWalletAccountOverride = smartWalletAccountOverride;
713713
}
714714
}
@@ -720,21 +720,21 @@ public WalletConnection(
720720
public class AuthOptions
721721
{
722722
public AuthProvider authProvider;
723-
public string authToken;
724-
public string recoveryCode;
723+
public string jwtOrPayload;
724+
public string encryptionKey;
725725

726726
/// <summary>
727727
/// Initializes a new instance of the <see cref="AuthOptions"/> class with the specified parameters.
728728
/// </summary>
729729
/// <param name="authProvider">The authentication provider to use.</param>
730-
/// <param name="authToken">The auth token to use for validation e.g. jwt</param>
731-
/// <param name="recoveryCode">The recovery code used for CustomAuth when recovery is User Managed</param>
730+
/// <param name="jwtOrPayload">Used for custom JWT or AuthEndpoint methods, pass JWT or auth payload respectively.</param>
731+
/// <param name="encryptionKey">Used for custom JWT or AuthEndpoint methods, developer-manaed recovery encryption key.</param>
732732
/// <returns>A new instance of the <see cref="AuthOptions"/> class.</returns>
733-
public AuthOptions(AuthProvider authProvider, string authToken = null, string recoveryCode = null)
733+
public AuthOptions(AuthProvider authProvider, string jwtOrPayload = null, string encryptionKey = null)
734734
{
735735
this.authProvider = authProvider;
736-
this.authToken = authToken;
737-
this.recoveryCode = recoveryCode;
736+
this.jwtOrPayload = jwtOrPayload;
737+
this.encryptionKey = encryptionKey;
738738
}
739739
}
740740

@@ -781,8 +781,13 @@ public enum AuthProvider
781781
Facebook,
782782

783783
/// <summary>
784-
/// Bring your own auth.
784+
/// JWT-Based Authentication Flow, checks JWT against developer-set JWKS URI.
785785
/// </summary>
786-
CustomAuth
786+
JWT,
787+
788+
/// <summary>
789+
/// Custom Authentication Flow, checks payload against developer-set Auth Endpoint.
790+
/// </summary>
791+
AuthEndpoint,
787792
}
788793
}

Assets/Thirdweb/Core/Scripts/Wallets/ThirdwebEmbeddedWallet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public ThirdwebEmbeddedWallet(string clientId, string bundleId)
2121
_web3 = null;
2222
_provider = WalletProvider.EmbeddedWallet;
2323
_signerProvider = WalletProvider.LocalWallet;
24-
_embeddedWallet = new EmbeddedWallet(clientId, bundleId);
24+
_embeddedWallet = new EmbeddedWallet(clientId, bundleId, "unity", ThirdwebSDK.version);
2525
}
2626

2727
public async Task<string> Connect(WalletConnection walletConnection, string rpc)

Assets/Thirdweb/Core/Scripts/WalletsUI/EmbeddedWalletUI.cs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public async Task<User> Connect(EmbeddedWallet embeddedWallet, string email, Aut
8383
AuthProvider.Google => "Google",
8484
AuthProvider.Apple => "Apple",
8585
AuthProvider.Facebook => "Facebook",
86-
AuthProvider.CustomAuth => "CustomAuth",
86+
AuthProvider.JWT => "CustomAuth",
8787
_ => throw new UnityException($"Unsupported auth provider: {authOptions.authProvider}"),
8888
};
8989
return await _embeddedWallet.GetUserAsync(_email, authProvider);
@@ -109,8 +109,11 @@ public async Task<User> Connect(EmbeddedWallet embeddedWallet, string email, Aut
109109
case AuthProvider.Facebook:
110110
await LoginWithOauth("Facebook");
111111
break;
112-
case AuthProvider.CustomAuth:
113-
await LoginWithCustomJwt(authOptions.authToken, authOptions.recoveryCode);
112+
case AuthProvider.JWT:
113+
await LoginWithJWT(authOptions.jwtOrPayload, authOptions.encryptionKey);
114+
break;
115+
case AuthProvider.AuthEndpoint:
116+
await LoginWithAuthEndpoint(authOptions.jwtOrPayload, authOptions.encryptionKey);
114117
break;
115118
default:
116119
throw new UnityException($"Unsupported auth provider: {authOptions.authProvider}");
@@ -224,7 +227,7 @@ private async Task LoginWithOauth(string authProviderStr)
224227
{
225228
try
226229
{
227-
var res = await _embeddedWallet.SignInWithOauth(authProviderStr, authResultJson, null);
230+
var res = await _embeddedWallet.SignInWithOauthAsync(authProviderStr, authResultJson, null);
228231
_user = res.User;
229232
}
230233
catch (Exception e)
@@ -239,7 +242,7 @@ private async void OnSubmitRecoveryOauth(string authProviderStr, string authResu
239242
try
240243
{
241244
string recoveryCode = RecoveryInput.text;
242-
var res = await _embeddedWallet.SignInWithOauth(authProviderStr, authResult, recoveryCode);
245+
var res = await _embeddedWallet.SignInWithOauthAsync(authProviderStr, authResult, recoveryCode);
243246
_user = res.User;
244247
ShowRecoveryCodes(res);
245248
}
@@ -260,11 +263,32 @@ private async Task<string> GetLoginLink(string authProvider)
260263

261264
#endregion
262265

263-
#region Custom JWT Flow
266+
#region JWT Flow
264267

265-
private async Task LoginWithCustomJwt(string jwtToken, string recoveryCode)
268+
private async Task LoginWithJWT(string jwtToken, string encryptionKey, string recoveryCode = null)
266269
{
267-
var res = await _embeddedWallet.SignInWithJwtAuthAsync(jwtToken, recoveryCode);
270+
if (string.IsNullOrEmpty(jwtToken))
271+
throw new UnityException("JWT token is required for JWT login!");
272+
if (string.IsNullOrEmpty(encryptionKey))
273+
throw new UnityException("Encryption key is required for JWT login!");
274+
275+
var res = await _embeddedWallet.SignInWithJwtAsync(jwtToken, encryptionKey, recoveryCode);
276+
_user = res.User;
277+
ShowRecoveryCodes(res);
278+
}
279+
280+
#endregion
281+
282+
#region Auth Endpoint Flow
283+
284+
private async Task LoginWithAuthEndpoint(string payload, string encryptionKey, string recoveryCode = null)
285+
{
286+
if (string.IsNullOrEmpty(payload))
287+
throw new UnityException("Auth payload is required for Auth Endpoint login!");
288+
if (string.IsNullOrEmpty(encryptionKey))
289+
throw new UnityException("Encryption key is required for Auth Endpoint login!");
290+
291+
var res = await _embeddedWallet.SignInWithAuthEndpointAsync(payload, encryptionKey, recoveryCode);
268292
_user = res.User;
269293
ShowRecoveryCodes(res);
270294
}
Binary file not shown.

0 commit comments

Comments
 (0)