Skip to content

Commit

Permalink
Move loading of custom HTML headers and static assets (#49)
Browse files Browse the repository at this point in the history
* Move loading of custom HTML headers and static assets from caddy-security to here

* Sign CLA
  • Loading branch information
poettig committed Dec 3, 2023
1 parent d11513d commit 2ff7fcd
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
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: ng-cla@openenterprise.co.uk
- name: Michael Ellis
email: Michael94Ellis@gmail.com
- name: Peter Oettig
email: oss-cla@oettig.de

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

0 comments on commit 2ff7fcd

Please sign in to comment.