auth/internal/domain/config.go

141 lines
3.6 KiB
Go
Raw Normal View History

2021-11-14 21:10:51 +00:00
package domain
import (
2021-12-28 00:23:29 +00:00
"net"
"path/filepath"
2021-11-14 21:10:51 +00:00
"testing"
2021-12-28 00:23:29 +00:00
"time"
2021-11-14 21:10:51 +00:00
2021-12-28 00:23:29 +00:00
"github.com/valyala/fasttemplate"
2021-11-14 21:10:51 +00:00
)
2021-12-28 00:23:29 +00:00
type (
Config struct {
2022-01-08 10:52:24 +00:00
Code ConfigCode `yaml:"code"`
Database ConfigDatabase `yaml:"database"`
IndieAuth ConfigIndieAuth `yaml:"indieAuth"`
JWT ConfigJWT `yaml:"jwt"`
Server ConfigServer `yaml:"server"`
TicketAuth ConfigTicketAuth `yaml:"ticketAuth"`
Name string `yaml:"name"`
RunMode string `yaml:"runMode"`
2021-12-28 00:23:29 +00:00
}
ConfigServer struct {
2022-01-08 10:52:24 +00:00
CertificateFile string `yaml:"certFile"`
Domain string `yaml:"domain"`
Host string `yaml:"host"`
KeyFile string `yaml:"keyFile"`
Port string `yaml:"port"`
Protocol string `yaml:"protocol"`
RootURL string `yaml:"rootUrl"`
StaticRootPath string `yaml:"staticRootPath"`
StaticURLPrefix string `yaml:"staticUrlPrefix"`
EnablePprof bool `yaml:"enablePprof"`
2021-12-28 00:23:29 +00:00
}
ConfigDatabase struct {
2022-01-08 10:52:24 +00:00
Path string `yaml:"path"`
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
2022-01-12 18:04:40 +00:00
Secret string `yaml:"secret"`
2022-01-08 10:52:24 +00:00
Algorithm string `yaml:"algorithm"` // HS256
NonceLength int `yaml:"nonceLength"` // 22
}
ConfigIndieAuth struct {
Enabled bool `yaml:"enabled"` // true
Username string `yaml:"username"`
Password string `yaml:"password"`
2022-01-08 10:52:24 +00:00
}
ConfigTicketAuth struct {
Expiry time.Duration `yaml:"expiry"` // 1m
Length int `yaml:"length"` // 24
2021-12-28 00:23:29 +00:00
}
2022-01-29 17:50:45 +00:00
ConfigRelMeAuth struct {
Enabled bool `yaml:"enabled"` // true
Providers []ConfigRelMeAuthProvider `yaml:"providers"`
}
ConfigRelMeAuthProvider struct {
Type string `yaml:"type"`
ID string `yaml:"id"`
Secret string `yaml:"secret"`
}
2021-12-28 00:23:29 +00:00
)
2022-01-29 17:50:45 +00:00
// TestConfig returns a valid config for tests.
2021-12-28 00:23:29 +00:00
func TestConfig(tb testing.TB) *Config {
2021-11-14 21:10:51 +00:00
tb.Helper()
2021-12-28 00:23:29 +00:00
return &Config{
Name: "IndieAuth",
RunMode: "dev",
Server: ConfigServer{
CertificateFile: filepath.Join("https", "cert.pem"),
Domain: "localhost",
EnablePprof: false,
Host: "0.0.0.0",
KeyFile: filepath.Join("https", "key.pem"),
Port: "3000",
Protocol: "http",
RootURL: "{{protocol}}://{{domain}}:{{port}}/",
StaticRootPath: "/",
StaticURLPrefix: "/static",
},
2022-01-08 10:52:24 +00:00
Database: ConfigDatabase{
Type: "memory",
Path: "",
},
Code: ConfigCode{
Expiry: 10 * time.Minute,
Length: 32,
},
JWT: ConfigJWT{
Expiry: time.Hour,
NonceLength: 22,
2022-01-12 18:04:40 +00:00
Secret: "hackme",
2022-01-08 10:52:24 +00:00
Algorithm: "HS256",
},
IndieAuth: ConfigIndieAuth{
Enabled: true,
Username: "user",
Password: "password",
2022-01-08 10:52:24 +00:00
},
TicketAuth: ConfigTicketAuth{
Expiry: time.Minute,
Length: 24,
},
2021-12-28 00:23:29 +00:00
}
2021-11-14 21:10:51 +00:00
}
2022-01-12 18:04:40 +00:00
// GetAddress return host:port address.
func (cs ConfigServer) GetAddress() string {
return net.JoinHostPort(cs.Host, cs.Port)
}
2022-01-29 17:50:45 +00:00
// GetRootURL returns generated root URL from template RootURL.
2022-01-12 18:04:40 +00:00
func (cs ConfigServer) GetRootURL() string {
return fasttemplate.ExecuteString(cs.RootURL, `{{`, `}}`, map[string]interface{}{
"domain": cs.Domain,
"host": cs.Host,
"port": cs.Port,
"protocol": cs.Protocol,
"staticRootPath": cs.StaticRootPath,
"staticUrlPrefix": cs.StaticURLPrefix,
})
}