mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-01 18:57:24 +00:00
Merge pull request #263 from BinaryAlley/bugfix/fix-broken-login-url-after-admin-registration
Fixes the login URL being broken after the admin account registration
This commit is contained in:
3 files changed
+77
-6
No files matched your search
@@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Common.Services;
|
||||
@@ -20,6 +21,11 @@ public class UrlService : IUrlService
|
||||
/// </summary>
|
||||
private static readonly HashSet<string> s_knownRouteTemplates = GetKnownRouteTemplates();
|
||||
|
||||
/// <summary>
|
||||
/// Matches an optional route parameter segment, such as <c>/{returnUrl?}</c>, that was left without a value.
|
||||
/// </summary>
|
||||
private static readonly Regex s_unfilledOptionalSegmentPattern = new(@"/?\{[^{}?]+\?\}", RegexOptions.Compiled | RegexOptions.CultureInvariant);
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="UrlService"/> class.
|
||||
/// </summary>
|
||||
@@ -62,7 +68,16 @@ public class UrlService : IUrlService
|
||||
string path = routeTemplate.Replace("{culture}", culture, StringComparison.OrdinalIgnoreCase);
|
||||
if (additionalRouteValues is not null)
|
||||
foreach (PropertyInfo property in additionalRouteValues.GetType().GetProperties())
|
||||
path = path.Replace($"{{{property.Name}}}", property.GetValue(additionalRouteValues)?.ToString(), StringComparison.OrdinalIgnoreCase);
|
||||
{
|
||||
string? routeValue = property.GetValue(additionalRouteValues)?.ToString();
|
||||
path = path.Replace($"{{{property.Name}}}", routeValue, StringComparison.OrdinalIgnoreCase);
|
||||
// An optional parameter is only filled in when a value is supplied, otherwise its segment is removed below.
|
||||
if (!string.IsNullOrEmpty(routeValue))
|
||||
path = path.Replace($"{{{property.Name}?}}", routeValue, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
// Optional parameters that received no value would otherwise end up literally in the URL (and be percent encoded by the
|
||||
// browser, like "/auth/login/%7BreturnUrl?%7D"), producing a link that does not match the intended route.
|
||||
path = s_unfilledOptionalSegmentPattern.Replace(path, string.Empty);
|
||||
// Ensure the path starts with a slash, so that it is relative to the server root when concatenated with the path base.
|
||||
if (!path.StartsWith('/'))
|
||||
path = "/" + path;
|
||||
|
||||
+4
-5
@@ -46,9 +46,9 @@
|
||||
const manifestUrl = '@_urlService.GetAbsoluteUrl(WebRoutes.Books.GET_READING_MANIFEST, new { bookId = default(Guid) })'.replace('/@(default(Guid))', `/${bookId}`) + `?libraryId=${libraryId}`;
|
||||
const availabilityUrl = '@_urlService.GetAbsoluteUrl(WebRoutes.Books.GET_READING_AVAILABILITY, new { bookId = default(Guid) })'.replace('/@(default(Guid))', `/${bookId}`) + `?libraryId=${libraryId}`;
|
||||
const libraryIndexUrl = '@_urlService.GetAbsoluteUrl(WebRoutes.Books.INDEX)';
|
||||
// The login route takes the page to return to as an optional route value. The template is kept, so the reader can append
|
||||
// the current page to it when a JSON request discovers that the session ended.
|
||||
const signInUrlTemplate = '@_urlService.GetAbsoluteUrl(WebRoutes.Authentication.LOGIN_VIEW)';
|
||||
// The login page URL, without the optional return URL segment. The reader appends the current page to it as a query value
|
||||
// when a JSON request discovers that the session ended.
|
||||
const signInUrl = '@_urlService.GetAbsoluteUrl(WebRoutes.Authentication.LOGIN_VIEW)';
|
||||
const sectionUrlTemplate = '@_urlService.GetAbsoluteUrl(WebRoutes.Books.GET_READING_SECTION, new { bookId = default(Guid) })'.replace('/@(default(Guid))', `/${bookId}`) + `?libraryId=${libraryId}&locationRef=`;
|
||||
const resourceUrlTemplate = '@_urlService.GetAbsoluteUrl(WebRoutes.Books.GET_READING_RESOURCE, new { bookId = default(Guid) })'.replace('/@(default(Guid))', `/${bookId}`) + `?libraryId=${libraryId}&resourceKey=`;
|
||||
const startConsumptionUrl = '@_urlService.GetAbsoluteUrl(WebRoutes.MediaConsumption.START_CONSUMPTION, new { libraryId = Model.LibraryId, mediaKind = "Book", mediaItemId = Model.BookId })';
|
||||
@@ -289,10 +289,9 @@
|
||||
// redirects of the authentication middleware do, so the reader page is not encoded into the path of the login route.
|
||||
function redirectIfSessionEnded(response) {
|
||||
if (response.status === 401) {
|
||||
if (!signInUrlTemplate)
|
||||
if (!signInUrl)
|
||||
return true;
|
||||
const returnUrl = encodeURIComponent(window.location.pathname + window.location.search + window.location.hash);
|
||||
const signInUrl = signInUrlTemplate.replace('/{returnUrl?}', '');
|
||||
window.location.href = `${signInUrl}?returnUrl=${returnUrl}`;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -214,6 +214,63 @@ public class UrlServiceTests
|
||||
Assert.Equal("/lumina/de-de/auth/register", url);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRelativeUrl_WhenOptionalRouteValueIsNotSupplied_ShouldRemoveOptionalSegment()
|
||||
{
|
||||
// Arrange
|
||||
_mockHttpContextAccessor.HttpContext.Returns(TestHttpContextFactory.Create(culture: "en-us"));
|
||||
|
||||
// Act
|
||||
string? url = _sut.GetRelativeUrl(WebRoutes.Authentication.LOGIN_VIEW);
|
||||
|
||||
// Assert
|
||||
Assert.Equal("/en-us/auth/login", url);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
public void GetRelativeUrl_WhenOptionalRouteValueIsEmpty_ShouldRemoveOptionalSegment(string? returnUrl)
|
||||
{
|
||||
// Arrange
|
||||
_mockHttpContextAccessor.HttpContext.Returns(TestHttpContextFactory.Create(culture: "en-us"));
|
||||
|
||||
// Act
|
||||
string? url = _sut.GetRelativeUrl(WebRoutes.Authentication.LOGIN_VIEW, new { returnUrl });
|
||||
|
||||
// Assert
|
||||
Assert.Equal("/en-us/auth/login", url);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRelativeUrl_WhenOptionalRouteValueIsSupplied_ShouldReplaceOptionalPlaceholder()
|
||||
{
|
||||
// Arrange
|
||||
_mockHttpContextAccessor.HttpContext.Returns(TestHttpContextFactory.Create(culture: "en-us"));
|
||||
|
||||
// Act
|
||||
string? url = _sut.GetRelativeUrl(WebRoutes.Authentication.LOGIN_VIEW, new { returnUrl = "libraries" });
|
||||
|
||||
// Assert
|
||||
Assert.Equal("/en-us/auth/login/libraries", url);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetAbsoluteUrl_WhenOptionalRouteValueIsNotSupplied_ShouldBuildWellFormedUrl()
|
||||
{
|
||||
// Arrange
|
||||
DefaultHttpContext httpContext = TestHttpContextFactory.Create(culture: "en-us");
|
||||
httpContext.Request.Host = new HostString("localhost:5012");
|
||||
httpContext.Request.Scheme = "http";
|
||||
_mockHttpContextAccessor.HttpContext.Returns(httpContext);
|
||||
|
||||
// Act
|
||||
string? url = _sut.GetAbsoluteUrl(WebRoutes.Authentication.LOGIN_VIEW);
|
||||
|
||||
// Assert
|
||||
Assert.Equal("http://localhost:5012/en-us/auth/login", url);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetAbsoluteUrl_WhenCalled_ShouldComposeSchemeAndHostWithRelativeUrl()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user