Skip to content

Commit

Permalink
Merge pull request #256 from DuendeSoftware/brock/get_all_names_dyn_p…
Browse files Browse the repository at this point in the history
…roviders

Add missing GetAllSchemeNames to the IIdentityProviderStore
  • Loading branch information
brockallen authored May 29, 2021
2 parents 27eea08 + 244baa6 commit 0062708
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 3 deletions.
12 changes: 12 additions & 0 deletions hosts/AspNetIdentity/Quickstart/Account/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class AccountController : Controller
private readonly IIdentityServerInteractionService _interaction;
private readonly IClientStore _clientStore;
private readonly IAuthenticationSchemeProvider _schemeProvider;
private readonly IIdentityProviderStore _identityProviderStore;
private readonly IEventService _events;

public AccountController(
Expand All @@ -36,13 +37,15 @@ public AccountController(
IIdentityServerInteractionService interaction,
IClientStore clientStore,
IAuthenticationSchemeProvider schemeProvider,
IIdentityProviderStore identityProviderStore,
IEventService events)
{
_userManager = userManager;
_signInManager = signInManager;
_interaction = interaction;
_clientStore = clientStore;
_schemeProvider = schemeProvider;
_identityProviderStore = identityProviderStore;
_events = events;
}

Expand Down Expand Up @@ -244,6 +247,15 @@ private async Task<LoginViewModel> BuildLoginViewModelAsync(string returnUrl)
AuthenticationScheme = x.Name
}).ToList();

var dyanmicSchemes = (await _identityProviderStore.GetAllSchemeNamesAsync())
.Where(x => x.Enabled)
.Select(x => new ExternalProvider
{
AuthenticationScheme = x.Scheme,
DisplayName = x.DisplayName
});
providers.AddRange(dyanmicSchemes);

var allowLocal = true;
if (context?.Client.ClientId != null)
{
Expand Down
15 changes: 13 additions & 2 deletions hosts/EntityFramework/Quickstart/Account/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,24 @@ public class AccountController : Controller
private readonly IIdentityServerInteractionService _interaction;
private readonly IClientStore _clientStore;
private readonly IAuthenticationSchemeProvider _schemeProvider;
private readonly IIdentityProviderStore _identityProviderStore;
private readonly IEventService _events;

public AccountController(
IIdentityServerInteractionService interaction,
IClientStore clientStore,
IAuthenticationSchemeProvider schemeProvider,
IIdentityProviderStore identityProviderStore,
IEventService events,
TestUserStore users = null)
{
// if the TestUserStore is not in DI, then we'll just use the global users collection
// this is where you would plug in your own custom identity management library (e.g. ASP.NET Identity)
_users = users ?? new TestUserStore(TestUsers.Users);
_users = users ?? throw new Exception("Please call 'AddTestUsers(TestUsers.Users)' on the IIdentityServerBuilder in Startup or remove the TestUserStore from the AccountController.");

_interaction = interaction;
_clientStore = clientStore;
_schemeProvider = schemeProvider;
_identityProviderStore = identityProviderStore;
_events = events;
}

Expand Down Expand Up @@ -270,6 +272,15 @@ private async Task<LoginViewModel> BuildLoginViewModelAsync(string returnUrl)
AuthenticationScheme = x.Name
}).ToList();

var dyanmicSchemes = (await _identityProviderStore.GetAllSchemeNamesAsync())
.Where(x => x.Enabled)
.Select(x => new ExternalProvider
{
AuthenticationScheme = x.Scheme,
DisplayName = x.DisplayName
});
providers.AddRange(dyanmicSchemes);

var allowLocal = true;
if (context?.Client.ClientId != null)
{
Expand Down
12 changes: 12 additions & 0 deletions hosts/main/Quickstart/Account/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ public class AccountController : Controller
private readonly IIdentityServerInteractionService _interaction;
private readonly IClientStore _clientStore;
private readonly IAuthenticationSchemeProvider _schemeProvider;
private readonly IIdentityProviderStore _identityProviderStore;
private readonly IEventService _events;

public AccountController(
IIdentityServerInteractionService interaction,
IClientStore clientStore,
IAuthenticationSchemeProvider schemeProvider,
IIdentityProviderStore identityProviderStore,
IEventService events,
TestUserStore users = null)
{
Expand All @@ -48,6 +50,7 @@ public AccountController(
_interaction = interaction;
_clientStore = clientStore;
_schemeProvider = schemeProvider;
_identityProviderStore = identityProviderStore;
_events = events;
}

Expand Down Expand Up @@ -269,6 +272,15 @@ private async Task<LoginViewModel> BuildLoginViewModelAsync(string returnUrl)
AuthenticationScheme = x.Name
}).ToList();

var dyanmicSchemes = (await _identityProviderStore.GetAllSchemeNamesAsync())
.Where(x => x.Enabled)
.Select(x => new ExternalProvider
{
AuthenticationScheme = x.Scheme,
DisplayName = x.DisplayName
});
providers.AddRange(dyanmicSchemes);

var allowLocal = true;
if (context?.Client.ClientId != null)
{
Expand Down
12 changes: 12 additions & 0 deletions src/EntityFramework.Storage/Stores/IdentityProviderStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Duende.IdentityServer.EntityFramework.Interfaces;
Expand Down Expand Up @@ -42,6 +43,17 @@ public IdentityProviderStore(IConfigurationDbContext context, ILogger<IdentityPr
Logger = logger;
}

/// <inheritdoc/>
public async Task<IEnumerable<IdentityProviderName>> GetAllSchemeNamesAsync()
{
var query = Context.IdentityProviders.Select(x => new IdentityProviderName {
Enabled = x.Enabled,
Scheme = x.Scheme,
DisplayName = x.DisplayName
});
return await query.ToArrayAsync();
}

/// <inheritdoc/>
public async Task<IdentityProvider> GetBySchemeAsync(string scheme)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Duende.IdentityServer.Stores;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Duende.IdentityServer.Hosting.DynamicProviders
Expand All @@ -20,6 +21,7 @@ public class CachingIdentityProviderStore<T> : IIdentityProviderStore
{
private readonly IIdentityProviderStore _inner;
private readonly ICache<IdentityProvider> _cache;
private readonly ICache<IEnumerable<IdentityProviderName>> _allCache;
private readonly IdentityServerOptions _options;
private readonly IHttpContextAccessor _httpContextAccessor;

Expand All @@ -28,19 +30,38 @@ public class CachingIdentityProviderStore<T> : IIdentityProviderStore
/// </summary>
/// <param name="inner"></param>
/// <param name="cache"></param>
/// <param name="allCache"></param>
/// <param name="options"></param>
/// <param name="httpContextAccessor"></param>
public CachingIdentityProviderStore(T inner,
ICache<IdentityProvider> cache,
ICache<IEnumerable<IdentityProviderName>> allCache,
IdentityServerOptions options,
IHttpContextAccessor httpContextAccessor)
{
_inner = inner;
_cache = cache;
_allCache = allCache;
_options = options;
_httpContextAccessor = httpContextAccessor;
}

/// <inheritdoc/>
public async Task<IEnumerable<IdentityProviderName>> GetAllSchemeNamesAsync()
{
var result = await _allCache.GetAsync("::all::");
if (result == null)
{
result = await _inner.GetAllSchemeNamesAsync();
if (result != null)
{
await _allCache.SetAsync("::all::", result, _options.Caching.IdentityProviderCacheDuration);
}
}

return result;
}

/// <inheritdoc/>
public async Task<IdentityProvider> GetBySchemeAsync(string scheme)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ public InMemoryOidcProviderStore(IEnumerable<OidcProvider> providers)
_providers = providers;
}

public Task<IEnumerable<IdentityProviderName>> GetAllSchemeNamesAsync()
{
var items = _providers.Select(x => new IdentityProviderName {
Enabled = x.Enabled,
DisplayName = x.DisplayName,
Scheme = x.Scheme
});
return Task.FromResult(items);
}

public Task<IdentityProvider> GetBySchemeAsync(string scheme)
{
var item = _providers.FirstOrDefault(x => x.Scheme == scheme);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
// Copyright (c) Duende Software. All rights reserved.
// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.

using Duende.IdentityServer.Models;
using Duende.IdentityServer.Stores;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Duende.IdentityServer.Hosting.DynamicProviders
{
class NopIdentityProviderStore : IIdentityProviderStore
{
public Task<IEnumerable<IdentityProviderName>> GetAllSchemeNamesAsync()
{
return Task.FromResult(Enumerable.Empty<IdentityProviderName>());
}

public Task<IdentityProvider> GetBySchemeAsync(string scheme)
{
return Task.FromResult<IdentityProvider>(null);
Expand Down
21 changes: 21 additions & 0 deletions src/Storage/Models/IdentityProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@

namespace Duende.IdentityServer.Models
{
/// <summary>
/// Models name for a scheme
/// </summary>
public class IdentityProviderName
{
/// <summary>
/// Scheme name for the provider.
/// </summary>
public string Scheme { get; set; }

/// <summary>
/// Display name for the provider.
/// </summary>
public string DisplayName { get; set; }

/// <summary>
/// Flag that indicates if the provider should be used.
/// </summary>
public bool Enabled { get; set; }
}

/// <summary>
/// Models general storage for an external authentication provider/handler scheme
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions src/Storage/Stores/IIdentityProviderStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

using Duende.IdentityServer.Models;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Duende.IdentityServer.Stores
Expand All @@ -11,6 +13,11 @@ namespace Duende.IdentityServer.Stores
/// </summary>
public interface IIdentityProviderStore
{
/// <summary>
/// Gets all identity providers name.
/// </summary>
Task<IEnumerable<IdentityProviderName>> GetAllSchemeNamesAsync();

/// <summary>
/// Gets the identity provider by scheme name.
/// </summary>
Expand Down

0 comments on commit 0062708

Please sign in to comment.