diff --git a/courier/sms.go b/courier/sms.go index 3fbb8d78dba8..7872e219e7c4 100644 --- a/courier/sms.go +++ b/courier/sms.go @@ -3,9 +3,12 @@ package courier import ( "context" "encoding/json" - "errors" "net/http" + "github.com/pkg/errors" + + "github.com/ory/herodot" + "github.com/gofrs/uuid" "github.com/ory/kratos/request" @@ -25,10 +28,6 @@ type smsClient struct { } func newSMS(ctx context.Context, deps Dependencies) *smsClient { - if !deps.CourierConfig(ctx).CourierSMSEnabled() { - deps.Logger().Error("messages will not be sent - no sms gate server address is set in config") - } - return &smsClient{ RequestConfig: deps.CourierConfig(ctx).CourierSMSRequestConfig(), @@ -68,6 +67,10 @@ func (c *courier) QueueSMS(ctx context.Context, t SMSTemplate) (uuid.UUID, error } func (c *courier) dispatchSMS(ctx context.Context, msg Message) error { + if !c.deps.CourierConfig(ctx).CourierSMSEnabled() { + return errors.WithStack(herodot.ErrInternalServerError.WithReasonf("Courier tried to deliver an sms but courier.sms.enabled is set to false!")) + } + tmpl, err := c.smsClient.NewTemplateFromMessage(c.deps, msg) if err != nil { return err diff --git a/courier/smtp.go b/courier/smtp.go index 6eba348764a1..8d5e19f83be2 100644 --- a/courier/smtp.go +++ b/courier/smtp.go @@ -8,6 +8,8 @@ import ( "strconv" "time" + "github.com/ory/kratos/driver/config" + "github.com/gofrs/uuid" "github.com/pkg/errors" @@ -117,7 +119,7 @@ func (c *courier) QueueEmail(ctx context.Context, t EmailTemplate) (uuid.UUID, e func (c *courier) dispatchEmail(ctx context.Context, msg Message) error { if c.smtpClient.Host == "" { - return errors.WithStack(herodot.ErrInternalServerError.WithReasonf("Courier tried to deliver an email but courier.smtp_url is not set!")) + return errors.WithStack(herodot.ErrInternalServerError.WithReasonf("Courier tried to deliver an email but %s is not set!", config.ViperKeyCourierSMTPURL)) } from := c.deps.CourierConfig(ctx).CourierSMTPFrom() diff --git a/driver/config/.snapshots/TestCourierSMS-case=configs_set.json b/driver/config/.snapshots/TestCourierSMS-case=configs_set.json new file mode 100644 index 000000000000..471e0019a448 --- /dev/null +++ b/driver/config/.snapshots/TestCourierSMS-case=configs_set.json @@ -0,0 +1,15 @@ +{ + "auth": { + "config": { + "password": "YourPass", + "user": "YourUsername" + }, + "type": "basic_auth" + }, + "body": "base64://e30=", + "header": { + "Content-Type": "application/x-www-form-urlencoded" + }, + "method": "POST", + "url": "https://api.twilio.com/2010-04-01/Accounts/YourAccountID/Messages.json" +} diff --git a/driver/config/.snapshots/TestCourierSMS-case=defaults.json b/driver/config/.snapshots/TestCourierSMS-case=defaults.json new file mode 100644 index 000000000000..19765bd501b6 --- /dev/null +++ b/driver/config/.snapshots/TestCourierSMS-case=defaults.json @@ -0,0 +1 @@ +null diff --git a/driver/config/config_test.go b/driver/config/config_test.go index fb674635fa19..808517557294 100644 --- a/driver/config/config_test.go +++ b/driver/config/config_test.go @@ -16,6 +16,8 @@ import ( "testing" "time" + "github.com/ory/x/snapshotx" + "github.com/ghodss/yaml" "github.com/spf13/cobra" @@ -1046,6 +1048,26 @@ func TestChangeMinPasswordLength(t *testing.T) { }) } +func TestCourierSMS(t *testing.T) { + ctx := context.Background() + + t.Run("case=configs set", func(t *testing.T) { + conf, _ := config.New(ctx, logrusx.New("", ""), os.Stderr, + configx.WithConfigFiles("stub/.kratos.courier.sms.yaml"), configx.SkipValidation()) + assert.True(t, conf.CourierSMSEnabled()) + snapshotx.SnapshotTExcept(t, conf.CourierSMSRequestConfig(), nil) + assert.Equal(t, "+49123456789", conf.CourierSMSFrom()) + }) + + t.Run("case=defaults", func(t *testing.T) { + conf, _ := config.New(ctx, logrusx.New("", ""), os.Stderr, configx.SkipValidation()) + + assert.False(t, conf.CourierSMSEnabled()) + snapshotx.SnapshotTExcept(t, conf.CourierSMSRequestConfig(), nil) + assert.Equal(t, "Ory Kratos", conf.CourierSMSFrom()) + }) +} + func TestCourierTemplatesConfig(t *testing.T) { ctx := context.Background() diff --git a/driver/config/stub/.kratos.courier.sms.yaml b/driver/config/stub/.kratos.courier.sms.yaml new file mode 100644 index 000000000000..1c2fbae89c3a --- /dev/null +++ b/driver/config/stub/.kratos.courier.sms.yaml @@ -0,0 +1,28 @@ +dsn: sqlite://foo.db?mode=memory&_fk=true + +selfservice: + default_browser_return_url: http://return-to-3-test.ory.sh/ + +identity: + default_schema_id: default + schemas: + - id: default + url: base64://ewogICIkaWQiOiAib3J5Oi8vaWRlbnRpdHktdGVzdC1zY2hlbWEiLAogICIkc2NoZW1hIjogImh0dHA6Ly9qc29uLXNjaGVtYS5vcmcvZHJhZnQtMDcvc2NoZW1hIyIsCiAgInRpdGxlIjogIklkZW50aXR5U2NoZW1hIiwKICAidHlwZSI6ICJvYmplY3QiLAogICJwcm9wZXJ0aWVzIjogewogICAgInRyYWl0cyI6IHsKICAgICAgInR5cGUiOiAib2JqZWN0IiwKICAgICAgInByb3BlcnRpZXMiOiB7CiAgICAgICAgIm5hbWUiOiB7CiAgICAgICAgICAidHlwZSI6ICJvYmplY3QiLAogICAgICAgICAgInByb3BlcnRpZXMiOiB7CiAgICAgICAgICAgICJmaXJzdCI6IHsKICAgICAgICAgICAgICAidHlwZSI6ICJzdHJpbmciCiAgICAgICAgICAgIH0sCiAgICAgICAgICAgICJsYXN0IjogewogICAgICAgICAgICAgICJ0eXBlIjogInN0cmluZyIKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0KICAgICAgfSwKICAgICAgInJlcXVpcmVkIjogWwogICAgICAgICJuYW1lIgogICAgICBdLAogICAgICAiYWRkaXRpb25hbFByb3BlcnRpZXMiOiB0cnVlCiAgICB9CiAgfQp9 + +courier: + smtp: + connection_uri: smtp://foo:bar@baz/ + sms: + enabled: true + from: '+49123456789' + request_config: + url: https://api.twilio.com/2010-04-01/Accounts/YourAccountID/Messages.json + method: POST + body: base64://e30= + header: + 'Content-Type': 'application/x-www-form-urlencoded' + auth: + type: basic_auth + config: + user: YourUsername + password: YourPass