Skip to content

Commit 37c9130

Browse files
chore(sdk): Add doc (#1975)
1 parent 15abbd8 commit 37c9130

File tree

2 files changed

+102
-21
lines changed

2 files changed

+102
-21
lines changed

src/Digdir.Library.Dialogporten.WebApiClient/IDialogTokenValidator.cs

+48
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,73 @@
33

44
namespace Altinn.ApiClients.Dialogporten;
55

6+
/// <summary>
7+
/// Represents a service that can validate a dialog token.
8+
/// </summary>
69
public interface IDialogTokenValidator
710
{
11+
/// <summary>
12+
/// Validates a dialog token.
13+
/// </summary>
14+
/// <param name="token">The token to validate.</param>
15+
/// <param name="dialogId">The optional dialog ID associated with the token. If the token does not represent this ID, the validation will fail.</param>
16+
/// <param name="requiredActions">The optional list of required actions for the token.</param>
17+
/// <param name="options">The optional validation parameters.</param>
18+
/// <returns>The result of the validation.</returns>
819
IValidationResult Validate(ReadOnlySpan<char> token,
920
Guid? dialogId = null,
1021
string[]? requiredActions = null,
1122
DialogTokenValidationParameters? options = null);
1223
}
1324

25+
/// <summary>
26+
/// Represents the parameters used to validate a dialog token.
27+
/// </summary>
28+
/// <remarks>
29+
/// Global defaults are provided by and can be altered through <see cref="DialogTokenValidationParameters.Default"/>.
30+
/// </remarks>
1431
public class DialogTokenValidationParameters
1532
{
33+
/// <summary>
34+
/// Gets the default validation parameters which are used when no parameters are provided.
35+
/// </summary>
36+
/// <remarks>
37+
/// This may be altered to change the global default behavior of the validation.
38+
/// </remarks>
1639
public static DialogTokenValidationParameters Default { get; } = new();
40+
41+
/// <summary>
42+
/// Gets or sets a value indicating whether to validate the token's lifetime.
43+
/// </summary>
1744
public bool ValidateLifetime { get; set; } = true;
45+
46+
/// <summary>
47+
/// Gets or sets the clock skew to apply when validating the token's lifetime.
48+
/// </summary>
1849
public TimeSpan ClockSkew { get; set; } = TimeSpan.FromSeconds(10);
1950
}
2051

52+
/// <summary>
53+
/// Represents the result of a dialog token validation.
54+
/// </summary>
2155
public interface IValidationResult
2256
{
57+
/// <summary>
58+
/// Indicates whether the token is valid based on the validation parameters.
59+
/// </summary>
2360
[MemberNotNullWhen(true, nameof(ClaimsPrincipal))]
2461
bool IsValid { get; }
62+
63+
/// <summary>
64+
/// A dictionary of errors that occurred during validation.
65+
/// </summary>
2566
Dictionary<string, List<string>> Errors { get; }
67+
68+
/// <summary>
69+
/// The <see cref="ClaimsPrincipal"/> extracted from the token.
70+
/// </summary>
71+
/// <remarks>
72+
/// This property will not be null if the token is valid. It will be null if the token has an invalid format.
73+
/// </remarks>
2674
ClaimsPrincipal? ClaimsPrincipal { get; }
2775
}

src/Digdir.Library.Dialogporten.WebApiClient/README.md

+54-21
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,72 @@ Uses refit IApiResponse on returns.
88

99
## Installation
1010

11-
Install nuget
11+
Install the [nuget package](https://www.nuget.org/packages/Altinn.ApiClients.Dialogporten) through Package Manager Console:
12+
```
13+
Install-Package Altinn.ApiClients.Dialogporten
14+
```
1215

13-
`dotnet add package Altinn.ApiClients.Dialogporten --version 1.55.2`
16+
Or via .NET Core CLI:
17+
```
18+
dotnet add package Altinn.ApiClients.Dialogporten
19+
```
1420

1521
## Usage
22+
This package needs some configuration to work. The configuration is done through the `DialogportenSettings` class. The settings are as follows:
23+
- `BaseUri` - The base URI of the Dialogporten API.
24+
- `ThrowOnPublicKeyFetchInit` - If true, the client will throw an exception if the public key fetch fails on startup. Default true.
25+
- `Maskinporten` - The [Maskinporten settings](https://github.com/Altinn/altinn-apiclient-maskinporten).
26+
- `ClientId` - The client ID (secret).
27+
- `EncodedJwk` - The encoded JWK (secret).
28+
- `Environment` - The environment (test/prod).
29+
- `Scope` - Whitespace separated list of scopes to use against Dialogporten.
1630

17-
This library provides extensions methods providing means to create dialogporten clients.
31+
### Registering with `IServiceCollection`
32+
There are two ways to register
1833

19-
Setup
34+
#### Register through [action parameter](https://learn.microsoft.com/en-us/dotnet/core/extensions/options-library-authors#actiontoptions-parameter):
35+
```csharp
36+
var builder = WebApplication.CreateBuilder(args);
37+
builder.Services.AddDialogportenClient(x =>
38+
{
39+
x.BaseUri = "https://platform.tt02.altinn.no/dialogporten";
40+
// x.ThrowOnPublicKeyFetchInit = false;
41+
x.Maskinporten.ClientId = "YOUR_CLIENT_ID";
42+
x.Maskinporten.EncodedJwk = "YOUR_ENCODED_JWK";
43+
x.Maskinporten.Environment = "test";
44+
x.Maskinporten.Scope = "digdir:dialogporten.serviceprovider digdir:dialogporten.serviceprovider.search";
45+
}
46+
```
2047

21-
```json
48+
#### Register through [options instance parameter](https://learn.microsoft.com/en-us/dotnet/core/extensions/options-library-authors#options-instance-parameter):
49+
```csharp
50+
var builder = WebApplication.CreateBuilder(args);
51+
var dialogportenSettings = builder.Configuration
52+
.GetSection("DialogportenSettings")
53+
.Get<DialogportenSettings>()!;
54+
builder.Services.AddDialogportenClient(dialogportenSettings);
55+
```
56+
In this case, the configuration should look like this:
57+
```json5
2258
{
23-
"dialogportenSettings": {
24-
"BaseUri": "",
25-
"ThrowOnPublicKeyFetchInit": "",
59+
"DialogportenSettings": {
60+
"BaseUri": "https://platform.tt02.altinn.no/dialogporten",
61+
// "ThrowOnPublicKeyFetchInit": false,
2662
"Maskinporten": {
27-
"ClientId": "",
28-
"Environment": "",
29-
"Scope": "",
30-
"EncodedJwk": ""
63+
"ClientId": "YOUR_CLIENT_ID",
64+
"EncodedJwk": "YOUR_ENCODED_JWK",
65+
"Environment": "test",
66+
"Scope": "digdir:dialogporten.serviceprovider digdir:dialogporten.serviceprovider.search"
3167
}
3268
}
3369
}
3470
```
3571

36-
```C#
37-
var configuration = new ConfigurationBuilder()
38-
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
39-
.Build();
72+
### Available services
73+
The following services are available after registration:
74+
- [`Altinn.ApiClients.Dialogporten.Features.V1.IServiceownerApi`](./Features/V1/RefitterInterface.cs) - Used to interact with the Dialogporten ServiceOwner API.
75+
- [`Altinn.ApiClients.Dialogporten.IDialogTokenValidator`](IDialogTokenValidator.cs) - Used to validate Dialogporten tokens.
4076

41-
var services = new ServiceCollection();
77+
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.
4278

43-
services.AddSingleton<IConfiguration>(configuration);
44-
45-
services.AddDialogportenClient();
46-
```
79+
See [sample project](../Digdir.Library.Dialogporten.WebApiClient.WebApiSample/Program.cs) for examples on how to use the services.

0 commit comments

Comments
 (0)