Skip to content

Commit 396eb4b

Browse files
authored
Merge pull request #818 from DuendeSoftware/wca/introspection-foss-docs
Added docs for the OAuth2introspection library
2 parents a6f3a95 + 90626ff commit 396eb4b

5 files changed

Lines changed: 278 additions & 6 deletions

File tree

astro.config.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,12 @@ export default defineConfig({
214214
autogenerate: { directory: "identitymodel-oidcclient" },
215215
collapsed: true,
216216
},
217+
{
218+
label: "Introspection for ASP.NET Core",
219+
badge: "oss",
220+
autogenerate: { directory: "introspection" },
221+
collapsed: true,
222+
},
217223
],
218224
}),
219225
redirectFrom({

src/content/docs/identityserver/apis/aspnetcore/reference.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ redirect_from:
1313

1414
If you are using [reference tokens](/identityserver/tokens/reference.md), you need an authentication handler that
1515
implements the back-channel validation via the [OAuth 2.0 token introspection](https://tools.ietf.org/html/rfc7662)
16-
protocol, e.g. [this](https://github.com/IdentityModel/IdentityModel.AspNetCore.OAuth2Introspection) one:
16+
protocol, e.g. [Duende.AspNetCore.Authentication.OAuth2Introspection](/introspection/index.mdx):
1717

18-
```cs
18+
```csharp
19+
// Program.cs
1920
builder.Services.AddAuthentication("token")
2021
.AddOAuth2Introspection("token", options =>
2122
{
@@ -32,7 +33,8 @@ builder.Services.AddAuthentication("token")
3233
It is not uncommon to use the same API with both JWTs and reference tokens. In this case you set up two authentication
3334
handlers, make one the default handler and provide some forwarding logic, e.g.:
3435

35-
```cs
36+
```csharp
37+
// Program.cs
3638
builder.Services.AddAuthentication("token")
3739

3840
// JWT tokens
@@ -59,7 +61,8 @@ builder.Services.AddAuthentication("token")
5961

6062
The logic of the forward selector looks like this:
6163

62-
```cs
64+
```csharp
65+
// IntrospectionUtilities.cs
6366
/// <summary>
6467
/// Provides a forwarding func for JWT vs reference tokens (based on existence of dot in token)
6568
/// </summary>

src/content/docs/index.mdx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ import {
3333
Badge,
3434
} from "@astrojs/starlight/components";
3535
import Newsletter from "~/components/Newsletter.astro";
36-
import TestimonialGrid from "~/components/testimonial-grid.astro";
37-
import Testimonial from "~/components/testimonial.astro";
3836

3937
<CardGrid>
4038
<Card title="Install templates" icon="rocket">
@@ -93,6 +91,12 @@ import Testimonial from "~/components/testimonial.astro";
9391
Learn more
9492
</LinkButton>
9593
</Card>
94+
<Card title="Introspection for ASP.NET Core" icon="github">
95+
<Badge text={"OSS"} /> ASP.NET Core authentication handler for OAuth 2.0 token introspection.
96+
<LinkButton href="/introspection/" variant="secondary">
97+
Learn more
98+
</LinkButton>
99+
</Card>
96100
</CardGrid>
97101

98102
<hr />
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
---
2+
title: Duende Introspection Authentication Handler
3+
description: An ASP.NET Core authentication handler for OAuth 2.0 token introspection.
4+
sidebar:
5+
label: Overview
6+
order: 1
7+
---
8+
9+
import { CardGrid, LinkCard } from "@astrojs/starlight/components";
10+
11+
The `Duende.AspNetCore.Authentication.OAuth2Introspection` library implements an ASP.NET Core authentication handler for
12+
OAuth 2.0 token introspection, as defined in [RFC 7662](https://datatracker.ietf.org/doc/html/rfc7662), "OAuth 2.0 Token Introspection".
13+
14+
## Use Case
15+
16+
Using this library, you can request information about access tokens from an authorization server, allowing your
17+
ASP.NET Core application to validate tokens and retrieve metadata about them. This enables you to use opaque tokens in
18+
your application, where the token itself does not carry any information about the user or the scopes granted, but instead
19+
relies on the authorization server to provide this information.
20+
21+
:::tip
22+
By default, you can also use this library to introspect JWT tokens.
23+
You can disable this behavior by setting `SkipTokensWithDots = true` in the [`OAuth2IntrospectionOptions`](/introspection-auth-handler/options.mdx).
24+
:::
25+
26+
## Features
27+
28+
- Implements the OAuth 2.0 token introspection protocol
29+
- Supports both opaque and JWT access token introspection
30+
- Supports caching of introspection results to reduce load on the authorization server
31+
- Provides a customizable authentication handler for ASP.NET Core
32+
- Integrates seamlessly with ASP.NET Core's authentication middleware
33+
34+
## Installation
35+
36+
If you want to use the `Duende.AspNetCore.Authentication.OAuth2Introspection` library, you need to add the NuGet package
37+
to your ASP.NET Core project.
38+
39+
You can achieve this by running the following command in your terminal:
40+
41+
```bash
42+
dotnet package add Duende.AspNetCore.Authentication.OAuth2Introspection
43+
```
44+
45+
## Configuration
46+
47+
To configure the OAuth 2.0 token introspection handler in your ASP.NET Core application, you need to add it to the
48+
authentication services in your `Startup.cs` or `Program.cs` file.
49+
50+
Here's an example on how to set it up:
51+
52+
```csharp
53+
// Program.cs
54+
using Duende.AspNetCore.Authentication.OAuth2Introspection;
55+
56+
builder.Services.AddAuthentication(OAuth2IntrospectionDefaults.AuthenticationScheme)
57+
.AddOAuth2Introspection(options =>
58+
{
59+
// Replace with your authorization server's URL
60+
options.Authority = "https://demo.duendesoftware.com";
61+
62+
options.ClientId = "client_id_for_introspection_endpoint";
63+
options.ClientSecret = "client_secret_for_introspection_endpoint";
64+
});
65+
```
66+
67+
More details on the available options can be found on the [options page](/introspection-auth-handler/options.mdx).
68+
69+
### Configuring the Backchannel HTTP Client
70+
71+
You can configure the HTTP client used by the introspection handler to make requests to the introspection endpoint, for example,
72+
when your ASP.NET Core application lives behind a proxy or requires specific HTTP client settings.
73+
74+
```csharp
75+
// Program.cs
76+
builder.Services.AddHttpClient(OAuth2IntrospectionDefaults.BackchannelHttpClientName)
77+
.AddHttpMessageHandler(() =>
78+
{
79+
// Configure client/handler for the back channel HTTP Client here
80+
return new HttpClientHandler
81+
{
82+
UseProxy = true,
83+
Proxy = new WebProxy(WebProxyUri, true)
84+
};
85+
});
86+
```
87+
88+
## License and Feedback
89+
90+
`Duende.AspNetCore.Authentication.OAuth2Introspection` is released as open source under the
91+
[Apache 2.0 license](https://github.com/DuendeSoftware/foss/blob/main/LICENSE).
92+
Bug reports and contributions are welcome at
93+
[the GitHub repository](https://github.com/DuendeSoftware/foss).
94+
95+
<CardGrid>
96+
<LinkCard
97+
href="https://github.com/DuendeSoftware/foss/tree/main/introspection"
98+
title="GitHub Repository"
99+
description="View the source code for this library on GitHub."
100+
/>
101+
<LinkCard
102+
href="https://www.nuget.org/packages/Duende.AspNetCore.Authentication.OAuth2Introspection"
103+
title="NuGet Package"
104+
description="View the package on NuGet.org."
105+
/>
106+
</CardGrid>
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
---
2+
title: Configuring OAuth 2.0 Token Introspection
3+
description: Learn more about the various options when adding the ASP.NET Core authentication handler for OAuth 2.0 token introspection.
4+
sidebar:
5+
label: Options
6+
order: 2
7+
---
8+
9+
#### OAuth2IntrospectionOptions
10+
11+
`OAuth2IntrospectionOptions` is the options class to configure the ASP.NET Core authentication handler for OAuth 2.0 token introspection.
12+
13+
You set the options when registering the authentication handler at startup time, using a lambda expression in the `AddOAuth2Introspection` method:
14+
15+
```csharp
16+
// Program.cs
17+
builder.Services.AddAuthentication(OAuth2IntrospectionDefaults.AuthenticationScheme)
18+
.AddOAuth2Introspection(options =>
19+
{
20+
// configure options here...
21+
});
22+
```
23+
24+
## Main
25+
26+
Top-level settings. Available directly on the `OAuth2IntrospectionOptions` class.
27+
28+
* **`Authority`**
29+
30+
The URL of the token server. When configured, the handler will use this URI to discover the introspection endpoint.
31+
32+
* **`IntrospectionEndpoint`**
33+
34+
Sets the URL of the introspection endpoint. If this is set, the `Authority` will not be used to discover the introspection endpoint.
35+
36+
* **`ClientId`**
37+
38+
Specifies the ID of the introspection client. This setting is required.
39+
40+
* **`ClientSecret`**
41+
42+
Specifies the shared secret of the introspection client.
43+
44+
* **`ClientCredentialStyle`**
45+
46+
Specifies how the client credentials are sent to the introspection endpoint. The default is `ClientCredentialStyle.PostBody`, which sends the credentials in the body of the request.
47+
You can also set this to `ClientCredentialStyle.AuthorizationHeader` to send the credentials in the `Authorization` header as a Basic authentication scheme.
48+
49+
* **`ClientCredentialStyle `**
50+
51+
Specifies how the authorization header is formatted when used. The default is `BasicAuthenticationHeaderStyle.Rfc2617`, which formats the header according to the [original basic authentication spec](https://tools.ietf.org/html/rfc2617#section-2).
52+
You can also set this to `BasicAuthenticationHeaderStyle.Rfc6749`, which formats the header according to [RFC 6749](https://tools.ietf.org/html/rfc6749#section-2.3.1).
53+
54+
* **`TokenTypeHint`**
55+
56+
Specifies the token type hint of the introspection client. Defaults to `"access_token"`.
57+
58+
* **`NameClaimType`**
59+
60+
Specifies the claim type to use for the name claim. Defaults to `"name"`.
61+
62+
* **`RoleClaimType`**
63+
64+
Specifies the claim type to use for the role claim. Defaults to `"role"`.
65+
66+
* **`AuthenticationType`**
67+
68+
Specifies the authentication type to use for the authenticated identity. If not set, the authentication scheme name is used as the authentication type.
69+
Defaults to `null`.
70+
71+
* **`DiscoveryPolicy`**
72+
73+
Specifies the policy used for the discovery client.
74+
75+
* **`SkipTokensWithDots`**
76+
77+
Specifies whether to skip tokens that contain dots (`.`) in the introspection request. Defaults to `false`.
78+
79+
* **`SaveToken`**
80+
81+
Specifies whether the token should be stored in the context, so it is available for the duration of the HTTP request. Defaults to `true`.
82+
83+
* **`EnableCaching`**
84+
85+
Specifies whether the outcome of the token validation should be cached.
86+
87+
This reduces the number of requests to the introspection endpoint, improving performance and reducing load on the authorization server.
88+
Defaults to `false`.
89+
90+
* **`CacheDuration`**
91+
92+
Specifies for how long the outcome of the token validation should be cached. Defaults to `TimeSpan.FromMinutes(5)`.
93+
94+
* **`CacheKeyPrefix`**
95+
96+
Specifies the prefix to use for the cache key. Defaults to `string.Empty`.
97+
98+
* **`CacheKeyGenerator`**
99+
100+
Specifies the method to use for generating the cache key.
101+
Defaults to `CacheUtils.CacheKeyFromToken`, which generates a cache key using the configured `CacheKeyPrefix` combined with the SHA-256 hash from the token.
102+
103+
* **`TokenRetriever`**
104+
105+
Specifies the method to use for retrieving the token from the HTTP request.
106+
107+
Defaults to `TokenRetrieval.FromAuthorizationHeader`, which retrieves the token from the `Authorization` header.
108+
You can also set this to `TokenRetrieval.FromQueryString`, which retrieves the token from the query string, or use a custom method.
109+
110+
* **`Events`**
111+
112+
Gets or sets the `OAuth2IntrospectionEvents` instance used to handle authentication events.
113+
114+
## Events
115+
116+
The `OAuth2IntrospectionEvents` class allows you to handle various events during the authentication process.
117+
You can override methods to customize the behavior of the authentication handler, or set custom logic for specific events like `OnTokenValidated`, `OnAuthenticationFailed`, etc.
118+
119+
* **`OnTokenValidated`**
120+
121+
This event is triggered when the token has been successfully validated. You can use this to add additional claims or perform custom logic after the token validation.
122+
123+
* **`OnAuthenticationFailed`**
124+
125+
This event is triggered when exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
126+
127+
* **`OnTokenValidated`**
128+
129+
This event is triggered after the security token has passed validation and a `ClaimsIdentity` has been generated.
130+
131+
* **`OnUpdateClientAssertion`**
132+
133+
This event is triggered when client assertion need to be updated.
134+
135+
* **`OnSendingRequest`**
136+
137+
This event is triggered when sending the token introspection request.
138+
139+
* **`AuthenticationFailed`**
140+
141+
Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
142+
143+
* **`TokenValidated`**
144+
145+
Invoked after the security token has passed validation and a ClaimsIdentity has been generated.
146+
147+
* **`UpdateClientAssertion`**
148+
149+
Invoked when client assertion need to be updated.
150+
151+
* **`SendingRequest`**
152+
153+
Invoked when sending the token introspection request.

0 commit comments

Comments
 (0)