-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 1 commit
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 | ||
| use_external_id: true | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ type ( | |
| AcceptLoginRequestParams struct { | ||
| LoginChallenge string | ||
| IdentityID string | ||
| ExternalID string | ||
| SessionID string | ||
| AuthenticationMethods session.AuthenticationMethods | ||
| } | ||
|
|
@@ -93,7 +94,12 @@ func (h *DefaultHydra) AcceptLoginRequest(ctx context.Context, params AcceptLogi | |
| remember := h.d.Config().SessionPersistentCookie(ctx) | ||
| rememberFor := int64(h.d.Config().SessionLifespan(ctx) / time.Second) | ||
|
|
||
| alr := hydraclientgo.NewAcceptOAuth2LoginRequest(params.IdentityID) | ||
| subject := params.IdentityID | ||
Check warningCode scanning / CodeQL Useless assignment to local variable Warning
This definition of subject is never used.
|
||
| if h.d.Config().OAuth2ProviderUseExternalID(ctx) && params.ExternalID != "" { | ||
| subject = params.ExternalID | ||
| } | ||
|
||
|
|
||
| alr := hydraclientgo.NewAcceptOAuth2LoginRequest(subject) | ||
| alr.IdentityProviderSessionId = ¶ms.SessionID | ||
| alr.Remember = &remember | ||
| alr.RememberFor = &rememberFor | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // 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/internal" | ||
| "github.com/ory/kratos/internal/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 := internal.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=use_external_id=false", func(t *testing.T) { | ||
| conf.MustSet(ctx, config.ViperKeyOAuth2ProviderUseExternalID, false) | ||
| 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=use_external_id=true", func(t *testing.T) { | ||
| conf.MustSet(ctx, config.ViperKeyOAuth2ProviderUseExternalID, true) | ||
| 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) | ||
|
|
||
| fakeHydra.Params() | ||
|
|
||
| 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) | ||
| }) | ||
| } |
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.
We have the same pattern for the tokenizer, but there the setting is
Could you adjust the code here?
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.
Hi there! Thx for your review and apologies for the delay in getting back to this—I’ve been a bit tied up lately. I changed it like you requested