forked from dexidp/dex
-
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.
fix: export custom claims translation (dexidp#31)
- Loading branch information
Alex Palesandro
committed
Nov 9, 2023
1 parent
579bf92
commit d14abf1
Showing
5 changed files
with
95 additions
and
23 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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package claims | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"golang.org/x/exp/maps" | ||
"golang.org/x/exp/slices" | ||
) | ||
|
||
func getProtectedClaims() []string { | ||
return []string{"iss", "sub", "aud", "exp", "iat", "azp", "nonce", "at_hash", "c_hash"} | ||
} | ||
|
||
func generateIDClaims(baseIDClaims map[string]interface{}, customClaims map[string]interface{}) map[string]interface{} { | ||
finalClaims := map[string]interface{}{} | ||
maps.Copy(finalClaims, baseIDClaims) | ||
// Adding the immutable claims to the token | ||
protectedClaims := getProtectedClaims() | ||
for claim := range customClaims { | ||
if !slices.Contains(protectedClaims, claim) { | ||
finalClaims[claim] = customClaims[claim] | ||
} | ||
} | ||
return finalClaims | ||
} | ||
|
||
func GenerateTokenFromTemplate(baseIDClaims map[string]interface{}, customClaims map[string]interface{}) ([]byte, | ||
error, | ||
) { | ||
payload, err := json.Marshal(generateIDClaims(baseIDClaims, customClaims)) | ||
if err != nil { | ||
return []byte{}, fmt.Errorf("could not serialize claims: %v", err) | ||
} | ||
return payload, nil | ||
} |
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package claims | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_GroupTranslation(t *testing.T) { | ||
baseIDToken := map[string]interface{}{ | ||
"groups": []string{"group1", "group2"}, | ||
} | ||
receivedIDToken := map[string]interface{}{ | ||
"groups": []string{"test2:group1", "test2:group2"}, | ||
} | ||
res, err := GenerateTokenFromTemplate(baseIDToken, receivedIDToken) | ||
assert.NoError(t, err) | ||
assert.Equal(t, res, []byte(`{"groups":["test2:group1","test2:group2"]}`)) | ||
} | ||
|
||
func Test_EmptyInput(t *testing.T) { | ||
res, err := GenerateTokenFromTemplate(map[string]interface{}{}, map[string]interface{}{}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, res, []byte(`{}`)) | ||
} | ||
|
||
func Test_ProtectedClaims(t *testing.T) { | ||
baseIDToken := map[string]interface{}{ | ||
"iss": "iss", | ||
"sub": "sub", | ||
"aud": "aud", | ||
"groups": []string{"group1", "group2"}, | ||
} | ||
receivedIDToken := map[string]interface{}{ | ||
"iss": "iss2", | ||
"groups": []string{"test2:group1", "test2:group2"}, | ||
} | ||
res, err := GenerateTokenFromTemplate(baseIDToken, receivedIDToken) | ||
assert.NoError(t, err) | ||
assert.Equal(t, res, []byte(`{"aud":"aud","groups":["test2:group1","test2:group2"],"iss":"iss","sub":"sub"}`)) | ||
} | ||
|
||
func Test_NotStandardClaims(t *testing.T) { | ||
baseIDToken := map[string]interface{}{ | ||
"groups": []string{"group1", "group2"}, | ||
} | ||
receivedIDToken := map[string]interface{}{ | ||
"groups": []string{"test2:group1", "test2:group2"}, | ||
"custom": "custom", | ||
} | ||
res, err := GenerateTokenFromTemplate(baseIDToken, receivedIDToken) | ||
assert.NoError(t, err) | ||
assert.Equal(t, res, []byte(`{"custom":"custom","groups":["test2:group1","test2:group2"]}`)) | ||
} |
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