-
-
Notifications
You must be signed in to change notification settings - Fork 954
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow SMS as passwordless login method
This patch allows Ory Kratos to use the SMS gateway for login and registration with code via SMS.
- Loading branch information
Showing
15 changed files
with
123 additions
and
16 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
1 change: 1 addition & 0 deletions
1
courier/template/courier/builtin/templates/registration_code/valid/sms.body.gotmpl
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 @@ | ||
Your registration code is: {{ .RegistrationCode }} |
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,54 @@ | ||
// Copyright © 2023 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package sms | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"os" | ||
|
||
"github.com/ory/kratos/courier/template" | ||
) | ||
|
||
type ( | ||
RegistrationCodeValid struct { | ||
deps template.Dependencies | ||
model *RegistrationCodeValidModel | ||
} | ||
RegistrationCodeValidModel struct { | ||
To string `json:"to"` | ||
RegistrationCode string `json:"registration_code"` | ||
Identity map[string]interface{} `json:"identity"` | ||
RequestURL string `json:"request_url"` | ||
TransientPayload map[string]interface{} `json:"transient_payload"` | ||
} | ||
) | ||
|
||
func NewRegistrationCodeValid(d template.Dependencies, m *RegistrationCodeValidModel) *RegistrationCodeValid { | ||
return &RegistrationCodeValid{deps: d, model: m} | ||
} | ||
|
||
func (t *RegistrationCodeValid) PhoneNumber() (string, error) { | ||
return t.model.To, nil | ||
} | ||
|
||
func (t *RegistrationCodeValid) SMSBody(ctx context.Context) (string, error) { | ||
return template.LoadText( | ||
ctx, | ||
t.deps, | ||
os.DirFS(t.deps.CourierConfig().CourierTemplatesRoot(ctx)), | ||
"registration_code/valid/sms.body.gotmpl", | ||
"registration_code/valid/sms.body*", | ||
t.model, | ||
t.deps.CourierConfig().CourierSMSTemplatesRegistrationCodeValid(ctx).Body.PlainText, | ||
) | ||
} | ||
|
||
func (t *RegistrationCodeValid) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(t.model) | ||
} | ||
|
||
func (t *RegistrationCodeValid) TemplateType() template.TemplateType { | ||
return template.TypeRegistrationCodeValid | ||
} |
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,37 @@ | ||
// Copyright © 2023 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package sms_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ory/kratos/courier/template/sms" | ||
"github.com/ory/kratos/internal" | ||
) | ||
|
||
func TestNewRegistrationCodeValid(t *testing.T) { | ||
_, reg := internal.NewFastRegistryWithMocks(t) | ||
|
||
const ( | ||
expectedPhone = "+12345678901" | ||
otp = "012345" | ||
) | ||
|
||
tpl := sms.NewRegistrationCodeValid(reg, &sms.RegistrationCodeValidModel{To: expectedPhone, RegistrationCode: otp}) | ||
|
||
expectedBody := fmt.Sprintf("Your registration code is: %s\n", otp) | ||
|
||
actualBody, err := tpl.SMSBody(context.Background()) | ||
require.NoError(t, err) | ||
assert.Equal(t, expectedBody, actualBody) | ||
|
||
actualPhone, err := tpl.PhoneNumber() | ||
require.NoError(t, err) | ||
assert.Equal(t, expectedPhone, actualPhone) | ||
} |
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
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