Skip to content

Commit 35e940d

Browse files
Update dialogporten client package and use improved token validator.
1 parent 8c97ad3 commit 35e940d

File tree

9 files changed

+63
-48
lines changed

9 files changed

+63
-48
lines changed

src/Altinn.DialogportenAdapter.EventSimulator/Altinn.DialogportenAdapter.EventSimulator.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="Cocona" Version="2.2.0" />
12-
<PackageReference Include="Altinn.Platform.Storage.Interface" Version="4.0.4" />
12+
<PackageReference Include="Altinn.Platform.Storage.Interface" Version="4.0.7" />
1313
<PackageReference Include="Refit.HttpClientFactory" Version="8.0.0" />
1414
</ItemGroup>
1515

src/Altinn.DialogportenAdapter.WebApi/Altinn.DialogportenAdapter.WebApi.csproj

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Altinn.ApiClients.Dialogporten" Version="1.54.0-rc" />
11+
<PackageReference Include="Altinn.ApiClients.Dialogporten" Version="1.56.1" />
1212
<PackageReference Include="Altinn.ApiClients.Maskinporten" Version="9.2.1" />
13-
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.3.2" />
14-
<PackageReference Include="Altinn.Platform.Storage.Interface" Version="4.0.4" />
13+
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.4.0" />
14+
<PackageReference Include="Altinn.Platform.Storage.Interface" Version="4.0.7" />
1515
<PackageReference Include="Azure.Identity" Version="1.13.2" />
16-
<PackageReference Include="JWTCookieAuthentication" Version="4.0.1" />
17-
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
18-
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0"/>
16+
<PackageReference Include="JWTCookieAuthentication" Version="4.0.4" />
17+
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.23.0" />
18+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.2" />
1919
<PackageReference Include="Refit.HttpClientFactory" Version="8.0.0" />
20-
<PackageReference Include="UUIDNext" Version="4.0.0" />
20+
<PackageReference Include="UUIDNext" Version="4.1.1" />
2121
</ItemGroup>
2222

2323
</Project>

src/Altinn.DialogportenAdapter.WebApi/Common/Constants.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
using System.Collections.Immutable;
2+
using Altinn.DialogportenAdapter.WebApi.Infrastructure.Dialogporten;
23
using Altinn.Platform.Storage.Interface.Enums;
34

45
namespace Altinn.DialogportenAdapter.WebApi.Common;
56

6-
public static class Constants
7+
internal static class Constants
78
{
89
public const int DefaultMaxStringLength = 255;
10+
11+
public const string InstanceDataValueDialogIdKey = "dialog.id";
12+
public const string InstanceDataValueDisableSyncKey = "dialog.disableAutomaticSync";
913

1014
public static readonly ImmutableArray<string> SupportedEventTypes =
1115
[
@@ -26,4 +30,10 @@ public static class Constants
2630
InstanceEventType.MessageArchived.ToString(),
2731
InstanceEventType.MessageRead.ToString(),
2832
];
33+
34+
public static readonly ImmutableArray<(DialogGuiActionPriority Priority, int Limit)> PriorityLimits = [
35+
(DialogGuiActionPriority.Primary, 1),
36+
(DialogGuiActionPriority.Secondary, 1),
37+
(DialogGuiActionPriority.Tertiary, 5 )
38+
];
2939
}

src/Altinn.DialogportenAdapter.WebApi/Features/Command/Delete/DeleteDialogService.cs src/Altinn.DialogportenAdapter.WebApi/Features/Command/Delete/InstanceService.cs

+12-15
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,54 @@
44

55
namespace Altinn.DialogportenAdapter.WebApi.Features.Command.Delete;
66

7-
public record DeleteDialogDto(int PartyId, Guid InstanceGuid, bool Hard, string DialogToken);
7+
internal sealed record DeleteInstanceDto(int PartyId, Guid InstanceGuid, bool Hard, string DialogToken);
88

9-
public enum DeleteDialogResult
9+
internal enum DeleteInstanceResult
1010
{
1111
Success,
1212
InstanceNotFound,
1313
Unauthorized
1414
}
1515

16-
internal sealed class DeleteDialogService
16+
internal sealed class InstanceService
1717
{
1818
private readonly IStorageApi _storageApi;
1919
private readonly IDialogTokenValidator _dialogTokenValidator;
2020

21-
public DeleteDialogService(IStorageApi storageApi, IDialogTokenValidator dialogTokenValidator)
21+
public InstanceService(IStorageApi storageApi, IDialogTokenValidator dialogTokenValidator)
2222
{
2323
_storageApi = storageApi ?? throw new ArgumentNullException(nameof(storageApi));
2424
_dialogTokenValidator = dialogTokenValidator ?? throw new ArgumentNullException(nameof(dialogTokenValidator));
2525
}
2626

27-
public async Task<DeleteDialogResult> DeleteDialog(DeleteDialogDto request, CancellationToken cancellationToken)
27+
public async Task<DeleteInstanceResult> Delete(DeleteInstanceDto request, CancellationToken cancellationToken)
2828
{
2929
var instance = await _storageApi
3030
.GetInstance(request.PartyId, request.InstanceGuid, cancellationToken)
3131
.ContentOrDefault();
3232

3333
if (instance is null)
3434
{
35-
return DeleteDialogResult.InstanceNotFound;
35+
return DeleteInstanceResult.InstanceNotFound;
3636
}
3737

3838
var dialogId = request.InstanceGuid.ToVersion7(instance.Created!.Value);
39-
var result = ValidateDialogToken(request.DialogToken, dialogId);
40-
if (!result.IsValid)
39+
if (!ValidateDialogToken(request.DialogToken, dialogId))
4140
{
42-
return DeleteDialogResult.Unauthorized;
41+
return DeleteInstanceResult.Unauthorized;
4342
}
4443

4544
await _storageApi.DeleteInstance(request.PartyId, request.InstanceGuid, request.Hard, cancellationToken);
46-
return DeleteDialogResult.Success;
45+
return DeleteInstanceResult.Success;
4746
}
4847

49-
private IValidationResult ValidateDialogToken(ReadOnlySpan<char> token, Guid dialogId)
48+
private bool ValidateDialogToken(ReadOnlySpan<char> token, Guid dialogId)
5049
{
5150
const string bearerPrefix = "Bearer ";
5251
token = token.StartsWith(bearerPrefix, StringComparison.OrdinalIgnoreCase)
5352
? token[bearerPrefix.Length..]
5453
: token;
55-
var result = _dialogTokenValidator.Validate(token);
56-
// TODO: Validate dialog id
57-
// TODO: Validate action
58-
return result;
54+
var result = _dialogTokenValidator.Validate(token, dialogId, ["delete"]);
55+
return result.IsValid;
5956
}
6057
}

src/Altinn.DialogportenAdapter.WebApi/Features/Command/Sync/ActivityDtoTransformer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Altinn.DialogportenAdapter.WebApi.Features.Command.Sync;
77

8-
internal class ActivityDtoTransformer
8+
internal sealed class ActivityDtoTransformer
99
{
1010
public List<ActivityDto> GetActivities(InstanceEventList events)
1111
{

src/Altinn.DialogportenAdapter.WebApi/Features/Command/Sync/StorageDialogportenDataMerger.cs

+1-6
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ namespace Altinn.DialogportenAdapter.WebApi.Features.Command.Sync;
77

88
internal sealed class StorageDialogportenDataMerger
99
{
10-
private static readonly List<(DialogGuiActionPriority Priority, int Limit)> PriorityLimits = [
11-
(DialogGuiActionPriority.Primary, 1),
12-
(DialogGuiActionPriority.Secondary, 1),
13-
(DialogGuiActionPriority.Tertiary, 5 )
14-
];
1510

1611
private readonly Settings _settings;
1712

@@ -273,7 +268,7 @@ private static List<GuiActionDto> MergeGuiActions(IEnumerable<GuiActionDto> exis
273268
.ExceptBy(storageActions.Select(x => x.Id), x => x.Id)
274269
.ToList();
275270

276-
var priorityCapacity = PriorityLimits
271+
var priorityCapacity = Constants.PriorityLimits
277272
.GroupJoin(result, x => x.Priority, x => x.Priority,
278273
(priorityLimit, existingActions) =>
279274
(

src/Altinn.DialogportenAdapter.WebApi/Features/Command/Sync/SyncInstanceToDialogService.cs

+14-7
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ public async Task Sync(SyncInstanceToDialogDto dto, CancellationToken cancellati
6161
return;
6262
}
6363

64-
if (instance is not null)
64+
if (ShouldUpdateInstanceWithDialogId(instance, dialogId))
6565
{
6666
// Update the instance with the dialogId before we start to modify the dialog
67-
// This way we can keep track of which instances that have been synced to dialogporten
68-
// even if the dialogporten api is down or we have a bug in the sync process.
69-
// TODO: Si til team core (storage) at de ikke skal sende event dersom datavalues endres
67+
// This way we can keep track of which instances that have been attempted synced
68+
// to dialogporten even if the dialogporten api is down or we have a bug in the
69+
// sync process.
7070
await UpdateInstanceWithDialogId(dto, dialogId, cancellationToken);
7171
}
7272

@@ -133,9 +133,8 @@ private static bool ShouldSoftDeleteDialog([NotNullWhen(true)] Instance? instanc
133133

134134
private static bool IsDialogSyncDisabled(Instance? instance)
135135
{
136-
const string disableSyncKey = "dialog.disableAutomaticSync";
137136
return instance?.DataValues is not null
138-
&& instance.DataValues.TryGetValue(disableSyncKey, out var disableSyncString)
137+
&& instance.DataValues.TryGetValue(Constants.InstanceDataValueDisableSyncKey, out var disableSyncString)
139138
&& bool.TryParse(disableSyncString, out var disableSync)
140139
&& disableSync;
141140
}
@@ -164,6 +163,14 @@ private static bool ShouldPurgeDialog(Instance? instance, [NotNullWhen(true)] Di
164163
{
165164
return instance is null or { Status.IsHardDeleted: true } && existingDialog is not null;
166165
}
166+
167+
private static bool ShouldUpdateInstanceWithDialogId([NotNullWhen(true)] Instance? instance, Guid dialogId)
168+
{
169+
return instance?.DataValues is null
170+
|| !instance.DataValues.TryGetValue(Constants.InstanceDataValueDialogIdKey, out var dialogIdString)
171+
|| !Guid.TryParse(dialogIdString, out var instanceDialogId)
172+
|| instanceDialogId != dialogId;
173+
}
167174

168175
private Task UpsertDialog(DialogDto dialog, bool disableAltinnEvents, CancellationToken cancellationToken)
169176
{
@@ -201,7 +208,7 @@ private Task UpdateInstanceWithDialogId(SyncInstanceToDialogDto dto, Guid dialog
201208
{
202209
Values = new()
203210
{
204-
{ "dialog.id", dialogId.ToString() }
211+
{ Constants.InstanceDataValueDialogIdKey, dialogId.ToString() }
205212
}
206213
}, cancellationToken);
207214
}

src/Altinn.DialogportenAdapter.WebApi/Program.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,15 @@
103103
[FromRoute] Guid instanceGuid,
104104
[FromQuery] bool hard,
105105
[FromHeader(Name = "Authorization")] string authorization,
106-
[FromServices] DeleteDialogService deleteService,
106+
[FromServices] InstanceService instanceService,
107107
CancellationToken cancellationToken) =>
108108
{
109-
var request = new DeleteDialogDto(instanceOwner, instanceGuid, hard, authorization);
110-
return await deleteService.DeleteDialog(request, cancellationToken) switch
109+
var request = new DeleteInstanceDto(instanceOwner, instanceGuid, hard, authorization);
110+
return await instanceService.Delete(request, cancellationToken) switch
111111
{
112-
DeleteDialogResult.Success => Results.NoContent(),
113-
DeleteDialogResult.InstanceNotFound => Results.NotFound(),
114-
DeleteDialogResult.Unauthorized => Results.Unauthorized(),
112+
DeleteInstanceResult.Success => Results.NoContent(),
113+
DeleteInstanceResult.InstanceNotFound => Results.NotFound(),
114+
DeleteInstanceResult.Unauthorized => Results.Unauthorized(),
115115
_ => Results.InternalServerError()
116116
};
117117
});

tests/Altinn.DialogportenAdapter.Unit.Tests/Altinn.DialogportenAdapter.Unit.Tests.csproj

+10-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
14-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
15-
<PackageReference Include="xunit" Version="2.5.3"/>
16-
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3"/>
13+
<PackageReference Include="coverlet.collector" Version="6.0.4">
14+
<PrivateAssets>all</PrivateAssets>
15+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
16+
</PackageReference>
17+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
18+
<PackageReference Include="xunit" Version="2.9.3" />
19+
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2">
20+
<PrivateAssets>all</PrivateAssets>
21+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
22+
</PackageReference>
1723
</ItemGroup>
1824

1925
<ItemGroup>

0 commit comments

Comments
 (0)