Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move loading of custom HTML headers and static assets #49

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions assets/cla/consent.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@
email: [email protected]
- name: Michael Ellis
email: [email protected]
- name: Peter Oettig
email: [email protected]

22 changes: 22 additions & 0 deletions pkg/authn/portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package authn

import (
"context"
"os"
"sort"

"github.com/greenpau/go-authcrunch/pkg/acl"
Expand Down Expand Up @@ -488,6 +489,27 @@ func (p *Portal) configureUserInterface() error {
}
}

if p.config.UI.CustomHTMLHeaderPath != "" {
b, err := os.ReadFile(p.config.UI.CustomHTMLHeaderPath)
if err != nil {
return errors.ErrCustomHTMLHeaderNotReadable.WithArgs(p.config.UI.CustomHTMLHeaderPath, p.config.Name, err)
}
for k, v := range ui.PageTemplates {
headIndex := strings.Index(v, "<meta name=\"description\"")
if headIndex < 1 {
continue
}
v = v[:headIndex] + string(b) + v[headIndex:]
ui.PageTemplates[k] = v
}
}

for _, staticAsset := range p.config.UI.StaticAssets {
if err := ui.StaticAssets.AddAsset(staticAsset.Path, staticAsset.ContentType, staticAsset.FsPath); err != nil {
return errors.ErrStaticAssetAddFailed.WithArgs(staticAsset.Path, staticAsset.ContentType, staticAsset.FsPath, p.config.Name, err)
}
}

if p.config.UI.LogoURL != "" {
p.ui.LogoURL = p.config.UI.LogoURL
p.ui.LogoDescription = p.config.UI.LogoDescription
Expand Down
2 changes: 2 additions & 0 deletions pkg/authn/ui/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type Parameters struct {
PasswordRecoveryEnabled bool `json:"password_recovery_enabled,omitempty" xml:"password_recovery_enabled,omitempty" yaml:"password_recovery_enabled,omitempty"`
CustomCSSPath string `json:"custom_css_path,omitempty" xml:"custom_css_path,omitempty" yaml:"custom_css_path,omitempty"`
CustomJsPath string `json:"custom_js_path,omitempty" xml:"custom_js_path,omitempty" yaml:"custom_js_path,omitempty"`
CustomHTMLHeaderPath string `json:"custom_html_header_path,omitempty" xml:"custom_html_header_path,omitempty" yaml:"custom_html_header_path,omitempty"`
StaticAssets []StaticAsset `json:"static_assets,omitempty" xml:"static_assets,omitempty" yaml:"static_assets,omitempty"`
Language string `json:"language,omitempty" xml:"language,omitempty" yaml:"language,omitempty"`
DisabledPages map[string]bool `json:"disabled_pages,omitempty" xml:"disabled_pages,omitempty" yaml:"disabled_pages,omitempty"`
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/authn/ui/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"os"
)

// StaticAssets is an instance of StaticAssetLibrary.
Expand All @@ -28,6 +28,7 @@ var StaticAssets *StaticAssetLibrary
// StaticAsset is a single static web asset.
type StaticAsset struct {
Path string `json:"path,omitempty" xml:"path,omitempty" yaml:"path,omitempty"`
FsPath string `json:"fs_path,omitempty" xml:"fs_path,omitempty" yaml:"fs_path,omitempty"`
Restricted bool `json:"restricted,omitempty" xml:"restricted,omitempty" yaml:"restricted,omitempty"`
ContentType string `json:"content_type,omitempty" xml:"content_type,omitempty" yaml:"content_type,omitempty"`
Content string `json:"content,omitempty" xml:"content,omitempty" yaml:"content,omitempty"`
Expand Down Expand Up @@ -76,7 +77,7 @@ func (sal *StaticAssetLibrary) GetAsset(path string) (*StaticAsset, error) {

// AddAsset adds asset to StaticAssetLibrary
func (sal *StaticAssetLibrary) AddAsset(path, contentType, fsPath string) error {
rawContent, err := ioutil.ReadFile(fsPath)
rawContent, err := os.ReadFile(fsPath)
if err != nil {
return fmt.Errorf("failed to load asset file %s: %s", fsPath, err)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/errors/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package errors
// Portal errors.
const (
ErrStaticAssetAddFailed StandardError = "failed adding custom static asset %s (%s) from %s for %s portal: %v"
ErrCustomHTMLHeaderNotReadable StandardError = "failed to read custom HTML header file %s for %s portal: %v"
ErrUserInterfaceThemeNotFound StandardError = "user interface validation for %s portal failed: %s theme not found"
ErrUserInterfaceBuiltinTemplateAddFailed StandardError = "user interface validation for %s portal failed for built-in template %s in %s theme: %v"
ErrUserInterfaceCustomTemplateAddFailed StandardError = "user interface validation for %s portal failed for custom template %s in %s: %v"
Expand Down