/
email_provider.go
311 lines (262 loc) · 9.93 KB
/
email_provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package management
import (
"context"
"encoding/json"
"net/http"
)
const (
// EmailProviderMandrill constant.
EmailProviderMandrill = "mandrill"
// EmailProviderSES constant.
EmailProviderSES = "ses"
// EmailProviderSendGrid constant.
EmailProviderSendGrid = "sendgrid"
// EmailProviderSparkPost constant.
EmailProviderSparkPost = "sparkpost"
// EmailProviderMailgun constant.
EmailProviderMailgun = "mailgun"
// EmailProviderSMTP constant.
EmailProviderSMTP = "smtp"
// EmailProviderAzureCS constant.
EmailProviderAzureCS = "azure_cs"
// EmailProviderMS365 constant.
EmailProviderMS365 = "ms365"
)
// EmailProvider is used to configure Email Providers.
//
// See: https://auth0.com/docs/customize/email
type EmailProvider struct {
// The name of the email provider.
// Can be one of "mandrill", "ses", "sendgrid", "sparkpost", "mailgun" or "smtp".
Name *string `json:"name,omitempty"`
// Indicates whether the email provider is enabled or not.
// Defaults to true.
Enabled *bool `json:"enabled,omitempty"`
// The default FROM address.
DefaultFromAddress *string `json:"default_from_address,omitempty"`
// Credentials required to use the provider.
Credentials interface{} `json:"-"`
// Specific provider settings.
Settings interface{} `json:"-"`
}
// EmailProviderCredentialsMandrill represent the
// credentials required to use the mandrill provider.
type EmailProviderCredentialsMandrill struct {
APIKey *string `json:"api_key,omitempty"`
}
// EmailProviderCredentialsSES represent the
// credentials required to use the ses provider.
type EmailProviderCredentialsSES struct {
AccessKeyID *string `json:"accessKeyId,omitempty"`
SecretAccessKey *string `json:"secretAccessKey,omitempty"`
Region *string `json:"region,omitempty"`
}
// EmailProviderCredentialsSendGrid represent the
// credentials required to use the sendgrid provider.
type EmailProviderCredentialsSendGrid struct {
APIKey *string `json:"api_key,omitempty"`
}
// EmailProviderCredentialsSparkPost represent the
// credentials required to use the sparkpost provider.
type EmailProviderCredentialsSparkPost struct {
APIKey *string `json:"api_key,omitempty"`
Region *string `json:"region,omitempty"`
}
// EmailProviderCredentialsMailgun represent the
// credentials required to use the mailgun provider.
type EmailProviderCredentialsMailgun struct {
APIKey *string `json:"api_key,omitempty"`
Domain *string `json:"domain,omitempty"`
Region *string `json:"region,omitempty"`
}
// EmailProviderCredentialsSMTP represent the
// credentials required to use the smtp provider.
type EmailProviderCredentialsSMTP struct {
SMTPHost *string `json:"smtp_host,omitempty"`
SMTPPort *int `json:"smtp_port,omitempty"`
SMTPUser *string `json:"smtp_user,omitempty"`
SMTPPass *string `json:"smtp_pass,omitempty"`
}
// EmailProviderCredentialsAzureCS represent the
// credentials required to use the azure_cs provider.
type EmailProviderCredentialsAzureCS struct {
// Azure Communication Services Connection String.
ConnectionString *string `json:"connectionString,omitempty"`
}
// EmailProviderCredentialsMS365 represent the
// credentials required to use the ms365 provider.
type EmailProviderCredentialsMS365 struct {
// Microsoft 365 Tenant ID.
TenantID *string `json:"tenantId,omitempty"`
// Microsoft 365 Client ID.
ClientID *string `json:"clientId,omitempty"`
// Microsoft 365 Client Secret.
ClientSecret *string `json:"clientSecret,omitempty"`
}
// EmailProviderSettingsMandrill are the provider
// specific settings used by the mandrill provider.
type EmailProviderSettingsMandrill struct {
Message *EmailProviderSettingsMandrillMessage `json:"message,omitempty"`
}
// EmailProviderSettingsMandrillMessage contains the
// message settings content for the mandrill provider.
type EmailProviderSettingsMandrillMessage struct {
ViewContentLink *bool `json:"view_content_link,omitempty"`
}
// EmailProviderSettingsSES are the provider
// specific settings used by the ses provider.
type EmailProviderSettingsSES struct {
Message *EmailProviderSettingsSESMessage `json:"message,omitempty"`
}
// EmailProviderSettingsSESMessage contains the
// message settings content for the ses provider.
type EmailProviderSettingsSESMessage struct {
ConfigurationSetName *string `json:"configuration_set_name,omitempty"`
}
// EmailProviderSettingsSMTP are the provider
// specific settings used by the smtp provider.
type EmailProviderSettingsSMTP struct {
Headers *EmailProviderSettingsSMTPHeaders `json:"headers,omitempty"`
}
// EmailProviderSettingsSMTPHeaders contains the
// headers settings content for the smtp provider.
type EmailProviderSettingsSMTPHeaders struct {
XMCViewContentLink *string `json:"X-MC-ViewContentLink,omitempty"`
XSESConfigurationSet *string `json:"X-SES-Configuration-Set,omitempty"`
}
type emailProvider EmailProvider
type emailProviderWrapper struct {
*emailProvider
RawCredentials json.RawMessage `json:"credentials,omitempty"`
RawSettings json.RawMessage `json:"settings,omitempty"`
}
// MarshalJSON is a custom serializer for the EmailProvider type.
func (ep *EmailProvider) MarshalJSON() ([]byte, error) {
wrapper := &emailProviderWrapper{(*emailProvider)(ep), nil, nil}
if ep.Credentials != nil {
credentialsJSON, err := json.Marshal(ep.Credentials)
if err != nil {
return nil, err
}
wrapper.RawCredentials = credentialsJSON
}
if ep.Settings != nil {
settingsJSON, err := json.Marshal(ep.Settings)
if err != nil {
return nil, err
}
wrapper.RawSettings = settingsJSON
}
return json.Marshal(wrapper)
}
// UnmarshalJSON is a custom deserializer for the EmailProvider type.
func (ep *EmailProvider) UnmarshalJSON(b []byte) error {
wrapper := &emailProviderWrapper{(*emailProvider)(ep), nil, nil}
if err := json.Unmarshal(b, wrapper); err != nil {
return err
}
var credentials, settings interface{}
switch ep.GetName() {
case EmailProviderMandrill:
credentials = &EmailProviderCredentialsMandrill{}
settings = &EmailProviderSettingsMandrill{}
case EmailProviderSES:
credentials = &EmailProviderCredentialsSES{}
settings = &EmailProviderSettingsSES{}
case EmailProviderSendGrid:
credentials = &EmailProviderCredentialsSendGrid{}
// No settings for sendgrid.
settings = nil
case EmailProviderSparkPost:
credentials = &EmailProviderCredentialsSparkPost{}
// No settings for sparkpost.
settings = nil
case EmailProviderMailgun:
credentials = &EmailProviderCredentialsMailgun{}
// No settings for mailgun.
settings = nil
case EmailProviderSMTP:
credentials = &EmailProviderCredentialsSMTP{}
settings = &EmailProviderSettingsSMTP{}
case EmailProviderAzureCS:
credentials = &EmailProviderCredentialsAzureCS{}
// No settings for azure_cs.
settings = nil
case EmailProviderMS365:
credentials = &EmailProviderCredentialsMS365{}
// No settings for ms365.
settings = nil
case "":
credentials = nil
settings = nil
default:
// Just making sure we're covered if
// new email providers are introduced.
credentials = make(map[string]interface{})
settings = make(map[string]interface{})
}
if wrapper.RawCredentials != nil {
if err := json.Unmarshal(wrapper.RawCredentials, &credentials); err != nil {
return err
}
}
if wrapper.RawSettings != nil {
if err := json.Unmarshal(wrapper.RawSettings, &settings); err != nil {
return err
}
}
ep.Credentials = credentials
ep.Settings = settings
return nil
}
// EmailProviderManager manages the Auth0 EmailProvider.
type EmailProviderManager manager
// Create an email provider.
//
// The credentials object requires different properties depending on the email
// provider (which is specified using the name property):
//
// - `mandrill` requires `api_key`
// - `sendgrid` requires `api_key`
// - `sparkpost` requires `api_key`. Optionally, set `region` to `eu` to use the
// SparkPost service hosted in Western Europe; set to `null` to use the
// SparkPost service hosted in North America. `eu` or `null` are the only valid
// values for `region`.
// - ses requires accessKeyId, secretAccessKey, and region
// - smtp requires smtp_host, smtp_port, smtp_user, and smtp_pass
// - `mailgun` requires `api_key` and `domain`. Optionally, set region to eu to
// use the Mailgun service hosted in Europe; set to null otherwise. "eu" or null
// are the only valid values for region.
//
// Depending on the type of provider it is possible to specify settings object
// with different configuration options, which will be used when sending an
// email:
//
// - `smtp` provider, `settings` may contain `headers` object. When using AWS
// SES SMTP host, you may provide a name of configuration set in an
// `X-SES-Configuration-Set` header. The value must be a string.
//
// See: https://auth0.com/docs/api/management/v2#!/Emails/post_provider
func (m *EmailProviderManager) Create(ctx context.Context, ep *EmailProvider, opts ...RequestOption) error {
return m.management.Request(ctx, http.MethodPost, m.management.URI("emails", "provider"), ep, opts...)
}
// Read email provider details.
//
// See: https://auth0.com/docs/api/management/v2#!/Emails/get_provider
func (m *EmailProviderManager) Read(ctx context.Context, opts ...RequestOption) (ep *EmailProvider, err error) {
opts = append(opts, IncludeFields("name", "enabled", "default_from_address", "credentials", "settings"))
err = m.management.Request(ctx, http.MethodGet, m.management.URI("emails", "provider"), &ep, opts...)
return
}
// Update an email provider.
//
// See: https://auth0.com/docs/api/management/v2#!/Emails/patch_provider
func (m *EmailProviderManager) Update(ctx context.Context, ep *EmailProvider, opts ...RequestOption) (err error) {
return m.management.Request(ctx, http.MethodPatch, m.management.URI("emails", "provider"), ep, opts...)
}
// Delete the email provider.
//
// See: https://auth0.com/docs/api/management/v2#!/Emails/delete_provider
func (m *EmailProviderManager) Delete(ctx context.Context, opts ...RequestOption) (err error) {
return m.management.Request(ctx, http.MethodDelete, m.management.URI("emails", "provider"), nil, opts...)
}