Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #325 from aspriddell/update-token-injection
Browse files Browse the repository at this point in the history
update client token injection to be accessible outside of the library
  • Loading branch information
aspriddell authored Aug 16, 2022
2 parents 9ce68b5 + 8f847c3 commit 4af9211
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@

namespace DragonFruit.Six.Api.Authentication.Entities
{
public class ClientAccessToken
public class ClientTokenInjector
{
internal readonly IUbisoftToken Token;
private readonly DateTime _tokenExpiryOffset;

internal ClientAccessToken(IUbisoftToken token)
internal ClientTokenInjector(IUbisoftToken token)
{
Token = token;
_tokenExpiryOffset = new DateTime(Math.Max(Token.Expiry.Ticks - 3000000000, 0), DateTimeKind.Utc);
}

internal bool Expired => _tokenExpiryOffset < DateTime.UtcNow;

public IUbisoftToken Token { get; }

/// <summary>
/// Injects Ubisoft authentication headers into the <see cref="ApiRequest"/> provided
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions DragonFruit.Six.Api/Dragon6Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace DragonFruit.Six.Api
{
public abstract class Dragon6Client : ApiClient<ApiJsonSerializer>
{
private ClientAccessToken _access;
private ClientTokenInjector _access;
private readonly AsyncLock _accessSync = new();

protected Dragon6Client(string userAgent = null, UbisoftService app = UbisoftService.RainbowSix)
Expand Down Expand Up @@ -66,7 +66,7 @@ static Dragon6Client()
_ => base.ValidateAndProcess<T>(response)
};

protected internal async ValueTask<ClientAccessToken> RequestAccessToken()
protected internal async ValueTask<ClientTokenInjector> RequestToken()
{
if (_access?.Expired is false)
{
Expand All @@ -79,7 +79,7 @@ protected internal async ValueTask<ClientAccessToken> RequestAccessToken()
if (_access?.Expired is not false)
{
var token = await GetToken(_access?.Token.SessionId).ConfigureAwait(false);
_access = new ClientAccessToken(token);
_access = new ClientTokenInjector(token);
}

return _access;
Expand Down
2 changes: 1 addition & 1 deletion DragonFruit.Six.Api/UbiApiRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async ValueTask IAsyncRequestExecutingCallback.OnRequestExecutingAsync(ApiClient
// the Dragon6Client caches auth tokens and allows the headers to be injected
if (client is Dragon6Client d6Client)
{
var token = await d6Client.RequestAccessToken().ConfigureAwait(false);
var token = await d6Client.RequestToken().ConfigureAwait(false);
token.Inject(this);
}
}
Expand Down

0 comments on commit 4af9211

Please sign in to comment.