56 lines
2.6 KiB
Go
56 lines
2.6 KiB
Go
package hwsauth
|
|
|
|
import (
|
|
"git.haelnorr.com/h/golib/env"
|
|
"git.haelnorr.com/h/golib/jwt"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// Config holds the configuration settings for the authenticator.
|
|
// All time-based settings are in minutes.
|
|
type Config struct {
|
|
SSL bool // ENV HWSAUTH_SSL: Enable SSL secure cookies (default: false)
|
|
TrustedHost string // ENV HWSAUTH_TRUSTED_HOST: Full server address for SSL (required if SSL is true)
|
|
SecretKey string // ENV HWSAUTH_SECRET_KEY: Secret key for signing JWT tokens (required)
|
|
AccessTokenExpiry int64 // ENV HWSAUTH_ACCESS_TOKEN_EXPIRY: Access token expiry in minutes (default: 5)
|
|
RefreshTokenExpiry int64 // ENV HWSAUTH_REFRESH_TOKEN_EXPIRY: Refresh token expiry in minutes (default: 1440)
|
|
TokenFreshTime int64 // ENV HWSAUTH_TOKEN_FRESH_TIME: Token fresh time in minutes (default: 5)
|
|
LandingPage string // ENV HWSAUTH_LANDING_PAGE: Redirect destination for authenticated users (default: "/profile")
|
|
DatabaseType string // ENV HWSAUTH_DATABASE_TYPE: Database type (postgres, mysql, sqlite, mariadb) (default: "postgres")
|
|
DatabaseVersion string // ENV HWSAUTH_DATABASE_VERSION: Database version string (default: "15")
|
|
JWTTableName string // ENV HWSAUTH_JWT_TABLE_NAME: Custom JWT blacklist table name (default: "jwtblacklist")
|
|
}
|
|
|
|
// ConfigFromEnv loads configuration from environment variables.
|
|
//
|
|
// Required environment variables:
|
|
// - HWSAUTH_SECRET_KEY: Secret key for JWT signing
|
|
// - HWSAUTH_TRUSTED_HOST: Required if HWSAUTH_SSL is true
|
|
//
|
|
// Returns an error if required variables are missing or invalid.
|
|
func ConfigFromEnv() (*Config, error) {
|
|
ssl := env.Bool("HWSAUTH_SSL", false)
|
|
trustedHost := env.String("HWSAUTH_TRUSTED_HOST", "")
|
|
if ssl && trustedHost == "" {
|
|
return nil, errors.New("SSL is enabled and no HWS_TRUSTED_HOST set")
|
|
}
|
|
cfg := &Config{
|
|
SSL: ssl,
|
|
TrustedHost: trustedHost,
|
|
SecretKey: env.String("HWSAUTH_SECRET_KEY", ""),
|
|
AccessTokenExpiry: env.Int64("HWSAUTH_ACCESS_TOKEN_EXPIRY", 5),
|
|
RefreshTokenExpiry: env.Int64("HWSAUTH_REFRESH_TOKEN_EXPIRY", 1440),
|
|
TokenFreshTime: env.Int64("HWSAUTH_TOKEN_FRESH_TIME", 5),
|
|
LandingPage: env.String("HWSAUTH_LANDING_PAGE", "/profile"),
|
|
DatabaseType: env.String("HWSAUTH_DATABASE_TYPE", jwt.DatabasePostgreSQL),
|
|
DatabaseVersion: env.String("HWSAUTH_DATABASE_VERSION", "15"),
|
|
JWTTableName: env.String("HWSAUTH_JWT_TABLE_NAME", "jwtblacklist"),
|
|
}
|
|
|
|
if cfg.SecretKey == "" {
|
|
return nil, errors.New("Envar not set: HWSAUTH_SECRET_KEY")
|
|
}
|
|
|
|
return cfg, nil
|
|
}
|