Skip to content

Commit

Permalink
fix: rename "phone" courier channel to "sms" (#3680)
Browse files Browse the repository at this point in the history
Co-authored-by: zepatrik <[email protected]>
  • Loading branch information
jonas-jonas and zepatrik authored Jan 5, 2024
1 parent 699e5d5 commit eb8d1b9
Show file tree
Hide file tree
Showing 31 changed files with 321 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}
},
"verification": {
"via": "phone"
"via": "sms"
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions contrib/quickstart/kratos/phone-password/kratos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,12 @@ identity:

courier:
channels:
- id: phone
- id: sms
type: http
request_config:
url: https://api.twilio.com/2010-04-01/Accounts/AXXXXXXXXXXXXXX/Messages.json
method: POST
body: base64://ZnVuY3Rpb24oY3R4KSB7CkJvZHk6IGN0eC5ib2R5LApUbzogY3R4LnRvLEZyb206IGN0eC5mcm9tCn0=
body: base64://ZnVuY3Rpb24oY3R4KSB7ClRvOiBjdHguUmVjaXBpZW50LApCb2R5OiBjdHguQm9keSwKfQ==
headers:
Content-Type: application/x-www-form-urlencoded
auth:
Expand Down
10 changes: 5 additions & 5 deletions courier/http_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ func (c *httpChannel) Dispatch(ctx context.Context, msg Message) (err error) {

builder, err := request.NewBuilder(ctx, c.requestConfig, c.d)
if err != nil {
return err
return errors.WithStack(err)
}

tmpl, err := newTemplate(c.d, msg)
if err != nil {
return err
return errors.WithStack(err)
}

td := httpDataModel{
Expand All @@ -80,12 +80,12 @@ func (c *httpChannel) Dispatch(ctx context.Context, msg Message) (err error) {

req, err := builder.BuildRequest(ctx, td)
if err != nil {
return err
return errors.WithStack(err)
}

res, err := c.d.HTTPClient(ctx).Do(req)
if err != nil {
return err
return errors.WithStack(err)
}

if res.StatusCode >= 200 && res.StatusCode < 300 {
Expand All @@ -109,7 +109,7 @@ func (c *httpChannel) Dispatch(ctx context.Context, msg Message) (err error) {
WithField("message_subject", msg.Subject).
WithError(err).
Error("sending mail via HTTP failed.")
return err
return errors.WithStack(err)
}

func newTemplate(d template.Dependencies, msg Message) (Template, error) {
Expand Down
2 changes: 1 addition & 1 deletion driver/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,7 @@ func (p *Config) CourierTemplatesVerificationCodeValid(ctx context.Context) *Cou
}

func (p *Config) CourierSMSTemplatesVerificationCodeValid(ctx context.Context) *CourierSMSTemplate {
return p.CourierSMSTemplatesHelper(ctx, ViperKeyCourierTemplatesVerificationCodeValidEmail)
return p.CourierSMSTemplatesHelper(ctx, ViperKeyCourierTemplatesVerificationCodeValidSMS)
}

func (p *Config) CourierTemplatesLoginCodeValid(ctx context.Context) *CourierEmailTemplate {
Expand Down
8 changes: 7 additions & 1 deletion embedx/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1999,7 +1999,13 @@
"title": "Channel id",
"description": "The channel id. Corresponds to the .via property of the identity schema for recovery, verification, etc. Currently only phone is supported.",
"maxLength": 32,
"enum": ["phone"]
"enum": ["sms"]
},
"type": {
"type": "string",
"title": "Channel type",
"description": "The channel type. Currently only http is supported.",
"enum": ["http"]
},
"request_config": {
"$ref": "#/definitions/httpRequestConfig"
Expand Down
39 changes: 37 additions & 2 deletions embedx/embedx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ package embedx

import (
"context"
"embed"
"io/fs"
"strings"
"testing"

"github.com/stretchr/testify/require"

"github.com/stretchr/testify/assert"

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

func TestAddSchemaResources(t *testing.T) {

for _, tc := range []struct {
description string
dependencies []SchemaType
Expand Down Expand Up @@ -65,7 +69,38 @@ func TestAddSchemaResources(t *testing.T) {
assert.NoError(t, err)
}
}

})
}
}

//go:embed testdata/identity_meta.*
var identityMetaTestCases embed.FS

func TestIdentityMetaSchema(t *testing.T) {
c := jsonschema.NewCompiler()
err := AddSchemaResources(c, IdentityMeta, IdentityExtension)
require.NoError(t, err)

schema, err := c.Compile(context.Background(), IdentityMeta.GetSchemaID())
require.NoError(t, err)

require.NoError(t, fs.WalkDir(identityMetaTestCases, ".", func(path string, d fs.DirEntry, err error) error {
if d.IsDir() {
return nil
}

t.Run("case="+path, func(t *testing.T) {
f, err := identityMetaTestCases.Open(path)
require.NoError(t, err)

err = schema.Validate(f)
if strings.HasSuffix(path, "invalid.schema.json") {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})

return nil
}))
}
2 changes: 1 addition & 1 deletion embedx/identity_extension.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"properties": {
"via": {
"type": "string",
"enum": ["email", "phone"]
"enum": ["email", "sms"]
}
}
},
Expand Down
34 changes: 28 additions & 6 deletions embedx/identity_meta.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
"properties": {
"properties": {
"type": "object",
"required": [
"traits"
],
"required": ["traits"],
"properties": {
"traits": {
"type": "object",
Expand All @@ -27,6 +25,32 @@
"patternProperties": {
".*": {
"type": "object",
"if": {
"properties": {
"ory.sh/kratos": {
"type": "object",
"properties": {
"verification": {}
},
"required": ["verification"]
}
},
"required": ["ory.sh/kratos"]
},
"then": {
"properties": {
"format": {
"enum": [
"email",
"tel",
"date",
"time",
"date-time",
"no-validate"
]
}
}
},
"allOf": [
{
"$ref": "ory://identity-extension"
Expand All @@ -40,9 +64,7 @@
}
}
},
"required": [
"properties"
]
"required": ["properties"]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
23 changes: 23 additions & 0 deletions embedx/testdata/identity_meta.simple.valid.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"properties": {
"traits": {
"type": "object",
"properties": {
"email": {
"type": "string",
"format": "email",
"ory.sh/kratos": {
"credentials": {
"password": {
"identifier": true
}
},
"verification": {
"via": "email"
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"properties": {
"traits": {
"type": "object",
"properties": {
"email": {
"type": "string",
"format": "unknown",
"ory.sh/kratos": {
"credentials": {
"password": {
"identifier": true
}
},
"verification": {
"via": "email"
}
}
}
}
}
}
}
1 change: 0 additions & 1 deletion identity/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ package identity

const (
AddressTypeEmail = "email"
AddressTypePhone = "phone"
)
9 changes: 0 additions & 9 deletions identity/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,6 @@ func ParseCredentialsType(in string) (CredentialsType, bool) {
return "", false
}

// swagger:ignore
type CredentialsIdentifierAddressType string

const (
CredentialsIdentifierAddressTypeEmail CredentialsIdentifierAddressType = AddressTypeEmail
CredentialsIdentifierAddressTypePhone CredentialsIdentifierAddressType = AddressTypePhone
CredentialsIdentifierAddressTypeNone CredentialsIdentifierAddressType = "none"
)

// Credentials represents a specific credential type
//
// swagger:model identityCredentials
Expand Down
1 change: 0 additions & 1 deletion identity/credentials_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ type CodeAddressType string

const (
CodeAddressTypeEmail CodeAddressType = AddressTypeEmail
CodeAddressTypePhone CodeAddressType = AddressTypePhone
)

// CredentialsCode represents a one time login/registration code
Expand Down
8 changes: 4 additions & 4 deletions identity/extension_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewSchemaExtensionCredentials(i *Identity) *SchemaExtensionCredentials {
return &SchemaExtensionCredentials{i: i}
}

func (r *SchemaExtensionCredentials) setIdentifier(ct CredentialsType, value interface{}, addressType CredentialsIdentifierAddressType) {
func (r *SchemaExtensionCredentials) setIdentifier(ct CredentialsType, value interface{}) {
cred, ok := r.i.GetCredentials(ct)
if !ok {
cred = &Credentials{
Expand All @@ -49,11 +49,11 @@ func (r *SchemaExtensionCredentials) Run(ctx jsonschema.ValidationContext, s sch
defer r.l.Unlock()

if s.Credentials.Password.Identifier {
r.setIdentifier(CredentialsTypePassword, value, CredentialsIdentifierAddressTypeNone)
r.setIdentifier(CredentialsTypePassword, value)
}

if s.Credentials.WebAuthn.Identifier {
r.setIdentifier(CredentialsTypeWebAuthn, value, CredentialsIdentifierAddressTypeNone)
r.setIdentifier(CredentialsTypeWebAuthn, value)
}

if s.Credentials.Code.Identifier {
Expand All @@ -63,7 +63,7 @@ func (r *SchemaExtensionCredentials) Run(ctx jsonschema.ValidationContext, s sch
return ctx.Error("format", "%q is not a valid %q", value, s.Credentials.Code.Via)
}

r.setIdentifier(CredentialsTypeCodeAuth, value, CredentialsIdentifierAddressTypeEmail)
r.setIdentifier(CredentialsTypeCodeAuth, value)
// case f.AddCase(AddressTypePhone):
// if !jsonschema.Formats["tel"](value) {
// return ctx.Error("format", "%q is not a valid %q", value, s.Credentials.Code.Via)
Expand Down
Loading

0 comments on commit eb8d1b9

Please sign in to comment.