-
-
Notifications
You must be signed in to change notification settings - Fork 964
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #99
- Loading branch information
Showing
38 changed files
with
766 additions
and
139 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
File renamed without changes.
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
File renamed without changes.
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
File renamed without changes.
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
File renamed without changes.
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
contrib/sql/migrations/20191100000005_identities.mysql.down.sql
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 @@ | ||
ALTER TABLE identity_credential_identifiers MODIFY COLUMN identifier VARCHAR(255); |
File renamed without changes.
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 @@ | ||
drop_table("courier_messages") |
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,10 @@ | ||
create_table("courier_messages") { | ||
t.Column("id", "uuid", {primary: true}) | ||
|
||
t.Column("type", "int") | ||
t.Column("status", "int") | ||
|
||
t.Column("body", "string") | ||
t.Column("subject", "string") | ||
t.Column("recipient", "string") | ||
} |
Empty file.
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,145 @@ | ||
package courier | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"fmt" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/cenkalti/backoff" | ||
"github.com/gofrs/uuid" | ||
"github.com/pkg/errors" | ||
"gopkg.in/gomail.v2" | ||
|
||
"github.com/ory/kratos/driver/configuration" | ||
"github.com/ory/kratos/x" | ||
) | ||
|
||
type ( | ||
smtpDependencies interface { | ||
PersistenceProvider | ||
x.LoggingProvider | ||
} | ||
Courier struct { | ||
dialer *gomail.Dialer | ||
d smtpDependencies | ||
c configuration.Provider | ||
} | ||
Provider interface { | ||
Courier() *Courier | ||
} | ||
) | ||
|
||
func NewSMTP(d smtpDependencies, c configuration.Provider) *Courier { | ||
uri := c.CourierSMTPURL() | ||
sslSkipVerify, _ := strconv.ParseBool(uri.Query().Get("skip_ssl_verify")) | ||
password, _ := uri.User.Password() | ||
port, _ := strconv.ParseInt(uri.Port(), 10, 64) | ||
return &Courier{ | ||
d: d, | ||
c: c, | ||
dialer: &gomail.Dialer{ | ||
Host: uri.Hostname(), | ||
Port: int(port), | ||
Username: uri.User.Username(), | ||
Password: password, | ||
SSL: uri.Scheme == "smtps", | ||
TLSConfig: &tls.Config{InsecureSkipVerify: sslSkipVerify}, | ||
}, | ||
} | ||
} | ||
|
||
func (m *Courier) SendEmail(ctx context.Context, t EmailTemplate) (uuid.UUID, error) { | ||
body, err := t.EmailBody() | ||
if err != nil { | ||
return uuid.Nil, err | ||
} | ||
|
||
subject, err := t.EmailBody() | ||
if err != nil { | ||
return uuid.Nil, err | ||
} | ||
|
||
recipient, err := t.EmailRecipient() | ||
if err != nil { | ||
return uuid.Nil, err | ||
} | ||
|
||
message := &Message{ | ||
Status: MessageStatusQueued, | ||
Type: MessageTypeEmail, | ||
Body: body, | ||
Subject: subject, | ||
Recipient: recipient, | ||
} | ||
if err := m.d.CourierPersister().AddMessage(ctx, message); err != nil { | ||
return uuid.Nil, err | ||
} | ||
return message.ID, nil | ||
} | ||
|
||
func (m *Courier) Work(ctx context.Context) error { | ||
errChan := make(chan error) | ||
defer close(errChan) | ||
|
||
go m.watchMessages(ctx, errChan) | ||
|
||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
case err := <-errChan: | ||
return err | ||
} | ||
} | ||
|
||
func (m *Courier) watchMessages(ctx context.Context, errChan chan error) { | ||
for { | ||
if err := backoff.Retry(func() error { | ||
messages, err := m.d.CourierPersister().NextMessages(ctx, 10) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for k := range messages { | ||
var msg Message = messages[k] | ||
|
||
switch msg.Type { | ||
case MessageTypeEmail: | ||
from := m.c.CourierSMTPFrom() | ||
gm := gomail.NewMessage() | ||
gm.SetHeader("From", from) | ||
gm.SetHeader("To", msg.Recipient) | ||
gm.SetHeader("Subject", msg.Subject) | ||
gm.SetBody("text/plain", msg.Body) | ||
gm.AddAlternative("text/html", msg.Body) | ||
|
||
if err := m.dialer.DialAndSend(gm); err != nil { | ||
m.d.Logger(). | ||
WithError(err). | ||
WithField("smtp_server", fmt.Sprintf("smtp(s)://%s:%d", m.dialer.Host, m.dialer.Port)). | ||
WithField("email_to", msg.Recipient).WithField("email_from", from). | ||
Error("Unable to send email using SMTP connection.") | ||
continue | ||
} | ||
|
||
if err := m.d.CourierPersister().SetMessageStatus(ctx, msg.ID, MessageStatusSent); err != nil { | ||
m.d.Logger(). | ||
WithError(err). | ||
WithField("message_id", msg.ID). | ||
Error(`Unable to set the message status to "sent".`) | ||
return err | ||
} | ||
default: | ||
return errors.Errorf("received unexpected message type: %d", msg.Type) | ||
} | ||
} | ||
|
||
return nil | ||
}, backoff.NewExponentialBackOff()); err != nil { | ||
errChan <- err | ||
return | ||
} | ||
time.Sleep(time.Second) | ||
} | ||
} |
Oops, something went wrong.