feat: add Go current-session HTTP adapter
This commit is contained in:
1 parent
c849077591
commit
772795e7eb
10 files changed
+1608
-7
No files matched your search
@@ -0,0 +1,61 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type AuthConfig struct {
|
||||
Required bool
|
||||
Configured bool
|
||||
SessionSecret string
|
||||
}
|
||||
|
||||
func ParseAuthConfig(getenv func(string) string) (AuthConfig, error) {
|
||||
if getenv == nil {
|
||||
return AuthConfig{}, errors.New("auth config getenv is required")
|
||||
}
|
||||
|
||||
sessionSecret := firstAuthEnv(getenv,
|
||||
"ZHINIAN_AUTH_SESSION_SECRET",
|
||||
"AUTH_SESSION_SECRET",
|
||||
"NEXTAUTH_SECRET",
|
||||
)
|
||||
hasSecret := sessionSecret != ""
|
||||
explicitRequired, hasExplicitRequired := authBool(getenv("ZHINIAN_AUTH_REQUIRED"))
|
||||
disabled, _ := authBool(getenv("ZHINIAN_AUTH_DISABLED"))
|
||||
|
||||
required := getenv("NODE_ENV") == "production" || hasSecret
|
||||
if hasExplicitRequired {
|
||||
required = explicitRequired
|
||||
}
|
||||
if disabled {
|
||||
required = false
|
||||
}
|
||||
|
||||
return AuthConfig{
|
||||
Required: required,
|
||||
Configured: (required || hasSecret) && hasSecret,
|
||||
SessionSecret: sessionSecret,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func firstAuthEnv(getenv func(string) string, names ...string) string {
|
||||
for _, name := range names {
|
||||
if value := strings.TrimSpace(getenv(name)); value != "" {
|
||||
return value
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func authBool(value string) (bool, bool) {
|
||||
switch strings.ToLower(strings.TrimSpace(value)) {
|
||||
case "1", "true", "yes", "on":
|
||||
return true, true
|
||||
case "0", "false", "no", "off":
|
||||
return false, true
|
||||
default:
|
||||
return false, false
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user