Class RefreshTokenService<TRequest, TResponse>
implement this class to define your own refresh token endpoints.
Inherited Members
Namespace: FastEndpoints.Security
Assembly: FastEndpoints.Security.dll
Syntax
public abstract class RefreshTokenService<TRequest, TResponse> : Endpoint<TRequest, TResponse>, IEndpoint, IEventBus, IServiceResolverBase where TRequest : TokenRequest, new() where TResponse : TokenResponse, new()
Type Parameters
Name | Description |
---|---|
TRequest | the type of the request dto that will be accepted by the refresh endpoint |
TResponse | the type of the response dto that will be sent by the refresh endpoint |
Methods
CreateCustomToken<T>(string, Action<UserPrivileges>, Func<TResponse, T>, bool, object?)
create a token response and map it to a different type. useful if you need to create the token manually by yourself.
Declaration
public Task<T> CreateCustomToken<T>(string userId, Action<UserPrivileges> privileges, Func<TResponse, T> map, bool isRenewal = false, object? request = null)
Parameters
Type | Name | Description |
---|---|---|
string | userId | the id of the user to create the token for |
Action<UserPrivileges> | privileges | the user privileges to be embedded in the jwt such as roles/claims/permissions |
Func<TResponse, T> | map | a func that maps properties from |
bool | isRenewal | specify if this is an initial login request or a renewal/refresh request |
object | request | the request dto |
Returns
Type | Description |
---|---|
Task<T> |
Type Parameters
Name | Description |
---|---|
T | the type to map to |
OnAfterInitialTokenCreationAsync(object?, TResponse)
a hook for modifying the created token response when a login request comes in.
Declaration
public virtual Task OnAfterInitialTokenCreationAsync(object? request, TResponse response)
Parameters
Type | Name | Description |
---|---|---|
object | request | the request dto. maybe null unless you supply it to the CreateTokenWith<TService>(string, Action<UserPrivileges>, TRequest?) method. |
TResponse | response | the token response dto that is created |
Returns
Type | Description |
---|---|
Task |
OnAfterRenewalTokenCreationAsync(TRequest?, TResponse)
a hook for modifying the created token response when a renewal request comes in.
Declaration
public virtual Task OnAfterRenewalTokenCreationAsync(TRequest? request, TResponse response)
Parameters
Type | Name | Description |
---|---|---|
TRequest | request | the request dto. maybe null unless you supply it to the CreateTokenWith<TService>(string, Action<UserPrivileges>, TRequest?) method. |
TResponse | response | the token response dto that is created |
Returns
Type | Description |
---|---|
Task |
OnBeforeInitialTokenCreationAsync(JwtCreationOptions, object?)
a hook for modifying jwt creation options per request when a login request comes in. this method is called right before the actual jwt token is created allowing you to override token creation parameters per request if needed.
Declaration
public virtual Task OnBeforeInitialTokenCreationAsync(JwtCreationOptions jwtOptions, object? request)
Parameters
Type | Name | Description |
---|---|---|
JwtCreationOptions | jwtOptions | jwt token creation options which you can modify per request |
object | request | the request dto. maybe null unless you supply it to the CreateTokenWith<TService>(string, Action<UserPrivileges>, TRequest?) method. |
Returns
Type | Description |
---|---|
Task |
OnBeforeRenewalTokenCreationAsync(JwtCreationOptions, TRequest?)
a hook for modifying jwt creation options per request when a renewal request comes in. this method is called right before the actual jwt token is created allowing you to override token creation parameters per request if needed.
Declaration
public virtual Task OnBeforeRenewalTokenCreationAsync(JwtCreationOptions jwtOptions, TRequest? request)
Parameters
Type | Name | Description |
---|---|---|
JwtCreationOptions | jwtOptions | jwt token creation options which you can modify per request |
TRequest | request | the request dto. maybe null unless you supply it to the CreateTokenWith<TService>(string, Action<UserPrivileges>, TRequest?) method. |
Returns
Type | Description |
---|---|
Task |
PersistTokenAsync(TResponse)
this method will be called whenever a new access/refresh token pair is being generated. store the tokens and expiry dates however you wish for the purpose of verifying future refresh requests.
Declaration
public abstract Task PersistTokenAsync(TResponse response)
Parameters
Type | Name | Description |
---|---|---|
TResponse | response | the response dto containing the tokens that's about to be sent to the requesting client |
Returns
Type | Description |
---|---|
Task |
RefreshRequestValidationAsync(TRequest)
validate the incoming refresh request by checking the token and expiry against the previously stored data.
if the token is not valid and a new token pair should not be created, simply add validation errors using the AddError()
method.
the failures you add will be sent to the requesting client. if no failures are added, validation passes and a new token pair will be created and sent to
the client.
Declaration
public abstract Task RefreshRequestValidationAsync(TRequest req)
Parameters
Type | Name | Description |
---|---|---|
TRequest | req | the incoming refresh request dto |
Returns
Type | Description |
---|---|
Task |
SetRenewalPrivilegesAsync(TRequest, UserPrivileges)
specify the user privileges to be embedded in the jwt when a refresh request is received and validation has passed. this only applies to renewal/refresh requests received to the refresh endpoint and not the initial jwt creation.
Declaration
public abstract Task SetRenewalPrivilegesAsync(TRequest request, UserPrivileges privileges)
Parameters
Type | Name | Description |
---|---|---|
TRequest | request | the request dto received from the client |
UserPrivileges | privileges | the user privileges to be embedded in the jwt such as roles/claims/permissions |
Returns
Type | Description |
---|---|
Task |
Setup(Action<RefreshServiceOptions>)
configure the refresh token service options
Declaration
public void Setup(Action<RefreshServiceOptions> options)
Parameters
Type | Name | Description |
---|---|---|
Action<RefreshServiceOptions> | options | action to be performed on the refresh service options object |