lavender/issuer/manager.go

58 lines
1.2 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.]+$")
var MeWellKnown = &WellKnownOIDC{}
2023-10-01 21:44:49 +01:00
type Manager struct {
m map[string]*WellKnownOIDC
}
func NewManager(myNamespace string, services []SsoConfig) (*Manager, error) {
2023-10-01 21:44:49 +01:00
l := &Manager{m: make(map[string]*WellKnownOIDC)}
l.m[myNamespace] = MeWellKnown
for _, ssoService := range services {
if !isValidNamespace.MatchString(ssoService.Namespace) {
return nil, fmt.Errorf("invalid namespace: %s", ssoService.Namespace)
2023-10-04 14:51:38 +01:00
}
2024-08-29 17:57:31 +01:00
conf, err := ssoService.FetchConfig()
2023-10-01 21:44:49 +01:00
if err != nil {
return nil, err
}
2023-10-04 14:51:38 +01:00
// save by namespace
l.m[ssoService.Namespace] = conf
2023-10-01 21:44:49 +01:00
}
return l, nil
}
2024-02-07 01:18:17 +00:00
func (m *Manager) CheckNamespace(namespace string) bool {
_, ok := m.m[namespace]
2023-10-01 21:44:49 +01:00
return ok
}
2024-02-07 01:18:17 +00:00
func (m *Manager) GetService(namespace string) *WellKnownOIDC {
return m.m[namespace]
}
func (m *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
}
2024-02-07 01:18:17 +00:00
return m.GetService(login[n+1:])
2023-10-01 21:44:49 +01:00
}