mirror of
https://github.com/qdm12/gluetun.git
synced 2026-05-10 04:30:20 +02:00
4a78989d9d
- main reason being it's a burden to always define sentinel errors at global scope, wrap them with `%w` instead of using a string directly - only use sentinel errors when it has to be checked using `errors.Is` - replace all usage of these sentinel errors in `fmt.Errorf` with direct strings that were in the sentinel error - exclude the sentinel error definition requirement from .golangci.yml - update unit tests to use ContainersError instead of ErrorIs so it stays as a "not a change detector test" without requiring a sentinel error
48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package auth
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
type internalRole struct {
|
|
name string
|
|
checker authorizationChecker
|
|
}
|
|
|
|
func settingsToLookupMap(settings Settings) (routeToRoles map[string][]internalRole, err error) {
|
|
routeToRoles = make(map[string][]internalRole)
|
|
for _, role := range settings.Roles {
|
|
var checker authorizationChecker
|
|
switch role.Auth {
|
|
case AuthNone:
|
|
checker = newNoneMethod()
|
|
case AuthAPIKey:
|
|
checker = newAPIKeyMethod(role.APIKey)
|
|
case AuthBasic:
|
|
checker = newBasicAuthMethod(role.Username, role.Password)
|
|
default:
|
|
return nil, fmt.Errorf("authentication method not supported: %s", role.Auth)
|
|
}
|
|
|
|
iRole := internalRole{
|
|
name: role.Name,
|
|
checker: checker,
|
|
}
|
|
for _, route := range role.Routes {
|
|
checkerExists := false
|
|
for _, role := range routeToRoles[route] {
|
|
if role.checker.equal(iRole.checker) {
|
|
checkerExists = true
|
|
break
|
|
}
|
|
}
|
|
if checkerExists {
|
|
// even if the role name is different, if the checker is the same, skip it.
|
|
continue
|
|
}
|
|
routeToRoles[route] = append(routeToRoles[route], iRole)
|
|
}
|
|
}
|
|
return routeToRoles, nil
|
|
}
|