Skip to content

Commit

Permalink
chore: rename phone channel to sms
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-jonas committed Jan 5, 2024
1 parent d8b42f0 commit 9808796
Show file tree
Hide file tree
Showing 20 changed files with 197 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}
},
"verification": {
"via": "phone"
"via": "sms"
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions contrib/quickstart/kratos/phone-password/kratos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +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)

Check warning on line 64 in courier/http_channel.go

View check run for this annotation

Codecov / codecov/patch

courier/http_channel.go#L64

Added line #L64 was not covered by tests
}

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

Check warning on line 69 in courier/http_channel.go

View check run for this annotation

Codecov / codecov/patch

courier/http_channel.go#L69

Added line #L69 was not covered by tests
}

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)

Check warning on line 83 in courier/http_channel.go

View check run for this annotation

Codecov / codecov/patch

courier/http_channel.go#L83

Added line #L83 was not covered by tests
}

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)

Check warning on line 112 in courier/http_channel.go

View check run for this annotation

Codecov / codecov/patch

courier/http_channel.go#L112

Added line #L112 was not covered by tests
}

func newTemplate(d template.Dependencies, msg Message) (Template, error) {
Expand Down
2 changes: 1 addition & 1 deletion courier/sms.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (c *courier) QueueSMS(ctx context.Context, t SMSTemplate) (uuid.UUID, error
message := &Message{
Status: MessageStatusQueued,
Type: MessageTypeSMS,
Channel: "phone",
Channel: "sms",
Recipient: recipient,
TemplateType: t.TemplateType(),
TemplateData: templateData,
Expand Down
4 changes: 2 additions & 2 deletions courier/sms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestQueueSMS(t *testing.T) {
conf, reg := internal.NewFastRegistryWithMocks(t)
conf.MustSet(ctx, config.ViperKeyCourierChannels, fmt.Sprintf(`[
{
"id": "phone",
"id": "sms",
"type": "http",
"request_config": %s
}
Expand Down Expand Up @@ -122,7 +122,7 @@ func TestDisallowedInternalNetwork(t *testing.T) {
conf, reg := internal.NewFastRegistryWithMocks(t)
conf.MustSet(ctx, config.ViperKeyCourierChannels, `[
{
"id": "phone",
"id": "sms",
"type": "http",
"request_config": {
"url": "http://127.0.0.1/",
Expand Down
2 changes: 1 addition & 1 deletion embedx/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1999,7 +1999,7 @@
"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",
Expand Down
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
20 changes: 14 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,18 @@
"patternProperties": {
".*": {
"type": "object",
"properties": {
"format": {
"enum": [
"email",
"tel",
"date",
"time",
"date-time",
"noformat"
]
}
},
"allOf": [
{
"$ref": "ory://identity-extension"
Expand All @@ -40,9 +50,7 @@
}
}
},
"required": [
"properties"
]
"required": ["properties"]
}
]
}
72 changes: 49 additions & 23 deletions identity/extension_verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@ import (
"sync"
"time"

"golang.org/x/exp/maps"

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

func init() {
jsonschema.Formats["noformat"] = func(v interface{}) bool {
return true
}
}

type SchemaExtensionVerification struct {
lifespan time.Duration
l sync.Mutex
Expand All @@ -24,40 +32,58 @@ func NewSchemaExtensionVerification(i *Identity, lifespan time.Duration) *Schema
return &SchemaExtensionVerification{i: i, lifespan: lifespan}
}

const (
ChannelTypeEmail = "email"
ChannelTypeSMS = "sms"
ChannelTypePhone = "phone"
)

func (r *SchemaExtensionVerification) Run(ctx jsonschema.ValidationContext, s schema.ExtensionConfig, value interface{}) error {
r.l.Lock()
defer r.l.Unlock()

switch s.Verification.Via {
case AddressTypeEmail:
if !jsonschema.Formats["email"](value) {
return ctx.Error("format", "%q is not valid %q", value, "email")
}

address := NewVerifiableEmailAddress(
strings.ToLower(strings.TrimSpace(
fmt.Sprintf("%s", value))), r.i.ID)

r.appendAddress(address)
if s.Verification.Via == "" {
return nil
}

format, ok := s.RawSchema["format"]
if !ok {
format = ""
}
formatString, ok := format.(string)
if !ok {
return nil
}

case AddressTypePhone:
if !jsonschema.Formats["tel"](value) {
return ctx.Error("format", "%q is not valid %q", value, "phone")
if formatString == "" {
switch s.Verification.Via {
case ChannelTypeEmail:
formatString = "email"
formatter, ok := jsonschema.Formats[formatString]
if !ok {
supportedKeys := maps.Keys(jsonschema.Formats)
return ctx.Error("format", "format %q is not supported. Supported formats are [%s]", formatString, strings.Join(supportedKeys, ", "))

Check warning on line 65 in identity/extension_verification.go

View check run for this annotation

Codecov / codecov/patch

identity/extension_verification.go#L64-L65

Added lines #L64 - L65 were not covered by tests
}

if !formatter(value) {
return ctx.Error("format", "%q is not valid %q", value, formatString)
}
default:
return ctx.Error("format", "no format specified. A format is required if verification is enabled. If this was intentional, please set \"format\" to \"noformat\"")
}
}

address := NewVerifiablePhoneAddress(fmt.Sprintf("%s", value), r.i.ID)

r.appendAddress(address)

return nil

case "":
return nil
var normalized string
switch formatString {
case "email":
normalized = strings.ToLower(strings.TrimSpace(fmt.Sprintf("%s", value)))
default:
normalized = strings.TrimSpace(fmt.Sprintf("%s", value))
}

return ctx.Error("", "verification.via has unknown value %q", s.Verification.Via)
address := NewVerifiableAddress(normalized, r.i.ID, s.Verification.Via)
r.appendAddress(address)
return nil
}

func (r *SchemaExtensionVerification) Finish() error {
Expand Down
Loading

0 comments on commit 9808796

Please sign in to comment.