github.com/greenpau/go-authcrunch@v1.0.50/pkg/authn/ui/static.go (about)

     1  // Copyright 2022 Paul Greenberg greenpau@outlook.com
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package ui
    16  
    17  import (
    18  	"crypto/sha1"
    19  	"encoding/base64"
    20  	"fmt"
    21  	"io"
    22  	"os"
    23  )
    24  
    25  // StaticAssets is an instance of StaticAssetLibrary.
    26  var StaticAssets *StaticAssetLibrary
    27  
    28  // StaticAsset is a single static web asset.
    29  type StaticAsset struct {
    30  	Path           string `json:"path,omitempty" xml:"path,omitempty" yaml:"path,omitempty"`
    31  	FsPath         string `json:"fs_path,omitempty" xml:"fs_path,omitempty" yaml:"fs_path,omitempty"`
    32  	Restricted     bool   `json:"restricted,omitempty" xml:"restricted,omitempty" yaml:"restricted,omitempty"`
    33  	ContentType    string `json:"content_type,omitempty" xml:"content_type,omitempty" yaml:"content_type,omitempty"`
    34  	Content        string `json:"content,omitempty" xml:"content,omitempty" yaml:"content,omitempty"`
    35  	EncodedContent string `json:"encoded_content,omitempty" xml:"encoded_content,omitempty" yaml:"encoded_content,omitempty"`
    36  	Checksum       string `json:"checksum,omitempty" xml:"checksum,omitempty" yaml:"checksum,omitempty"`
    37  }
    38  
    39  // StaticAssetLibrary contains a collection of static assets.
    40  type StaticAssetLibrary struct {
    41  	items map[string]*StaticAsset
    42  }
    43  
    44  func init() {
    45  	var err error
    46  	StaticAssets, err = NewStaticAssetLibrary()
    47  	if err != nil {
    48  		panic(err)
    49  	}
    50  }
    51  
    52  // NewStaticAssetLibrary returns an instance of StaticAssetLibrary.
    53  func NewStaticAssetLibrary() (*StaticAssetLibrary, error) {
    54  	sal := &StaticAssetLibrary{}
    55  	sal.items = make(map[string]*StaticAsset)
    56  	for path, item := range defaultStaticAssets {
    57  		s, err := base64.StdEncoding.DecodeString(item.EncodedContent)
    58  		if err != nil {
    59  			return nil, fmt.Errorf("static asset %s decoding error: %s", path, err)
    60  		}
    61  		item.Content = string(s)
    62  		h := sha1.New()
    63  		io.WriteString(h, item.Content)
    64  		item.Checksum = base64.URLEncoding.EncodeToString(h.Sum(nil))
    65  		sal.items[path] = item
    66  	}
    67  	return sal, nil
    68  }
    69  
    70  // GetAsset returns an asset from path
    71  func (sal *StaticAssetLibrary) GetAsset(path string) (*StaticAsset, error) {
    72  	if item, exists := sal.items[path]; exists {
    73  		return item, nil
    74  	}
    75  	return nil, fmt.Errorf("static asset %s not found", path)
    76  }
    77  
    78  // AddAsset adds asset to StaticAssetLibrary
    79  func (sal *StaticAssetLibrary) AddAsset(path, contentType, fsPath string) error {
    80  	rawContent, err := os.ReadFile(fsPath)
    81  	if err != nil {
    82  		return fmt.Errorf("failed to load asset file %s: %s", fsPath, err)
    83  	}
    84  	item := &StaticAsset{
    85  		Path:           path,
    86  		ContentType:    contentType,
    87  		EncodedContent: base64.StdEncoding.EncodeToString(rawContent),
    88  	}
    89  	s, err := base64.StdEncoding.DecodeString(item.EncodedContent)
    90  	if err != nil {
    91  		return fmt.Errorf("static asset %s decoding error: %s", path, err)
    92  	}
    93  	item.Content = string(s)
    94  	h := sha1.New()
    95  	io.WriteString(h, item.Content)
    96  	item.Checksum = base64.URLEncoding.EncodeToString(h.Sum(nil))
    97  	sal.items[path] = item
    98  	return nil
    99  }