forked from oauth2-proxy/oauth2-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable custom structure for group claim with default name group (oaut…
…h2-proxy#839) * Allow complex structure for groups in group claim. * Remove unused constant * Update variable name * Fix linting * Use helper method * Log error if not possible to append group value * Add missing import * Use own logger * Fix imports * Remove Dockerfile for testing * Add Changelog entry * Use formatGroup helper method and update tests * Return string instead of string array * Remove groups variable * Return error in format method. * Reorder imports Co-authored-by: Nick Meves <[email protected]>
- Loading branch information
Showing
3 changed files
with
91 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,12 +29,12 @@ const clientID = "https://test.myapp.com" | |
const secret = "secret" | ||
|
||
type idTokenClaims struct { | ||
Name string `json:"name,omitempty"` | ||
Email string `json:"email,omitempty"` | ||
Phone string `json:"phone_number,omitempty"` | ||
Picture string `json:"picture,omitempty"` | ||
Groups []string `json:"groups,omitempty"` | ||
OtherGroups []string `json:"other_groups,omitempty"` | ||
Name string `json:"name,omitempty"` | ||
Email string `json:"email,omitempty"` | ||
Phone string `json:"phone_number,omitempty"` | ||
Picture string `json:"picture,omitempty"` | ||
Groups interface{} `json:"groups,omitempty"` | ||
OtherGroups interface{} `json:"other_groups,omitempty"` | ||
jwt.StandardClaims | ||
} | ||
|
||
|
@@ -64,6 +64,29 @@ var defaultIDToken idTokenClaims = idTokenClaims{ | |
}, | ||
} | ||
|
||
var customGroupClaimIDToken idTokenClaims = idTokenClaims{ | ||
"Jane Dobbs", | ||
"[email protected]", | ||
"+4798765432", | ||
"http://mugbook.com/janed/me.jpg", | ||
[]map[string]interface{}{ | ||
{ | ||
"groupId": "Admin Group Id", | ||
"roles": []string{"Admin"}, | ||
}, | ||
}, | ||
[]string{"test:c", "test:d"}, | ||
jwt.StandardClaims{ | ||
Audience: "https://test.myapp.com", | ||
ExpiresAt: time.Now().Add(time.Duration(5) * time.Minute).Unix(), | ||
Id: "id-some-id", | ||
IssuedAt: time.Now().Unix(), | ||
Issuer: "https://issuer.example.com", | ||
NotBefore: 0, | ||
Subject: "123456789", | ||
}, | ||
} | ||
|
||
var minimalIDToken idTokenClaims = idTokenClaims{ | ||
"", | ||
"", | ||
|
@@ -283,7 +306,7 @@ func TestCreateSessionStateFromBearerToken(t *testing.T) { | |
GroupsClaim string | ||
ExpectedUser string | ||
ExpectedEmail string | ||
ExpectedGroups []string | ||
ExpectedGroups interface{} | ||
}{ | ||
"Default IDToken": { | ||
IDToken: defaultIDToken, | ||
|
@@ -306,6 +329,13 @@ func TestCreateSessionStateFromBearerToken(t *testing.T) { | |
ExpectedEmail: defaultIDToken.Email, | ||
ExpectedGroups: []string{"test:c", "test:d"}, | ||
}, | ||
"Custom Groups Claim2": { | ||
IDToken: customGroupClaimIDToken, | ||
GroupsClaim: "groups", | ||
ExpectedUser: customGroupClaimIDToken.Subject, | ||
ExpectedEmail: customGroupClaimIDToken.Email, | ||
ExpectedGroups: []string{"{\"groupId\":\"Admin Group Id\",\"roles\":[\"Admin\"]}"}, | ||
}, | ||
} | ||
for testName, tc := range testCases { | ||
t.Run(testName, func(t *testing.T) { | ||
|
@@ -373,3 +403,31 @@ func TestOIDCProvider_findVerifiedIdToken(t *testing.T) { | |
assert.Equal(t, nil, err) | ||
assert.Equal(t, true, verifiedIDToken == nil) | ||
} | ||
|
||
func Test_formatGroup(t *testing.T) { | ||
testCases := map[string]struct { | ||
RawGroup interface{} | ||
ExpectedFormattedGroupValue string | ||
}{ | ||
"String Group": { | ||
RawGroup: "group", | ||
ExpectedFormattedGroupValue: "group", | ||
}, | ||
"Map Group": { | ||
RawGroup: map[string]string{"id": "1", "name": "Test"}, | ||
ExpectedFormattedGroupValue: "{\"id\":\"1\",\"name\":\"Test\"}", | ||
}, | ||
"List Group": { | ||
RawGroup: []string{"First", "Second"}, | ||
ExpectedFormattedGroupValue: "[\"First\",\"Second\"]", | ||
}, | ||
} | ||
|
||
for testName, tc := range testCases { | ||
t.Run(testName, func(t *testing.T) { | ||
formattedGroup, err := formatGroup(tc.RawGroup) | ||
assert.Nil(t, err) | ||
assert.Equal(t, tc.ExpectedFormattedGroupValue, formattedGroup) | ||
}) | ||
} | ||
} |