Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update claims instead of overwrite from webhook response #3889

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"amr": null,
"c_hash": "",
"ext": {
"hooked": "legacy"
"hooked": "legacy",
"sid": ""
}
},
"headers": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"amr": null,
"c_hash": "",
"ext": {
"hooked": "legacy"
"hooked": "legacy",
"sid": ""
}
},
"headers": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"amr": null,
"c_hash": "",
"ext": {
"hooked": "legacy"
"hooked": "legacy",
"sid": ""
}
},
"headers": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"amr": null,
"c_hash": "",
"ext": {
"hooked": "legacy"
"hooked": "legacy",
"sid": ""
}
},
"headers": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"amr": null,
"c_hash": "",
"ext": {
"hooked": "legacy"
"hooked": "legacy",
"sid": ""
}
},
"headers": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"amr": null,
"c_hash": "",
"ext": {
"hooked": "legacy"
"hooked": "legacy",
"sid": ""
}
},
"headers": {
Expand Down
19 changes: 16 additions & 3 deletions oauth2/token_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,26 @@ func executeHookAndUpdateSession(ctx context.Context, reg x.HTTPClientProvider,
)
}

// Overwrite existing session data (extra claims).
session.Extra = respBody.Session.AccessToken
// Update existing session data (extra claims).
session.Extra = updateExtraClaims(session.Extra, respBody.Session.AccessToken)
idTokenClaims := session.IDTokenClaims()
idTokenClaims.Extra = respBody.Session.IDToken
idTokenClaims.Extra = updateExtraClaims(idTokenClaims.Extra, respBody.Session.IDToken)
return nil
}

func updateExtraClaims(claimsToUpdate, webhookExtraClaims map[string]interface{}) map[string]interface{} {
if webhookExtraClaims == nil {
return claimsToUpdate
}
if claimsToUpdate == nil {
claimsToUpdate = make(map[string]interface{})
}
for key, value := range webhookExtraClaims {
claimsToUpdate[key] = value
}
return claimsToUpdate
}

// TokenHook is an AccessRequestHook called for all grant types.
func TokenHook(reg interface {
config.Provider
Expand Down
111 changes: 111 additions & 0 deletions oauth2/token_hook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package oauth2

import (
"reflect"
"testing"
)

func TestUpdateExtraClaims(t *testing.T) {
tests := []struct {
name string
priorExtraClaims map[string]interface{}
webhookExtraClaims map[string]interface{}
expected map[string]interface{}
}{
{
name: "Merge with no updates",
priorExtraClaims: map[string]interface{}{
"claim1": "value1",
"claim2": "value2",
},
webhookExtraClaims: map[string]interface{}{
"claim3": "value3",
"claim4": "value4",
},
expected: map[string]interface{}{
"claim1": "value1",
"claim2": "value2",
"claim3": "value3",
"claim4": "value4",
},
},
{
name: "Merge with updates",
priorExtraClaims: map[string]interface{}{
"claim1": "value1",
"claim2": "value2",
},
webhookExtraClaims: map[string]interface{}{
"claim2": "newValue2", // Overwrites prior claim2
"claim3": "value3",
},
expected: map[string]interface{}{
"claim1": "value1",
"claim2": "newValue2",
"claim3": "value3",
},
},
{
name: "Empty webhook claims",
priorExtraClaims: map[string]interface{}{
"claim1": "value1",
},
webhookExtraClaims: map[string]interface{}{},
expected: map[string]interface{}{
"claim1": "value1",
},
},
{
name: "Empty prior claims",
priorExtraClaims: map[string]interface{}{},
webhookExtraClaims: map[string]interface{}{
"claim1": "value1",
},
expected: map[string]interface{}{
"claim1": "value1",
},
},
{
name: "Both maps empty",
priorExtraClaims: map[string]interface{}{},
webhookExtraClaims: map[string]interface{}{},
expected: map[string]interface{}{},
},
{
name: "Nil webhook claims",
priorExtraClaims: map[string]interface{}{"claim1": "value1"},
webhookExtraClaims: nil,
expected: map[string]interface{}{"claim1": "value1"},
},
{
name: "Nil prior claims",
priorExtraClaims: nil,
webhookExtraClaims: map[string]interface{}{"claim1": "value1"},
expected: map[string]interface{}{"claim1": "value1"},
},
{
name: "Both maps nil",
priorExtraClaims: nil,
webhookExtraClaims: nil,
expected: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Act
if tt.priorExtraClaims == nil {
tt.priorExtraClaims = nil // Explicitly ensure nil for this test case
}
actual := updateExtraClaims(tt.priorExtraClaims, tt.webhookExtraClaims)

// Assert
if !reflect.DeepEqual(actual, tt.expected) {
t.Errorf("claimsToUpdate = %v, want %v", tt.priorExtraClaims, tt.expected)
}
})
}
}
Loading