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

Replace JWTDecoder package with System.IdentityModel.Tokens.Jwt #83

Merged
merged 2 commits into from
Dec 30, 2023
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
11 changes: 6 additions & 5 deletions Gotrue/Client.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Threading.Tasks;
using System.Web;
using Newtonsoft.Json;
Expand Down Expand Up @@ -415,12 +416,12 @@ public async Task<Session> SetSession(string accessToken, string refreshToken, b
if (string.IsNullOrEmpty(accessToken) || string.IsNullOrEmpty(refreshToken))
throw new GotrueException("`accessToken` and `refreshToken` cannot be empty.", NoSessionFound);

var payload = JWTDecoder.Decoder.DecodePayload<User>(accessToken);
var payload = new JwtSecurityTokenHandler().ReadJwtToken(accessToken).Payload;

if (payload == null || payload.ExpiresAt() == DateTime.MinValue)
if (payload == null || payload.ValidTo == DateTime.MinValue)
throw new GotrueException("`accessToken`'s payload was of an unknown structure.", NoSessionFound);

if (payload.Expired() || forceAccessTokenRefresh)
if (payload.ValidTo < DateTime.UtcNow || forceAccessTokenRefresh)
{
var result = await _api.RefreshAccessToken(accessToken, refreshToken);

Expand All @@ -437,7 +438,7 @@ public async Task<Session> SetSession(string accessToken, string refreshToken, b
AccessToken = accessToken,
RefreshToken = refreshToken,
TokenType = "bearer",
ExpiresIn = payload.Exp!.Value,
ExpiresIn = payload.Expiration!.Value,
User = await _api.GetUser(accessToken)
};

Expand Down Expand Up @@ -484,7 +485,7 @@ public async Task<Session> SetSession(string accessToken, string refreshToken, b
var session = new Session
{
AccessToken = accessToken,
ExpiresIn = int.Parse(expiresIn),
ExpiresIn = long.Parse(expiresIn),
RefreshToken = refreshToken,
TokenType = tokenType,
User = user
Expand Down
2 changes: 1 addition & 1 deletion Gotrue/Gotrue.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JWTDecoder" Version="0.9.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="supabase-core" Version="0.0.3" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.0.3" />
</ItemGroup>
<ItemGroup>
<None Remove="Exceptions\" />
Expand Down
2 changes: 1 addition & 1 deletion Gotrue/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Session
public string? AccessToken { get; set; }

[JsonProperty("expires_in")]
public int ExpiresIn { get; set; }
public long ExpiresIn { get; set; }

[JsonProperty("refresh_token")]
public string? RefreshToken { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion Gotrue/StatelessClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public async Task<bool> DeleteUser(string uid, string serviceRoleToken, Stateles
var session = new Session
{
AccessToken = accessToken,
ExpiresIn = int.Parse(expiresIn),
ExpiresIn = long.Parse(expiresIn),
RefreshToken = refreshToken,
TokenType = tokenType,
User = user
Expand Down
4 changes: 2 additions & 2 deletions Gotrue/TokenRefresh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ private TimeSpan GetInterval()
return TimeSpan.Zero;
}

var interval = (int)Math.Floor(_client.CurrentSession.ExpiresIn * 4.0f / 5.0f);
var interval = (long)Math.Floor(_client.CurrentSession.ExpiresIn * 4.0f / 5.0f);

var timeoutSeconds = Convert.ToInt32((_client.CurrentSession.CreatedAt.AddSeconds(interval) - DateTime.UtcNow).TotalSeconds);
var timeoutSeconds = Convert.ToInt64((_client.CurrentSession.CreatedAt.AddSeconds(interval) - DateTime.UtcNow).TotalSeconds);

if (timeoutSeconds > _client.Options.MaximumRefreshWaitTime)
timeoutSeconds = _client.Options.MaximumRefreshWaitTime;
Expand Down
4 changes: 0 additions & 4 deletions Gotrue/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ public class User

[JsonProperty("exp")]
internal int? Exp { get; set; }

internal DateTime ExpiresAt() => Exp.HasValue ? DateTimeOffset.FromUnixTimeSeconds(Exp.Value).UtcDateTime : DateTime.MinValue;

internal bool Expired() => ExpiresAt() < DateTime.UtcNow;
}

/// <summary>
Expand Down
10 changes: 5 additions & 5 deletions GotrueTests/GotrueTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.29.0" />
<PackageReference Include="coverlet.collector" Version="3.2.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.2" />
<PackageReference Include="coverlet.collector" Version="3.2.0"><IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.29.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading