93 lines
2.2 KiB
Go
93 lines
2.2 KiB
Go
package config
|
|
|
|
import (
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/caarlos0/env/v11"
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
type Conf struct {
|
|
Server Server
|
|
Postgres Postgres
|
|
Redis Redis
|
|
Client Client
|
|
Tokens Tokens
|
|
}
|
|
|
|
type Server struct {
|
|
Addr string `env:"SERVER_ADDR" envDefault:":8080"`
|
|
FrontendURL string `env:"FRONTEND_URL,required"`
|
|
}
|
|
|
|
type Postgres struct {
|
|
URL string `env:"POSTGRES_URL,required"`
|
|
MigrationFilePath string `env:"POSTGRES_MIGRATION_FILE_PATH,required"`
|
|
}
|
|
|
|
type Redis struct {
|
|
URL string `env:"REDIS_URL,required"`
|
|
}
|
|
|
|
type Client struct {
|
|
ID string `env:"CLIENT_ID,required"`
|
|
Secret string `env:"CLIENT_SECRET,required"`
|
|
Name string `env:"CLIENT_NAME" envDefault:"Basic Client"`
|
|
RedirectURIs []string `env:"CLIENT_REDIRECT_URIS" envSeparator:","`
|
|
GrantTypes []string `env:"CLIENT_GRANT_TYPES" envSeparator:"," envDefault:"refresh_token"`
|
|
Scopes []string `env:"CLIENT_SCOPES" envSeparator:"," envDefault:"profile:read,profile:edit,profile:consent:read,profile:consent:delete,auth:register"`
|
|
}
|
|
|
|
// Tokens holds the TTLs for opaque access/refresh tokens.
|
|
// NOTE: tokens issued by this service are opaque (random), not JWTs —
|
|
// this used to be a "JWT" config section with an unused JWT_SECRET var.
|
|
type Tokens struct {
|
|
AccessTTL time.Duration `env:"ACCESS_TOKEN_TTL" envDefault:"24h"`
|
|
RefreshTTL time.Duration `env:"REFRESH_TOKEN_TTL" envDefault:"168h"`
|
|
}
|
|
|
|
func New() (*Conf, error) {
|
|
_ = godotenv.Load()
|
|
|
|
var conf Conf
|
|
if err := env.Parse(&conf); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &conf, nil
|
|
}
|
|
|
|
func (c *Client) RedirectURIsJSON() string {
|
|
if len(c.RedirectURIs) == 0 {
|
|
return "[]"
|
|
}
|
|
var b strings.Builder
|
|
b.WriteString("[\"")
|
|
b.WriteString(strings.Join(c.RedirectURIs, "\",\""))
|
|
b.WriteString("\"]")
|
|
return b.String()
|
|
}
|
|
|
|
func (c *Client) GrantTypesJSON() string {
|
|
if len(c.GrantTypes) == 0 {
|
|
return "[]"
|
|
}
|
|
var b strings.Builder
|
|
b.WriteString("[\"")
|
|
b.WriteString(strings.Join(c.GrantTypes, "\",\""))
|
|
b.WriteString("\"]")
|
|
return b.String()
|
|
}
|
|
|
|
func (c *Client) ScopesJSON() string {
|
|
if len(c.Scopes) == 0 {
|
|
return "[]"
|
|
}
|
|
var b strings.Builder
|
|
b.WriteString("[\"")
|
|
b.WriteString(strings.Join(c.Scopes, "\",\""))
|
|
b.WriteString("\"]")
|
|
return b.String()
|
|
}
|