Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetUserAuthDetails #110

Merged
merged 1 commit into from
Dec 25, 2024
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
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
Loading