Skip to content

Commit 95a5b74

Browse files
committed
Logout user if renewing via API token failed
1 parent 9b26ce7 commit 95a5b74

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

pkg/platform/authentication/auth.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,18 @@ func (s *Auth) Sync() error {
122122

123123
// MaybeRenew will renew the JWT if it has expired
124124
// This should only be called from the state-svc.
125-
func (s *Auth) MaybeRenew() error {
125+
func (s *Auth) MaybeRenew() (rerr error) {
126+
defer func() {
127+
if rerr == nil {
128+
return
129+
}
130+
131+
var errUnauthorized *apiAuth.PostLoginUnauthorized
132+
if errors.As(rerr, &errUnauthorized) {
133+
logging.Warning("API token invalid, clearing stored token: %s", errUnauthorized.Error())
134+
rerr = s.Logout()
135+
}
136+
}()
126137
// If we're out of sync then we should just always renew
127138
if s.SyncRequired() {
128139
err := s.Sync()
@@ -185,6 +196,7 @@ func (s *Auth) updateRollbarPerson() {
185196
}
186197

187198
func (s *Auth) resetSession() {
199+
s.client = nil
188200
s.clientAuth = nil
189201
s.lastRenewal = nil
190202
s.bearerToken = ""
@@ -359,10 +371,7 @@ func (s *Auth) Logout() error {
359371
return locale.WrapError(err, "err_logout_cfg", "Could not update config, if this persists please try running '[ACTIONABLE]state clean config[/RESET]'.")
360372
}
361373

362-
s.client = nil
363-
s.clientAuth = nil
364-
s.bearerToken = ""
365-
s.user = nil
374+
s.resetSession()
366375

367376
// This is a bit of a hack, but it's safe to assume that the global legacy use-case should be reset whenever we logout a specific instance
368377
// Handling it any other way would be far too error-prone by comparison

0 commit comments

Comments
 (0)