Skip to content

Commit

Permalink
Replace JWTDecoder package with System.IdentityModel.Tokens.Jwt (#83)
Browse files Browse the repository at this point in the history
* Replace JWTDecoder package with System.IdentityModel.Tokens.Jwt

* Update System.IdentityModel.Tokens.Jwt
  • Loading branch information
FantasyTeddy authored Dec 30, 2023
1 parent b07aa13 commit 9df823d
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 19 deletions.
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

0 comments on commit 9df823d

Please sign in to comment.