Skip to content

Commit

Permalink
Update the console sandbox to support token refreshing/introspection/…
Browse files Browse the repository at this point in the history
…revocation with the resource owner password credentials grant
  • Loading branch information
kevinchalet committed Mar 4, 2024
1 parent cdb80a6 commit 3503ecd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions sandbox/OpenIddict.Sandbox.AspNetCore.Server/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ await manager.CreateAsync(new OpenIddictApplicationDescriptor
Permissions.Endpoints.Token,
Permissions.GrantTypes.AuthorizationCode,
Permissions.GrantTypes.DeviceCode,
Permissions.GrantTypes.Password,
Permissions.GrantTypes.RefreshToken,
Permissions.ResponseTypes.Code,
Permissions.Scopes.Email,
Expand Down
44 changes: 43 additions & 1 deletion sandbox/OpenIddict.Sandbox.Console.Client/InteractiveService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,53 @@ await _service.AuthenticateWithClientCredentialsAsync(new()
CancellationToken = stoppingToken,
ProviderName = provider,
Username = username,
Password = password
Password = password,
Scopes = [Scopes.OfflineAccess]
});

AnsiConsole.MarkupLine("[green]Resource owner password credentials authentication successful:[/]");
AnsiConsole.Write(CreateClaimTable(response.Principal));

// If introspection is supported by the server, ask the user if the access token should be introspected.
var configuration = await _service.GetServerConfigurationByProviderNameAsync(provider, stoppingToken);
if (configuration.IntrospectionEndpoint is not null && await IntrospectAccessTokenAsync(stoppingToken))
{
AnsiConsole.MarkupLine("[steelblue]Claims extracted from the token introspection response:[/]");
AnsiConsole.Write(CreateClaimTable((await _service.IntrospectTokenAsync(new()
{
CancellationToken = stoppingToken,
ProviderName = provider,
Token = response.AccessToken,
TokenTypeHint = TokenTypeHints.AccessToken
})).Principal));
}

// If revocation is supported by the server, ask the user if the access token should be revoked.
if (configuration.RevocationEndpoint is not null && await RevokeAccessTokenAsync(stoppingToken))
{
await _service.RevokeTokenAsync(new()
{
CancellationToken = stoppingToken,
ProviderName = provider,
Token = response.AccessToken,
TokenTypeHint = TokenTypeHints.AccessToken
});

AnsiConsole.MarkupLine("[steelblue]Access token revoked.[/]");
}

// If a refresh token was returned by the authorization server, ask the user
// if the access token should be refreshed using the refresh_token grant.
if (!string.IsNullOrEmpty(response.RefreshToken) && await RefreshTokenAsync(stoppingToken))
{
AnsiConsole.MarkupLine("[steelblue]Claims extracted from the refreshed identity:[/]");
AnsiConsole.Write(CreateClaimTable((await _service.AuthenticateWithRefreshTokenAsync(new()
{
CancellationToken = stoppingToken,
ProviderName = provider,
RefreshToken = response.RefreshToken
})).Principal));
}
}

else if (type is GrantTypes.DeviceCode)
Expand Down

0 comments on commit 3503ecd

Please sign in to comment.