lavender/issuer/manager.go

62 lines
1.3 KiB
Go
Raw Normal View History

2023-10-01 21:44:49 +01:00
package issuer
2023-10-04 14:51:38 +01:00
import (
"fmt"
"regexp"
"strings"
)
var isValidNamespace = regexp.MustCompile("^[0-9a-z.]+$")
2023-10-01 21:44:49 +01:00
type Manager struct {
m map[string]*WellKnownOIDC
}
func NewManager(services []SsoConfig) (*Manager, error) {
l := &Manager{m: make(map[string]*WellKnownOIDC)}
for _, i := range services {
2023-10-04 14:51:38 +01:00
if !isValidNamespace.MatchString(i.Namespace) {
return nil, fmt.Errorf("invalid namespace: %s", i.Namespace)
}
2023-10-01 21:44:49 +01:00
conf, err := i.FetchConfig()
if err != nil {
return nil, err
}
2023-10-04 14:51:38 +01:00
// save by namespace
l.m[i.Namespace] = conf
2023-10-01 21:44:49 +01:00
}
return l, nil
}
2023-10-09 16:29:10 +01:00
func NewManagerForTests(services []*WellKnownOIDC) *Manager {
2023-10-09 00:04:28 +01:00
l := &Manager{m: make(map[string]*WellKnownOIDC, len(services))}
for _, i := range services {
if !isValidNamespace.MatchString(i.Config.Namespace) {
panic("Invalid namespace in tests")
}
2023-10-09 16:29:10 +01:00
l.m[i.Config.Namespace] = i
2023-10-09 00:04:28 +01:00
}
return l
}
2023-10-04 14:51:38 +01:00
func (l *Manager) CheckNamespace(namespace string) bool {
_, ok := l.m[namespace]
2023-10-01 21:44:49 +01:00
return ok
}
func (l *Manager) FindServiceFromLogin(login string) *WellKnownOIDC {
2023-10-04 14:51:38 +01:00
// @ should have at least one byte before it
n := strings.IndexByte(login, '@')
if n < 1 {
return nil
}
// there should not be a second @
n2 := strings.IndexByte(login[n+1:], '@')
if n2 != -1 {
return nil
}
return l.m[login[n+1:]]
2023-10-01 21:44:49 +01:00
}