-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostConfigureOAuth2IntrospectionOptions.cs
More file actions
73 lines (61 loc) · 3.1 KB
/
PostConfigureOAuth2IntrospectionOptions.cs
File metadata and controls
73 lines (61 loc) · 3.1 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
// Copyright (c) Duende Software. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
using Duende.IdentityModel.Client;
using IdentityModel.AspNetCore.OAuth2Introspection.Infrastructure;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Options;
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace IdentityModel.AspNetCore.OAuth2Introspection
{
internal class PostConfigureOAuth2IntrospectionOptions : IPostConfigureOptions<OAuth2IntrospectionOptions>
{
private readonly IDistributedCache _cache;
private readonly IHttpClientFactory _httpClientFactory;
public PostConfigureOAuth2IntrospectionOptions(IHttpClientFactory httpClientFactory, IDistributedCache cache = null)
{
_cache = cache;
_httpClientFactory = httpClientFactory;
}
public void PostConfigure(string name, OAuth2IntrospectionOptions options)
{
options.Validate();
if (options.EnableCaching && _cache == null)
{
throw new ArgumentException("Caching is enabled, but no IDistributedCache is found in the services collection", nameof(_cache));
}
options.IntrospectionClient = new AsyncLazy<HttpClient>(() => InitializeIntrospectionClient(options));
}
private async Task<HttpClient> InitializeIntrospectionClient(OAuth2IntrospectionOptions options)
{
if (!options.IntrospectionEndpoint.IsPresent())
{
options.IntrospectionEndpoint = await GetIntrospectionEndpointFromDiscoveryDocument(options).ConfigureAwait(false);
}
return _httpClientFactory.CreateClient(OAuth2IntrospectionDefaults.BackChannelHttpClientName);
}
private async Task<string> GetIntrospectionEndpointFromDiscoveryDocument(OAuth2IntrospectionOptions options)
{
var client = _httpClientFactory.CreateClient(OAuth2IntrospectionDefaults.BackChannelHttpClientName);
var disco = await client.GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest
{
Address = options.Authority,
Policy = options?.DiscoveryPolicy ?? new DiscoveryPolicy()
}).ConfigureAwait(false);
if (disco.IsError)
{
switch (disco.ErrorType)
{
case ResponseErrorType.Http:
throw new InvalidOperationException($"Discovery endpoint {options.Authority} is unavailable: {disco.Error}");
case ResponseErrorType.PolicyViolation:
throw new InvalidOperationException($"Policy error while contacting the discovery endpoint {options.Authority}: {disco.Error}");
case ResponseErrorType.Exception:
throw new InvalidOperationException($"Error parsing discovery document from {options.Authority}: {disco.Error}");
}
}
return disco.IntrospectionEndpoint;
}
}
}