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

fix(oauth2): ensure refresh token exists before using expired credential #349

Merged
merged 2 commits into from
Mar 29, 2023
Merged
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
14 changes: 14 additions & 0 deletions cmd/gateclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,20 @@ func NewGateClient(ui output.Ui, gateEndpoint, defaultHeaders, configLocation st
updatedMessage := ""

if gateClient.Config.Auth != nil && gateClient.Config.Auth.OAuth2 != nil {
// The below will fail if the token is expired and there is no refresh token.
// This may happen if refresh tokens are not supported on the identity provider
if gateClient.Config.Auth.OAuth2.CachedToken != nil {
token := gateClient.Config.Auth.OAuth2.CachedToken

// The valid method below will return true if the token is set and not expired
// So, to check if it is expired. The token has an internal method to do this, and it is done
// as a part of the "Valid" method. So just use that, but ensure we are only checking if there
// is indeed an access token set
if token.AccessToken != "" && !token.Valid() && token.RefreshToken == "" {
gateClient.Config.Auth.OAuth2.CachedToken = nil
}
}

updatedConfig, err = authenticateOAuth2(ui.Output, httpClient, gateClient.GateEndpoint(), gateClient.Config.Auth)
if err != nil {
ui.Error(fmt.Sprintf("OAuth2 Authentication failed: %v", err))
Expand Down