Skip to content

Commit e41f5ff

Browse files
authored
Merge pull request ActiveState#3370 from ActiveState/DX-2922
Fix auth error interrupts user
2 parents 82e16de + 95a5b74 commit e41f5ff

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

cmd/state/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,10 @@ func run(args []string, isInteractive bool, cfg *config.Instance, out output.Out
168168
if auth.AvailableAPIToken() != "" {
169169
jwt, err := svcmodel.GetJWT(context.Background())
170170
if err != nil {
171-
return locale.NewError("err_main_jwt", "", errs.JoinMessage(err))
171+
multilog.Critical("Could not get JWT: %v", errs.JoinMessage(err))
172+
} else {
173+
auth.UpdateSession(jwt)
172174
}
173-
auth.UpdateSession(jwt)
174175
}
175176

176177
projectfile.RegisterMigrator(migrator.NewMigrator(auth, cfg))

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)