connector/gitlab: implement useLoginAsID as in GitHub connector
This commit is contained in:
		| @@ -32,6 +32,7 @@ type Config struct { | ||||
| 	ClientSecret string   `json:"clientSecret"` | ||||
| 	RedirectURI  string   `json:"redirectURI"` | ||||
| 	Groups       []string `json:"groups"` | ||||
| 	UseLoginAsID bool     `json:"useLoginAsID"` | ||||
| } | ||||
|  | ||||
| type gitlabUser struct { | ||||
| @@ -55,6 +56,7 @@ func (c *Config) Open(id string, logger log.Logger) (connector.Connector, error) | ||||
| 		clientSecret: c.ClientSecret, | ||||
| 		logger:       logger, | ||||
| 		groups:       c.Groups, | ||||
| 		useLoginAsID: c.UseLoginAsID, | ||||
| 	}, nil | ||||
| } | ||||
|  | ||||
| @@ -76,6 +78,8 @@ type gitlabConnector struct { | ||||
| 	clientSecret string | ||||
| 	logger       log.Logger | ||||
| 	httpClient   *http.Client | ||||
| 	// if set to true will use the user's handle rather than their numeric id as the ID | ||||
| 	useLoginAsID bool | ||||
| } | ||||
|  | ||||
| func (c *gitlabConnector) oauth2Config(scopes connector.Scopes) *oauth2.Config { | ||||
| @@ -148,6 +152,9 @@ func (c *gitlabConnector) HandleCallback(s connector.Scopes, r *http.Request) (i | ||||
| 		Email:         user.Email, | ||||
| 		EmailVerified: true, | ||||
| 	} | ||||
| 	if c.useLoginAsID { | ||||
| 		identity.UserID = user.Username | ||||
| 	} | ||||
|  | ||||
| 	if s.Groups { | ||||
| 		groups, err := c.getGroups(ctx, client, s.Groups, user.Username) | ||||
|   | ||||
| @@ -104,7 +104,7 @@ func TestUsernameIncludedInFederatedIdentity(t *testing.T) { | ||||
| func TestLoginUsedAsIDWhenConfigured(t *testing.T) { | ||||
|  | ||||
| 	s := newTestServer(map[string]interface{}{ | ||||
| 		"/api/v4/user": gitlabUser{Email: "some@email.com", ID: 12345678, Name: "Joe Bloggs"}, | ||||
| 		"/api/v4/user": gitlabUser{Email: "some@email.com", ID: 12345678, Name: "Joe Bloggs", Username: "joebloggs"}, | ||||
| 		"/oauth/token": map[string]interface{}{ | ||||
| 			"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9", | ||||
| 			"expires_in":   "30", | ||||
| @@ -121,11 +121,11 @@ func TestLoginUsedAsIDWhenConfigured(t *testing.T) { | ||||
| 	req, err := http.NewRequest("GET", hostURL.String(), nil) | ||||
| 	expectNil(t, err) | ||||
|  | ||||
| 	c := gitlabConnector{baseURL: s.URL, httpClient: newClient()} | ||||
| 	c := gitlabConnector{baseURL: s.URL, httpClient: newClient(), useLoginAsID: true} | ||||
| 	identity, err := c.HandleCallback(connector.Scopes{Groups: true}, req) | ||||
|  | ||||
| 	expectNil(t, err) | ||||
| 	expectEquals(t, identity.UserID, "12345678") | ||||
| 	expectEquals(t, identity.UserID, "joebloggs") | ||||
| 	expectEquals(t, identity.Username, "Joe Bloggs") | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user