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

feat: support MFA via SMS #3682

Merged
merged 23 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions cmd/clidoc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@
"NewInfoSelfServiceRegistrationRegisterCode": text.NewInfoSelfServiceRegistrationRegisterCode(),
"NewErrorValidationLoginLinkedCredentialsDoNotMatch": text.NewErrorValidationLoginLinkedCredentialsDoNotMatch(),
"NewErrorValidationAddressUnknown": text.NewErrorValidationAddressUnknown(),
"NewInfoSelfServiceLoginCodeMFA": text.NewInfoSelfServiceLoginCodeMFA(),
"NewInfoSelfServiceLoginCodeMFAHint": text.NewInfoSelfServiceLoginCodeMFAHint("{maskedIdentifier}"),
}
}

Expand Down Expand Up @@ -248,7 +250,7 @@
r := regexp.MustCompile(`(?s)<!-- START MESSAGE TABLE -->(.*?)<!-- END MESSAGE TABLE -->`)
result := r.ReplaceAllString(string(content), "<!-- START MESSAGE TABLE -->\n"+w.String()+"\n<!-- END MESSAGE TABLE -->")

f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o755)

Check warning on line 253 in cmd/clidoc/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/clidoc/main.go#L253

Added line #L253 was not covered by tests
if err != nil {
return err
}
Expand All @@ -267,7 +269,7 @@
func writeMessagesJson(path string, sortedMessages []*text.Message) error {
result := codeEncode(sortedMessages)

f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o755)

Check warning on line 272 in cmd/clidoc/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/clidoc/main.go#L272

Added line #L272 was not covered by tests
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$id": "https://schemas.ory.sh/presets/kratos/quickstart/email-password/identity.schema.json",
"$id": "https://schemas.ory.sh/presets/kratos/quickstart/phone-password/identity.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Person",
"type": "object",
Expand Down Expand Up @@ -36,9 +36,6 @@
"credentials": {
"password": {
"identifier": true
},
"code": {
"identifier": true
}
},
"verification": {
Expand Down
20 changes: 20 additions & 0 deletions embedx/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,26 @@
"code": {
"type": "object",
"additionalProperties": false,
"oneOf": [
{
"properties": {
"passwordless_enabled": { "const": true },
"mfa_enabled": { "const": false }
}
},
{
"properties": {
"mfa_enabled": { "const": true },
"passwordless_enabled": { "const": false }
}
},
{
"properties": {
"mfa_enabled": { "const": false },
"passwordless_enabled": { "const": false }
}
}
],
"properties": {
"passwordless_enabled": {
"type": "boolean",
Expand Down
8 changes: 8 additions & 0 deletions internal/client-go/api_frontend.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/client-go/model_identity_credentials_code.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions internal/httpclient/api_frontend.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/httpclient/model_identity_credentials_code.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions internal/testhelpers/selfservice_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type initFlowOptions struct {
returnTo string
refresh bool
oauth2LoginChallenge string
via string
}

func (o *initFlowOptions) apply(opts []InitFlowWithOption) *initFlowOptions {
Expand Down Expand Up @@ -86,6 +87,9 @@ func getURLFromInitOptions(ts *httptest.Server, path string, forced bool, opts .
if o.oauth2LoginChallenge != "" {
q.Set("login_challenge", o.oauth2LoginChallenge)
}
if o.via != "" {
q.Set("via", o.via)
}

u := urlx.ParseOrPanic(ts.URL + path)
u.RawQuery = q.Encode()
Expand Down Expand Up @@ -118,6 +122,12 @@ func InitFlowWithOAuth2LoginChallenge(hlc string) InitFlowWithOption {
}
}

func InitFlowWithVia(via string) InitFlowWithOption {
jonas-jonas marked this conversation as resolved.
Show resolved Hide resolved
return func(o *initFlowOptions) {
o.via = via
}
}

func InitializeLoginFlowViaBrowser(t *testing.T, client *http.Client, ts *httptest.Server, forced bool, isSPA bool, expectInitError bool, expectGetError bool, opts ...InitFlowWithOption) *kratos.LoginFlow {
publicClient := NewSDKCustomClient(ts, client)

Expand Down Expand Up @@ -164,9 +174,12 @@ func InitializeLoginFlowViaAPI(t *testing.T, client *http.Client, ts *httptest.S
if o.aal != "" {
req = req.Aal(string(o.aal))
}
if o.via != "" {
req = req.Via(o.via)
}

rs, _, err := req.Execute()
require.NoError(t, err)
rs, res, err := req.Execute()
require.NoError(t, err, "%s", ioutilx.MustReadAll(res.Body))
assert.Empty(t, rs.Active)

return rs
Expand Down
5 changes: 4 additions & 1 deletion internal/testhelpers/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ func NewKratosServerWithCSRFAndRouters(t *testing.T, reg driver.Registry) (publi
ran := negroni.New()
ran.UseFunc(x.RedirectAdminMiddleware)
ran.UseHandler(ra)
public = httptest.NewServer(x.NewTestCSRFHandler(rp, reg))
rpn := negroni.New()
rpn.UseFunc(x.HTTPLoaderContextMiddleware(reg))
rpn.UseHandler(rp)
public = httptest.NewServer(x.NewTestCSRFHandler(rpn, reg))
admin = httptest.NewServer(ran)
ctx := context.Background()

Expand Down
31 changes: 28 additions & 3 deletions selfservice/flow/login/extension_identifier_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,57 @@ package login
import (
"context"

"github.com/pkg/errors"
"github.com/samber/lo"

"github.com/ory/herodot"
"github.com/ory/kratos/text"

"github.com/ory/jsonschema/v3"
"github.com/ory/kratos/schema"
)

type identifierLabelExtension struct {
field string
identifierLabelCandidates []string
}

var _ schema.CompileExtension = new(identifierLabelExtension)
var (
_ schema.CompileExtension = new(identifierLabelExtension)
ErrUnknownTrait = herodot.ErrBadRequest.WithReasonf("Trait does not exist in identity schema")
jonas-jonas marked this conversation as resolved.
Show resolved Hide resolved
)

func GetIdentifierLabelFromSchema(ctx context.Context, schemaURL string) (*text.Message, error) {
ext := &identifierLabelExtension{}
return GetIdentifierLabelFromSchemaWithField(ctx, schemaURL, "")
}

func GetIdentifierLabelFromSchemaWithField(ctx context.Context, schemaURL string, trait string) (*text.Message, error) {
ext := &identifierLabelExtension{
field: trait,
}

runner, err := schema.NewExtensionRunner(ctx, schema.WithCompileRunners(ext))
if err != nil {
return nil, err
}
c := jsonschema.NewCompiler()
c.ExtractAnnotations = true
runner.Register(c)

_, err = c.Compile(ctx, schemaURL)
s, err := c.Compile(ctx, schemaURL)
if err != nil {
return nil, err
}

if trait != "" {
f, ok := s.Properties["traits"].Properties[trait]
if !ok {
knownTraits := lo.Keys(s.Properties["traits"].Properties)
return nil, errors.WithStack(ErrUnknownTrait.WithDetail("trait", trait).WithDetail("known_traits", knownTraits))
}
return text.NewInfoNodeLabelGenerated(f.Title), nil
}

metaLabel := text.NewInfoNodeLabelID()
if label := ext.getLabel(); label != "" {
metaLabel = text.NewInfoNodeLabelGenerated(label)
Expand Down
5 changes: 5 additions & 0 deletions selfservice/flow/login/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ type createNativeLoginFlow struct {
//
// in: query
ReturnTo string `json:"return_to"`

// Via should contain the identity's credential the code should be sent to. Only relevant in aal2 flows.
//
// in: query
Via string `json:"via"`
}

// swagger:route GET /self-service/login/api frontend createNativeLoginFlow
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{
"organization_id": null,
"refresh": false,
"requested_aal": "aal2",
"state": "choose_method",
"type": "browser",
"ui": {
"messages": [
{
"id": 1010004,
"text": "Please complete the second authentication challenge.",
"type": "info"
}
],
"method": "POST",
"nodes": [
{
"attributes": {
"disabled": false,
"name": "csrf_token",
"node_type": "input",
"required": true,
"type": "hidden"
},
"group": "default",
"messages": [],
"meta": {},
"type": "input"
},
{
"attributes": {
"disabled": false,
"name": "identifier",
"node_type": "input",
"required": true,
"type": "text",
"value": ""
},
"group": "default",
"messages": [
{
"context": {
"maskedTo": "fi****@ory.sh"
},
"id": 1010020,
"text": "We will send a code to fi****@ory.sh. To verify that this is your address please enter it here.",
"type": "info"
}
],
"meta": {
"label": {
"context": {
"title": "Email"
},
"id": 1070002,
"text": "Email",
"type": "info"
}
},
"type": "input"
},
{
"attributes": {
"disabled": false,
"name": "method",
"node_type": "input",
"type": "submit",
"value": "code"
},
"group": "code",
"messages": [],
"meta": {
"label": {
"id": 1010019,
"text": "Continue with code",
"type": "info"
}
},
"type": "input"
}
]
}
}

Loading
Loading