Skip to content

Commit

Permalink
Merge pull request #1077 from ITfoxtec/1.14.x-development
Browse files Browse the repository at this point in the history
1.14.x development
  • Loading branch information
Revsgaard authored Dec 11, 2024
2 parents b7ef4eb + dd98340 commit 69cf03a
Show file tree
Hide file tree
Showing 17 changed files with 75 additions and 34 deletions.
39 changes: 25 additions & 14 deletions src/FoxIDs.Control/Controllers/Tracks/TExternalUserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,8 @@ public TExternalUserController(TelemetryScopedLogger logger, IMapper mapper, ITe
try
{
if (!await ModelState.TryValidateObjectAsync(userRequest)) return BadRequest(ModelState);

var mExternalUser = await tenantDataRepository.GetAsync<ExternalUser>(await ExternalUser.IdFormatAsync(RouteBinding, userRequest.UpPartyName, await GetLinkClaimHashAsync(userRequest.LinkClaimValue, userRequest.RedemptionClaimValue)), required: !userRequest.LinkClaimValue.IsNullOrWhiteSpace());
if (mExternalUser == null)
{
var idKey = new Track.IdKey { TenantName = RouteBinding.TenantName, TrackName = RouteBinding.TrackName };
(var mExternalUsers, _) = await tenantDataRepository.GetListAsync<ExternalUser>(idKey, whereQuery: u => u.RedemptionClaimValue.Equals(userRequest.RedemptionClaimValue));
mExternalUser = mExternalUsers?.FirstOrDefault();
if (mExternalUser == null)
{
throw new FoxIDsDataException() { StatusCode = DataStatusCode.NotFound };
}
}

var mExternalUser = await GetExternalUserAsync(userRequest);
return Ok(mapper.Map<Api.ExternalUser>(mExternalUser));
}
catch (FoxIDsDataException ex)
Expand Down Expand Up @@ -100,6 +90,9 @@ public TExternalUserController(TelemetryScopedLogger logger, IMapper mapper, ITe

/// <summary>
/// Update external user.
///
/// Select which external user to update with the attributes 'LinkClaimValue' and / or 'RedemptionClaimValue' and set the new values with the attributes 'UpdateLinkClaimValue' and 'UpdateLinkRedemptionClaimValue'.
/// If there are no changes to the attributes 'LinkClaimValue' and 'RedemptionClaimValue' they should be equal to the attributes 'UpdateLinkClaimValue' and 'UpdateLinkRedemptionClaimValue'.
/// </summary>
/// <param name="userRequest">External user.</param>
/// <returns>External user.</returns>
Expand All @@ -111,7 +104,7 @@ public TExternalUserController(TelemetryScopedLogger logger, IMapper mapper, ITe
{
if (!await ModelState.TryValidateObjectAsync(userRequest)) return BadRequest(ModelState);

var mExternalUser = await tenantDataRepository.GetAsync<ExternalUser>(await ExternalUser.IdFormatAsync(RouteBinding, userRequest.UpPartyName, await GetLinkClaimHashAsync(userRequest.LinkClaimValue, userRequest.RedemptionClaimValue)));
var mExternalUser = await GetExternalUserAsync(userRequest);

mExternalUser.LinkClaimValue = userRequest.UpdateLinkClaimValue;
if(mExternalUser.LinkClaimValue.IsNullOrWhiteSpace())
Expand Down Expand Up @@ -164,7 +157,8 @@ public async Task<IActionResult> DeleteExternalUser(Api.ExternalUserId userReque
{
if (!await ModelState.TryValidateObjectAsync(userRequest)) return BadRequest(ModelState);

await tenantDataRepository.DeleteAsync<ExternalUser>(await ExternalUser.IdFormatAsync(RouteBinding, userRequest.UpPartyName, await GetLinkClaimHashAsync(userRequest.LinkClaimValue, userRequest.RedemptionClaimValue)));
var mExternalUser = await GetExternalUserAsync(userRequest);
await tenantDataRepository.DeleteAsync<ExternalUser>(mExternalUser.Id);
return NoContent();
}
catch (FoxIDsDataException ex)
Expand All @@ -178,6 +172,23 @@ public async Task<IActionResult> DeleteExternalUser(Api.ExternalUserId userReque
}
}

private async Task<ExternalUser> GetExternalUserAsync(Api.ExternalUserId userRequest)
{
var mExternalUser = await tenantDataRepository.GetAsync<ExternalUser>(await ExternalUser.IdFormatAsync(RouteBinding, userRequest.UpPartyName, await GetLinkClaimHashAsync(userRequest.LinkClaimValue, userRequest.RedemptionClaimValue)), required: !userRequest.LinkClaimValue.IsNullOrWhiteSpace());
if (mExternalUser == null)
{
var idKey = new Track.IdKey { TenantName = RouteBinding.TenantName, TrackName = RouteBinding.TrackName };
(var mExternalUsers, _) = await tenantDataRepository.GetListAsync<ExternalUser>(idKey, whereQuery: u => u.UpPartyName.Equals(userRequest.UpPartyName) && u.RedemptionClaimValue.Equals(userRequest.RedemptionClaimValue));
mExternalUser = mExternalUsers?.FirstOrDefault();
if (mExternalUser == null)
{
throw new FoxIDsDataException() { StatusCode = DataStatusCode.NotFound };
}
}

return mExternalUser;
}

private Task<string> GetLinkClaimHashAsync(string linkClaimValue, string redemptionClaimValue)
{
if (linkClaimValue.IsNullOrWhiteSpace())
Expand Down
2 changes: 1 addition & 1 deletion src/FoxIDs.Control/FoxIDs.Control.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Version>1.14.2</Version>
<Version>1.14.3</Version>
<RootNamespace>FoxIDs</RootNamespace>
<Authors>Anders Revsgaard</Authors>
<Company>ITfoxtec</Company>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Builder;
using ITfoxtec.Identity.Util;
using Microsoft.AspNetCore.Builder;
using Microsoft.OpenApi.Models;
using System.Collections.Generic;

Expand All @@ -12,15 +13,14 @@ public static void UseApiSwagger(this IApplicationBuilder builder)
{
c.PreSerializeFilters.Add((openApiDocument, httpRequest) =>
{
openApiDocument.Servers = new List<OpenApiServer> { new OpenApiServer { Url = httpRequest.HttpContext.GetHost(addTrailingSlash: false) } };
openApiDocument.Servers = new List<OpenApiServer> { new OpenApiServer { Url = UrlCombine.Combine(httpRequest.HttpContext.GetHost(addTrailingSlash: false), Constants.Routes.ApiPath) } };
});
c.RouteTemplate = "api/swagger/{documentname}/swagger.json";
});
#if DEBUG
builder.UseSwaggerUI(c =>
{
c.SwaggerEndpoint($"/api/swagger/{Constants.ControlApi.Version}/swagger.json", "FoxIDs Control API");
c.RoutePrefix = "api";
});
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public static IServiceCollection AddInfrastructure(this IServiceCollection servi
services.AddSingleton(new OpenSearchClientQueryLog(openSearchQueryLogSettings));
}

services.AddApiSwagger();
services.AddApiSwagger(settings);
services.AddAutoMapper();

if(settings.Payment?.EnablePayment == true && settings.Usage?.EnableInvoice == true)
Expand Down Expand Up @@ -181,7 +181,7 @@ public static IServiceCollection AddAuthenticationAndAuthorization(this IService
return services;
}

public static IServiceCollection AddApiSwagger(this IServiceCollection services)
public static IServiceCollection AddApiSwagger(this IServiceCollection services, FoxIDsControlSettings settings)
{
services.AddSwaggerGen(c =>
{
Expand Down
3 changes: 2 additions & 1 deletion src/FoxIDs.Control/wwwroot/robots.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
User-agent: *
Disallow: /
Disallow: /
Allow: /api/swagger/
2 changes: 1 addition & 1 deletion src/FoxIDs.ControlClient/FoxIDs.ControlClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Version>1.14.2</Version>
<Version>1.14.3</Version>
<RootNamespace>FoxIDs.Client</RootNamespace>
<Authors>Anders Revsgaard</Authors>
<Company>ITfoxtec</Company>
Expand Down
2 changes: 1 addition & 1 deletion src/FoxIDs.ControlClient/Pages/ExternalUsers.razor
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
else
{
<button type="button" class="btn btn-link" @onclick="@(async () => await ShowUpdateExternalUserAsync(externalUser))">
@(!externalUser.LinkClaimValue.IsNullOrWhiteSpace() ? externalUser.LinkClaimValue : externalUser.RedemptionClaimValue) (@(externalUser.UpPartyDisplayName ?? externalUser.UpPartyName))
@(!externalUser.RedemptionClaimValue.IsNullOrWhiteSpace() ? externalUser.RedemptionClaimValue : externalUser.LinkClaimValue) (@(externalUser.UpPartyDisplayName ?? externalUser.UpPartyName))
</button>
}
</li>
Expand Down
2 changes: 1 addition & 1 deletion src/FoxIDs.ControlShared/FoxIDs.ControlShared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Version>1.14.2</Version>
<Version>1.14.3</Version>
<RootNamespace>FoxIDs</RootNamespace>
<Authors>Anders Revsgaard</Authors>
<Company>ITfoxtec</Company>
Expand Down
2 changes: 1 addition & 1 deletion src/FoxIDs.Shared/FoxIDs.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Version>1.14.2</Version>
<Version>1.14.3</Version>
<RootNamespace>FoxIDs</RootNamespace>
<Authors>Anders Revsgaard</Authors>
<Company>ITfoxtec</Company>
Expand Down
4 changes: 4 additions & 0 deletions src/FoxIDs.Shared/Models/Tracks/ExternalUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public static async Task<string> IdFormatAsync(RouteBinding routeBinding, string
[JsonProperty(PropertyName = "user_id")]
public string UserId { get; set; }

[MaxLength(Constants.Models.Party.NameLength)]
[JsonProperty(PropertyName = "up_party_name")]
public string UpPartyName { get; set; }

[MaxLength(Constants.Models.Claim.ValueLength)]
[JsonProperty(PropertyName = "link_claim_value")]
public string LinkClaimValue { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/FoxIDs.SharedBase/FoxIDs.SharedBase.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Version>1.14.2</Version>
<Version>1.14.3</Version>
<RootNamespace>FoxIDs</RootNamespace>
<Authors>Anders Revsgaard</Authors>
<Company>ITfoxtec</Company>
Expand Down
18 changes: 14 additions & 4 deletions src/FoxIDs/Controllers/Site/ErrorController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ public async Task<IActionResult> Index()
var exception = exceptionHandlerPathFeature?.Error;

if (exceptionHandlerPathFeature != null && exception != null && exceptionHandlerPathFeature.Path.EndsWith($"/{Constants.Routes.OAuthController}/{Constants.Endpoints.Token}", StringComparison.OrdinalIgnoreCase))
{
logger.Error(exception);
{
return HandleOAuthTokenException(exception);
}
else
Expand Down Expand Up @@ -257,18 +256,29 @@ private IActionResult HandleOAuthTokenException(Exception exception)
var oauthRequestException = FindException<OAuthRequestException>(exception);
if (oauthRequestException != null)
{
if (oauthRequestException is OAuthRefreshTokenGrantNotFoundException)
{
logger.Warning(exception);
}
else
{
logger.Error(exception);
}
return TokenResponseResult(error: oauthRequestException.Error, errorDescription: oauthRequestException.ErrorDescription);
}

var routeCreationException = FindException<RouteCreationException>(exception);
if (routeCreationException != null)
{
logger.Error(exception);
return TokenResponseResult(errorDescription: routeCreationException.Message);
}
}

logger.Error(exception);
return TokenResponseResult(errorDescription: exception.GetAllMessagesJoined());
}


logger.Error(exception);
return TokenResponseResult();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace FoxIDs
/// <summary>
/// Extension methods for HTML form and redirect actions.
/// </summary>
public static class HtmActionExtensions
public static class HtmlActionExtensions
{
/// <summary>
/// Converts URL and Dictionary&lt;string, string&gt; to a HTML Post ContentResult.
Expand Down
2 changes: 1 addition & 1 deletion src/FoxIDs/FoxIDs.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Version>1.14.2</Version>
<Version>1.14.3</Version>
<RootNamespace>FoxIDs</RootNamespace>
<Authors>Anders Revsgaard</Authors>
<Company>ITfoxtec</Company>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace FoxIDs.Logic
{
[Serializable]
public class OAuthRefreshTokenGrantNotFoundException : OAuthRequestException
{
public OAuthRefreshTokenGrantNotFoundException() { }
public OAuthRefreshTokenGrantNotFoundException(string errorDescription) : base(errorDescription)
{ }
public OAuthRefreshTokenGrantNotFoundException(string errorDescription, Exception inner) : base(errorDescription, inner)
{ }
}
}
3 changes: 2 additions & 1 deletion src/FoxIDs/Logic/OAuth/OAuthRefreshTokenGrantDownLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using ITfoxtec.Identity;
using ITfoxtec.Identity.Util;
using Microsoft.AspNetCore.Http;
using OpenSearch.Client;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -49,7 +50,7 @@ public async Task<string> CreateRefreshTokenGrantAsync(TClient client, List<Clai
var grant = await GetRefreshTokenGrantAsync(client, refreshToken);
if (grant == null)
{
throw new OAuthRequestException($"Refresh Token grant not found for client id '{client.ClientId}' and probably timed out.") { RouteBinding = RouteBinding, Error = IdentityConstants.ResponseErrors.InvalidGrant };
throw new OAuthRefreshTokenGrantNotFoundException($"Refresh Token grant not found for client id '{client.ClientId}' and probably timed out.") { RouteBinding = RouteBinding, Error = IdentityConstants.ResponseErrors.InvalidGrant };
}
if (!grant.ClientId.Equals(client.ClientId, StringComparison.InvariantCultureIgnoreCase))
{
Expand Down
2 changes: 1 addition & 1 deletion src/FoxIDs/Logic/Tracks/StateUpPartyLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task<string> GetAndDeleteStateCookieAsync<T>(T upParty) where T : U
{
await stateCookieRepository.DeleteAsync(upParty);
}
return stateCookie.State;
return stateCookie?.State;
}

public async Task DeleteStateCookieAsync<T>(T upParty) where T : UpParty
Expand Down

0 comments on commit 69cf03a

Please sign in to comment.