factor out auth config init's for lint
This commit is contained in:
parent
618d8691d3
commit
bd7d83d179
105
cmd/config.go
105
cmd/config.go
@ -53,43 +53,7 @@ func addConfigFlags(flags *pflag.FlagSet) {
|
||||
flags.Bool("branding.disableUsedPercentage", false, "disable used disk percentage graph")
|
||||
}
|
||||
|
||||
//nolint:gocyclo
|
||||
func getAuthentication(flags *pflag.FlagSet, defaults ...interface{}) (settings.AuthMethod, auth.Auther) {
|
||||
method := settings.AuthMethod(mustGetString(flags, "auth.method"))
|
||||
|
||||
var defaultAuther map[string]interface{}
|
||||
if len(defaults) > 0 {
|
||||
if hasAuth := defaults[0]; hasAuth != true {
|
||||
for _, arg := range defaults {
|
||||
switch def := arg.(type) {
|
||||
case *settings.Settings:
|
||||
method = def.AuthMethod
|
||||
case auth.Auther:
|
||||
ms, err := json.Marshal(def)
|
||||
checkErr(err)
|
||||
err = json.Unmarshal(ms, &defaultAuther)
|
||||
checkErr(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var auther auth.Auther
|
||||
if method == auth.MethodProxyAuth {
|
||||
header := mustGetString(flags, "auth.header")
|
||||
|
||||
if header == "" {
|
||||
header = defaultAuther["header"].(string)
|
||||
}
|
||||
|
||||
if header == "" {
|
||||
checkErr(nerrors.New("you must set the flag 'auth.header' for method 'proxy'"))
|
||||
}
|
||||
|
||||
auther = &auth.ProxyAuth{Header: header}
|
||||
}
|
||||
|
||||
if method == auth.MethodJWTAuth {
|
||||
func authInitJWT(flags *pflag.FlagSet) auth.Auther {
|
||||
header := mustGetString(flags, "auth.jwt-header.header")
|
||||
aud := mustGetString(flags, "auth.jwt-header.aud")
|
||||
iss := mustGetString(flags, "auth.jwt-header.iss")
|
||||
@ -112,20 +76,30 @@ func getAuthentication(flags *pflag.FlagSet, defaults ...interface{}) (settings.
|
||||
checkErr(nerrors.New("you must set the flag 'auth.claim' for method 'jwt-header'"))
|
||||
}
|
||||
|
||||
auther = &auth.JWTAuth{
|
||||
return &auth.JWTAuth{
|
||||
Header: header,
|
||||
Aud: aud,
|
||||
Iss: iss,
|
||||
CertsURL: certsurl,
|
||||
UsernameClaim: usernameClaim,
|
||||
}
|
||||
}
|
||||
|
||||
func authInitProxy(flags *pflag.FlagSet, defaultAuther map[string]interface{}) auth.Auther {
|
||||
header := mustGetString(flags, "auth.header")
|
||||
|
||||
if header == "" {
|
||||
header = defaultAuther["header"].(string)
|
||||
}
|
||||
|
||||
if method == auth.MethodNoAuth {
|
||||
auther = &auth.NoAuth{}
|
||||
if header == "" {
|
||||
checkErr(nerrors.New("you must set the flag 'auth.header' for method 'proxy'"))
|
||||
}
|
||||
|
||||
if method == auth.MethodJSONAuth {
|
||||
return &auth.ProxyAuth{Header: header}
|
||||
}
|
||||
|
||||
func authInitJSON(flags *pflag.FlagSet, defaultAuther map[string]interface{}) auth.Auther {
|
||||
jsonAuth := &auth.JSONAuth{}
|
||||
host := mustGetString(flags, "recaptcha.host")
|
||||
key := mustGetString(flags, "recaptcha.key")
|
||||
@ -150,10 +124,10 @@ func getAuthentication(flags *pflag.FlagSet, defaults ...interface{}) (settings.
|
||||
Secret: secret,
|
||||
}
|
||||
}
|
||||
auther = jsonAuth
|
||||
}
|
||||
return jsonAuth
|
||||
}
|
||||
|
||||
if method == auth.MethodHookAuth {
|
||||
func authInitHook(flags *pflag.FlagSet, defaultAuther map[string]interface{}) auth.Auther {
|
||||
command := mustGetString(flags, "auth.command")
|
||||
|
||||
if command == "" {
|
||||
@ -164,7 +138,48 @@ func getAuthentication(flags *pflag.FlagSet, defaults ...interface{}) (settings.
|
||||
checkErr(nerrors.New("you must set the flag 'auth.command' for method 'hook'"))
|
||||
}
|
||||
|
||||
auther = &auth.HookAuth{Command: command}
|
||||
return &auth.HookAuth{Command: command}
|
||||
}
|
||||
|
||||
func getAuthentication(flags *pflag.FlagSet, defaults ...interface{}) (settings.AuthMethod, auth.Auther) {
|
||||
method := settings.AuthMethod(mustGetString(flags, "auth.method"))
|
||||
|
||||
var defaultAuther map[string]interface{}
|
||||
if len(defaults) > 0 {
|
||||
if hasAuth := defaults[0]; hasAuth != true {
|
||||
for _, arg := range defaults {
|
||||
switch def := arg.(type) {
|
||||
case *settings.Settings:
|
||||
method = def.AuthMethod
|
||||
case auth.Auther:
|
||||
ms, err := json.Marshal(def)
|
||||
checkErr(err)
|
||||
err = json.Unmarshal(ms, &defaultAuther)
|
||||
checkErr(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var auther auth.Auther
|
||||
if method == auth.MethodProxyAuth {
|
||||
auther = authInitProxy(flags, defaultAuther)
|
||||
}
|
||||
|
||||
if method == auth.MethodJWTAuth {
|
||||
auther = authInitJWT(flags)
|
||||
}
|
||||
|
||||
if method == auth.MethodNoAuth {
|
||||
auther = &auth.NoAuth{}
|
||||
}
|
||||
|
||||
if method == auth.MethodJSONAuth {
|
||||
auther = authInitJSON(flags, defaultAuther)
|
||||
}
|
||||
|
||||
if method == auth.MethodHookAuth {
|
||||
auther = authInitHook(flags, defaultAuther)
|
||||
}
|
||||
|
||||
if auther == nil {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user