-
Notifications
You must be signed in to change notification settings - Fork 972
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: extract identifier label for login from the default identity sc…
…hema
- Loading branch information
Showing
12 changed files
with
287 additions
and
26 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
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,64 @@ | ||
// Copyright © 2023 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package login | ||
|
||
import ( | ||
"context" | ||
"sort" | ||
|
||
"github.com/ory/jsonschema/v3" | ||
"github.com/ory/kratos/schema" | ||
) | ||
|
||
type identifierLabelExtension struct { | ||
identifierLabelCandidates []string | ||
} | ||
|
||
var _ schema.CompileExtension = new(identifierLabelExtension) | ||
|
||
func GetIdentifierLabelFromSchema(ctx context.Context, schemaURL string) (string, error) { | ||
ext := &identifierLabelExtension{} | ||
|
||
runner, err := schema.NewExtensionRunner(ctx, schema.WithCompileRunners(ext)) | ||
if err != nil { | ||
return "", err | ||
} | ||
c := jsonschema.NewCompiler() | ||
runner.Register(c) | ||
|
||
_, err = c.Compile(ctx, schemaURL) | ||
if err != nil { | ||
return "", err | ||
} | ||
return ext.getLabel(), nil | ||
} | ||
|
||
func (i *identifierLabelExtension) Run(_ jsonschema.CompilerContext, config schema.ExtensionConfig, rawSchema map[string]interface{}) error { | ||
if config.Credentials.Password.Identifier || | ||
config.Credentials.WebAuthn.Identifier || | ||
config.Credentials.TOTP.AccountName || | ||
config.Credentials.Code.Identifier { | ||
if title, ok := rawSchema["title"]; ok { | ||
// The jsonschema compiler validates the title to be a string, so this should always work. | ||
switch t := title.(type) { | ||
case string: | ||
if t != "" { | ||
i.identifierLabelCandidates = append(i.identifierLabelCandidates, t) | ||
} | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (i *identifierLabelExtension) getLabel() string { | ||
if len(i.identifierLabelCandidates) == 0 { | ||
// sane default is set elsewhere | ||
return "" | ||
} | ||
// sort the candidates to get a deterministic result | ||
sort.Strings(i.identifierLabelCandidates) | ||
// just take the first, no good way to decide which one is the best | ||
return i.identifierLabelCandidates[0] | ||
} |
137 changes: 137 additions & 0 deletions
137
selfservice/flow/login/extension_identifier_label_test.go
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,137 @@ | ||
// Copyright © 2023 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package login | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/tidwall/sjson" | ||
|
||
"github.com/ory/kratos/schema" | ||
) | ||
|
||
func constructSchema(t *testing.T, ecModifier, ucModifier func(*schema.ExtensionConfig)) string { | ||
var emailConfig, usernameConfig schema.ExtensionConfig | ||
|
||
if ecModifier != nil { | ||
ecModifier(&emailConfig) | ||
} | ||
if ucModifier != nil { | ||
ucModifier(&usernameConfig) | ||
} | ||
|
||
ec, err := json.Marshal(&emailConfig) | ||
require.NoError(t, err) | ||
uc, err := json.Marshal(&usernameConfig) | ||
require.NoError(t, err) | ||
|
||
ec, err = sjson.DeleteBytes(ec, "verification") | ||
require.NoError(t, err) | ||
ec, err = sjson.DeleteBytes(ec, "recovery") | ||
require.NoError(t, err) | ||
ec, err = sjson.DeleteBytes(ec, "credentials.code.via") | ||
require.NoError(t, err) | ||
uc, err = sjson.DeleteBytes(uc, "verification") | ||
require.NoError(t, err) | ||
uc, err = sjson.DeleteBytes(uc, "recovery") | ||
require.NoError(t, err) | ||
uc, err = sjson.DeleteBytes(uc, "credentials.code.via") | ||
require.NoError(t, err) | ||
|
||
return "base64://" + base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf(` | ||
{ | ||
"properties": { | ||
"traits": { | ||
"properties": { | ||
"email": { | ||
"title": "Email", | ||
"ory.sh/kratos": %s | ||
}, | ||
"username": { | ||
"title": "Username", | ||
"ory.sh/kratos": %s | ||
} | ||
} | ||
} | ||
} | ||
}`, ec, uc))) | ||
} | ||
|
||
func TestGetIdentifierLabelFromSchema(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
for _, tc := range []struct { | ||
name string | ||
emailConfig, usernameConfig func(*schema.ExtensionConfig) | ||
expected string | ||
}{ | ||
{ | ||
name: "email for password", | ||
emailConfig: func(c *schema.ExtensionConfig) { | ||
c.Credentials.Password.Identifier = true | ||
}, | ||
expected: "Email", | ||
}, | ||
{ | ||
name: "email for webauthn", | ||
emailConfig: func(c *schema.ExtensionConfig) { | ||
c.Credentials.WebAuthn.Identifier = true | ||
}, | ||
expected: "Email", | ||
}, | ||
{ | ||
name: "email for totp", | ||
emailConfig: func(c *schema.ExtensionConfig) { | ||
c.Credentials.TOTP.AccountName = true | ||
}, | ||
expected: "Email", | ||
}, | ||
{ | ||
name: "email for code", | ||
emailConfig: func(c *schema.ExtensionConfig) { | ||
c.Credentials.Code.Identifier = true | ||
}, | ||
expected: "Email", | ||
}, | ||
{ | ||
name: "email for all", | ||
emailConfig: func(c *schema.ExtensionConfig) { | ||
c.Credentials.Password.Identifier = true | ||
c.Credentials.WebAuthn.Identifier = true | ||
c.Credentials.TOTP.AccountName = true | ||
c.Credentials.Code.Identifier = true | ||
}, | ||
expected: "Email", | ||
}, | ||
{ | ||
name: "username works as well", | ||
usernameConfig: func(c *schema.ExtensionConfig) { | ||
c.Credentials.Password.Identifier = true | ||
}, | ||
expected: "Username", | ||
}, | ||
{ | ||
name: "multiple identifiers", | ||
emailConfig: func(c *schema.ExtensionConfig) { | ||
c.Credentials.Password.Identifier = true | ||
}, | ||
usernameConfig: func(c *schema.ExtensionConfig) { | ||
c.Credentials.Password.Identifier = true | ||
}, | ||
expected: "Email", | ||
}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
label, err := GetIdentifierLabelFromSchema(ctx, constructSchema(t, tc.emailConfig, tc.usernameConfig)) | ||
require.NoError(t, err) | ||
assert.Equal(t, tc.expected, label) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.