Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding OAuth2.0 personal clients and refresh routes.
- Loading branch information
1 parent
108cf40
commit d5e335f
Showing
14 changed files
with
697 additions
and
163 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
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
118 changes: 118 additions & 0 deletions
118
src/MangaDexSharp/Credentialing/ConfigurationCredentialsService.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,118 @@ | ||
namespace MangaDexSharp; | ||
|
||
|
||
/// <summary> | ||
/// Represents a provider that fetches the <see cref="ICredentialsService"/> from the configuration | ||
/// </summary> | ||
public class ConfigurationCredentialsService : ICredentialsService | ||
{ | ||
private readonly IConfiguration _config; | ||
|
||
/// <summary> | ||
/// Where to fetch the API token from in the config file | ||
/// </summary> | ||
public static string TokenPath { get; set; } = "Mangadex:Token"; | ||
|
||
/// <summary> | ||
/// Where to fetch the API url from in the config file | ||
/// </summary> | ||
public static string ApiPath { get; set; } = "Mangadex:ApiUrl"; | ||
|
||
/// <summary> | ||
/// Where to fetch the Auth URL service from in the config file | ||
/// </summary> | ||
public static string AuthPath { get; set; } = "Mangadex:AuthUrl"; | ||
|
||
/// <summary> | ||
/// Where to fetch the client ID from in the config file | ||
/// </summary> | ||
public static string ClientIdPath { get; set; } = "Mangadex:ClientId"; | ||
|
||
/// <summary> | ||
/// Where to fetch the client secret from in the config file | ||
/// </summary> | ||
public static string ClientSecretPath { get; set; } = "Mangadex:ClientSecret"; | ||
|
||
/// <summary> | ||
/// Where to fetch the username (for the password grant OAuth2 requests) from in the config file | ||
/// </summary> | ||
public static string UsernamePath { get; set; } = "Mangadex:Username"; | ||
|
||
/// <summary> | ||
/// Where to fetch the password (for the password grant OAuth2 requests) from in the config file | ||
/// </summary> | ||
public static string PasswordPath { get; set; } = "Mangadex:Password"; | ||
|
||
/// <summary> | ||
/// Where to fetch the User-Agent header from in the config file | ||
/// </summary> | ||
public static string UserAgentPath { get; set; } = "Mangadex:UserAgent"; | ||
|
||
/// <summary> | ||
/// Where to fetch the ThrowOnError flag from in the config file | ||
/// </summary> | ||
public static string ErrorThrownPath { get; set; } = "Mangadex:ThrowOnError"; | ||
|
||
/// <summary> | ||
/// The authentication token from the configuration file | ||
/// </summary> | ||
public string? Token => _config[TokenPath]; | ||
|
||
/// <summary> | ||
/// The API url from the configuration file | ||
/// </summary> | ||
public string ApiUrl => _config[ApiPath] ?? API_ROOT; | ||
|
||
/// <summary> | ||
/// The Auth URL service for MangaDex | ||
/// </summary> | ||
public string AuthUrl => _config[AuthPath] ?? AUTH_URL; | ||
|
||
/// <summary> | ||
/// The User-Agent header to send with requests | ||
/// </summary> | ||
public string UserAgent => _config[UserAgentPath] ?? API_USER_AGENT; | ||
|
||
/// <summary> | ||
/// The client ID for the authorization endpoint | ||
/// </summary> | ||
public string? ClientId => _config[ClientIdPath]; | ||
|
||
/// <summary> | ||
/// The client secret for the authorization endpoint | ||
/// </summary> | ||
public string? ClientSecret => _config[ClientSecretPath]; | ||
|
||
/// <summary> | ||
/// The username for password grant requests | ||
/// </summary> | ||
public string? Username => _config[UsernamePath]; | ||
|
||
/// <summary> | ||
/// The password for password grant requests | ||
/// </summary> | ||
public string? Password => _config[PasswordPath]; | ||
|
||
/// <summary> | ||
/// Whether or not to throw an exception if the API returns an error | ||
/// </summary> | ||
public bool ThrowOnError => _config[ErrorThrownPath] == "true"; | ||
|
||
/// <summary> | ||
/// Represents a provider that fetches the <see cref="ICredentialsService"/> from the configuration | ||
/// </summary> | ||
/// <param name="config">The <see cref="IConfiguration"/> object to fetch the variables from</param> | ||
public ConfigurationCredentialsService(IConfiguration config) | ||
{ | ||
_config = config; | ||
} | ||
|
||
/// <summary> | ||
/// Fetches the user's authentication token from the config file | ||
/// </summary> | ||
/// <returns>The user's authentication token</returns> | ||
public Task<string?> GetToken() | ||
{ | ||
return Task.FromResult(Token); | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
src/MangaDexSharp/Credentialing/HardCodedCredentialsService.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,95 @@ | ||
namespace MangaDexSharp; | ||
|
||
/// <summary> | ||
/// Represents a provider that stores the credentials in-memory | ||
/// </summary> | ||
public class HardCodedCredentialsService : ICredentialsService | ||
{ | ||
/// <summary> | ||
/// The user's authentication token | ||
/// </summary> | ||
public string? Token { get; set; } | ||
|
||
/// <summary> | ||
/// The API url | ||
/// </summary> | ||
public string ApiUrl { get; set; } | ||
|
||
/// <summary> | ||
/// The Auth URL service for MangaDex | ||
/// </summary> | ||
public string AuthUrl { get; set; } | ||
|
||
/// <summary> | ||
/// The client ID for the authorization endpoint | ||
/// </summary> | ||
public string? ClientId { get; set; } | ||
|
||
/// <summary> | ||
/// The client secret for the authorization endpoint | ||
/// </summary> | ||
public string? ClientSecret { get; set; } | ||
|
||
/// <summary> | ||
/// The User-Agent header to send with requests | ||
/// </summary> | ||
public string UserAgent { get; set; } | ||
|
||
/// <summary> | ||
/// Whether or not to throw an exception if the API returns an error | ||
/// </summary> | ||
public bool ThrowOnError { get; set; } | ||
|
||
/// <summary> | ||
/// The username for the password grant OAuth2 requests | ||
/// </summary> | ||
public string? Username { get; set; } | ||
|
||
/// <summary> | ||
/// The password for the password grant OAuth2 requests | ||
/// </summary> | ||
public string? Password { get; set; } | ||
|
||
/// <summary> | ||
/// Represents a provider that stores the credentials in-memory | ||
/// </summary> | ||
/// <param name="token">The user's authentication token</param> | ||
/// <param name="apiUrl">The API url</param> | ||
/// <param name="userAgent">The User-Agent header to send with requests</param> | ||
/// <param name="throwOnError">Whether or not to throw an exception if the API returns an error</param> | ||
/// <param name="authUrl">The Auth URL service for MangaDex</param> | ||
/// <param name="clientId">The client ID for the authorization endpoint</param> | ||
/// <param name="clientSecret">The client secret for the authorization endpoint</param> | ||
/// <param name="username">The username for the password grant OAuth2 requests</param> | ||
/// <param name="password">The password for the password grant OAuth2 requests</param> | ||
public HardCodedCredentialsService( | ||
string? token = null, | ||
string? apiUrl = null, | ||
string? userAgent = null, | ||
bool throwOnError = false, | ||
string? authUrl = null, | ||
string? clientId = null, | ||
string? clientSecret = null, | ||
string? username = null, | ||
string? password = null) | ||
{ | ||
UserAgent = userAgent ?? API_USER_AGENT; | ||
Token = token; | ||
ApiUrl = apiUrl ?? API_ROOT; | ||
AuthUrl = authUrl ?? AUTH_URL; | ||
ClientId = clientId; | ||
ClientSecret = clientSecret; | ||
Username = username; | ||
Password = password; | ||
ThrowOnError = throwOnError; | ||
} | ||
|
||
/// <summary> | ||
/// Returns the user's API token from in memory | ||
/// </summary> | ||
/// <returns>The user's authentication token</returns> | ||
public Task<string?> GetToken() | ||
{ | ||
return Task.FromResult(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
namespace MangaDexSharp; | ||
|
||
/// <summary> | ||
/// A service that provides a method for fetching the API token and URL data | ||
/// </summary> | ||
public interface ICredentialsService | ||
{ | ||
/// <summary> | ||
/// The URL for the MangaDex API | ||
/// </summary> | ||
string ApiUrl { get; } | ||
|
||
/// <summary> | ||
/// The Auth URL service for MangaDex | ||
/// </summary> | ||
string AuthUrl { get; } | ||
|
||
/// <summary> | ||
/// The User-Agent header to send with requests | ||
/// </summary> | ||
string UserAgent { get; } | ||
|
||
/// <summary> | ||
/// The client ID for the authorization endpoint | ||
/// </summary> | ||
string? ClientId { get; } | ||
|
||
/// <summary> | ||
/// The client secret for the authorization endpoint | ||
/// </summary> | ||
string? ClientSecret { get; } | ||
|
||
/// <summary> | ||
/// The username for password grant requests | ||
/// </summary> | ||
string? Username { get; } | ||
|
||
/// <summary> | ||
/// The password for password grant requests | ||
/// </summary> | ||
string? Password { get; } | ||
|
||
/// <summary> | ||
/// How to fetch the user's authentication token | ||
/// </summary> | ||
/// <returns>The user's authentication token</returns> | ||
Task<string?> GetToken(); | ||
|
||
/// <summary> | ||
/// Whether or not to throw an exception if the API returns an error | ||
/// </summary> | ||
bool ThrowOnError { get; } | ||
} |
Oops, something went wrong.