-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathHttpClientDynamicRegistrationExtensions.cs
More file actions
83 lines (78 loc) · 4.49 KB
/
Copy pathHttpClientDynamicRegistrationExtensions.cs
File metadata and controls
83 lines (78 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using LtiAdvantage.DynamicRegistration;
namespace LtiAdvantage.IdentityModel.Client
{
/// <summary>
/// HttpClient extensions implementing the tool side of LTI Dynamic Registration 1.0.
/// </summary>
public static class HttpClientDynamicRegistrationExtensions
{
/// <summary>
/// Fetches the platform's OpenID configuration document (<see cref="PlatformOpenIdConfiguration"/>).
/// </summary>
/// <param name="client">The HTTP client.</param>
/// <param name="openIdConfigurationUrl">The URL passed by the platform in the registration init launch.</param>
/// <param name="cancellationToken">Optional cancellation token.</param>
public static async Task<PlatformOpenIdConfiguration> GetPlatformOpenIdConfigurationAsync(
this HttpClient client, string openIdConfigurationUrl, CancellationToken cancellationToken = default)
{
if (client == null) throw new ArgumentNullException(nameof(client));
if (string.IsNullOrWhiteSpace(openIdConfigurationUrl))
throw new ArgumentException("URL is required", nameof(openIdConfigurationUrl));
using var response = await client.GetAsync(openIdConfigurationUrl, cancellationToken).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
{
var errorBody = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
throw new HttpRequestException(
$"Dynamic Registration failed: {(int)response.StatusCode} {response.ReasonPhrase}. Body: {errorBody}");
}
var body = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
return JsonSerializer.Deserialize<PlatformOpenIdConfiguration>(body);
}
/// <summary>
/// POSTs a tool registration request to the platform's <c>registration_endpoint</c>
/// using the bearer registration token supplied during the registration init launch.
/// Returns the platform-assigned <see cref="ToolConfiguration"/> (echoed config + <c>client_id</c>).
/// </summary>
/// <param name="client">The HTTP client.</param>
/// <param name="registrationEndpoint">The platform's registration endpoint URL.</param>
/// <param name="registrationAccessToken">Bearer token from the registration init launch.</param>
/// <param name="tool">The tool configuration to register.</param>
/// <param name="cancellationToken">Optional cancellation token.</param>
public static async Task<ToolConfiguration> RegisterToolAsync(
this HttpClient client,
string registrationEndpoint,
string registrationAccessToken,
ToolConfiguration tool,
CancellationToken cancellationToken = default)
{
if (client == null) throw new ArgumentNullException(nameof(client));
if (string.IsNullOrWhiteSpace(registrationEndpoint))
throw new ArgumentException("URL is required", nameof(registrationEndpoint));
if (string.IsNullOrWhiteSpace(registrationAccessToken))
throw new ArgumentException("Token is required", nameof(registrationAccessToken));
if (tool == null) throw new ArgumentNullException(nameof(tool));
var json = JsonSerializer.Serialize(tool);
using var request = new HttpRequestMessage(HttpMethod.Post, registrationEndpoint)
{
Content = new StringContent(json, Encoding.UTF8, "application/json")
};
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", registrationAccessToken);
using var response = await client.SendAsync(request, cancellationToken).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
{
var errorBody = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
throw new HttpRequestException(
$"Dynamic Registration failed: {(int)response.StatusCode} {response.ReasonPhrase}. Body: {errorBody}");
}
var body = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
return JsonSerializer.Deserialize<ToolConfiguration>(body);
}
}
}