2022-07-18 00:56:09 +00:00
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-01-07 20:04:06 +00:00
|
|
|
th "html/template"
|
2023-04-08 04:48:55 +00:00
|
|
|
"io/fs"
|
2023-01-07 20:04:06 +00:00
|
|
|
"path"
|
|
|
|
tt "text/template"
|
2022-07-18 00:56:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// New creates a new templates' provider.
|
|
|
|
func New(config Config) (provider *Provider, err error) {
|
|
|
|
provider = &Provider{
|
|
|
|
config: config,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = provider.load(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return provider, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provider of templates.
|
|
|
|
type Provider struct {
|
|
|
|
config Config
|
|
|
|
templates Templates
|
|
|
|
}
|
|
|
|
|
2023-01-03 03:49:02 +00:00
|
|
|
// LoadTemplatedAssets takes an embed.FS and loads each templated asset document into a Template.
|
2023-04-08 04:48:55 +00:00
|
|
|
func (p *Provider) LoadTemplatedAssets(fs fs.ReadFileFS) (err error) {
|
2023-01-03 03:49:02 +00:00
|
|
|
var (
|
|
|
|
data []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
if data, err = fs.ReadFile("public_html/index.html"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-07 20:04:06 +00:00
|
|
|
if p.templates.asset.index, err = tt.
|
2023-01-03 03:49:02 +00:00
|
|
|
New("assets/public_html/index.html").
|
|
|
|
Funcs(FuncMap()).
|
|
|
|
Parse(string(data)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if data, err = fs.ReadFile("public_html/api/index.html"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-07 20:04:06 +00:00
|
|
|
if p.templates.asset.api.index, err = tt.
|
2023-01-03 03:49:02 +00:00
|
|
|
New("assets/public_html/api/index.html").
|
|
|
|
Funcs(FuncMap()).
|
|
|
|
Parse(string(data)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if data, err = fs.ReadFile("public_html/api/openapi.yml"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-07 20:04:06 +00:00
|
|
|
if p.templates.asset.api.spec, err = tt.
|
2023-01-03 03:49:02 +00:00
|
|
|
New("api/public_html/openapi.yaml").
|
|
|
|
Funcs(FuncMap()).
|
|
|
|
Parse(string(data)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAssetIndexTemplate returns a Template used to generate the React index document.
|
|
|
|
func (p *Provider) GetAssetIndexTemplate() (t Template) {
|
|
|
|
return p.templates.asset.index
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAssetOpenAPIIndexTemplate returns a Template used to generate the OpenAPI index document.
|
|
|
|
func (p *Provider) GetAssetOpenAPIIndexTemplate() (t Template) {
|
|
|
|
return p.templates.asset.api.index
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAssetOpenAPISpecTemplate returns a Template used to generate the OpenAPI specification document.
|
|
|
|
func (p *Provider) GetAssetOpenAPISpecTemplate() (t Template) {
|
|
|
|
return p.templates.asset.api.spec
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetEventEmailTemplate returns an EmailTemplate used for generic event notifications.
|
2022-12-27 08:59:08 +00:00
|
|
|
func (p *Provider) GetEventEmailTemplate() (t *EmailTemplate) {
|
|
|
|
return p.templates.notification.event
|
2022-07-18 00:56:09 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 10:59:15 +00:00
|
|
|
// GetPasswordResetEmailTemplate returns the EmailTemplate for Identity Verification notifications.
|
|
|
|
func (p *Provider) GetPasswordResetEmailTemplate() (t *EmailTemplate) {
|
|
|
|
return p.templates.notification.reset
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOneTimePasswordEmailTemplate returns the EmailTemplate for Identity Verification notifications.
|
|
|
|
func (p *Provider) GetOneTimePasswordEmailTemplate() (t *EmailTemplate) {
|
|
|
|
return p.templates.notification.reset
|
2022-07-18 00:56:09 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 20:04:06 +00:00
|
|
|
// GetOpenIDConnectAuthorizeResponseFormPostTemplate returns a Template used to generate the OpenID Connect 1.0 Form Post Authorize Response.
|
|
|
|
func (p *Provider) GetOpenIDConnectAuthorizeResponseFormPostTemplate() (t *th.Template) {
|
|
|
|
return p.templates.oidc.formpost
|
|
|
|
}
|
|
|
|
|
2022-07-18 00:56:09 +00:00
|
|
|
func (p *Provider) load() (err error) {
|
|
|
|
var errs []error
|
|
|
|
|
2023-04-23 10:59:15 +00:00
|
|
|
if p.templates.notification.reset, err = loadEmailTemplate(TemplateNameEmailPasswordReset, p.config.EmailTemplatesPath); err != nil {
|
2022-07-18 00:56:09 +00:00
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
|
2022-12-27 08:59:08 +00:00
|
|
|
if p.templates.notification.event, err = loadEmailTemplate(TemplateNameEmailEvent, p.config.EmailTemplatesPath); err != nil {
|
2022-07-18 00:56:09 +00:00
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
|
2023-04-23 10:59:15 +00:00
|
|
|
if p.templates.notification.otp, err = loadEmailTemplate(TemplateNameOneTimePassword, p.config.EmailTemplatesPath); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
|
2023-01-07 20:04:06 +00:00
|
|
|
var data []byte
|
|
|
|
|
|
|
|
if data, err = embedFS.ReadFile(path.Join("src", TemplateCategoryOpenIDConnect, TemplateNameOIDCAuthorizeFormPost)); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
} else if p.templates.oidc.formpost, err = th.
|
|
|
|
New("oidc/AuthorizeResponseFormPost.html").
|
|
|
|
Funcs(FuncMap()).
|
|
|
|
Parse(string(data)); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
|
2022-12-23 05:06:49 +00:00
|
|
|
if len(errs) != 0 {
|
|
|
|
for i, e := range errs {
|
|
|
|
if i == 0 {
|
|
|
|
err = e
|
|
|
|
continue
|
|
|
|
}
|
2022-07-18 00:56:09 +00:00
|
|
|
|
2022-12-23 05:06:49 +00:00
|
|
|
err = fmt.Errorf("%v, %w", err, e)
|
2022-07-18 00:56:09 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 05:06:49 +00:00
|
|
|
return fmt.Errorf("one or more errors occurred loading templates: %w", err)
|
2022-07-18 00:56:09 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 05:06:49 +00:00
|
|
|
return nil
|
2022-07-18 00:56:09 +00:00
|
|
|
}
|