Skip to content

Commit

Permalink
better credentials handling + adjust tests
Browse files Browse the repository at this point in the history
Signed-off-by: Houssem Ben Mabrouk <[email protected]>
  • Loading branch information
orange-hbenmabrouk committed Apr 22, 2024
1 parent 24996b7 commit 4137a9c
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 141 deletions.
2 changes: 0 additions & 2 deletions connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ type Scopes struct {

// The client has requested group information about the end user.
Groups bool

Other []string
}

// Identity represents the ID Token claims supported by the server.
Expand Down
29 changes: 14 additions & 15 deletions connector/oidc/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,33 +369,32 @@ const (
exchangeCaller
)

func (c *oidcConnector) getTokenViaClientCredentials(s connector.Scopes) (token *oauth2.Token, err error) {
var clientID, clientSecret string

// extract clientID & clientSecret from scopes
for _, data := range s.Other {
if strings.Contains(data, "id-") {
scopeTokens := strings.Split(data, "id-")
clientID = scopeTokens[len(scopeTokens)-1]
}
if strings.Contains(data, "secret-") {
scopeTokens := strings.Split(data, "secret-")
clientSecret = scopeTokens[len(scopeTokens)-1]
}
func (c *oidcConnector) getTokenViaClientCredentials(r *http.Request) (token *oauth2.Token, err error) {
// Setup default clientID & clientSecret
clientID := c.oauth2Config.ClientID
clientSecret := c.oauth2Config.ClientSecret

// Override clientID & clientSecret if they exist!
q := r.Form
if q.Has("custom_client_id") && q.Has("custom_client_secret") {
clientID = q.Get("custom_client_id")
clientSecret = q.Get("custom_client_secret")
}

// check if parsed credentials are not empty
// Check if parsed credentials are not empty
if len(clientID) == 0 || len(clientSecret) == 0 {
return nil, fmt.Errorf("oidc: unable to parse clientID or clientSecret")
}

// Construct data to be sent to the external IdP
data := url.Values{
"grant_type": {"client_credentials"},
"client_id": {clientID},
"client_secret": {clientSecret},
"scope": {strings.Join(c.oauth2Config.Scopes, " ")},
}

// Request token from external IdP
resp, err := c.httpClient.PostForm(c.oauth2Config.Endpoint.TokenURL, data)
if err != nil {
return nil, fmt.Errorf("oidc: failed to get token: %v", err)
Expand Down Expand Up @@ -453,7 +452,7 @@ func (c *oidcConnector) HandleCallback(s connector.Scopes, r *http.Request) (ide
}
} else {
// get token via client_credentials
token, err = c.getTokenViaClientCredentials(s)
token, err = c.getTokenViaClientCredentials(r)
if err != nil {
return identity, err
}
Expand Down
Loading

0 comments on commit 4137a9c

Please sign in to comment.