Skip to content

Commit 58897c6

Browse files
authored
Custom SES email config from the builder (#91)
1 parent 929349c commit 58897c6

File tree

5 files changed

+67
-40
lines changed

5 files changed

+67
-40
lines changed

proto/builder/builder.gen.go

+10-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/builder/builder.ridl

+10-11
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,50 @@ webrpc = v1 # TODO, use v2
33
name = sequence-builder
44
version = v0.1.0
55

6-
76
# db table: "email_templates"
87
struct EmailTemplate
98
- id: uint64
109
+ go.field.name = ID
1110
+ go.tag.db = id,omitempty
12-
1311
- templateType?: EmailTemplateType
1412
+ go.field.name = TemplateType
1513
+ go.tag.db = template_type
16-
1714
- projectId: uint64
1815
+ go.field.name = ProjectID
1916
+ go.tag.db = project_id
20-
2117
- subject: string
2218
+ go.tag.db = subject
23-
2419
- introText: string
2520
+ go.tag.db = intro_text
26-
2721
- logoUrl: string
2822
+ go.field.name = LogoURL
2923
+ go.tag.db = logo_url
30-
3124
- template?: string
3225
+ go.field.name = Template
3326
+ go.tag.db = template
34-
27+
- fromEmail?: string
28+
+ go.tag.db = from_email
3529
- placeholders: []string
3630
+ go.tag.db = placeholders
37-
31+
- sesConfig?: SESSettings
32+
+ go.tag.db = ses_config
3833
- createdAt: timestamp
3934
+ json = createdAt
4035
+ go.tag.db = created_at
41-
4236
- updatedAt: timestamp
4337
+ json = updatedAt
4438
+ go.tag.db = updated_at
45-
4639
- deletedAt?: timestamp
4740
+ go.tag.db = deleted_at,omitempty
4841
+ go.tag.json = deletedAt,omitempty
4942
+ go.field.type = *time.Time
5043

44+
struct SESSettings
45+
- accessRoleARN: string
46+
+ go.field.name = AccessRoleARN
47+
- sourceARN: string
48+
+ go.field.name = SourceARN
49+
5150
enum EmailTemplateType: uint8
5251
- UNKNOWN
5352
- LOGIN

rpc/auth/email/provider.go

+10
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ func (p *AuthProvider) InitiateAuth(
111111
HTML: strings.Replace(*tpl.Template, "{auth_code}", secretCode, 1),
112112
Text: tpl.IntroText + "\n\n" + secretCode,
113113
}
114+
115+
if tpl.FromEmail != nil {
116+
msg.Source = *tpl.FromEmail
117+
}
118+
119+
if tpl.SesConfig != nil {
120+
msg.SourceARN = tpl.SesConfig.SourceARN
121+
msg.AccessRoleARN = tpl.SesConfig.AccessRoleARN
122+
}
123+
114124
if err := p.Sender.Send(ctx, msg); err != nil {
115125
return nil, fmt.Errorf("failed to send email: %w", err)
116126
}

rpc/auth/email/sender.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ import (
55
)
66

77
type Message struct {
8-
Recipient string
9-
Subject string
10-
HTML string
11-
Text string
8+
Recipient string
9+
Subject string
10+
HTML string
11+
Text string
12+
Source string
13+
SourceARN string
14+
AccessRoleARN string
1215
}
1316

1417
type Sender interface {

rpc/auth/email/ses.go

+30-22
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,47 @@ import (
1212
)
1313

1414
type sesSender struct {
15-
client *ses.Client
16-
source *string
17-
sourceARN *string
15+
cfg config.SESConfig
16+
awsCfg aws.Config
1817
}
1918

2019
func NewSESSender(awsCfg aws.Config, cfg config.SESConfig) Sender {
21-
if cfg.AccessRoleARN != "" {
20+
sender := &sesSender{
21+
cfg: cfg,
22+
awsCfg: awsCfg,
23+
}
24+
25+
return sender
26+
}
27+
28+
func (s *sesSender) Send(ctx context.Context, msg *Message) error {
29+
awsCfg := s.awsCfg
30+
accessRoleARN := s.cfg.AccessRoleARN
31+
if msg.AccessRoleARN != "" {
32+
accessRoleARN = msg.AccessRoleARN
33+
}
34+
if accessRoleARN != "" {
2235
stsClient := sts.NewFromConfig(awsCfg)
23-
creds := stscreds.NewAssumeRoleProvider(stsClient, cfg.AccessRoleARN)
36+
creds := stscreds.NewAssumeRoleProvider(stsClient, accessRoleARN)
2437
awsCfg.Credentials = aws.NewCredentialsCache(creds)
2538
}
26-
27-
if cfg.Region != "" {
28-
awsCfg.Region = cfg.Region
39+
if s.cfg.Region != "" {
40+
awsCfg.Region = s.cfg.Region
2941
}
3042

31-
sender := &sesSender{
32-
client: ses.NewFromConfig(awsCfg),
33-
}
43+
client := ses.NewFromConfig(awsCfg)
3444

35-
if cfg.Source != "" {
36-
sender.source = &cfg.Source
45+
source := &s.cfg.Source
46+
if msg.Source != "" {
47+
source = &msg.Source
3748
}
3849

39-
if cfg.SourceARN != "" {
40-
sender.sourceARN = &cfg.SourceARN
50+
sourceARN := &s.cfg.SourceARN
51+
if msg.SourceARN != "" {
52+
sourceARN = &msg.SourceARN
4153
}
4254

43-
return sender
44-
}
45-
46-
func (s *sesSender) Send(ctx context.Context, msg *Message) error {
47-
_, err := s.client.SendEmail(ctx, &ses.SendEmailInput{
55+
_, err := client.SendEmail(ctx, &ses.SendEmailInput{
4856
Destination: &types.Destination{
4957
ToAddresses: []string{msg.Recipient},
5058
},
@@ -64,8 +72,8 @@ func (s *sesSender) Send(ctx context.Context, msg *Message) error {
6472
Charset: aws.String("UTF-8"),
6573
},
6674
},
67-
Source: s.source,
68-
SourceArn: s.sourceARN,
75+
Source: source,
76+
SourceArn: sourceARN,
6977
})
7078
return err
7179
}

0 commit comments

Comments
 (0)