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

fix: Accept app references with urn:altinn:resource prefix #685

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ public CreateDialogCommandValidator(
.IsValidUri()
.MaximumLength(Constants.DefaultMaxUriLength)
.Must(x =>
(x?.StartsWith(Constants.ServiceResourcePrefixGeneric, StringComparison.InvariantCulture) ?? false)
|| (x?.StartsWith(Constants.ServiceResourcePrefixApp, StringComparison.InvariantCulture) ?? false))
.WithMessage($"'{{PropertyName}}' must start with '{Constants.ServiceResourcePrefixGeneric}' or '{Constants.ServiceResourcePrefixApp}'.");
x?.StartsWith(Constants.ServiceResourcePrefix, StringComparison.InvariantCulture) ?? false)
.WithMessage($"'{{PropertyName}}' must start with '{Constants.ServiceResourcePrefix}'.");

RuleFor(x => x.Party)
.IsValidPartyIdentifier()
Expand Down
3 changes: 1 addition & 2 deletions src/Digdir.Domain.Dialogporten.Domain/Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ public static class Constants
public const int DefaultMaxStringLength = 255;
public const int DefaultMaxUriLength = 1023;

public const string ServiceResourcePrefixGeneric = "urn:altinn:resource:";
public const string ServiceResourcePrefixApp = "urn:altinn:app:";
public const string ServiceResourcePrefix = "urn:altinn:resource:";
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ internal static class AuthorizedPartiesHelper
private const string PartyTypeOrganization = "Organization";
private const string PartyTypePerson = "Person";
private const string AttributeIdResource = "urn:altinn:resource";
private const string AttributeIdApp = "urn:altinn:app";
private const string AppIdPrefix = "app_";
private const string MainAdministratorRoleCode = "HADM";
private const string AccessManagerRoleCode = "ADMAI";
private static readonly string[] KeyRoleCodes = ["DAGL", "LEDE", "INNH", "DTPR", "DTSO", "BEST"];
Expand Down Expand Up @@ -57,8 +55,5 @@ private static AuthorizedParty MapFromDto(AuthorizedPartiesResultDto dto)
}

private static List<string> GetPrefixedResources(List<string> dtoAuthorizedResources) =>
dtoAuthorizedResources.Select(resource => resource.StartsWith(AppIdPrefix, StringComparison.Ordinal)
? AttributeIdApp + ":" + resource
: AttributeIdResource + ":" + resource)
.ToList();
dtoAuthorizedResources.Select(resource => $"{AttributeIdResource}:{resource}").ToList();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal static class DecisionRequestHelper
private const string AttributeIdResourceInstance = "urn:altinn:resourceinstance";
private const string AttributeIdOrg = "urn:altinn:org";
private const string AttributeIdApp = "urn:altinn:app";
private const string ReservedResourcePrefixForApps = "app_";
private const string AttributeIdAppInstance = "urn:altinn:instance-id";
private const string AttributeIdSubResource = "urn:altinn:subresource";
private const string PermitResponse = "Permit";
Expand Down Expand Up @@ -188,12 +189,14 @@ private static (string, string, string?) SplitNsAndValue(string serviceResource)
var ns = serviceResource[..lastColonIndex];
var value = serviceResource[(lastColonIndex + 1)..];

if (ns != AttributeIdApp) return (ns, value, null);
if (!value.StartsWith(ReservedResourcePrefixForApps, StringComparison.Ordinal))
{
return (ns, value, null);
}

// If the namespace is "urn:altinn:app", the value should have the format "app_{org}_{app_id}".
// We want to split this into the org and app id. If not, something is borked in the data,
// and we will probably cause an error in the PDP API (since it throws if the org/app combo
// doesn't let it get to the app policy)
// If the value starts with the reserved app prefix, we assume that the value is an app id
// and we need to split it into the org and app id based on the format "app_{org}_{app_id}".
// We also use the app namespace for the attribute id.
var parts = value.Split('_');
return parts.Length >= 3
? (AttributeIdApp, string.Join('_', parts[2..]), parts[1])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ private async Task<Dictionary<string, string[]>> GetResourceIdsByOrg(Cancellatio
.ToDictionary(
x => x.Key,
x => x.Select(
x => x.ResourceType == ResourceTypeAltinnApp
? $"{Constants.ServiceResourcePrefixApp}{x.Identifier}"
: $"{Constants.ServiceResourcePrefixGeneric}{x.Identifier}")
x => $"{Constants.ServiceResourcePrefix}{x.Identifier}")
.ToArray()
);

Expand Down
Loading