-
Notifications
You must be signed in to change notification settings - Fork 51
/
resource_fusionauth_themes.go
386 lines (361 loc) · 19.3 KB
/
resource_fusionauth_themes.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package fusionauth
import (
"fmt"
"github.com/FusionAuth/go-client/pkg/fusionauth"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)
func newTheme() *schema.Resource {
return &schema.Resource{
Create: createTheme,
Read: readTheme,
Update: updateTheme,
Delete: deleteTheme,
Schema: map[string]*schema.Schema{
"source_theme_id": {
Type: schema.TypeString,
Optional: true,
Description: "The optional Id of an existing Theme to make a copy of. If present, the defaultMessages, localizedMessages, templates, and stylesheet from the source Theme will be copied to the new Theme.",
ValidateFunc: validation.IsUUID,
},
"default_messages": {
Type: schema.TypeString,
Required: true,
Description: "A properties file formatted String containing at least all of the message keys defined in the FusionAuth shipped messages file. Required if not copying an existing Theme.",
DiffSuppressFunc: templateCompare,
},
"localized_messages": {
Type: schema.TypeMap,
Optional: true,
Description: "A Map of localized versions of the messages. The key is the Locale and the value is a properties file formatted String.",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "A unique name for the Theme.",
},
"stylesheet": {
Type: schema.TypeString,
Optional: true,
Description: "A CSS stylesheet used to style the templates.",
DiffSuppressFunc: templateCompare,
},
"email_complete": {
Type: schema.TypeString,
Optional: true,
Description: "A FreeMarker template that is rendered when the user requests the /email/complete page. This page is used after a user has verified their email address by clicking the URL in the email. After FusionAuth has updated their user object to indicate that their email was verified, the browser is redirected to this page.",
DiffSuppressFunc: templateCompare,
},
"email_send": {
Type: schema.TypeString,
Optional: true,
Description: "A FreeMarker template that is rendered when the user requests the /email/send page. This page is used after a user has asked for the verification email to be resent. This can happen if the URL in the email expired and the user clicked it. In this case, the user can provide their email address again and FusionAuth will resend the email. After the user submits their email and FusionAuth re-sends a verification email to them, the browser is redirected to this page.",
DiffSuppressFunc: templateCompare,
},
"email_verify": {
Type: schema.TypeString,
Optional: true,
Description: "A FreeMarker template that is rendered when the user requests the /email/verify page by clicking the URL from the verification email and the verificationId has expired. FusionAuth expires verificationId after a period of time (which is configurable). If the user has a URL from the verification email that has expired, this page will be rendered and the error will be displayed to the user.",
DiffSuppressFunc: templateCompare,
},
"helpers": {
Type: schema.TypeString,
Optional: true,
Description: "A FreeMarker template that contains all of the macros and templates used by the rest of the loginTheme FreeMarker templates (i.e. oauth2Authorize). This allows you to configure the general layout of your UI configuration and login theme without having to copy and paste HTML into each of the templates.",
DiffSuppressFunc: templateCompare,
},
"oauth2_authorize": {
Type: schema.TypeString,
Optional: true,
Description: "A FreeMarker template that is rendered when the user requests the /oauth2/authorize page. This is the main login page for FusionAuth and is used for all interactive OAuth and OpenId Connect workflows.",
DiffSuppressFunc: templateCompare,
},
"oauth2_child_registration_not_allowed": {
Type: schema.TypeString,
Optional: true,
Description: "A FreeMarker template that is rendered when the user requests the /oauth2/child-registration-not-allowed page. This is where the child must provide their parent’s email address to ask their parent to create an account for them.",
DiffSuppressFunc: templateCompare,
},
"oauth2_child_registration_not_allowed_complete": {
Type: schema.TypeString,
Optional: true,
Description: "A FreeMarker template that is rendered when the user requests the /oauth2/child-registration-not-allowed-complete page. This is where the browser is taken after the child provides their parent’s email address.",
DiffSuppressFunc: templateCompare,
},
"oauth2_complete_registration": {
Type: schema.TypeString,
Optional: true,
Description: "A FreeMarker template that is rendered when the user requests the /oauth2/complete-registration page. This page is used for users that have accounts but might be missing required fields.",
DiffSuppressFunc: templateCompare,
},
"oauth2_error": {
Type: schema.TypeString,
Optional: true,
Description: "A FreeMarker template that is rendered when the user requests the /oauth2/error page. This page is used if the user starts or is in the middle of the OAuth workflow and any type of error occurs. This could be caused by the user messing with the URL or internally some type of information wasn’t passed between the OAuth endpoints correctly. For example, if you are federating login to an external IdP and that IdP does not properly echo the state parameter, FusionAuth’s OAuth workflow will break and this page will be displayed.",
DiffSuppressFunc: templateCompare,
},
"oauth2_logout": {
Type: schema.TypeString,
Optional: true,
Description: "A FreeMarker template that is rendered when the user requests the /oauth2/logout page. This page is used if the user initiates a logout. This page causes the user to be logged out of all associated applications via a front-channel mechanism before being redirected.",
DiffSuppressFunc: templateCompare,
},
"oauth2_register": {
Type: schema.TypeString,
Optional: true,
Description: "A FreeMarker template that is rendered when the user requests the /oauth2/register page. This page is used for users that need to register (sign-up)",
DiffSuppressFunc: templateCompare,
},
"oauth2_device": {
Type: schema.TypeString,
Optional: true,
Description: "A FreeMarker template",
DiffSuppressFunc: templateCompare,
},
"oauth2_device_complete": {
Type: schema.TypeString,
Optional: true,
Description: "A FreeMarker template",
DiffSuppressFunc: templateCompare,
},
"oauth2_passwordless": {
Type: schema.TypeString,
Optional: true,
Description: "A FreeMarker template",
DiffSuppressFunc: templateCompare,
},
"oauth2_wait": {
Type: schema.TypeString,
Optional: true,
Description: "A FreeMarker template",
DiffSuppressFunc: templateCompare,
},
"oauth2_two_factor": {
Type: schema.TypeString,
Optional: true,
Description: "A FreeMarker template that is rendered when the user requests the /oauth2/two-factor page. This page is used if the user has two-factor authentication enabled and they need to type in their code again. FusionAuth will properly handle the SMS or authenticator app processing on the back end. This page contains the form that the user will put their code into.",
DiffSuppressFunc: templateCompare,
},
"password_change": {
Type: schema.TypeString,
Optional: true,
Description: "A FreeMarker template that is rendered when the user requests the /password/change page. This page is used if the user is required to change their password or if they have requested a password reset. This page contains the form that allows the user to provide a new password.",
DiffSuppressFunc: templateCompare,
},
"password_complete": {
Type: schema.TypeString,
Optional: true,
Description: "A FreeMarker template that is rendered when the user requests the /password/complete page. This page is used after the user has successfully updated their password (or reset it). This page should instruct the user that their password was updated and that they need to login again.",
DiffSuppressFunc: templateCompare,
},
"password_forgot": {
Type: schema.TypeString,
Optional: true,
Description: "A FreeMarker template that is rendered when the user requests the /password/forgot page. This page is used when a user starts the forgot password workflow. This page renders the form where the user types in their email address.",
DiffSuppressFunc: templateCompare,
},
"password_sent": {
Type: schema.TypeString,
Optional: true,
Description: "A FreeMarker template that is rendered when the user requests the /password/sent page. This page is used when a user has submitted the forgot password form with their email. FusionAuth does not indicate back to the user if their email address was valid in order to prevent malicious activity that could reveal valid email addresses. Therefore, this page should indicate to the user that if their email was valid, they will receive an email shortly with a link to reset their password.",
DiffSuppressFunc: templateCompare,
},
"registration_complete": {
Type: schema.TypeString,
Optional: true,
Description: "A FreeMarker template that is rendered when the user requests the /registration/complete page. This page is used after a user has verified their email address for a specific application (i.e. a user registration) by clicking the URL in the email. After FusionAuth has updated their registration object to indicate that their email was verified, the browser is redirected to this page.",
DiffSuppressFunc: templateCompare,
},
"registration_send": {
Type: schema.TypeString,
Optional: true,
Description: "A FreeMarker template that is rendered when the user requests the /registration/send page. This page is used after a user has asked for the application specific verification email to be resent. This can happen if the URL in the email expired and the user clicked it. In this case, the user can provide their email address again and FusionAuth will resend the email. After the user submits their email and FusionAuth re-sends a verification email to them, the browser is redirected to this page.",
DiffSuppressFunc: templateCompare,
},
"registration_verify": {
Type: schema.TypeString,
Optional: true,
Description: "A FreeMarker template that is rendered when the user requests the /registration/verify page by clicking the URL from the application specific verification email and the verificationId has expired. FusionAuth expires verificationId after a period of time (which is configurable). If the user has a URL from the verification email that has expired, this page will be rendered and the error will be displayed to the user.",
DiffSuppressFunc: templateCompare,
},
},
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
}
}
func buildTheme(data *schema.ResourceData) fusionauth.Theme {
t := fusionauth.Theme{
DefaultMessages: data.Get("default_messages").(string),
// LocalizedMessages: data.Get("localized_messages").(map[string]string),
Name: data.Get("name").(string),
Stylesheet: data.Get("stylesheet").(string),
Templates: fusionauth.Templates{
EmailComplete: data.Get("email_complete").(string),
EmailSend: data.Get("email_send").(string),
EmailVerify: data.Get("email_verify").(string),
Helpers: data.Get("helpers").(string),
Oauth2Authorize: data.Get("oauth2_authorize").(string),
Oauth2ChildRegistrationNotAllowed: data.Get("oauth2_child_registration_not_allowed").(string),
Oauth2ChildRegistrationNotAllowedComplete: data.Get("oauth2_child_registration_not_allowed_complete").(string),
Oauth2CompleteRegistration: data.Get("oauth2_complete_registration").(string),
Oauth2Error: data.Get("oauth2_error").(string),
Oauth2Logout: data.Get("oauth2_logout").(string),
Oauth2TwoFactor: data.Get("oauth2_two_factor").(string),
Oauth2Register: data.Get("oauth2_register").(string),
Oauth2Device: data.Get("oauth2_device").(string),
Oauth2DeviceComplete: data.Get("oauth2_device_complete").(string),
Oauth2Passwordless: data.Get("oauth2_passwordless").(string),
Oauth2Wait: data.Get("oauth2_wait").(string),
PasswordChange: data.Get("password_change").(string),
PasswordComplete: data.Get("password_complete").(string),
PasswordForgot: data.Get("password_forgot").(string),
PasswordSent: data.Get("password_sent").(string),
RegistrationComplete: data.Get("registration_complete").(string),
RegistrationSend: data.Get("registration_send").(string),
RegistrationVerify: data.Get("registration_verify").(string),
},
}
m := data.Get("localized_messages").(map[string]interface{})
t.LocalizedMessages = make(map[string]string)
for k, v := range m {
t.LocalizedMessages[k] = v.(string)
}
return t
}
func createTheme(data *schema.ResourceData, i interface{}) error {
client := i.(Client)
t := buildTheme(data)
resp, faErrs, err := client.FAClient.CreateTheme("", fusionauth.ThemeRequest{
Theme: t,
})
if err != nil {
return fmt.Errorf("CreateTheme err: %v", err)
}
if err := checkResponse(resp.StatusCode, faErrs); err != nil {
return err
}
data.SetId(resp.Theme.Id)
return nil
}
func readTheme(data *schema.ResourceData, i interface{}) error {
client := i.(Client)
id := data.Id()
resp, faErrs, err := client.FAClient.RetrieveTheme(id)
if err != nil {
return err
}
if err := checkResponse(resp.StatusCode, faErrs); err != nil {
return err
}
t := resp.Theme
if err := data.Set("default_messages", t.DefaultMessages); err != nil {
return fmt.Errorf("theme.default_messages: %s", err.Error())
}
if err := data.Set("localized_messages", t.LocalizedMessages); err != nil {
return fmt.Errorf("theme.localized_messages: %s", err.Error())
}
if err := data.Set("name", t.Name); err != nil {
return fmt.Errorf("theme.name: %s", err.Error())
}
if err := data.Set("stylesheet", t.Stylesheet); err != nil {
return fmt.Errorf("theme.stylesheet: %s", err.Error())
}
if err := data.Set("email_complete", t.Templates.EmailComplete); err != nil {
return fmt.Errorf("theme.email_complete: %s", err.Error())
}
if err := data.Set("email_send", t.Templates.EmailSend); err != nil {
return fmt.Errorf("theme.email_send: %s", err.Error())
}
if err := data.Set("email_verify", t.Templates.EmailVerify); err != nil {
return fmt.Errorf("theme.email_verify: %s", err.Error())
}
if err := data.Set("helpers", t.Templates.Helpers); err != nil {
return fmt.Errorf("theme.helpers: %s", err.Error())
}
if err := data.Set("oauth2_authorize", t.Templates.Oauth2Authorize); err != nil {
return fmt.Errorf("theme.oauth2_authorize: %s", err.Error())
}
if err := data.Set("oauth2_child_registration_not_allowed", t.Templates.Oauth2ChildRegistrationNotAllowed); err != nil {
return fmt.Errorf("theme.oauth2_child_registration_not_allowed: %s", err.Error())
}
if err := data.Set("oauth2_child_registration_not_allowed_complete", t.Templates.Oauth2ChildRegistrationNotAllowedComplete); err != nil {
return fmt.Errorf("theme.oauth2_child_registration_not_allowed_complete: %s", err.Error())
}
if err := data.Set("oauth2_complete_registration", t.Templates.Oauth2CompleteRegistration); err != nil {
return fmt.Errorf("theme.oauth2_complete_registration: %s", err.Error())
}
if err := data.Set("oauth2_error", t.Templates.Oauth2Error); err != nil {
return fmt.Errorf("theme.oauth2_error: %s", err.Error())
}
if err := data.Set("oauth2_logout", t.Templates.Oauth2Logout); err != nil {
return fmt.Errorf("theme.oauth2_logout: %s", err.Error())
}
if err := data.Set("oauth2_two_factor", t.Templates.Oauth2TwoFactor); err != nil {
return fmt.Errorf("theme.oauth2_two_factor: %s", err.Error())
}
if err := data.Set("oauth2_register", t.Templates.Oauth2Register); err != nil {
return fmt.Errorf("theme.oauth2_register: %s", err.Error())
}
if err := data.Set("oauth2_device", t.Templates.Oauth2Device); err != nil {
return fmt.Errorf("theme.oauth2_device: %s", err.Error())
}
if err := data.Set("oauth2_device_complete", t.Templates.Oauth2DeviceComplete); err != nil {
return fmt.Errorf("theme.oauth2_device_complete: %s", err.Error())
}
if err := data.Set("oauth2_passwordless", t.Templates.Oauth2Passwordless); err != nil {
return fmt.Errorf("theme.oauth2_passwordless: %s", err.Error())
}
if err := data.Set("oauth2_wait", t.Templates.Oauth2Wait); err != nil {
return fmt.Errorf("theme.oauth2_wait: %s", err.Error())
}
if err := data.Set("password_change", t.Templates.PasswordChange); err != nil {
return fmt.Errorf("theme.password_change: %s", err.Error())
}
if err := data.Set("password_complete", t.Templates.PasswordComplete); err != nil {
return fmt.Errorf("theme.password_complete: %s", err.Error())
}
if err := data.Set("password_forgot", t.Templates.PasswordForgot); err != nil {
return fmt.Errorf("theme.password_forgot: %s", err.Error())
}
if err := data.Set("password_sent", t.Templates.PasswordSent); err != nil {
return fmt.Errorf("theme.password_sent: %s", err.Error())
}
if err := data.Set("registration_complete", t.Templates.RegistrationComplete); err != nil {
return fmt.Errorf("theme.registration_complete: %s", err.Error())
}
if err := data.Set("registration_send", t.Templates.RegistrationSend); err != nil {
return fmt.Errorf("theme.registration_send: %s", err.Error())
}
if err := data.Set("registration_verify", t.Templates.RegistrationVerify); err != nil {
return fmt.Errorf("theme.registration_verify: %s", err.Error())
}
return nil
}
func updateTheme(data *schema.ResourceData, i interface{}) error {
client := i.(Client)
t := buildTheme(data)
resp, faErrs, err := client.FAClient.UpdateTheme(data.Id(), fusionauth.ThemeRequest{
Theme: t,
})
if err != nil {
return fmt.Errorf("UpdateTheme err: %v", err)
}
if err := checkResponse(resp.StatusCode, faErrs); err != nil {
return err
}
data.SetId(resp.Theme.Id)
return nil
}
func deleteTheme(data *schema.ResourceData, i interface{}) error {
client := i.(Client)
id := data.Id()
resp, faErrs, err := client.FAClient.DeleteTheme(id)
if err != nil {
return err
}
if err := checkResponse(resp.StatusCode, faErrs); err != nil {
return err
}
return nil
}