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 {
|
2023-03-16 15:30:00 +00:00
|
|
|
Server ConfigServer `envPrefix:"SERVER_"`
|
2023-01-16 09:24:31 +00:00
|
|
|
Database ConfigDatabase `envPrefix:"DATABASE_"`
|
2023-07-06 23:11:53 +00:00
|
|
|
Name string `env:"NAME" envDefault:"IndieAuth"`
|
|
|
|
RunMode string `env:"RUN_MODE" envDefault:"dev"`
|
2023-01-16 09:24:31 +00:00
|
|
|
IndieAuth ConfigIndieAuth `envPrefix:"INDIEAUTH_"`
|
|
|
|
JWT ConfigJWT `envPrefix:"JWT_"`
|
2023-03-16 15:30:00 +00:00
|
|
|
Code ConfigCode `envPrefix:"CODE_"`
|
2023-01-16 09:24:31 +00:00
|
|
|
TicketAuth ConfigTicketAuth `envPrefix:"TICKETAUTH_"`
|
2021-12-28 00:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ConfigServer struct {
|
2023-01-16 09:24:31 +00:00
|
|
|
CertificateFile string `env:"CERT_FILE"`
|
2023-07-06 23:11:53 +00:00
|
|
|
Domain string `env:"DOMAIN" envDefault:"localhost"`
|
|
|
|
Host string `env:"HOST" envDefault:"0.0.0.0"`
|
2023-01-16 09:24:31 +00:00
|
|
|
KeyFile string `env:"KEY_FILE"`
|
2023-07-06 23:11:53 +00:00
|
|
|
Port string `env:"PORT" envDefault:"3000"`
|
|
|
|
Protocol string `env:"PROTOCOL" envDefault:"http"`
|
|
|
|
RootURL string `env:"ROOT_URL" envDefault:"{{protocol}}://{{domain}}:{{port}}/"`
|
2021-12-28 00:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ConfigDatabase struct {
|
2023-01-16 09:24:31 +00:00
|
|
|
Path string `env:"PATH"`
|
|
|
|
Type string `env:"TYPE" envDefault:"memory"` // memory
|
2022-01-08 10:52:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2023-01-16 09:24:31 +00:00
|
|
|
Expiry time.Duration `env:"EXPIRY" envDefault:"10m"` // 10m
|
|
|
|
Length uint8 `env:"LENGTH" envDefault:"32"` // 32
|
2022-01-08 10:52:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ConfigJWT struct {
|
2023-07-06 23:11:53 +00:00
|
|
|
Algorithm string `env:"ALGORITHM" envDefault:"HS256"`
|
2023-01-16 09:24:31 +00:00
|
|
|
Secret string `env:"SECRET"`
|
2023-07-06 23:11:53 +00:00
|
|
|
Expiry time.Duration `env:"EXPIRY" envDefault:"1h"`
|
2023-03-16 15:30:00 +00:00
|
|
|
NonceLength uint8 `env:"NONCE_LENGTH" envDefault:"22"`
|
2022-01-08 10:52:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ConfigIndieAuth struct {
|
2023-01-16 09:24:31 +00:00
|
|
|
Password string `env:"PASSWORD"`
|
|
|
|
Username string `env:"USERNAME"`
|
2023-07-06 23:11:53 +00:00
|
|
|
Enabled bool `env:"ENABLED" envDefault:"true"` // true
|
2022-01-08 10:52:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ConfigTicketAuth struct {
|
2023-01-16 09:24:31 +00:00
|
|
|
Expiry time.Duration `env:"EXPIRY" envDefault:"1m"` // 1m
|
|
|
|
Length uint8 `env:"LENGTH" envDefault:"24"` // 24
|
2021-12-28 00:23:29 +00:00
|
|
|
}
|
2022-01-29 17:50:45 +00:00
|
|
|
|
|
|
|
ConfigRelMeAuth struct {
|
2023-01-16 09:24:31 +00:00
|
|
|
Providers []ConfigRelMeAuthProvider `envPrefix:"PROVIDERS_"`
|
2023-07-06 23:11:53 +00:00
|
|
|
Enabled bool `env:"ENABLED" envDefault:"true"` // true
|
2022-01-29 17:50:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ConfigRelMeAuthProvider struct {
|
2023-01-16 09:24:31 +00:00
|
|
|
ID string `env:"ID"`
|
|
|
|
Secret string `env:"SECRET"`
|
|
|
|
Type string `env:"TYPE"`
|
2022-01-29 17:50:45 +00:00
|
|
|
}
|
2021-12-28 00:23:29 +00:00
|
|
|
)
|
|
|
|
|
2022-01-29 17:50:45 +00:00
|
|
|
// TestConfig returns a valid config for tests.
|
2022-12-26 14:09:34 +00:00
|
|
|
//
|
|
|
|
//nolint:gomnd // testing domain can contains non-standart values
|
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",
|
|
|
|
Host: "0.0.0.0",
|
|
|
|
KeyFile: filepath.Join("https", "key.pem"),
|
|
|
|
Port: "3000",
|
|
|
|
Protocol: "http",
|
|
|
|
RootURL: "{{protocol}}://{{domain}}:{{port}}/",
|
|
|
|
},
|
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{
|
2022-01-31 16:17:19 +00:00
|
|
|
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 {
|
2023-01-16 10:29:40 +00:00
|
|
|
return fasttemplate.ExecuteString(cs.RootURL, `{{`, `}}`, map[string]any{
|
2023-01-16 10:33:37 +00:00
|
|
|
"domain": cs.Domain,
|
|
|
|
"host": cs.Host,
|
|
|
|
"port": cs.Port,
|
|
|
|
"protocol": cs.Protocol,
|
2022-01-12 18:04:40 +00:00
|
|
|
})
|
|
|
|
}
|