Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Duende.AccessTokenManagement instead of IdentityModel.AspNetCore #403

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions IdentityServer/v6/docs/content/bff/extensibility/tokens.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,44 @@ If you do not use server-side sessions, then the access and refresh token will b
This would involve two steps

* turn off the *SaveTokens* flag on the OpenID Connect handler and handle the relevant events manually to store the tokens in your custom store
* implement and register the *IdentityModel.AspNetCore.AccessTokenManagement.IUserAccessTokenStore* interface
* implement and register the *Duende.AccessTokenManagement.IUserTokenStore* interface

The interface is responsible to storing, retrieving and clearing tokens for the automatic token management:

```cs
public interface IUserAccessTokenStore
public interface IUserTokenStore
{
/// <summary>
/// Stores tokens
/// </summary>
/// <param name="user">User the tokens belong to</param>
/// <param name="accessToken">The access token</param>
/// <param name="expiration">The access token expiration</param>
/// <param name="refreshToken">The refresh token (optional)</param>
/// <param name="token"></param>
/// <param name="parameters">Extra optional parameters</param>
/// <returns></returns>
Task StoreTokenAsync(ClaimsPrincipal user, string accessToken, DateTimeOffset expiration, string refreshToken = null, UserAccessTokenParameters parameters = null);
Task StoreTokenAsync(
ClaimsPrincipal user,
UserToken token,
UserTokenRequestParameters? parameters = null);

/// <summary>
/// Retrieves tokens from store
/// </summary>
/// <param name="user">User the tokens belong to</param>
/// <param name="parameters">Extra optional parameters</param>
/// <returns>access and refresh token and access token expiration</returns>
Task<UserAccessToken> GetTokenAsync(ClaimsPrincipal user, UserAccessTokenParameters parameters = null);
Task<UserToken> GetTokenAsync(
ClaimsPrincipal user,
UserTokenRequestParameters? parameters = null);

/// <summary>
/// Clears the stored tokens for a given user
/// </summary>
/// <param name="user">User the tokens belong to</param>
/// <param name="parameters">Extra optional parameters</param>
/// <returns></returns>
Task ClearTokenAsync(ClaimsPrincipal user, UserAccessTokenParameters parameters = null);
Task ClearTokenAsync(
ClaimsPrincipal user,
UserTokenRequestParameters? parameters = null);
}
```

Expand Down
2 changes: 1 addition & 1 deletion IdentityServer/v6/docs/content/tokens/refresh.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var response = await client.RequestRefreshTokenAsync(new RefreshTokenRequest
});
```

The [IdentityModel.AspNetCore](https://identitymodel.readthedocs.io/en/latest/aspnetcore/web.html) library can be used to automate refresh & access token lifetime management in ASP.NET Core.
The [Duende.AccessTokenManagement](https://github.com/DuendeSoftware/Duende.AccessTokenManagement/wiki) library can be used to automate refresh & access token lifetime management in ASP.NET Core.

## Refresh token security considerations
Refresh tokens are a high-value target for attackers, because they typically have a much higher lifetime than access tokens.
Expand Down
21 changes: 13 additions & 8 deletions IdentityServer/v7/docs/content/bff/extensibility/tokens.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,44 @@ If you do not use server-side sessions, then the access and refresh token will b
This would involve two steps

* turn off the *SaveTokens* flag on the OpenID Connect handler and handle the relevant events manually to store the tokens in your custom store
* implement and register the *IdentityModel.AspNetCore.AccessTokenManagement.IUserAccessTokenStore* interface
* implement and register the *Duende.AccessTokenManagement.IUserTokenStore* interface

The interface is responsible to storing, retrieving and clearing tokens for the automatic token management:

```cs
public interface IUserAccessTokenStore
public interface IUserTokenStore
{
/// <summary>
/// Stores tokens
/// </summary>
/// <param name="user">User the tokens belong to</param>
/// <param name="accessToken">The access token</param>
/// <param name="expiration">The access token expiration</param>
/// <param name="refreshToken">The refresh token (optional)</param>
/// <param name="token"></param>
/// <param name="parameters">Extra optional parameters</param>
/// <returns></returns>
Task StoreTokenAsync(ClaimsPrincipal user, string accessToken, DateTimeOffset expiration, string refreshToken = null, UserAccessTokenParameters parameters = null);
Task StoreTokenAsync(
ClaimsPrincipal user,
UserToken token,
UserTokenRequestParameters? parameters = null);

/// <summary>
/// Retrieves tokens from store
/// </summary>
/// <param name="user">User the tokens belong to</param>
/// <param name="parameters">Extra optional parameters</param>
/// <returns>access and refresh token and access token expiration</returns>
Task<UserAccessToken> GetTokenAsync(ClaimsPrincipal user, UserAccessTokenParameters parameters = null);
Task<UserToken> GetTokenAsync(
ClaimsPrincipal user,
UserTokenRequestParameters? parameters = null);

/// <summary>
/// Clears the stored tokens for a given user
/// </summary>
/// <param name="user">User the tokens belong to</param>
/// <param name="parameters">Extra optional parameters</param>
/// <returns></returns>
Task ClearTokenAsync(ClaimsPrincipal user, UserAccessTokenParameters parameters = null);
Task ClearTokenAsync(
ClaimsPrincipal user,
UserTokenRequestParameters? parameters = null);
}
```

Expand Down
6 changes: 3 additions & 3 deletions IdentityServer/v7/docs/content/samples/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ Key takeaways:
[link to source code]({{< param samples_base >}}/Basics/MvcBasic)

### MVC Client with automatic Access Token Management
This sample shows how to use [IdentityModel.AspNetCore](https://identitymodel.readthedocs.io/en/latest/aspnetcore/overview.html) to automatically manage access tokens.
This sample shows how to use [Duende.AccessTokenManagement](https://github.com/DuendeSoftware/Duende.AccessTokenManagement/wiki) to automatically manage access tokens.

The sample uses a special client ID in the sample IdentityServer with a short token lifetime (75 seconds). When repeating the API call, make sure you inspect the returned *iat* and *exp* claims to observer how the token is slides.
The sample uses a special client in the sample IdentityServer with a short token lifetime (75 seconds). When repeating the API call, make sure you inspect the returned *iat* and *exp* claims to observer how the token is slides.

You can also turn on debug tracing to get more insights in the token management library.

Key takeaways:

* use IdentityModel.AspNetCore to automate refreshing tokens
* use Duende.AccessTokenManagement to automate refreshing tokens

[link to source code]({{< param samples_base >}}/Basics/MvcTokenManagement)

Expand Down
2 changes: 1 addition & 1 deletion IdentityServer/v7/docs/content/tokens/refresh.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var response = await client.RequestRefreshTokenAsync(new RefreshTokenRequest
});
```

The [IdentityModel.AspNetCore](https://identitymodel.readthedocs.io/en/latest/aspnetcore/web.html) library can be used to automate refresh & access token lifetime management in ASP.NET Core.
The [Duende.AccessTokenManagement](https://github.com/DuendeSoftware/Duende.AccessTokenManagement/wiki) library can be used to automate refresh & access token lifetime management in ASP.NET Core.

## Refresh token security considerations
Refresh tokens are a high-value target for attackers, because they typically have a much higher lifetime than access tokens.
Expand Down
4 changes: 2 additions & 2 deletions IdentityServer/v7/docs/content/tokens/requesting.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ var response = await client.RequestClientCredentialsTokenAsync(new ClientCredent
```

### Automating token requests in ASP.NET Core and Worker applications
The [IdentityModel.AspNetCore](https://identitymodel.readthedocs.io/en/latest/aspnetcore/worker.html) library can automate client credential request and token lifetime management for you.
The [Duende.AccessTokenManagement](https://github.com/DuendeSoftware/Duende.AccessTokenManagement/wiki) library can automate client credential request and token lifetime management for you.

Using this library, you only need to register the token client in DI:

Expand Down Expand Up @@ -220,4 +220,4 @@ public void ConfigureServices(IServiceCollection services)
```

### Automating token management in ASP.NET Core
The [IdentityModel.AspNetCore](https://identitymodel.readthedocs.io/en/latest/aspnetcore/web.html) library can also be used to automate token lifetime management in ASP.NET Core applications for you.
The [Duende.AccessTokenManagement](https://github.com/DuendeSoftware/Duende.AccessTokenManagement/wiki) library can also be used to automate token lifetime management in ASP.NET Core applications for you.
Loading