-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ticket #765 : Add RealmRouter and inject the realm into "CurrentRealm…
…" object
- Loading branch information
Showing
12 changed files
with
149 additions
and
9 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
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
9 changes: 9 additions & 0 deletions
9
src/IdServer/SimpleIdServer.IdServer.Website/Infrastructures/CurrentRealm.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,9 @@ | ||
// Copyright (c) SimpleIdServer. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. | ||
|
||
namespace SimpleIdServer.IdServer.Website.Infrastructures; | ||
|
||
public class CurrentRealm | ||
{ | ||
public string Identifier { get; set; } | ||
} |
107 changes: 107 additions & 0 deletions
107
src/IdServer/SimpleIdServer.IdServer.Website/Infrastructures/RealmRouter.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,107 @@ | ||
// Copyright (c) SimpleIdServer. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. | ||
|
||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
using SimpleIdServer.IdServer.Domains; | ||
using System.Reflection; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace SimpleIdServer.IdServer.Website.Infrastructures; | ||
|
||
public class RealmRouter : IComponent, IHandleAfterRender, IDisposable | ||
{ | ||
private RenderHandle _renderHandle; | ||
internal static IServiceProvider _serviceProvider; | ||
[Inject] private NavigationManager NavigationManager { get; set; } | ||
[Parameter] public Assembly AppAssembly { get; set; } | ||
[Parameter] public RenderFragment<RouteData> Found { get; set; } | ||
[Parameter] public RenderFragment NotFound { get; set; } | ||
|
||
public void Attach(RenderHandle renderHandle) | ||
{ | ||
_renderHandle = renderHandle; | ||
} | ||
|
||
public async Task SetParametersAsync(ParameterView parameters) | ||
{ | ||
parameters.SetParameterProperties(this); | ||
var options = _serviceProvider.GetRequiredService<IOptions<IdServerWebsiteOptions>>(); | ||
var components = GetRouteableComponents(); | ||
if (!options.Value.IsReamEnabled) | ||
{ | ||
var locationPath = NavigationManager.ToBaseRelativePath(NavigationManager.Uri); | ||
var context = components.FirstOrDefault(c => c.Value.Any(r => Regex.IsMatch(locationPath, r))); | ||
if(context.Key == null) | ||
{ | ||
_renderHandle.Render(NotFound); | ||
return; | ||
} | ||
|
||
var routeData = new RouteData(context.Key, new Dictionary<string, object>()); | ||
_renderHandle.Render(Found(routeData)); | ||
return; | ||
} | ||
else | ||
{ | ||
var realms = await GetRealms(options); | ||
var locationPath = NavigationManager.ToBaseRelativePath(NavigationManager.Uri); | ||
var realm = string.IsNullOrWhiteSpace(locationPath) ? Constants.DefaultRealm : locationPath.Split("/").First(); | ||
_serviceProvider.GetRequiredService<CurrentRealm>().Identifier = realm; | ||
var pathWithoutRealm = locationPath.Replace(realm, string.Empty); | ||
var context = components.FirstOrDefault(c => c.Value.Any(r => Regex.IsMatch(pathWithoutRealm, r))); | ||
if (!realms.Any(r => r.Name == realm) || context.Key == null) | ||
{ | ||
_renderHandle.Render(NotFound); | ||
return; | ||
} | ||
|
||
var routeData = new RouteData(context.Key, new Dictionary<string, object>()); | ||
_renderHandle.Render(Found(routeData)); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
} | ||
|
||
public Task OnAfterRenderAsync() | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
private async Task<IEnumerable<Realm>> GetRealms(IOptions<IdServerWebsiteOptions> options) | ||
{ | ||
var httpClientFactory = _serviceProvider.GetRequiredService<IWebsiteHttpClientFactory>(); | ||
var url = $"{options.Value.IdServerBaseUrl}/realms"; | ||
var httpClient = await httpClientFactory.Build(); | ||
var requestMessage = new HttpRequestMessage | ||
{ | ||
RequestUri = new Uri(url), | ||
Method = HttpMethod.Get | ||
}; | ||
var httpResult = await httpClient.SendAsync(requestMessage); | ||
var json = await httpResult.Content.ReadAsStringAsync(); | ||
var realms = SidJsonSerializer.Deserialize<IEnumerable<Realm>>(json); | ||
return realms; | ||
} | ||
|
||
private Dictionary<Type, List<string>> GetRouteableComponents() | ||
{ | ||
var assembly = typeof(RealmRouter).Assembly; | ||
var components = new List<Type>(); | ||
var dic = new Dictionary<Type, List<string>>(); | ||
foreach(var type in assembly.ExportedTypes) | ||
{ | ||
if(typeof(IComponent).IsAssignableFrom(type) && type.IsDefined(typeof(RouteAttribute))) | ||
{ | ||
var routeAttributes = type.GetCustomAttributes<RouteAttribute>(inherit: false); | ||
var templates = routeAttributes.Select(r => Regex.Replace(r.Template, "{\\w*}", "\\w*")).ToList(); | ||
dic.Add(type, templates); | ||
} | ||
} | ||
|
||
return dic; | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
src/IdServer/SimpleIdServer.IdServer.Website/ServiceProviderExtensions.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,15 @@ | ||
// Copyright (c) SimpleIdServer. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. | ||
|
||
using SimpleIdServer.IdServer.Website.Infrastructures; | ||
|
||
namespace System; | ||
|
||
public static class ServiceProviderExtensions | ||
{ | ||
public static IServiceProvider AddSIDWebsite(this IServiceProvider serviceProvider) | ||
{ | ||
RealmRouter._serviceProvider = serviceProvider; | ||
return serviceProvider; | ||
} | ||
} |
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