-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: change format of party identifier (#376)
Issue #220
- Loading branch information
Showing
17 changed files
with
198 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...ication/Common/Extensions/FluentValidation/FluentValidation_PartyIdentifier_Extensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Digdir.Domain.Dialogporten.Application.Common.Numbers; | ||
using Digdir.Domain.Dialogporten.Domain.Parties; | ||
using FluentValidation; | ||
|
||
namespace Digdir.Domain.Dialogporten.Application.Common.Extensions.FluentValidation; | ||
|
||
public static class FluentValidationPartyIdentifierExtensions | ||
{ | ||
public static IRuleBuilderOptions<T, string> IsValidPartyIdentifier<T>(this IRuleBuilder<T, string> ruleBuilder) | ||
{ | ||
return ruleBuilder | ||
.Must(identifier => identifier is null || NorwegianPersonIdentifier.TryParse(identifier, out _) || NorwegianOrganizationIdentifier.TryParse(identifier, out _)) | ||
.WithMessage( | ||
$"'{{PropertyName}}' must be on format '{NorwegianOrganizationIdentifier.Prefix}{{norwegian org-nr}}' or " + | ||
$"'{NorwegianPersonIdentifier.Prefix}{{{{norwegian f-nr/d-nr}}}}' with valid numbers respectively."); | ||
} | ||
} |
52 changes: 0 additions & 52 deletions
52
src/Digdir.Domain.Dialogporten.Application/Common/Numbers/EndUserIdentifier.cs
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
src/Digdir.Domain.Dialogporten.Application/Common/Numbers/OrganizationNumber.cs
This file was deleted.
Oops, something went wrong.
18 changes: 0 additions & 18 deletions
18
src/Digdir.Domain.Dialogporten.Application/Common/Numbers/SocialSecurityNumber.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...orten.Application/Common/Numbers/Mod11.cs → ...main.Dialogporten.Domain/Numbers/Mod11.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/Digdir.Domain.Dialogporten.Domain/Parties/IPartyIdentifier.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Digdir.Domain.Dialogporten.Domain.Parties; | ||
|
||
public interface IPartyIdentifier | ||
{ | ||
static abstract string Prefix { get; } | ||
string Value { get; } | ||
static abstract bool IsValid(ReadOnlySpan<char> value); | ||
static abstract bool TryParse(ReadOnlySpan<char> value, [NotNullWhen(true)] out IPartyIdentifier? identifier); | ||
static abstract ReadOnlySpan<char> GetIdPart(ReadOnlySpan<char> value); | ||
} |
44 changes: 44 additions & 0 deletions
44
src/Digdir.Domain.Dialogporten.Domain/Parties/NorwegianOrganizationIdentifier.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Globalization; | ||
using Digdir.Domain.Dialogporten.Domain.Numbers; | ||
|
||
namespace Digdir.Domain.Dialogporten.Domain.Parties; | ||
|
||
public record NorwegianOrganizationIdentifier : IPartyIdentifier | ||
{ | ||
private static readonly int[] OrgNumberWeights = { 3, 2, 7, 6, 5, 4, 3, 2 }; | ||
public static string Prefix { get; } = "urn:altinn:organization:identifier-no::"; | ||
public string Value { get; } | ||
|
||
private NorwegianOrganizationIdentifier(ReadOnlySpan<char> value) | ||
{ | ||
Value = Prefix + value.ToString(); | ||
} | ||
|
||
public static bool TryParse(ReadOnlySpan<char> value, [NotNullWhen(true)] out IPartyIdentifier? identifier) | ||
{ | ||
if (!IsValid(value)) | ||
{ | ||
identifier = null; | ||
return false; | ||
} | ||
|
||
identifier = new NorwegianOrganizationIdentifier(GetIdPart(value)); | ||
return true; | ||
} | ||
|
||
public static bool IsValid(ReadOnlySpan<char> value) | ||
{ | ||
var idNumberWithoutPrefix = GetIdPart(value); | ||
return idNumberWithoutPrefix.Length == 9 | ||
&& Mod11.TryCalculateControlDigit(idNumberWithoutPrefix[..8], OrgNumberWeights, out var control) | ||
&& control == int.Parse(idNumberWithoutPrefix[8..9], CultureInfo.InvariantCulture); | ||
} | ||
|
||
public static ReadOnlySpan<char> GetIdPart(ReadOnlySpan<char> value) | ||
{ | ||
return value.StartsWith(Prefix, StringComparison.OrdinalIgnoreCase) | ||
? value[Prefix.Length..] | ||
: value; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/Digdir.Domain.Dialogporten.Domain/Parties/NorwegianPersonIdentifier.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Globalization; | ||
using Digdir.Domain.Dialogporten.Domain.Numbers; | ||
|
||
namespace Digdir.Domain.Dialogporten.Domain.Parties; | ||
|
||
public class NorwegianPersonIdentifier : IPartyIdentifier | ||
{ | ||
private static readonly int[] SocialSecurityNumberWeights1 = { 3, 7, 6, 1, 8, 9, 4, 5, 2, 1 }; | ||
private static readonly int[] SocialSecurityNumberWeights2 = { 5, 4, 3, 2, 7, 6, 5, 4, 3, 2, 1 }; | ||
|
||
public static string Prefix { get; } = "urn:altinn:person:identifier-no::"; | ||
public string Value { get; } | ||
|
||
private NorwegianPersonIdentifier(ReadOnlySpan<char> value) | ||
{ | ||
Value = Prefix + value.ToString(); | ||
} | ||
|
||
public static bool TryParse(ReadOnlySpan<char> value, [NotNullWhen(true)] out IPartyIdentifier? identifier) | ||
{ | ||
if (!IsValid(value)) | ||
{ | ||
identifier = null; | ||
return false; | ||
} | ||
|
||
identifier = new NorwegianPersonIdentifier(GetIdPart(value)); | ||
return true; | ||
} | ||
|
||
public static bool IsValid(ReadOnlySpan<char> value) | ||
{ | ||
var idNumberWithoutPrefix = GetIdPart(value); | ||
return idNumberWithoutPrefix.Length == 11 | ||
&& Mod11.TryCalculateControlDigit(idNumberWithoutPrefix[..9], SocialSecurityNumberWeights1, out var control1) | ||
&& Mod11.TryCalculateControlDigit(idNumberWithoutPrefix[..10], SocialSecurityNumberWeights2, out var control2) | ||
&& control1 == int.Parse(idNumberWithoutPrefix[9..10], CultureInfo.InvariantCulture) | ||
&& control2 == int.Parse(idNumberWithoutPrefix[10..11], CultureInfo.InvariantCulture); | ||
} | ||
|
||
public static ReadOnlySpan<char> GetIdPart(ReadOnlySpan<char> value) | ||
{ | ||
return value.StartsWith(Prefix, StringComparison.OrdinalIgnoreCase) | ||
? value[Prefix.Length..] | ||
: value; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Digdir.Domain.Dialogporten.Domain/Parties/SystemUserIdentifier.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Digdir.Domain.Dialogporten.Domain.Parties; | ||
|
||
public record SystemUserIdentifier : IPartyIdentifier | ||
{ | ||
public static string Prefix { get; } = "urn:altinn:systemuser::"; | ||
public string Value { get; } | ||
|
||
private SystemUserIdentifier(ReadOnlySpan<char> value) | ||
{ | ||
Value = Prefix + value.ToString(); | ||
} | ||
|
||
public static bool TryParse(ReadOnlySpan<char> value, [NotNullWhen(true)] out IPartyIdentifier? identifier) | ||
{ | ||
if (!IsValid(value)) | ||
{ | ||
identifier = null; | ||
return false; | ||
} | ||
|
||
identifier = new SystemUserIdentifier(GetIdPart(value)); | ||
return true; | ||
} | ||
|
||
public static bool IsValid(ReadOnlySpan<char> value) | ||
{ | ||
var idNumberWithoutPrefix = GetIdPart(value); | ||
return Guid.TryParse(idNumberWithoutPrefix, out _); | ||
} | ||
|
||
public static ReadOnlySpan<char> GetIdPart(ReadOnlySpan<char> value) | ||
{ | ||
return value.StartsWith(Prefix, StringComparison.OrdinalIgnoreCase) | ||
? value[Prefix.Length..] | ||
: value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.