Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instance authorization #531

Merged
merged 14 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Altinn.Correspondence.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Altinn.Correspondence.Tests
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Altinn.Correspondence.Integrations", "src\Altinn.Correspondence.Integrations\Altinn.Correspondence.Integrations.csproj", "{5C88BAAD-CF82-4D7A-8FB6-879605EF4E03}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Altinn.Correspondence.Common", "src\Altinn.Correspondence.Common\Altinn.Correspondence.Common.csproj", "{AC3CF570-F9E1-4616-A56B-FF94C89CBC0B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -47,6 +49,10 @@ Global
{5C88BAAD-CF82-4D7A-8FB6-879605EF4E03}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5C88BAAD-CF82-4D7A-8FB6-879605EF4E03}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5C88BAAD-CF82-4D7A-8FB6-879605EF4E03}.Release|Any CPU.Build.0 = Release|Any CPU
{AC3CF570-F9E1-4616-A56B-FF94C89CBC0B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AC3CF570-F9E1-4616-A56B-FF94C89CBC0B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AC3CF570-F9E1-4616-A56B-FF94C89CBC0B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AC3CF570-F9E1-4616-A56B-FF94C89CBC0B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Altinn.Common.PEP.Authorization;
using Altinn.Correspondence.Application.Configuration;
using Altinn.Correspondence.Common.Constants;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization.Infrastructure;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Altinn.Correspondence.Application.Configuration;
using Altinn.Correspondence.Common.Constants;
using System.Text.Json;

namespace Altinn.Correspondence.Tests.TestingController.Attachment.Base
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Altinn.Correspondence.Application.Configuration;
using Altinn.Correspondence.Common.Constants;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Altinn.Correspondence.API.Models;
using Altinn.Correspondence.API.Models.Enums;
using Altinn.Correspondence.Application.Configuration;
using Altinn.Correspondence.Common.Constants;
using Altinn.Correspondence.Core.Models.Notifications;
using Altinn.Correspondence.Core.Repositories;
using Altinn.Correspondence.Tests.Factories;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Altinn.Correspondence.API.Models;
using Altinn.Correspondence.API.Models.Enums;
using Altinn.Correspondence.Application.Configuration;
using Altinn.Correspondence.Common.Constants;
using Altinn.Correspondence.Tests.Factories;
using Altinn.Correspondence.Tests.TestingController.Correspondence.Base;
using System.Net;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Altinn.Correspondence.API.Models;
using Altinn.Correspondence.API.Models.Enums;
using Altinn.Correspondence.Application.Configuration;
using Altinn.Correspondence.Common.Constants;
using Altinn.Correspondence.Application.GetCorrespondences;
using Altinn.Correspondence.Tests.Factories;
using Altinn.Correspondence.Tests.Helpers;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Altinn.Correspondence.Application.Configuration;
using Altinn.Correspondence.Common.Constants;
using System.Text.Json;
using System.Text.Json.Serialization;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Altinn.Correspondence.API.Models;
using Altinn.Correspondence.API.Models.Enums;
using Altinn.Correspondence.Application.Configuration;
using Altinn.Correspondence.Common.Constants;
using Altinn.Correspondence.Core.Models.Entities;
using Altinn.Correspondence.Core.Services;
using Altinn.Correspondence.Tests.Factories;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Altinn.Correspondence.API.Models.Enums;
using Altinn.Correspondence.API.Models;
using Altinn.Correspondence.Application.Configuration;
using Altinn.Correspondence.Common.Constants;
using Altinn.Correspondence.Application.GetCorrespondenceHistory;
using Altinn.Correspondence.Core.Models.Enums;
using Altinn.Correspondence.Tests.Factories;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Altinn.Correspondence.API.Models.Enums;
using Altinn.Correspondence.Application.Configuration;
using Altinn.Correspondence.Common.Constants;
using Altinn.Correspondence.Application.GetCorrespondences;
using Altinn.Correspondence.Tests.Factories;
using Altinn.Correspondence.Tests.Helpers;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Altinn.Correspondence.API.Models;
using Altinn.Correspondence.Application.Configuration;
using Altinn.Correspondence.Common.Constants;
using Altinn.Correspondence.Core.Options;
using Altinn.Correspondence.Core.Repositories;
using Altinn.Correspondence.Core.Services;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Altinn.Correspondence.API.Models;
using Altinn.Correspondence.API.Models.Enums;
using Altinn.Correspondence.Application.Configuration;
using Altinn.Correspondence.Common.Constants;
using Altinn.Correspondence.Tests.Helpers;
using System.Net.Http.Json;
using System.Text;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Altinn.Correspondence.API.Models;
using Altinn.Correspondence.Application.CheckNotification;
using Altinn.Correspondence.Application.Configuration;
using Altinn.Correspondence.Common.Constants;
using Altinn.Correspondence.Tests.Factories;
using System.Net.Http.Json;
using System.Text.Json;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Altinn.Correspondence.API.Auth;
using Altinn.Correspondence.Application.Configuration;
using Altinn.Correspondence.Common.Constants;
using Altinn.Correspondence.Core.Options;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
Expand Down
2 changes: 1 addition & 1 deletion src/Altinn.Correspondence.API/Auth/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Altinn.Common.PEP.Authorization;
using Altinn.Correspondence.API.Helpers;
using Altinn.Correspondence.Application.Configuration;
using Altinn.Correspondence.Common.Constants;
using Altinn.Correspondence.Core.Options;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Altinn.Correspondence.API.Models;
using Altinn.Correspondence.Application;
using Altinn.Correspondence.Application.Configuration;
using Altinn.Correspondence.Common.Constants;
using Altinn.Correspondence.Application.DownloadAttachment;
using Altinn.Correspondence.Application.GetAttachmentDetails;
using Altinn.Correspondence.Application.GetAttachmentOverview;
Expand Down Expand Up @@ -29,7 +29,10 @@ public class AttachmentController(ILogger<CorrespondenceController> logger) : Co
[Consumes("application/json")]
[Produces("application/json")]
[Authorize(Policy = AuthorizationConstants.Sender)]
public async Task<ActionResult<Guid>> InitializeAttachment(InitializeAttachmentExt InitializeAttachmentExt, [FromServices] InitializeAttachmentHandler handler, CancellationToken cancellationToken)
public async Task<ActionResult<Guid>> InitializeAttachment(
InitializeAttachmentExt InitializeAttachmentExt,
[FromServices] InitializeAttachmentHandler handler,
CancellationToken cancellationToken)
{
var commandRequest = InitializeAttachmentMapper.MapToRequest(InitializeAttachmentExt);
var commandResult = await handler.Process(commandRequest, HttpContext.User, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Altinn.Correspondence.API.Models.Enums;
using Altinn.Correspondence.Application;
using Altinn.Correspondence.Application.CheckNotification;
using Altinn.Correspondence.Application.Configuration;
using Altinn.Correspondence.Common.Constants;
using Altinn.Correspondence.Application.DownloadCorrespondenceAttachment;
using Altinn.Correspondence.Application.GetCorrespondenceDetails;
using Altinn.Correspondence.Application.GetCorrespondenceOverview;
Expand Down Expand Up @@ -103,16 +103,14 @@ public async Task<ActionResult<InitializeCorrespondencesResponseExt>> UploadCorr
[Authorize(Policy = AuthorizationConstants.SenderOrRecipient, AuthenticationSchemes = AuthorizationConstants.AltinnTokenOrDialogportenScheme)]
public async Task<ActionResult<CorrespondenceOverviewExt>> GetCorrespondenceOverview(
Guid correspondenceId,
[FromQuery] string? onBehalfOf,
[FromServices] GetCorrespondenceOverviewHandler handler,
CancellationToken cancellationToken)
{
_logger.LogInformation("Getting Correspondence overview for {correspondenceId}", correspondenceId.ToString());

var commandResult = await handler.Process(new GetCorrespondenceOverviewRequest()
{
CorrespondenceId = correspondenceId,
OnBehalfOf = onBehalfOf
CorrespondenceId = correspondenceId
}, HttpContext.User, cancellationToken);

return commandResult.Match(
Expand All @@ -134,7 +132,6 @@ public async Task<ActionResult<CorrespondenceOverviewExt>> GetCorrespondenceOver
[Authorize(Policy = AuthorizationConstants.SenderOrRecipient, AuthenticationSchemes = AuthorizationConstants.AltinnTokenOrDialogportenScheme)]
public async Task<ActionResult<CorrespondenceDetailsExt>> GetCorrespondenceDetails(
Guid correspondenceId,
[FromQuery] string? onBehalfOf,
[FromServices] GetCorrespondenceDetailsHandler handler,
CancellationToken cancellationToken)
{
Expand All @@ -143,7 +140,6 @@ public async Task<ActionResult<CorrespondenceDetailsExt>> GetCorrespondenceDetai
var commandResult = await handler.Process(new GetCorrespondenceDetailsRequest()
{
CorrespondenceId = correspondenceId,
OnBehalfOf = onBehalfOf
}, HttpContext.User, cancellationToken);

return commandResult.Match(
Expand Down Expand Up @@ -239,7 +235,6 @@ public async Task<ActionResult<CorrespondencesExt>> GetCorrespondences(
[Route("{correspondenceId}/markasread")]
public async Task<ActionResult> MarkAsRead(
Guid correspondenceId,
[FromQuery] string? onBehalfOf,
[FromServices] UpdateCorrespondenceStatusHandler handler,
CancellationToken cancellationToken)
{
Expand All @@ -248,8 +243,7 @@ public async Task<ActionResult> MarkAsRead(
var commandResult = await handler.Process(new UpdateCorrespondenceStatusRequest
{
CorrespondenceId = correspondenceId,
Status = CorrespondenceStatus.Read,
OnBehalfOf = onBehalfOf,
Status = CorrespondenceStatus.Read
}, HttpContext.User, cancellationToken);

return commandResult.Match(
Expand All @@ -271,7 +265,6 @@ public async Task<ActionResult> MarkAsRead(
[Route("{correspondenceId}/confirm")]
public async Task<ActionResult> Confirm(
Guid correspondenceId,
[FromQuery] string? onBehalfOf,
[FromServices] UpdateCorrespondenceStatusHandler handler,
CancellationToken cancellationToken)
{
Expand All @@ -280,8 +273,7 @@ public async Task<ActionResult> Confirm(
var commandResult = await handler.Process(new UpdateCorrespondenceStatusRequest
{
CorrespondenceId = correspondenceId,
Status = CorrespondenceStatus.Confirmed,
OnBehalfOf = onBehalfOf,
Status = CorrespondenceStatus.Confirmed
}, HttpContext.User, cancellationToken);

return commandResult.Match(
Expand All @@ -303,7 +295,6 @@ public async Task<ActionResult> Confirm(
[Route("{correspondenceId}/archive")]
public async Task<ActionResult> Archive(
Guid correspondenceId,
[FromQuery] string? onBehalfOf,
[FromServices] UpdateCorrespondenceStatusHandler handler,
CancellationToken cancellationToken)
{
Expand All @@ -313,7 +304,6 @@ public async Task<ActionResult> Archive(
{
CorrespondenceId = correspondenceId,
Status = CorrespondenceStatus.Archived,
OnBehalfOf = onBehalfOf,
}, HttpContext.User, cancellationToken);

return commandResult.Match(
Expand All @@ -332,18 +322,17 @@ public async Task<ActionResult> Archive(
[HttpDelete]
[Route("{correspondenceId}/purge")]
[Authorize(Policy = AuthorizationConstants.SenderOrRecipient)]
[EnableCors(AuthorizationConstants.ArbeidsflateCors)]
public async Task<ActionResult> Purge(
Guid correspondenceId,
[FromQuery] string? onBehalfOf,
[FromServices] PurgeCorrespondenceHandler handler,
CancellationToken cancellationToken)
{
_logger.LogInformation("Purging Correspondence with id: {correspondenceId}", correspondenceId.ToString());

var commandResult = await handler.Process(new PurgeCorrespondenceRequest()
{
CorrespondenceId = correspondenceId,
OnBehalfOf = onBehalfOf
CorrespondenceId = correspondenceId
}, HttpContext.User, cancellationToken);

return commandResult.Match(
Expand All @@ -364,15 +353,13 @@ public async Task<ActionResult> Purge(
public async Task<ActionResult> DownloadCorrespondenceAttachmentData(
Guid correspondenceId,
Guid attachmentId,
[FromQuery] string? onBehalfOf,
[FromServices] DownloadCorrespondenceAttachmentHandler handler,
CancellationToken cancellationToken)
{
var commandResult = await handler.Process(new DownloadCorrespondenceAttachmentRequest()
{
CorrespondenceId = correspondenceId,
AttachmentId = attachmentId,
OnBehalfOf = onBehalfOf
AttachmentId = attachmentId
}, HttpContext.User, cancellationToken);
return commandResult.Match(
result => File(result.Stream, "application/octet-stream", result.FileName),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Altinn.Correspondence.API.Models;
using Altinn.Correspondence.Application;
using Altinn.Correspondence.Application.Configuration;
using Altinn.Correspondence.Common.Constants;
using Altinn.Correspondence.Application.DownloadCorrespondenceAttachment;
using Altinn.Correspondence.Application.GetCorrespondenceHistory;
using Altinn.Correspondence.Application.GetCorrespondenceOverview;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Altinn.Correspondence.API.Models;
using Altinn.Correspondence.Application;
using Altinn.Correspondence.Application.Configuration;
using Altinn.Correspondence.Common.Constants;
using Altinn.Correspondence.Application.GetAttachmentOverview;
using Altinn.Correspondence.Application.InitializeAttachment;
using Altinn.Correspondence.Application.InitializeCorrespondence;
Expand Down
2 changes: 1 addition & 1 deletion src/Altinn.Correspondence.API/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Altinn.Correspondence.API.Auth;
using Altinn.Correspondence.Application;
using Altinn.Correspondence.Application.Configuration;
using Altinn.Correspondence.Common.Constants;
using Altinn.Correspondence.Core.Options;
using Altinn.Correspondence.Integrations;
using Altinn.Correspondence.Integrations.Hangfire;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System.Runtime.CompilerServices;
using System.Security.Claims;
using System.Threading;
using Altinn.Correspondence.Core.Models.Entities;
using Altinn.Correspondence.Core.Repositories;
using Altinn.Correspondence.Core.Services;
Expand All @@ -10,6 +7,8 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Slack.Webhooks;
using System.Runtime.CompilerServices;
using System.Security.Claims;

[assembly: InternalsVisibleTo("Altinn.Correspondence.Tests")]
namespace Altinn.Correspondence.Application.CancelNotification
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
using System.Security.Claims;
using Altinn.Correspondence.Application.Helpers;
using Altinn.Correspondence.Core.Models.Enums;
using Altinn.Correspondence.Common.Helpers;
using Altinn.Correspondence.Core.Repositories;
using OneOf;
using System.Security.Claims;

namespace Altinn.Correspondence.Application.DownloadAttachment;

public class DownloadAttachmentHandler(
IAltinnAuthorizationService altinnAuthorizationService,
IStorageRepository storageRepository,
IAttachmentRepository attachmentRepository,
UserClaimsHelper userClaimsHelper) : IHandler<DownloadAttachmentRequest, Stream>
IAttachmentRepository attachmentRepository) : IHandler<DownloadAttachmentRequest, Stream>
{
public async Task<OneOf<Stream, Error>> Process(DownloadAttachmentRequest request, ClaimsPrincipal? user, CancellationToken cancellationToken)
{
Expand All @@ -19,17 +17,11 @@ public async Task<OneOf<Stream, Error>> Process(DownloadAttachmentRequest reques
{
return Errors.AttachmentNotFound;
}
var hasAccess = await altinnAuthorizationService.CheckUserAccess(user, attachment.ResourceId, attachment.Sender, attachment.Id.ToString(), new List<ResourceAccessLevel> { ResourceAccessLevel.Write }, cancellationToken);
var hasAccess = await altinnAuthorizationService.CheckAccessAsSender(user, attachment.ResourceId, attachment.Sender.WithoutPrefix(), null, cancellationToken);
if (!hasAccess)
{
return Errors.NoAccessToResource;
}

if (!userClaimsHelper.IsSender(attachment.Sender))
{
return Errors.InvalidSender;
}

var attachmentStream = await storageRepository.DownloadAttachment(attachment.Id, cancellationToken);
return attachmentStream;
}
Expand Down
Loading
Loading