Skip to content

token refresh saves tokens under the context name instead of the user name #536

Description

@gyanranjanpanda

Describe the bug

Title: bug: token refresh saves tokens under the context name instead of the user name

Describe the bug

For contexts created with microcks login <server> --name <ctx>, the automatic token refresh saves the new tokens under the context name, but users entries are keyed by the server URL. So it appends a new entry instead of updating the one the context points to.

login keys the user by server URL:

// cmd/login.go
localConfig.UpsertUser(config.User{Name: server, ...})
localConfig.UpsertContext(config.ContextRef{Name: ctxName, Server: server, User: server})

refreshAuthToken writes it back with ctxName:

// pkg/connectors/microcks_client.go:364
localCfg.UpsertUser(config.User{Name: ctxName, AuthToken: authToken, RefreshToken: refreshToken})

Same context-name vs user-name mix up that was fixed in #427 for deleteContext.

How to Reproduce?

  1. microcks login http://localhost:8080 --name dev-context
  2. Let the access token expire (or set auth-token to an expired JWT)
  3. Run any authenticated command
  4. cat ~/.config/microcks/config
contexts:
- name: dev-context
  user: http://localhost:8080     # still points here
users:
- name: http://localhost:8080
  auth-token: <still expired>
  refresh-token: OLD-REFRESH-TOKEN
- name: dev-context               # new entry, nothing resolves it
  auth-token: NEW-ACCESS-TOKEN
  refresh-token: NEW-REFRESH-TOKEN

Login without --name is fine, there the context name and user name are both the server URL.

Actual behavior

  • The context keeps replaying the same refresh token on every command, so once it hits the Keycloak SSO idle timeout you have to log in again (and it fails right away if one time use refresh tokens are on).
  • logout and context --delete only clean the referenced user, so the orphan entry with the newest tokens stays on disk.

Suggested fix

configCtx is already resolved a few lines above:

localCfg.UpsertUser(config.User{Name: configCtx.User.Name, AuthToken: authToken, RefreshToken: refreshToken})

The refresh test in #487 uses a config where contexts[].user equals the context name, so it passes with this bug. A regression test needs the two to differ.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions