This repository has been archived by the owner on Apr 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #356 from aspriddell/add-service-specific-token-reqs
Add service specific token injectors
- Loading branch information
Showing
16 changed files
with
175 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
DragonFruit.Six.Api/Authentication/Entities/ClientTokenAccessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Dragon6 API Copyright DragonFruit Network <[email protected]> | ||
// Licensed under Apache-2. Refer to the LICENSE file for more info | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using DragonFruit.Six.Api.Enums; | ||
using DragonFruit.Six.Api.Exceptions; | ||
using Nito.AsyncEx; | ||
|
||
namespace DragonFruit.Six.Api.Authentication.Entities | ||
{ | ||
public class ClientTokenAccessor | ||
{ | ||
private readonly AsyncLock _accessSync; | ||
private readonly UbisoftService _service; | ||
private readonly Func<UbisoftService, string, Task<IUbisoftToken>> _fetchTokenDelegate; | ||
|
||
private ClientTokenInjector _currentToken; | ||
|
||
internal ClientTokenAccessor(UbisoftService service, Func<UbisoftService, string, Task<IUbisoftToken>> fetchTokenDelegate) | ||
{ | ||
_accessSync = new AsyncLock(); | ||
|
||
_service = service; | ||
_fetchTokenDelegate = fetchTokenDelegate; | ||
} | ||
|
||
/// <summary> | ||
/// Gets a <see cref="ClientTokenInjector"/> containing a valid token that can be injected into http requests | ||
/// </summary> | ||
public async ValueTask<ClientTokenInjector> GetInjector() | ||
{ | ||
if (_currentToken?.Expired == false) | ||
{ | ||
return _currentToken; | ||
} | ||
|
||
using (await _accessSync.LockAsync().ConfigureAwait(false)) | ||
{ | ||
// check again in case of a backlog | ||
if (_currentToken?.Expired == false) | ||
{ | ||
return _currentToken; | ||
} | ||
|
||
for (int i = 0; i < 2; i++) | ||
{ | ||
var token = await _fetchTokenDelegate.Invoke(_service, _currentToken?.Token.SessionId).ConfigureAwait(false); | ||
_currentToken = new ClientTokenInjector(token); | ||
|
||
if (!_currentToken.Expired) | ||
{ | ||
return _currentToken; | ||
} | ||
} | ||
|
||
throw new InvalidTokenException(_currentToken?.Token); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets a valid <see cref="IUbisoftToken"/> | ||
/// </summary> | ||
public async ValueTask<IUbisoftToken> GetToken() | ||
{ | ||
var injector = await GetInjector().ConfigureAwait(false); | ||
return injector.Token; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
// Dragon6 API Copyright DragonFruit Network <[email protected]> | ||
// Licensed under Apache-2. Refer to the LICENSE file for more info | ||
|
||
using System; | ||
using System.Net.Http; | ||
using System.Text; | ||
using DragonFruit.Data; | ||
using DragonFruit.Data.Extensions; | ||
using DragonFruit.Six.Api.Enums; | ||
|
||
namespace DragonFruit.Six.Api.Authentication.Requests | ||
{ | ||
|
@@ -20,11 +20,10 @@ public sealed class UbisoftTokenRequest : ApiRequest | |
// tokens need an empty request body in UTF8, with app/json type... | ||
protected override HttpContent BodyContent => new StringContent(string.Empty, Encoding.UTF8, "application/json"); | ||
|
||
internal UbisoftTokenRequest() | ||
public UbisoftTokenRequest(UbisoftService service, string authentication) | ||
{ | ||
this.WithHeader(UbisoftIdentifiers.UbiAppIdHeader, service.AppId()); | ||
this.WithAuthHeader($"Basic {authentication}"); | ||
} | ||
|
||
public static UbisoftTokenRequest FromEncodedCredentials(string basicAuth) => new UbisoftTokenRequest().WithAuthHeader($"Basic {basicAuth}"); | ||
public static UbisoftTokenRequest FromUsername(string username, string password) => FromEncodedCredentials(Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}"))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
// Dragon6 API Copyright DragonFruit Network <[email protected]> | ||
// Licensed under Apache-2. Refer to the LICENSE file for more info | ||
|
||
using System; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using DragonFruit.Data; | ||
using DragonFruit.Six.Api.Authentication.Entities; | ||
using DragonFruit.Six.Api.Authentication.Requests; | ||
using DragonFruit.Six.Api.Enums; | ||
|
||
namespace DragonFruit.Six.Api.Authentication | ||
{ | ||
|
@@ -15,66 +18,49 @@ public static class UbisoftTokenExtensions | |
/// Gets a session token for the user credentials provided. | ||
/// </summary> | ||
/// <remarks> | ||
/// You should store this in some form of persistant storage, as requesting these | ||
/// You should store this in some form of persistent storage, as requesting these | ||
/// too many times will result in a cooldown which is reset every time you try to access the resource | ||
/// during said cooldown | ||
/// </remarks> | ||
/// <param name="client">The <see cref="Dragon6Client"/> to use</param> | ||
/// <param name="loginString">The base64 encoded string in the format username:password</param> | ||
/// <param name="service"><see cref="UbisoftService"/> to get the token for. If the <see cref="ApiClient{T}"/> is a <see cref="Dragon6Client"/>, this is optional</param> | ||
/// <param name="token">Optional cancellation token</param> | ||
public static UbisoftToken GetUbiToken(this ApiClient client, string loginString, CancellationToken token = default) | ||
public static async Task<UbisoftToken> GetUbiTokenAsync(this ApiClient client, string loginString, UbisoftService? service = null, CancellationToken token = default) | ||
{ | ||
return client.Perform<UbisoftToken>(UbisoftTokenRequest.FromEncodedCredentials(loginString), token); | ||
} | ||
if (client is Dragon6Client d6Client) | ||
{ | ||
service ??= d6Client.DefaultService; | ||
} | ||
|
||
/// <summary> | ||
/// Gets a session token for the user credentials provided. | ||
/// </summary> | ||
/// <remarks> | ||
/// You should store this in some form of persistant storage, as requesting these | ||
/// too many times will result in a cooldown which is reset every time you try to access the resource | ||
/// during said cooldown | ||
/// </remarks> | ||
/// <param name="client">The <see cref="Dragon6Client"/> to use</param> | ||
/// <param name="loginString">The base64 encoded string in the format username:password</param> | ||
/// <param name="token">Optional cancellation token</param> | ||
public static Task<UbisoftToken> GetUbiTokenAsync(this ApiClient client, string loginString, CancellationToken token = default) | ||
{ | ||
return client.PerformAsync<UbisoftToken>(UbisoftTokenRequest.FromEncodedCredentials(loginString), token); | ||
} | ||
if (!service.HasValue) | ||
{ | ||
throw new ArgumentException($"{nameof(service)} must be non-null when used with a client that does not inherit from {nameof(Dragon6Client)}"); | ||
} | ||
|
||
/// <summary> | ||
/// Gets a session token for the user credentials provided. | ||
/// </summary> | ||
/// <remarks> | ||
/// You should store this in some form of persistant storage, as requesting these | ||
/// too many times will result in a cooldown which is reset every time you try to access the resource | ||
/// during said cooldown | ||
/// </remarks> | ||
/// <param name="client">The <see cref="Dragon6Client"/> to use</param> | ||
/// <param name="username">The username to use</param> | ||
/// <param name="password">The password to use</param> | ||
/// <param name="token">Optional cancellation token</param> | ||
public static UbisoftToken GetUbiToken(this ApiClient client, string username, string password, CancellationToken token = default) | ||
{ | ||
return client.Perform<UbisoftToken>(UbisoftTokenRequest.FromUsername(username, password), token); | ||
var ubisoftToken = await client.PerformAsync<UbisoftToken>(new UbisoftTokenRequest(service.Value, loginString), token).ConfigureAwait(false); | ||
ubisoftToken.AppId = service.Value.AppId(); | ||
|
||
return ubisoftToken; | ||
} | ||
|
||
/// <summary> | ||
/// Gets a session token for the user credentials provided. | ||
/// </summary> | ||
/// <remarks> | ||
/// You should store this in some form of persistant storage, as requesting these | ||
/// You should store this in some form of persistent storage, as requesting these | ||
/// too many times will result in a cooldown which is reset every time you try to access the resource | ||
/// during said cooldown | ||
/// </remarks> | ||
/// <param name="client">The <see cref="Dragon6Client"/> to use</param> | ||
/// <param name="username">The username to use</param> | ||
/// <param name="password">The password to use</param> | ||
/// <param name="service"><see cref="UbisoftService"/> to get the token for. If the <see cref="ApiClient{T}"/> is a <see cref="Dragon6Client"/>, this is optional</param> | ||
/// <param name="token">Optional cancellation token</param> | ||
public static Task<UbisoftToken> GetUbiTokenAsync(this ApiClient client, string username, string password, CancellationToken token = default) | ||
public static Task<UbisoftToken> GetUbiTokenAsync(this ApiClient client, string username, string password, UbisoftService? service = null, CancellationToken token = default) | ||
{ | ||
return client.PerformAsync<UbisoftToken>(UbisoftTokenRequest.FromUsername(username, password), token); | ||
var basicLogin = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}")); | ||
return GetUbiTokenAsync(client, basicLogin, service, token); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.