Skip to content

Commit 47ae81a

Browse files
authored
chore(GraphQl): Add application user (#662)
Hard coded IUser set to LocalDevelopmentUser ## Related Issue(s) - #490 ## Verification - [x] **Your** code builds clean without any errors or warnings - [ ] Manual testing done (required) - [ ] Relevant automated test added (if you find this hard, leave it and we'll help out) ## Documentation - [ ] Documentation is updated (either in `docs`-directory, Altinnpedia or a separate linked PR in [altinn-studio-docs.](https://github.com/Altinn/altinn-studio-docs), if applicable)
1 parent dee62ef commit 47ae81a

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Security.Claims;
2+
using Digdir.Domain.Dialogporten.Application.Externals.Presentation;
3+
4+
namespace Digdir.Domain.Dialogporten.GraphQL.Common;
5+
6+
internal sealed class ApplicationUser : IUser
7+
{
8+
private readonly IHttpContextAccessor _httpContextAccessor;
9+
10+
public ApplicationUser(IHttpContextAccessor httpContextAccessor)
11+
{
12+
_httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor));
13+
}
14+
15+
public ClaimsPrincipal GetPrincipal()
16+
{
17+
return _httpContextAccessor.HttpContext?.User ?? throw new InvalidOperationException("No user principal found");
18+
}
19+
}

src/Digdir.Domain.Dialogporten.GraphQL/Common/Authentication/AuthenticationBuilderExtensions.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public static IServiceCollection AddDialogportenAuthentication(
1212
{
1313
var jwtTokenSchemas = configuration
1414
.GetSection(GraphQlSettings.SectionName)
15-
.Get<GraphQlSettings>()
16-
?.Authentication
17-
?.JwtBearerTokenSchemas;
15+
.Get<GraphQlSettings>()?
16+
.Authentication?
17+
.JwtBearerTokenSchemas;
1818

1919
if (jwtTokenSchemas is null || jwtTokenSchemas.Count == 0)
2020
// Validation should have caught this.

src/Digdir.Domain.Dialogporten.GraphQL/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ static void BuildAndRun(string[] args)
8282
.AddInfrastructure(builder.Configuration, builder.Environment)
8383
.AddAutoMapper(Assembly.GetExecutingAssembly())
8484
.AddApplicationInsightsTelemetry()
85-
.AddScoped<IUser, LocalDevelopmentUser>()
85+
.AddScoped<IUser, ApplicationUser>()
8686
.AddValidatorsFromAssembly(thisAssembly, ServiceLifetime.Transient, includeInternalTypes: true)
8787
.AddAzureAppConfiguration()
8888

0 commit comments

Comments
 (0)