-
Notifications
You must be signed in to change notification settings - Fork 969
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: link oidc credentials when login (#3563)
When user tries to login with OIDC for the first time but has already registered before with email/password a credentials identifier conflict may be detected by Kratos. In this case user needs to login with email/password first and then link OIDC credentials on a settings screen. This PR simplifies UX and allows user to link OIDC credentials to existing account right in the login flow, without switching to settings flow. Closes #2727 Closes #3222
- Loading branch information
Showing
33 changed files
with
905 additions
and
176 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 |
---|---|---|
|
@@ -10,6 +10,7 @@ import ( | |
"time" | ||
|
||
"github.com/ory/x/pointerx" | ||
"github.com/ory/x/sqlcon" | ||
|
||
"github.com/gofrs/uuid" | ||
|
||
|
@@ -556,6 +557,68 @@ func TestManager(t *testing.T) { | |
checkExtensionFields(fromStore, "[email protected]")(t) | ||
}) | ||
}) | ||
|
||
t.Run("method=ConflictingIdentity", func(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
conflicOnIdentifier := identity.NewIdentity(config.DefaultIdentityTraitsSchemaID) | ||
conflicOnIdentifier.Traits = identity.Traits(`{"email":"[email protected]"}`) | ||
require.NoError(t, reg.IdentityManager().Create(ctx, conflicOnIdentifier)) | ||
|
||
conflicOnVerifiableAddress := identity.NewIdentity(config.DefaultIdentityTraitsSchemaID) | ||
conflicOnVerifiableAddress.Traits = identity.Traits(`{"email":"[email protected]", "email_verify":"[email protected]"}`) | ||
require.NoError(t, reg.IdentityManager().Create(ctx, conflicOnVerifiableAddress)) | ||
|
||
conflicOnRecoveryAddress := identity.NewIdentity(config.DefaultIdentityTraitsSchemaID) | ||
conflicOnRecoveryAddress.Traits = identity.Traits(`{"email":"[email protected]", "email_recovery":"[email protected]"}`) | ||
require.NoError(t, reg.IdentityManager().Create(ctx, conflicOnRecoveryAddress)) | ||
|
||
t.Run("case=returns not found if no conflict", func(t *testing.T) { | ||
found, foundConflictAddress, err := reg.IdentityManager().ConflictingIdentity(ctx, &identity.Identity{ | ||
Credentials: map[identity.CredentialsType]identity.Credentials{ | ||
identity.CredentialsTypePassword: {Identifiers: []string{"[email protected]"}}, | ||
}, | ||
}) | ||
assert.ErrorIs(t, err, sqlcon.ErrNoRows) | ||
assert.Nil(t, found) | ||
assert.Empty(t, foundConflictAddress) | ||
}) | ||
|
||
t.Run("case=conflict on identifier", func(t *testing.T) { | ||
found, foundConflictAddress, err := reg.IdentityManager().ConflictingIdentity(ctx, &identity.Identity{ | ||
Credentials: map[identity.CredentialsType]identity.Credentials{ | ||
identity.CredentialsTypePassword: {Identifiers: []string{"[email protected]"}}, | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
assert.Equal(t, conflicOnIdentifier.ID, found.ID) | ||
assert.Equal(t, "[email protected]", foundConflictAddress) | ||
}) | ||
|
||
t.Run("case=conflict on verifiable address", func(t *testing.T) { | ||
found, foundConflictAddress, err := reg.IdentityManager().ConflictingIdentity(ctx, &identity.Identity{ | ||
VerifiableAddresses: []identity.VerifiableAddress{{ | ||
Value: "[email protected]", | ||
Via: "email", | ||
}}, | ||
}) | ||
require.NoError(t, err) | ||
assert.Equal(t, conflicOnVerifiableAddress.ID, found.ID) | ||
assert.Equal(t, "[email protected]", foundConflictAddress) | ||
}) | ||
|
||
t.Run("case=conflict on recovery address", func(t *testing.T) { | ||
found, foundConflictAddress, err := reg.IdentityManager().ConflictingIdentity(ctx, &identity.Identity{ | ||
RecoveryAddresses: []identity.RecoveryAddress{{ | ||
Value: "[email protected]", | ||
Via: "email", | ||
}}, | ||
}) | ||
require.NoError(t, err) | ||
assert.Equal(t, conflicOnRecoveryAddress.ID, found.ID) | ||
assert.Equal(t, "[email protected]", foundConflictAddress) | ||
}) | ||
}) | ||
} | ||
|
||
func TestManagerNoDefaultNamedSchema(t *testing.T) { | ||
|
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,61 @@ | ||
// Copyright © 2023 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package flow | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/tidwall/gjson" | ||
"github.com/tidwall/sjson" | ||
|
||
"github.com/ory/kratos/identity" | ||
"github.com/ory/x/sqlxx" | ||
) | ||
|
||
const internalContextDuplicateCredentialsPath = "registration_duplicate_credentials" | ||
|
||
type DuplicateCredentialsData struct { | ||
CredentialsType identity.CredentialsType | ||
CredentialsConfig sqlxx.JSONRawMessage | ||
DuplicateIdentifier string | ||
} | ||
|
||
type InternalContexter interface { | ||
EnsureInternalContext() | ||
GetInternalContext() sqlxx.JSONRawMessage | ||
SetInternalContext(sqlxx.JSONRawMessage) | ||
} | ||
|
||
// SetDuplicateCredentials sets the duplicate credentials data in the flow's internal context. | ||
func SetDuplicateCredentials(flow InternalContexter, creds DuplicateCredentialsData) error { | ||
if flow.GetInternalContext() == nil { | ||
flow.EnsureInternalContext() | ||
} | ||
bytes, err := sjson.SetBytes( | ||
flow.GetInternalContext(), | ||
internalContextDuplicateCredentialsPath, | ||
creds, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
flow.SetInternalContext(bytes) | ||
|
||
return nil | ||
} | ||
|
||
// DuplicateCredentials returns the duplicate credentials data from the flow's internal context. | ||
func DuplicateCredentials(flow InternalContexter) (*DuplicateCredentialsData, error) { | ||
if flow.GetInternalContext() == nil { | ||
flow.EnsureInternalContext() | ||
} | ||
raw := gjson.GetBytes(flow.GetInternalContext(), internalContextDuplicateCredentialsPath) | ||
if !raw.IsObject() { | ||
return nil, nil | ||
} | ||
var creds DuplicateCredentialsData | ||
err := json.Unmarshal([]byte(raw.Raw), &creds) | ||
|
||
return &creds, err | ||
} |
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
Oops, something went wrong.