Skip to content

Commit

Permalink
Fix possible null reference exception on expired access token use
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTTY committed Dec 23, 2022
1 parent e4c0784 commit 0145ef3
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Net;
using RobinTTY.NordigenApiClient.Models;
using RobinTTY.NordigenApiClient.Models.Jwt;

namespace RobinTTY.NordigenApiClient.Tests.Endpoints;

Expand Down Expand Up @@ -45,4 +46,22 @@ public async Task GetTokenWithInvalidCredentials()
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.Unauthorized));
Assert.That(response.Error, Is.Not.Null);
}

/// <summary>
/// Tests using the API with an expired access token.
/// Requires secrets.txt to contain expired access token / valid refresh token pair.
/// </summary>
/// <returns></returns>
[Test]
public async Task ReuseExpiredToken()
{
var secrets = await File.ReadAllLinesAsync("secrets.txt");
var httpClient = new HttpClient();
var credentials = new NordigenClientCredentials(secrets[0], secrets[1]);
var tokenPair = new JsonWebTokenPair(secrets[6], secrets[7]);
var apiClient = new NordigenClient(httpClient, credentials, tokenPair);

var result = await apiClient.RequisitionsEndpoint.GetRequisitions(10, 0);
TestExtensions.AssertNordigenApiResponseIsSuccessful(result, HttpStatusCode.OK);
}
}
2 changes: 1 addition & 1 deletion src/RobinTTY.NordigenApiClient.Tests/TestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal static NordigenClient GetConfiguredClient(bool useExistingJwt = true)
var httpClient = new HttpClient();
var secrets = File.ReadAllLines("secrets.txt");
var credentials = new NordigenClientCredentials(secrets[0], secrets[1]);
var tokenPair = new JsonWebTokenPair(secrets[2], secrets[3]);
var tokenPair = new JsonWebTokenPair(secrets[3], secrets[4]);
return useExistingJwt ? new NordigenClient(httpClient, credentials, tokenPair) : new NordigenClient(httpClient, credentials);
}
}
12 changes: 10 additions & 2 deletions src/RobinTTY.NordigenApiClient/NordigenClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,16 @@ internal async Task<NordigenApiResponse<TResponse, TError>> MakeRequest<TRespons
) where TResponse : class where TError : class
{
var requestUri = query != null ? UriQueryBuilder.BuildUriWithQueryString(uri, query) : uri;
JsonWebTokenPair = useAuthentication ? await TryGetValidTokenPair(cancellationToken) : null;
var client = useAuthentication ? _httpClient.UseNordigenAuthenticationHeader(JsonWebTokenPair) : _httpClient;
HttpClient client;
if (useAuthentication)
{
JsonWebTokenPair = await TryGetValidTokenPair(cancellationToken);
client = _httpClient.UseNordigenAuthenticationHeader(JsonWebTokenPair);
}
else
{
client = _httpClient;
}

HttpResponseMessage ? response;
if (method == HttpMethod.Get)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<PackageTags>Nordigen; API; client</PackageTags>
<PackageReleaseNotes>$([System.IO.File]::ReadAllText("$(MSBuildProjectDirectory)/release-notes.txt"))</PackageReleaseNotes>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>4.0.1</Version>
<Version>4.0.2</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/RobinTTY.NordigenApiClient/release-notes.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Fixes possible null reference exception on event invocation.
Fixes possible null reference exception on expired access token use.

0 comments on commit 0145ef3

Please sign in to comment.