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

OCM-4969 | feat: offline token deprecation messaging #2554

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
37 changes: 37 additions & 0 deletions pkg/ocm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"
"time"

"github.com/golang-jwt/jwt/v4"
sdk "github.com/openshift-online/ocm-sdk-go"
"github.com/sirupsen/logrus"

Expand Down Expand Up @@ -166,6 +167,12 @@ func (b *ClientBuilder) Build() (result *Client, err error) {
return nil, fmt.Errorf("error creating connection. Not able to get authentication token: %s", err)
}

// Only execute if the refresh token has changed. This helps limit warnings for users
// to only on login and when their refresh token is cycled by SSO instead of on every command.
if b.cfg.RefreshToken != refreshToken {
offlineTokenDeprecationWarning(refreshToken)
}

// Persist tokens in the configuration file, the SDK may have refreshed them
err = config.PersistTokens(b.cfg, accessToken, refreshToken)
if err != nil {
Expand Down Expand Up @@ -206,3 +213,33 @@ func (c *Client) KeepTokensAlive() error {

return nil
}

// Prints a deprecation warning if tokens have changed and the new refresh token contains the 'offline_access' scope
// Swallow and log errors as debug as this is a non-essential warning that should not block the user
func offlineTokenDeprecationWarning(refreshToken string) {
const offlineTokenDeprecationMessage = "Logging in with offline tokens is being deprecated and will no longer " +
"be maintained or enhanced. Instead, log in with --use-auth-code or --use-device-code. See 'rosa login --help' " +
"for usage. Learn more about deprecating offline tokens via https://console.redhat.com/openshift/token/rosa"

rprtr := reporter.CreateReporter()
parser := new(jwt.Parser)
token, _, err := parser.ParseUnverified(refreshToken, jwt.MapClaims{})
if err != nil {
rprtr.Debugf("Failed to parse refresh token for deprecation warning: %v", err)
return
}
claims, ok := token.Claims.(jwt.MapClaims)
if !ok {
rprtr.Debugf("Failed to get claims from refresh token for deprecation warning: %v", err)
return
}
scopes, ok := claims["scope"].(string)
if !ok {
rprtr.Debugf("Failed to get scopes from refresh token for deprecation warning: %v", err)
return
}
if strings.Contains(scopes, "offline_access") {
rprtr.Warnf(offlineTokenDeprecationMessage)
return
}
}