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 <inbox@dragonfruit.network> | ||
// 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
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
Oops, something went wrong.