Skip to content

Commit

Permalink
Consumer Api Poc: Public Relationship Template References (#941)
Browse files Browse the repository at this point in the history
* feat: Add method to IUserContext to get the client id

* feat: Add method to IUserContext to get client id

* feat: Add endpoint, controller and bruno files for querying the public relationship template references

* chore: Add example references to appsettings override

* chore: Make public relationship template references optional in appsettings

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
MH321Productions and mergify[bot] authored Nov 20, 2024
1 parent 2d129ee commit 42987e3
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,14 @@ public string GetUsername()
{
throw new NotSupportedException();
}

public string GetClientId()
{
throw new NotSupportedException();
}

public string? GetClientIdOrNull()
{
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
meta {
name: List Public Relationship Template References
type: http
seq: 1
}

get {
url: {{pocUrl}}/PublicRelationshipTemplateReferences
body: none
auth: inherit
}
1 change: 1 addition & 0 deletions Applications/ConsumerApi/src/http/environments/Local.bru
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
vars {
baseUrl: http://localhost:8081/api/v1
pocUrl: http://localhost:8081/api/poc
auth.url: http://localhost:8081/connect/token
auth.username: USRa
auth.password: Aaaaaaaa1!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ public interface IUserContext

string GetUsername();
string? GetUsernameOrNull();

string GetClientId();
string? GetClientIdOrNull();
}
12 changes: 12 additions & 0 deletions Infrastructure/UserContext/AspNetCoreUserContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class AspNetCoreUserContext : IUserContext
private const string DEVICE_ID_CLAIM = "device_id";
private const string USER_ID_CLAIM = "sub";
private const string SUBSCRIPTION_PLAN_CLAIM = "subscription_plan";
private const string CLIENT_ID_CLAIM = "client_id";
private readonly IHttpContextAccessor _context;

public AspNetCoreUserContext(IHttpContextAccessor context)
Expand Down Expand Up @@ -66,6 +67,17 @@ public string GetUsername()
return username;
}

public string GetClientId()
{
return GetClientIdOrNull() ?? throw new NotFoundException();
}

public string? GetClientIdOrNull()
{
var clientId = GetHttpContext().User.FindFirstValue(CLIENT_ID_CLAIM);
return clientId;
}

private HttpContext GetHttpContext()
{
return _context.HttpContext ?? throw new Exception("HttpContext is null");
Expand Down
10 changes: 10 additions & 0 deletions Modules/Challenges/src/Challenges.Jobs.Cleanup/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,14 @@ public string GetUsernameOrNull()
{
throw new NotSupportedException();
}

public string GetClientId()
{
throw new NotSupportedException();
}

public string? GetClientIdOrNull()
{
throw new NotSupportedException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,14 @@ public string GetUsernameOrNull()
{
throw new NotSupportedException();
}

public string GetClientId()
{
throw new NotSupportedException();
}

public string? GetClientIdOrNull()
{
throw new NotSupportedException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class Configuration
[Required]
public InfrastructureConfiguration Infrastructure { get; set; } = new();

public Dictionary<string, IEnumerable<PublicRelationshipTemplateReferenceDefinition>> PublicRelationshipTemplateReferences { get; set; } = [];

public class InfrastructureConfiguration
{
[Required]
Expand All @@ -31,4 +33,16 @@ public class SqlDatabaseConfiguration
public bool EnableHealthCheck { get; set; } = true;
}
}

public class PublicRelationshipTemplateReferenceDefinition
{
[Required]
public string Title { get; set; } = string.Empty;

[Required]
public string Description { get; set; } = string.Empty;

[Required]
public string TruncatedReference { get; set; } = string.Empty;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Backbone.BuildingBlocks.API.Mvc;
using Backbone.BuildingBlocks.Application.Abstractions.Infrastructure.UserContext;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;

namespace Backbone.Modules.Relationships.ConsumerApi.Controllers;

[Route("api/poc/[controller]")]
[Authorize("OpenIddict.Validation.AspNetCore")]
public class PublicRelationshipTemplateReferencesController : ApiControllerBase
{
private readonly Configuration _options;

public PublicRelationshipTemplateReferencesController(IMediator mediator, IOptions<Configuration> options) : base(mediator)
{
_options = options.Value;
}

[HttpGet]
public IActionResult ListPublicRelationshipTemplateReferences(IUserContext userContext)
{
var clientId = userContext.GetClientId();
var response = _options.PublicRelationshipTemplateReferences.GetValueOrDefault(clientId) ?? [];

return Ok(response);
}
}
31 changes: 31 additions & 0 deletions appsettings.override.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,37 @@
"ConnectionString": "User ID=postgres;Password=admin;Server=localhost;Port=5432;Database=enmeshed;" // postgres
// "ConnectionString": "Server=localhost;Database=enmeshed;User Id=sa;Password=Passw0rd;TrustServerCertificate=True" // sqlserver
}
},
"PublicRelationshipTemplateReferences": {
"test": [
{
"title": "a",
"description": "Description",
"truncatedReference": "Reference"
},
{
"title": "b",
"description": "Description",
"truncatedReference": "Reference"
},
{
"title": "c",
"description": "Description",
"truncatedReference": "Reference"
}
],
"client2": [
{
"title": "d",
"description": "Description",
"truncatedReference": "Reference"
},
{
"title": "e",
"description": "Description",
"truncatedReference": "Reference"
}
]
}
},
"Synchronization": {
Expand Down

0 comments on commit 42987e3

Please sign in to comment.