-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: allow using identity external_id as oauth2 subject #4529
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,3 +3,4 @@ oauth2_provider: | |
| headers: | ||
| Authorization: Basic | ||
| override_return_to: true | ||
| subject_source: external_id | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,8 +20,16 @@ const ( | |
| var ErrFakeAcceptLoginRequestFailed = errors.New("failed to accept login request") | ||
|
|
||
| type FakeHydra struct { | ||
| Skip bool | ||
| RequestURL string | ||
| Skip bool | ||
| RequestURL string | ||
| SubjectSource string | ||
| params []AcceptLoginRequestParams | ||
| } | ||
|
|
||
| func (h *FakeHydra) Params() []AcceptLoginRequestParams { | ||
| out := make([]AcceptLoginRequestParams, len(h.params)) | ||
| copy(out, h.params) | ||
| return out | ||
| } | ||
|
|
||
| var _ Hydra = &FakeHydra{} | ||
|
|
@@ -33,9 +41,23 @@ func NewFake() *FakeHydra { | |
| } | ||
|
|
||
| func (h *FakeHydra) AcceptLoginRequest(_ context.Context, params AcceptLoginRequestParams) (string, error) { | ||
| h.params = append(h.params, params) | ||
| if params.SessionID == "" { | ||
| return "", errors.New("session id must not be empty") | ||
| } | ||
|
|
||
| // Validate subject source just like DefaultHydra does | ||
| switch h.SubjectSource { | ||
| case "", "id": | ||
| // Use identity ID - no validation needed | ||
| case "external_id": | ||
| if params.ExternalID == "" { | ||
| return "", herodot.ErrBadRequest.WithReasonf("The identity does not have an external ID set, but it is required for the OAuth2 provider subject.") | ||
| } | ||
|
Comment on lines
+53
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FakeHydra enforces hard failure instead of fallback for missing Line 54-Line 56 rejects requests when Suggested behavior alignment case "external_id":
- if params.ExternalID == "" {
- return "", herodot.ErrBadRequest.WithReasonf("The identity does not have an external ID set, but it is required for the OAuth2 provider subject.")
- }
+ // Allow missing external_id and fall back to identity ID behavior.🤖 Prompt for AI Agents
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same reasoning as above — the fake intentionally mirrors the real There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
✏️ Learnings added
|
||
| default: | ||
| return "", herodot.ErrBadRequest.WithReasonf("Unknown OAuth2 provider subject source %q", h.SubjectSource) | ||
| } | ||
|
|
||
| switch params.LoginChallenge { | ||
| case FakeInvalidLoginChallenge: | ||
| return "", ErrFakeAcceptLoginRequestFailed | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| // Copyright © 2026 Ory Corp | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package login_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "net/url" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/gofrs/uuid" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/ory/kratos/driver/config" | ||
| "github.com/ory/kratos/hydra" | ||
| "github.com/ory/kratos/identity" | ||
| "github.com/ory/kratos/pkg" | ||
| "github.com/ory/kratos/pkg/testhelpers" | ||
| "github.com/ory/kratos/selfservice/flow" | ||
| "github.com/ory/kratos/selfservice/flow/login" | ||
| "github.com/ory/kratos/session" | ||
| "github.com/ory/x/sqlxx" | ||
| ) | ||
|
|
||
| func TestLoginExecutorWithExternalID(t *testing.T) { | ||
| ctx := context.Background() | ||
| conf, reg := pkg.NewFastRegistryWithMocks(t) | ||
| fakeHydra := hydra.NewFake() | ||
| reg.SetHydra(fakeHydra) | ||
|
|
||
| testhelpers.SetDefaultIdentitySchema(conf, "file://./stub/login.schema.json") | ||
| conf.MustSet(ctx, config.ViperKeySelfServiceBrowserDefaultReturnTo, "https://www.ory.sh/kratos/return_to") | ||
| conf.MustSet(ctx, config.ViperKeyOAuth2ProviderURL, "https://hydra.example.com") | ||
|
|
||
| i := &identity.Identity{ | ||
| ID: uuid.Must(uuid.NewV4()), | ||
| ExternalID: sqlxx.NullString("external-id"), | ||
| SchemaID: config.DefaultIdentityTraitsSchemaID, | ||
| State: identity.StateActive, | ||
| } | ||
| require.NoError(t, reg.Persister().CreateIdentity(ctx, i)) | ||
|
|
||
| t.Run("case=subject_source=id", func(t *testing.T) { | ||
| conf.MustSet(ctx, config.ViperKeyOAuth2ProviderSubjectSource, "id") | ||
| fakeHydra.SubjectSource = "id" | ||
| loginFlow, err := login.NewFlow(conf, time.Minute, hydra.FakeValidLoginChallenge, &http.Request{URL: &url.URL{Path: "/", RawQuery: "login_challenge=" + hydra.FakeValidLoginChallenge}}, flow.TypeBrowser) | ||
| require.NoError(t, err) | ||
| loginFlow.OAuth2LoginChallenge = hydra.FakeValidLoginChallenge | ||
|
|
||
| w := httptest.NewRecorder() | ||
| r := &http.Request{URL: &url.URL{Path: "/login/post"}} | ||
| sess := session.NewInactiveSession() | ||
| sess.CompletedLoginFor(identity.CredentialsTypePassword, identity.AuthenticatorAssuranceLevel1) | ||
|
|
||
| err = reg.LoginHookExecutor().PostLoginHook(w, r, identity.CredentialsTypePassword.ToUiNodeGroup(), loginFlow, i, sess, "") | ||
| require.NoError(t, err) | ||
|
|
||
| require.Len(t, fakeHydra.Params(), 1) | ||
| assert.Equal(t, i.ID.String(), fakeHydra.Params()[0].IdentityID) | ||
| assert.Equal(t, "external-id", fakeHydra.Params()[0].ExternalID) | ||
| }) | ||
|
|
||
| t.Run("case=subject_source=external_id", func(t *testing.T) { | ||
| conf.MustSet(ctx, config.ViperKeyOAuth2ProviderSubjectSource, "external_id") | ||
| fakeHydra.SubjectSource = "external_id" | ||
| loginFlow, err := login.NewFlow(conf, time.Minute, hydra.FakeValidLoginChallenge, &http.Request{URL: &url.URL{Path: "/", RawQuery: "login_challenge=" + hydra.FakeValidLoginChallenge}}, flow.TypeBrowser) | ||
| require.NoError(t, err) | ||
| loginFlow.OAuth2LoginChallenge = hydra.FakeValidLoginChallenge | ||
|
|
||
| w := httptest.NewRecorder() | ||
| r := &http.Request{URL: &url.URL{Path: "/login/post"}} | ||
| sess := session.NewInactiveSession() | ||
| sess.CompletedLoginFor(identity.CredentialsTypePassword, identity.AuthenticatorAssuranceLevel1) | ||
|
|
||
| err = reg.LoginHookExecutor().PostLoginHook(w, r, identity.CredentialsTypePassword.ToUiNodeGroup(), loginFlow, i, sess, "") | ||
| require.NoError(t, err) | ||
|
|
||
| params := fakeHydra.Params() | ||
| require.NotEmpty(t, params) | ||
| lastParams := params[len(params)-1] | ||
| assert.Equal(t, i.ID.String(), lastParams.IdentityID) | ||
| assert.Equal(t, "external-id", lastParams.ExternalID) | ||
| }) | ||
|
|
||
| t.Run("case=subject_source=external_id without external_id set", func(t *testing.T) { | ||
| iWithoutExtID := &identity.Identity{ | ||
| ID: uuid.Must(uuid.NewV4()), | ||
| SchemaID: config.DefaultIdentityTraitsSchemaID, | ||
| State: identity.StateActive, | ||
| } | ||
| require.NoError(t, reg.Persister().CreateIdentity(ctx, iWithoutExtID)) | ||
|
|
||
| conf.MustSet(ctx, config.ViperKeyOAuth2ProviderSubjectSource, "external_id") | ||
| fakeHydra.SubjectSource = "external_id" | ||
| loginFlow, err := login.NewFlow(conf, time.Minute, hydra.FakeValidLoginChallenge, &http.Request{URL: &url.URL{Path: "/", RawQuery: "login_challenge=" + hydra.FakeValidLoginChallenge}}, flow.TypeBrowser) | ||
| require.NoError(t, err) | ||
| loginFlow.OAuth2LoginChallenge = hydra.FakeValidLoginChallenge | ||
|
|
||
| w := httptest.NewRecorder() | ||
| r := &http.Request{URL: &url.URL{Path: "/login/post"}} | ||
| sess := session.NewInactiveSession() | ||
| sess.CompletedLoginFor(identity.CredentialsTypePassword, identity.AuthenticatorAssuranceLevel1) | ||
|
|
||
| err = reg.LoginHookExecutor().PostLoginHook(w, r, identity.CredentialsTypePassword.ToUiNodeGroup(), loginFlow, iWithoutExtID, sess, "") | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "external ID set") | ||
| }) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
external_iderror semantics conflict with the stated fallback compatibility behavior.Line 2294 documents a hard error when
external_idis missing, but the PR objectives require fallback to identity UUID for backward compatibility. Please align this config contract with fallback behavior.Suggested schema wording update
📝 Committable suggestion
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error behavior is intentional — @jonas-jonas explicitly requested in his review that we return an error (rather than fall back) when
external_idis missing, to match the tokenizer pattern and keep behavior predictable. The description is correct as-is.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.