🏷️ Refactored Config domain

This commit is contained in:
Maxim Lebedev 2022-01-08 15:52:24 +05:00
parent 832a0be91a
commit 3965895e01
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
1 changed files with 65 additions and 40 deletions

View File

@ -11,39 +11,56 @@ import (
type ( type (
Config struct { Config struct {
Database ConfigDatabase Code ConfigCode `yaml:"code"`
IndieAuth ConfigIndieAuth Database ConfigDatabase `yaml:"database"`
Server ConfigServer IndieAuth ConfigIndieAuth `yaml:"indieAuth"`
Name string JWT ConfigJWT `yaml:"jwt"`
RunMode string Server ConfigServer `yaml:"server"`
} TicketAuth ConfigTicketAuth `yaml:"ticketAuth"`
Name string `yaml:"name"`
ConfigIndieAuth struct { RunMode string `yaml:"runMode"`
JWTSecret interface{}
AccessTokenExpirationTime time.Duration
JWTSigningAlgorithm string
JWTSigningPrivateKeyFile string
CodeLength int
JWTNonceLength int
Enabled bool
} }
ConfigServer struct { ConfigServer struct {
CertificateFile string CertificateFile string `yaml:"certFile"`
Domain string Domain string `yaml:"domain"`
Host string Host string `yaml:"host"`
KeyFile string KeyFile string `yaml:"keyFile"`
Protocol string Port string `yaml:"port"`
RootURL string Protocol string `yaml:"protocol"`
StaticRootPath string RootURL string `yaml:"rootUrl"`
StaticURLPrefix string StaticRootPath string `yaml:"staticRootPath"`
Port string StaticURLPrefix string `yaml:"staticUrlPrefix"`
EnablePprof bool EnablePprof bool `yaml:"enablePprof"`
} }
ConfigDatabase struct { ConfigDatabase struct {
Path string Path string `yaml:"path"`
Type string Type string `yaml:"type"` // memory
}
// Configuration of a one-time code after giving permission to an
// application. The client needs to request the server with this code to
// exchange it for a token or user information.
ConfigCode struct {
Expiry time.Duration `yaml:"expiry"` // 10m
Length int `yaml:"length"` // 32
}
ConfigJWT struct {
Expiry time.Duration `yaml:"expiry"` // 1h
Secret interface{} `yaml:"secret"`
Algorithm string `yaml:"algorithm"` // HS256
NonceLength int `yaml:"nonceLength"` // 22
}
ConfigIndieAuth struct {
Enabled bool `yaml:"enabled"` // true
}
ConfigTicketAuth struct {
Expiry time.Duration `yaml:"expiry"` // 1m
Length int `yaml:"length"` // 24
} }
) )
@ -71,19 +88,6 @@ func TestConfig(tb testing.TB) *Config {
return &Config{ return &Config{
Name: "IndieAuth", Name: "IndieAuth",
RunMode: "dev", RunMode: "dev",
Database: ConfigDatabase{
Path: filepath.Join("test", "development.db"),
Type: "bolt",
},
IndieAuth: ConfigIndieAuth{
AccessTokenExpirationTime: time.Hour,
CodeLength: 32, //nolint: gomnd
Enabled: true,
JWTNonceLength: 22, //nolint: gomnd
JWTSecret: []byte("hackme"),
JWTSigningAlgorithm: "HS256",
JWTSigningPrivateKeyFile: filepath.Join("jwt", "private.pem"),
},
Server: ConfigServer{ Server: ConfigServer{
CertificateFile: filepath.Join("https", "cert.pem"), CertificateFile: filepath.Join("https", "cert.pem"),
Domain: "localhost", Domain: "localhost",
@ -96,5 +100,26 @@ func TestConfig(tb testing.TB) *Config {
StaticRootPath: "/", StaticRootPath: "/",
StaticURLPrefix: "/static", StaticURLPrefix: "/static",
}, },
Database: ConfigDatabase{
Type: "memory",
Path: "",
},
Code: ConfigCode{
Expiry: 10 * time.Minute,
Length: 32,
},
JWT: ConfigJWT{
Expiry: time.Hour,
NonceLength: 22,
Secret: []byte("hackme"),
Algorithm: "HS256",
},
IndieAuth: ConfigIndieAuth{
Enabled: true,
},
TicketAuth: ConfigTicketAuth{
Expiry: time.Minute,
Length: 24,
},
} }
} }