From 37c91300e40739a4fca205cfad19dc493a051c27 Mon Sep 17 00:00:00 2001
From: Magnus Sandgren <5285192+MagnusSandgren@users.noreply.github.com>
Date: Thu, 27 Feb 2025 12:27:47 +0100
Subject: [PATCH] chore(sdk): Add doc (#1975)
---
.../IDialogTokenValidator.cs | 48 ++++++++++++
.../README.md | 75 +++++++++++++------
2 files changed, 102 insertions(+), 21 deletions(-)
diff --git a/src/Digdir.Library.Dialogporten.WebApiClient/IDialogTokenValidator.cs b/src/Digdir.Library.Dialogporten.WebApiClient/IDialogTokenValidator.cs
index ebcfadbb2..3b284c7ce 100644
--- a/src/Digdir.Library.Dialogporten.WebApiClient/IDialogTokenValidator.cs
+++ b/src/Digdir.Library.Dialogporten.WebApiClient/IDialogTokenValidator.cs
@@ -3,25 +3,73 @@
namespace Altinn.ApiClients.Dialogporten;
+///
+/// Represents a service that can validate a dialog token.
+///
public interface IDialogTokenValidator
{
+ ///
+ /// Validates a dialog token.
+ ///
+ /// The token to validate.
+ /// The optional dialog ID associated with the token. If the token does not represent this ID, the validation will fail.
+ /// The optional list of required actions for the token.
+ /// The optional validation parameters.
+ /// The result of the validation.
IValidationResult Validate(ReadOnlySpan token,
Guid? dialogId = null,
string[]? requiredActions = null,
DialogTokenValidationParameters? options = null);
}
+///
+/// Represents the parameters used to validate a dialog token.
+///
+///
+/// Global defaults are provided by and can be altered through .
+///
public class DialogTokenValidationParameters
{
+ ///
+ /// Gets the default validation parameters which are used when no parameters are provided.
+ ///
+ ///
+ /// This may be altered to change the global default behavior of the validation.
+ ///
public static DialogTokenValidationParameters Default { get; } = new();
+
+ ///
+ /// Gets or sets a value indicating whether to validate the token's lifetime.
+ ///
public bool ValidateLifetime { get; set; } = true;
+
+ ///
+ /// Gets or sets the clock skew to apply when validating the token's lifetime.
+ ///
public TimeSpan ClockSkew { get; set; } = TimeSpan.FromSeconds(10);
}
+///
+/// Represents the result of a dialog token validation.
+///
public interface IValidationResult
{
+ ///
+ /// Indicates whether the token is valid based on the validation parameters.
+ ///
[MemberNotNullWhen(true, nameof(ClaimsPrincipal))]
bool IsValid { get; }
+
+ ///
+ /// A dictionary of errors that occurred during validation.
+ ///
Dictionary> Errors { get; }
+
+ ///
+ /// The extracted from the token.
+ ///
+ ///
+ /// This property will not be null if the token is valid. It will be null if the token has an invalid format.
+ ///
ClaimsPrincipal? ClaimsPrincipal { get; }
}
diff --git a/src/Digdir.Library.Dialogporten.WebApiClient/README.md b/src/Digdir.Library.Dialogporten.WebApiClient/README.md
index dc400f372..118c79341 100644
--- a/src/Digdir.Library.Dialogporten.WebApiClient/README.md
+++ b/src/Digdir.Library.Dialogporten.WebApiClient/README.md
@@ -8,39 +8,72 @@ Uses refit IApiResponse on returns.
## Installation
-Install nuget
+Install the [nuget package](https://www.nuget.org/packages/Altinn.ApiClients.Dialogporten) through Package Manager Console:
+```
+Install-Package Altinn.ApiClients.Dialogporten
+```
-`dotnet add package Altinn.ApiClients.Dialogporten --version 1.55.2`
+Or via .NET Core CLI:
+```
+dotnet add package Altinn.ApiClients.Dialogporten
+```
## Usage
+This package needs some configuration to work. The configuration is done through the `DialogportenSettings` class. The settings are as follows:
+- `BaseUri` - The base URI of the Dialogporten API.
+- `ThrowOnPublicKeyFetchInit` - If true, the client will throw an exception if the public key fetch fails on startup. Default true.
+- `Maskinporten` - The [Maskinporten settings](https://github.com/Altinn/altinn-apiclient-maskinporten).
+ - `ClientId` - The client ID (secret).
+ - `EncodedJwk` - The encoded JWK (secret).
+ - `Environment` - The environment (test/prod).
+ - `Scope` - Whitespace separated list of scopes to use against Dialogporten.
-This library provides extensions methods providing means to create dialogporten clients.
+### Registering with `IServiceCollection`
+There are two ways to register
-Setup
+#### Register through [action parameter](https://learn.microsoft.com/en-us/dotnet/core/extensions/options-library-authors#actiontoptions-parameter):
+```csharp
+var builder = WebApplication.CreateBuilder(args);
+builder.Services.AddDialogportenClient(x =>
+{
+ x.BaseUri = "https://platform.tt02.altinn.no/dialogporten";
+ // x.ThrowOnPublicKeyFetchInit = false;
+ x.Maskinporten.ClientId = "YOUR_CLIENT_ID";
+ x.Maskinporten.EncodedJwk = "YOUR_ENCODED_JWK";
+ x.Maskinporten.Environment = "test";
+ x.Maskinporten.Scope = "digdir:dialogporten.serviceprovider digdir:dialogporten.serviceprovider.search";
+}
+```
-```json
+#### Register through [options instance parameter](https://learn.microsoft.com/en-us/dotnet/core/extensions/options-library-authors#options-instance-parameter):
+```csharp
+var builder = WebApplication.CreateBuilder(args);
+var dialogportenSettings = builder.Configuration
+ .GetSection("DialogportenSettings")
+ .Get()!;
+builder.Services.AddDialogportenClient(dialogportenSettings);
+```
+In this case, the configuration should look like this:
+```json5
{
- "dialogportenSettings": {
- "BaseUri": "",
- "ThrowOnPublicKeyFetchInit": "",
+ "DialogportenSettings": {
+ "BaseUri": "https://platform.tt02.altinn.no/dialogporten",
+ // "ThrowOnPublicKeyFetchInit": false,
"Maskinporten": {
- "ClientId": "",
- "Environment": "",
- "Scope": "",
- "EncodedJwk": ""
+ "ClientId": "YOUR_CLIENT_ID",
+ "EncodedJwk": "YOUR_ENCODED_JWK",
+ "Environment": "test",
+ "Scope": "digdir:dialogporten.serviceprovider digdir:dialogporten.serviceprovider.search"
}
}
}
```
-```C#
-var configuration = new ConfigurationBuilder()
- .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
- .Build();
+### Available services
+The following services are available after registration:
+- [`Altinn.ApiClients.Dialogporten.Features.V1.IServiceownerApi`](./Features/V1/RefitterInterface.cs) - Used to interact with the Dialogporten ServiceOwner API.
+- [`Altinn.ApiClients.Dialogporten.IDialogTokenValidator`](IDialogTokenValidator.cs) - Used to validate Dialogporten tokens.
-var services = new ServiceCollection();
+A background service (`IHostedService`) is also registered that periodically fetches the public key from the Dialogporten API. This is required to validate dialog token signatures.
-services.AddSingleton(configuration);
-
-services.AddDialogportenClient();
-```
+See [sample project](../Digdir.Library.Dialogporten.WebApiClient.WebApiSample/Program.cs) for examples on how to use the services.