Skip to content

Commit

Permalink
GetUserAuthDetails (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFirekeeper authored Dec 25, 2024
1 parent 22ffb86 commit 3025488
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Thirdweb.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@

#region InAppWallet - OAuth

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

// var inAppWalletAuthDetails = inAppWalletOAuth.GetUserAuthDetails();
// Console.WriteLine($"InAppWallet OAuth auth details: {JsonConvert.SerializeObject(inAppWalletAuthDetails, Formatting.Indented)}");

#endregion

#region Smart Wallet - Gasless Transaction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Nethereum.Signer;
using Nethereum.Signer.EIP712;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Thirdweb.EWS;

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

/// <summary>
/// Gets the user auth details from the corresponding auth provider.
/// </summary>
/// <returns>The user auth details as a JObject</returns>
public JObject GetUserAuthDetails()
{
var authToken = this.EmbeddedWallet.GetSessionData()?.AuthToken;
if (string.IsNullOrEmpty(authToken))
{
throw new InvalidOperationException("Cannot get user auth details without an active session.");
}

var parts = authToken.Split('.');
if (parts.Length != 3)
{
Console.WriteLine("Invalid JWT");
}

static string Base64UrlDecode(string input)
{
var paddedInput = input.Replace('-', '+').Replace('_', '/');
switch (paddedInput.Length % 4)
{
case 2:
paddedInput += "==";
break;
case 3:
paddedInput += "=";
break;
default:
break;
}
var decodedBytes = Convert.FromBase64String(paddedInput);
return Encoding.UTF8.GetString(decodedBytes);
}

var payload = JObject.Parse(Base64UrlDecode(parts[1]));
var jwtToken = payload["storedToken"]?["jwtToken"]?.ToString();

parts = jwtToken.Split('.');
if (parts.Length != 3)
{
Console.WriteLine("Invalid JWT");
}

payload = JObject.Parse(Base64UrlDecode(parts[1]));
return payload;
}

[Obsolete("Use GetUserDetails instead.")]
public string GetEmail()
{
Expand Down

0 comments on commit 3025488

Please sign in to comment.