Skip to content

Commit 3025488

Browse files
authored
GetUserAuthDetails (#110)
1 parent 22ffb86 commit 3025488

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

Thirdweb.Console/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@
455455

456456
#region InAppWallet - OAuth
457457

458-
// var inAppWalletOAuth = await InAppWallet.Create(client: client, authProvider: AuthProvider.Steam);
458+
// var inAppWalletOAuth = await InAppWallet.Create(client: client, authProvider: AuthProvider.Github);
459459
// if (!await inAppWalletOAuth.IsConnected())
460460
// {
461461
// _ = await inAppWalletOAuth.LoginWithOauth(
@@ -472,6 +472,9 @@
472472
// var inAppWalletOAuthAddress = await inAppWalletOAuth.GetAddress();
473473
// Console.WriteLine($"InAppWallet OAuth address: {inAppWalletOAuthAddress}");
474474

475+
// var inAppWalletAuthDetails = inAppWalletOAuth.GetUserAuthDetails();
476+
// Console.WriteLine($"InAppWallet OAuth auth details: {JsonConvert.SerializeObject(inAppWalletAuthDetails, Formatting.Indented)}");
477+
475478
#endregion
476479

477480
#region Smart Wallet - Gasless Transaction

Thirdweb/Thirdweb.Wallets/InAppWallet/EcosystemWallet/EcosystemWallet.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Nethereum.Signer;
66
using Nethereum.Signer.EIP712;
77
using Newtonsoft.Json;
8+
using Newtonsoft.Json.Linq;
89
using Thirdweb.EWS;
910

1011
namespace Thirdweb;
@@ -292,6 +293,55 @@ public async Task<UserStatusResponse> GetUserDetails()
292293
return await GetUserStatus(this.HttpClient).ConfigureAwait(false);
293294
}
294295

296+
/// <summary>
297+
/// Gets the user auth details from the corresponding auth provider.
298+
/// </summary>
299+
/// <returns>The user auth details as a JObject</returns>
300+
public JObject GetUserAuthDetails()
301+
{
302+
var authToken = this.EmbeddedWallet.GetSessionData()?.AuthToken;
303+
if (string.IsNullOrEmpty(authToken))
304+
{
305+
throw new InvalidOperationException("Cannot get user auth details without an active session.");
306+
}
307+
308+
var parts = authToken.Split('.');
309+
if (parts.Length != 3)
310+
{
311+
Console.WriteLine("Invalid JWT");
312+
}
313+
314+
static string Base64UrlDecode(string input)
315+
{
316+
var paddedInput = input.Replace('-', '+').Replace('_', '/');
317+
switch (paddedInput.Length % 4)
318+
{
319+
case 2:
320+
paddedInput += "==";
321+
break;
322+
case 3:
323+
paddedInput += "=";
324+
break;
325+
default:
326+
break;
327+
}
328+
var decodedBytes = Convert.FromBase64String(paddedInput);
329+
return Encoding.UTF8.GetString(decodedBytes);
330+
}
331+
332+
var payload = JObject.Parse(Base64UrlDecode(parts[1]));
333+
var jwtToken = payload["storedToken"]?["jwtToken"]?.ToString();
334+
335+
parts = jwtToken.Split('.');
336+
if (parts.Length != 3)
337+
{
338+
Console.WriteLine("Invalid JWT");
339+
}
340+
341+
payload = JObject.Parse(Base64UrlDecode(parts[1]));
342+
return payload;
343+
}
344+
295345
[Obsolete("Use GetUserDetails instead.")]
296346
public string GetEmail()
297347
{

0 commit comments

Comments
 (0)