Skip to content

Commit

Permalink
adding tests for client_credentials flow
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 18, 2024
1 parent 0920db9 commit 392b04a
Showing 1 changed file with 145 additions and 0 deletions.
145 changes: 145 additions & 0 deletions connector/oidc/oidc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,142 @@ func TestHandleCallback(t *testing.T) {
}
}

func TestHandleClientCredentialsCallback(t *testing.T) {
t.Helper()

tests := []struct {
name string
clientID string
clientSecret string
userIDKey string
userNameKey string
overrideClaimMapping bool
preferredUsernameKey string
emailKey string
groupsKey string
insecureSkipEmailVerified bool
scopes []string
expectUserID string
expectUserName string
expectGroups []string
expectPreferredUsername string
expectedEmailField string
token map[string]interface{}
newGroupFromClaims []NewGroupFromClaims
expectedHandlerError error
}{
{
name: "withCorrectCredentials",
userIDKey: "", // not configured
userNameKey: "", // not configured
expectUserID: "subvalue",
expectUserName: "namevalue",
expectGroups: nil,
expectedEmailField: "emailvalue",
scopes: []string{"openid", "id-clientidvalue", "secret-clientsecretvalue"},
token: map[string]interface{}{
"sub": "subvalue",
"name": "namevalue",
"email": "emailvalue",
"email_verified": false,
},
expectedHandlerError: nil,
},
{
name: "withoutCredentials",
userIDKey: "", // not configured
userNameKey: "", // not configured
expectUserID: "",
expectUserName: "",
expectGroups: nil,
expectedEmailField: "",
scopes: []string{"openid"},
token: nil,
expectedHandlerError: fmt.Errorf("oidc: unable to parse clientID or clientSecret"),
},
{
name: "missingSingleCredentialPrefix",
userIDKey: "", // not configured
userNameKey: "", // not configured
expectUserID: "",
expectUserName: "",
expectGroups: nil,
expectedEmailField: "",
scopes: []string{"openid", "id-clientidvalue", "clientsecretvalue"},
token: nil,
expectedHandlerError: fmt.Errorf("oidc: unable to parse clientID or clientSecret"),
},
{
name: "missingBothCredentialPrefixes",
userIDKey: "", // not configured
userNameKey: "", // not configured
expectUserID: "",
expectUserName: "",
expectGroups: nil,
expectedEmailField: "",
scopes: []string{"openid", "clientidvalue", "clientsecretvalue"},
token: nil,
expectedHandlerError: fmt.Errorf("oidc: unable to parse clientID or clientSecret"),
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
idTokenDesired := true
testServer, err := setupServer(tc.token, idTokenDesired)
if err != nil {
t.Fatal("failed to setup test server", err)
}
defer testServer.Close()

serverURL := testServer.URL
basicAuth := true
config := Config{
Issuer: serverURL,
ClientID: tc.clientID,
ClientSecret: tc.clientSecret,
Scopes: tc.scopes,
RedirectURI: fmt.Sprintf("%s/callback", serverURL),
UserIDKey: tc.userIDKey,
UserNameKey: tc.userNameKey,
InsecureSkipEmailVerified: tc.insecureSkipEmailVerified,
InsecureEnableGroups: true,
BasicAuthUnsupported: &basicAuth,
OverrideClaimMapping: tc.overrideClaimMapping,
}
config.ClaimMapping.PreferredUsernameKey = tc.preferredUsernameKey
config.ClaimMapping.EmailKey = tc.emailKey
config.ClaimMapping.GroupsKey = tc.groupsKey
config.ClaimMutations.NewGroupFromClaims = tc.newGroupFromClaims

conn, err := newConnector(config)
if err != nil {
t.Fatal("failed to create new connector", err)
}
req, err := newRequestWithoutAuthCode(testServer.URL)
if err != nil {
t.Fatal("failed to create request", err)
}

// mimic the functionality of server/oauth2 parseScopes
s := connector.Scopes{}
s.Other = append(s.Other, tc.scopes...)

identity, err := conn.HandleCallback(s, req)
expectEquals(t, err, tc.expectedHandlerError)
if err != nil {
return
}
expectEquals(t, identity.UserID, tc.expectUserID)
expectEquals(t, identity.Username, tc.expectUserName)
expectEquals(t, identity.PreferredUsername, tc.expectPreferredUsername)
expectEquals(t, identity.Email, tc.expectedEmailField)
expectEquals(t, identity.EmailVerified, false)
expectEquals(t, identity.Groups, tc.expectGroups)
})
}
}

func TestRefresh(t *testing.T) {
t.Helper()

Expand Down Expand Up @@ -828,6 +964,15 @@ func newRequestWithAuthCode(serverURL string, code string) (*http.Request, error
return req, nil
}

func newRequestWithoutAuthCode(serverURL string) (*http.Request, error) {
req, err := http.NewRequest("GET", serverURL, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %v", err)
}

return req, nil
}

func n(pub *rsa.PublicKey) string {
return encode(pub.N.Bytes())
}
Expand Down

0 comments on commit 392b04a

Please sign in to comment.