Adds URL setting to OAuthConfiguration

This commit is contained in:
Jordan Brown
2020-12-07 12:34:09 -05:00
parent a9149482ad
commit 3b4bd2d703
6 changed files with 52 additions and 34 deletions
+6 -1
View File
@@ -133,11 +133,16 @@ Create an `appsettings.Production.json` file next to `appsettings.json`. This wi
- `Security:<Provider Name>OAuth`: Sets the OAuth client ID and secret for a given `<Provider Name>`. The currently supported providers are `GitHub`, `Discord`, and `TGForums`. Setting these fields to `null` disables logins with the provider, but does not stop users from associating their accounts using the API. Sample Entry:
```json
"GitHubOAuth":{
"ClientId": "... (Note for `TGForums`, this is the redirect_uri used)",
"Url": "...", (Used with certain providers)
"ClientId": "...",
"ClientSecret": "..."
}
```
The following providers use the `Url` setting:
- `TGForums`: Used as the OAuth redirect url.
### Database Configuration
If using a MariaDB/MySQL server, our client library [recommends you set 'utf8mb4' as your default charset](https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql#1-recommended-server-charset) disregard at your own risk.
@@ -1,37 +1,13 @@
using System;
namespace Tgstation.Server.Host.Configuration
{
/// <summary>
/// OAuth options.
/// OAuth configuration options.
/// </summary>
class OAuthConfiguration
sealed class OAuthConfiguration : OAuthConfigurationBase
{
/// <summary>
/// The client ID.
/// The redirect or server URL. Not used by all providers.
/// </summary>
public string ClientId { get; set; }
/// <summary>
/// The client secret.
/// </summary>
public string ClientSecret { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="OAuthConfiguration"/> <see langword="class"/>.
/// </summary>
public OAuthConfiguration() { }
/// <summary>
/// Initializes a new instance of the <see cref="OAuthConfiguration"/> <see langword="class"/>.
/// </summary>
/// <param name="oAuthConfiguration">The <see cref="OAuthConfiguration"/> to copy settings from.</param>
public OAuthConfiguration(OAuthConfiguration oAuthConfiguration)
{
if (oAuthConfiguration == null)
throw new ArgumentNullException(nameof(oAuthConfiguration));
ClientId = oAuthConfiguration.ClientId;
ClientSecret = oAuthConfiguration.ClientSecret;
}
public string Url { get; set; }
}
}
@@ -0,0 +1,37 @@
using System;
namespace Tgstation.Server.Host.Configuration
{
/// <summary>
/// Base OAuth options.
/// </summary>
abstract class OAuthConfigurationBase
{
/// <summary>
/// The client ID.
/// </summary>
public string ClientId { get; set; }
/// <summary>
/// The client secret.
/// </summary>
public string ClientSecret { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="OAuthConfigurationBase"/> <see langword="class"/>.
/// </summary>
public OAuthConfigurationBase() { }
/// <summary>
/// Initializes a new instance of the <see cref="OAuthConfigurationBase"/> <see langword="class"/>.
/// </summary>
/// <param name="oAuthConfiguration">The <see cref="OAuthConfigurationBase"/> to copy settings from.</param>
public OAuthConfigurationBase(OAuthConfigurationBase oAuthConfiguration)
{
if (oAuthConfiguration == null)
throw new ArgumentNullException(nameof(oAuthConfiguration));
ClientId = oAuthConfiguration.ClientId;
ClientSecret = oAuthConfiguration.ClientSecret;
}
}
}
@@ -21,9 +21,9 @@ namespace Tgstation.Server.Host.Security.OAuth
/// <summary>
/// Initializes a new instance of the <see cref="DiscordTokenRequest"/> <see langword="class"/>.
/// </summary>
/// <param name="oAuthConfiguration">The <see cref="OAuthConfiguration"/> for the <see cref="OAuthTokenRequest"/>.</param>
/// <param name="oAuthConfiguration">The <see cref="OAuthConfigurationBase"/> for the <see cref="OAuthTokenRequest"/>.</param>
/// <param name="code">The OAuth code for the <see cref="OAuthTokenRequest"/>.</param>
public DiscordTokenRequest(OAuthConfiguration oAuthConfiguration, string code)
public DiscordTokenRequest(OAuthConfigurationBase oAuthConfiguration, string code)
: base(oAuthConfiguration, code)
{
GrantType = "authorization_code";
@@ -6,7 +6,7 @@ namespace Tgstation.Server.Host.Security.OAuth
/// <summary>
/// Generic OAuth token request.
/// </summary>
class OAuthTokenRequest : OAuthConfiguration
class OAuthTokenRequest : OAuthConfigurationBase
{
/// <summary>
/// The OAuth code.
@@ -18,7 +18,7 @@ namespace Tgstation.Server.Host.Security.OAuth
/// </summary>
/// <param name="oAuthConfiguration">The <see cref="OAuthConfiguration"/> to build from.</param>
/// <param name="code">The OAuth code received from the browser.</param>
public OAuthTokenRequest(OAuthConfiguration oAuthConfiguration, string code)
public OAuthTokenRequest(OAuthConfigurationBase oAuthConfiguration, string code)
: base(oAuthConfiguration)
{
Code = code ?? throw new ArgumentNullException(nameof(code));
@@ -65,7 +65,7 @@ namespace Tgstation.Server.Host.Security.OAuth
{
UriBuilder builder = new UriBuilder("https://tgstation13.org/phpBB/oauth_create_session.php")
{
Query = $"site_private_token={HttpUtility.UrlEncode(Convert.ToBase64String(Encoding.UTF8.GetBytes(OAuthConfiguration.ClientSecret)))}&return_uri={HttpUtility.UrlEncode(OAuthConfiguration.ClientId)}"
Query = $"site_private_token={HttpUtility.UrlEncode(Convert.ToBase64String(Encoding.UTF8.GetBytes(OAuthConfiguration.ClientSecret)))}&return_uri={HttpUtility.UrlEncode(OAuthConfiguration.Url)}"
};
using var request = new HttpRequestMessage(HttpMethod.Get, builder.Uri);