Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Login providers should not be visible if the service is not configured #15305

Merged
merged 3 commits into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ public void Configure(AuthenticationOptions options)
return;
}

if (string.IsNullOrWhiteSpace(_facebookSettings.AppId) || string.IsNullOrWhiteSpace(_facebookSettings.AppSecret))
{
_logger.LogWarning("The Facebook login provider is enabled but not configured.");

return;
lampersky marked this conversation as resolved.
Show resolved Hide resolved
}

var loginSettings = GetFacebookLoginSettingsAsync().GetAwaiter().GetResult();
if (loginSettings == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ public void Configure(AuthenticationOptions options)
return;
}

if (string.IsNullOrWhiteSpace(_gitHubAuthenticationSettings.ClientID) ||
string.IsNullOrWhiteSpace(_gitHubAuthenticationSettings.ClientSecret))
{
_logger.LogWarning("The Github login provider is enabled but not configured.");

return;
lampersky marked this conversation as resolved.
Show resolved Hide resolved
}

// Register the OpenID Connect client handler in the authentication handlers collection.
options.AddScheme(GitHubDefaults.AuthenticationScheme, builder =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,35 @@ public class GoogleOptionsConfiguration :
IConfigureOptions<AuthenticationOptions>,
IConfigureNamedOptions<GoogleOptions>
{
private readonly GoogleAuthenticationSettings _gitHubAuthenticationSettings;
private readonly GoogleAuthenticationSettings _googleAuthenticationSettings;
private readonly IDataProtectionProvider _dataProtectionProvider;
private readonly ILogger _logger;

public GoogleOptionsConfiguration(
IOptions<GoogleAuthenticationSettings> gitHubAuthenticationSettings,
IOptions<GoogleAuthenticationSettings> googleAuthenticationSettings,
IDataProtectionProvider dataProtectionProvider,
ILogger<GoogleOptionsConfiguration> logger)
{
_gitHubAuthenticationSettings = gitHubAuthenticationSettings.Value;
_googleAuthenticationSettings = googleAuthenticationSettings.Value;
_dataProtectionProvider = dataProtectionProvider;
_logger = logger;
}

public void Configure(AuthenticationOptions options)
{
if (_gitHubAuthenticationSettings == null)
if (_googleAuthenticationSettings == null)
{
return;
lampersky marked this conversation as resolved.
Show resolved Hide resolved
}

if (string.IsNullOrWhiteSpace(_googleAuthenticationSettings.ClientID) ||
string.IsNullOrWhiteSpace(_googleAuthenticationSettings.ClientSecret))
lampersky marked this conversation as resolved.
Show resolved Hide resolved
{
_logger.LogWarning("The Google login provider is enabled but not configured.");

return;
}

options.AddScheme(GoogleDefaults.AuthenticationScheme, builder =>
{
builder.DisplayName = "Google";
Expand All @@ -47,27 +55,27 @@ public void Configure(string name, GoogleOptions options)
return;
}

if (_gitHubAuthenticationSettings == null)
if (_googleAuthenticationSettings == null)
{
return;
}

options.ClientId = _gitHubAuthenticationSettings.ClientID;
options.ClientId = _googleAuthenticationSettings.ClientID;
try
{
options.ClientSecret = _dataProtectionProvider.CreateProtector(GoogleConstants.Features.GoogleAuthentication).Unprotect(_gitHubAuthenticationSettings.ClientSecret);
options.ClientSecret = _dataProtectionProvider.CreateProtector(GoogleConstants.Features.GoogleAuthentication).Unprotect(_googleAuthenticationSettings.ClientSecret);
}
catch
{
_logger.LogError("The Consumer Secret could not be decrypted. It may have been encrypted using a different key.");
}

if (_gitHubAuthenticationSettings.CallbackPath.HasValue)
if (_googleAuthenticationSettings.CallbackPath.HasValue)
{
options.CallbackPath = _gitHubAuthenticationSettings.CallbackPath;
options.CallbackPath = _googleAuthenticationSettings.CallbackPath;
}

options.SaveTokens = _gitHubAuthenticationSettings.SaveTokens;
options.SaveTokens = _googleAuthenticationSettings.SaveTokens;
}

public void Configure(GoogleOptions options) => Debug.Fail("This infrastructure method shouldn't be called.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Identity.Web;
using OrchardCore.Microsoft.Authentication.Settings;
Expand All @@ -17,10 +18,12 @@ public class AzureADOptionsConfiguration :
public const string AzureAdOpenIdConnectScheme = MicrosoftIdentityDefaults.AzureAd + OpenIdConnectDefaults.AuthenticationScheme;

private readonly AzureADSettings _azureADSettings;
private readonly ILogger _logger;

public AzureADOptionsConfiguration(IOptions<AzureADSettings> azureADSettings)
public AzureADOptionsConfiguration(IOptions<AzureADSettings> azureADSettings, ILogger<AzureADOptionsConfiguration> logger)
{
_azureADSettings = azureADSettings.Value;
_logger = logger;
}

public void Configure(AuthenticationOptions options)
Expand All @@ -31,6 +34,13 @@ public void Configure(AuthenticationOptions options)
return;
}

if (string.IsNullOrWhiteSpace(settings.AppId) || string.IsNullOrWhiteSpace(settings.TenantId))
{
_logger.LogWarning("The AzureAD login provider is enabled but not configured.");

return;
lampersky marked this conversation as resolved.
Show resolved Hide resolved
}

// Register the OpenID Connect client handler in the authentication handlers collection.
options.AddScheme(Constants.AzureAd, builder =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ public void Configure(AuthenticationOptions options)
return;
}

if (string.IsNullOrWhiteSpace(settings.Item1.ConsumerKey) ||
string.IsNullOrWhiteSpace(settings.Item1.ConsumerSecret))
{
lampersky marked this conversation as resolved.
Show resolved Hide resolved
_logger.LogWarning("The Twitter login provider is enabled but not configured.");

return;
}

options.AddScheme(TwitterDefaults.AuthenticationScheme, builder =>
{
builder.DisplayName = "Twitter";
Expand Down
Loading