Add auth
This commit is contained in:
@@ -2,6 +2,7 @@ package glance
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"iter"
|
||||
@@ -30,10 +31,16 @@ type config struct {
|
||||
Server struct {
|
||||
Host string `yaml:"host"`
|
||||
Port uint16 `yaml:"port"`
|
||||
Proxied bool `yaml:"proxied"`
|
||||
AssetsPath string `yaml:"assets-path"`
|
||||
BaseURL string `yaml:"base-url"`
|
||||
} `yaml:"server"`
|
||||
|
||||
Auth struct {
|
||||
SecretKey string `yaml:"secret-key"`
|
||||
Users map[string]*user `yaml:"users"`
|
||||
} `yaml:"auth"`
|
||||
|
||||
Document struct {
|
||||
Head template.HTML `yaml:"head"`
|
||||
} `yaml:"document"`
|
||||
@@ -59,6 +66,12 @@ type config struct {
|
||||
Pages []page `yaml:"pages"`
|
||||
}
|
||||
|
||||
type user struct {
|
||||
Password string `yaml:"password"`
|
||||
PasswordHashString string `yaml:"password-hash"`
|
||||
PasswordHash []byte `yaml:"-"`
|
||||
}
|
||||
|
||||
type page struct {
|
||||
Title string `yaml:"name"`
|
||||
Slug string `yaml:"slug"`
|
||||
@@ -422,11 +435,39 @@ func configFilesWatcher(
|
||||
}, nil
|
||||
}
|
||||
|
||||
// TODO: Refactor, we currently validate in two different places, this being
|
||||
// one of them, which doesn't modify the data and only checks for logical errors
|
||||
// and then again when creating the application which does modify the data and do
|
||||
// further validation. Would be better if validation was done in a single place.
|
||||
func isConfigStateValid(config *config) error {
|
||||
if len(config.Pages) == 0 {
|
||||
return fmt.Errorf("no pages configured")
|
||||
}
|
||||
|
||||
if len(config.Auth.Users) > 0 && config.Auth.SecretKey == "" {
|
||||
return fmt.Errorf("secret-key must be set when users are configured")
|
||||
}
|
||||
|
||||
for username := range config.Auth.Users {
|
||||
if username == "" {
|
||||
return fmt.Errorf("user has no name")
|
||||
}
|
||||
|
||||
if len(username) < 3 {
|
||||
return errors.New("usernames must be at least 3 characters")
|
||||
}
|
||||
|
||||
user := config.Auth.Users[username]
|
||||
|
||||
if user.Password == "" {
|
||||
if user.PasswordHashString == "" {
|
||||
return fmt.Errorf("user %s must have a password or a password-hash set", username)
|
||||
}
|
||||
} else if len(user.Password) < 6 {
|
||||
return fmt.Errorf("the password for %s must be at least 6 characters", username)
|
||||
}
|
||||
}
|
||||
|
||||
if config.Server.AssetsPath != "" {
|
||||
if _, err := os.Stat(config.Server.AssetsPath); os.IsNotExist(err) {
|
||||
return fmt.Errorf("assets directory does not exist: %s", config.Server.AssetsPath)
|
||||
|
||||
Reference in New Issue
Block a user