User OAuth tokens and profiles are stored globally instead of being scoped per instance

Open
#738 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
55/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
javascript

Research direction

Start with the OAuth connect and completion flow in server/api.go, then trace the user profile and token storage and lookup functions named in server/plugin.go. Check how the state identifies the instance and how callers retrieve credentials. Done means credentials and profiles can be stored and retrieved independently per instance, with legacy keys still readable for migration.

Written by the indexing model from the issue text.

Description

Summary

User OAuth credentials and profile metadata are stored in the KV store keyed only by the Mattermost userID (<userID>_userinfo and <userID>_gitlabtoken). This prevents users from connecting to more than one GitLab instance and causes immediate token invalidation when switching the default instance.

Steps to Reproduce
  1. Mattermost has two GitLab instances configured.
  2. User connects to Instance A via /gitlab connect.
  3. User attempts to connect to or interact with Instance B.
    • Result: Connecting to Instance B completely overwrites the stored OAuth token and user profile from Instance A.
  4. If an administrator sets Instance B as default, all existing users' tokens remain mapped to Instance A's authority, causing all subsequent API operations to fail with 401 Unauthorized.
Expected Behavior

Users should be able to authenticate with multiple configured GitLab instances independently, with their session tokens stored under instance-scoped keys.

Root Cause / Code References
  • server/plugin.go: storeGitlabUserInfo writes to info.UserID + GitlabUserInfoKey (_userinfo) and storeGitlabUserToken writes to userID + GitlabTokenKey (_gitlabtoken).
  • server/plugin.go: getGitlabUserInfoByMattermostID and getGitlabUserTokenByMattermostID retrieve credentials using only userID.
  • server/api.go: OAuth state parameter is formatted as model.NewId()[0:15] + "_" + userID without any instance identifier, preventing /oauth/complete from knowing which instance issued the authorization code.
Proposed Fix
  1. Include target instanceName in the OAuth state token during /oauth/connect.
  2. Namespace user KV storage keys by instance name: <userID>_<instanceName>_userinfo and <userID>_<instanceName>_gitlabtoken.
  3. Provide a fallback reader to lazily migrate legacy un-namespaced keys for existing installations.
Dominant language
JavaScript
Stars
158
Forks
97
Avg merge
2d 17h
Merged PRs (30d)
21

Getting set up

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 mattermost/mattermost-plugin-gitlab

All issues in mattermost/mattermost-plugin-gitlab

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.