-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
192 additions
and
184 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
32 changes: 32 additions & 0 deletions
32
src/Artemis.Plugins.Modules.Discord/Authentication/DiscordAuthClientBase.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,32 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Artemis.Core; | ||
|
||
namespace Artemis.Plugins.Modules.Discord.Authentication; | ||
|
||
public abstract class DiscordAuthClientBase : IDiscordAuthClient | ||
{ | ||
protected readonly HttpClient HttpClient = new(); | ||
protected readonly PluginSetting<string> Token; | ||
protected DiscordAuthClientBase(PluginSetting<string> token) | ||
{ | ||
Token = token; | ||
} | ||
|
||
public abstract string ClientId { get; } | ||
public abstract string Origin { get; } | ||
public string? AccessToken => Token.Value; | ||
public abstract Task<TokenResponse> GetAccessTokenAsync(string challengeCode); | ||
|
||
protected void SaveToken(TokenResponse newToken) | ||
{ | ||
Token.Value = newToken.AccessToken; | ||
Token.Save(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
HttpClient.Dispose(); | ||
} | ||
} |
38 changes: 1 addition & 37 deletions
38
src/Artemis.Plugins.Modules.Discord/Authentication/IDiscordAuthClient.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 |
---|---|---|
@@ -1,48 +1,12 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Artemis.Core; | ||
|
||
namespace Artemis.Plugins.Modules.Discord.Authentication; | ||
|
||
public interface IDiscordAuthClient : IDisposable | ||
{ | ||
string ClientId { get; } | ||
string Origin { get; } | ||
bool HasToken { get; } | ||
string AccessToken { get; } | ||
string? AccessToken { get; } | ||
Task<TokenResponse> GetAccessTokenAsync(string challengeCode); | ||
Task RefreshAccessTokenAsync(); | ||
} | ||
|
||
public abstract class DiscordAuthClientBase : IDiscordAuthClient | ||
{ | ||
protected HttpClient HttpClient = new(); | ||
protected readonly PluginSetting<SavedToken> Token; | ||
protected DiscordAuthClientBase(PluginSetting<SavedToken> token) | ||
{ | ||
Token = token; | ||
} | ||
|
||
public abstract string ClientId { get; } | ||
public abstract string Origin { get; } | ||
public bool HasToken => Token.Value != null; | ||
public string AccessToken => Token.Value?.AccessToken ?? throw new InvalidOperationException("No token available"); | ||
public abstract Task<TokenResponse> GetAccessTokenAsync(string challengeCode); | ||
public abstract Task RefreshAccessTokenAsync(); | ||
public void Dispose() | ||
{ | ||
HttpClient.Dispose(); | ||
} | ||
|
||
protected void SaveToken(TokenResponse newToken) | ||
{ | ||
Token.Value = new SavedToken | ||
{ | ||
AccessToken = newToken.AccessToken, | ||
RefreshToken = newToken.RefreshToken, | ||
ExpirationDate = DateTime.UtcNow.AddSeconds(newToken.ExpiresIn) | ||
}; | ||
Token.Save(); | ||
} | ||
} |
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
19 changes: 0 additions & 19 deletions
19
src/Artemis.Plugins.Modules.Discord/Authentication/SavedToken.cs
This file was deleted.
Oops, something went wrong.
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
3 changes: 3 additions & 0 deletions
3
src/Artemis.Plugins.Modules.Discord/DiscordPackets/CommandData/Error.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,3 @@ | ||
namespace Artemis.Plugins.Modules.Discord.DiscordPackets.CommandData; | ||
|
||
public record Error(int Code, string Message); |
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.