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

Open Beginner friendly
#536 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
88/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
go

Research direction

Start in pkg/connectors/microcks_client.go at refreshAuthToken and inspect how configCtx resolves the referenced user. Review the refresh test from #487, then add coverage where the context name differs from contexts[].user. Done means refreshed tokens update the referenced server-named user without creating an orphan entry.

Written by the indexing model from the issue text.

Description

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.

Dominant language
Go
Stars
52
Forks
67
Avg merge
1d 1h
Merged PRs (30d)
13

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microcks/microcks-cli

All issues in microcks/microcks-cli

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.