@@ -13,9 +13,10 @@ import (
16− gitlabProviderName = "GitLab"
16+ gitlabProviderName = "GitLab"
17− gitlabDefaultScope = "openid email"
17+ gitlabDefaultScope = "openid email"
18− gitlabUserClaim = "nickname"
18+ gitlabUserClaim = "nickname"
19+ gitlabProjectPrefix = "project:"
21// GitLabProvider represents a GitLab based Identity Provider
22// GitLabProvider represents a GitLab based Identity Provider
@@ -23,6 +24,8 @@ type GitLabProvider struct {
23 *OIDCProvider
24 *OIDCProvider
25 allowedProjects []*gitlabProject
26 allowedProjects []*gitlabProject
27+ // Expose this for unit testing
28+ oidcRefreshFunc func(context.Context, *sessions.SessionState) (bool, error)
28var _ Provider = (*GitLabProvider)(nil)
31var _ Provider = (*GitLabProvider)(nil)
@@ -35,11 +38,14 @@ func NewGitLabProvider(p *ProviderData) *GitLabProvider {
35 p.Scope = gitlabDefaultScope
38 p.Scope = gitlabDefaultScope
41+ oidcProvider := &OIDCProvider{
38 return &GitLabProvider{
46 return &GitLabProvider{
39− OIDCProvider: &OIDCProvider{
47+ OIDCProvider: oidcProvider,
40− ProviderData: p,
48+ oidcRefreshFunc: oidcProvider.RefreshSession,
@@ -245,5 +251,41 @@ func (p *GitLabProvider) getProjectInfo(ctx context.Context, s *sessions.Session
247func formatProject(project *gitlabProject) string {
253func formatProject(project *gitlabProject) string {
248− return fmt.Sprintf("project:%s", project.Name)
254+ return gitlabProjectPrefix + project.Name
257+// RefreshSession refreshes the session with the OIDCProvider implementation
258+// but preserves the custom GitLab projects added in the `EnrichSession` stage.
259+func (p *GitLabProvider) RefreshSession(ctx context.Context, s *sessions.SessionState) (bool, error) {
260+ projects := getSessionProjects(s)
261+ // This will overwrite s.Groups with the new IDToken's `groups` claims
262+ refreshed, err := p.oidcRefreshFunc(ctx, s)
263+ if refreshed && err == nil {
264+ s.Groups = append(s.Groups, projects...)
265+ s.Groups = deduplicateGroups(s.Groups)
267+ return refreshed, err
270+func getSessionProjects(s *sessions.SessionState) []string {
271+ var projects []string
272+ for _, group := range s.Groups {
273+ if strings.HasPrefix(group, gitlabProjectPrefix) {
274+ projects = append(projects, group)
280+func deduplicateGroups(groups []string) []string {
281+ groupSet := make(map[string]struct{})
282+ for _, group := range groups {
283+ groupSet[group] = struct{}{}
286+ uniqueGroups := make([]string, 0, len(groupSet))
287+ for group := range groupSet {
288+ uniqueGroups = append(uniqueGroups, group)