mirror of
https://github.com/1f349/lavender.git
synced 2024-12-22 23:54:10 +00:00
58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
package issuer
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
var isValidNamespace = regexp.MustCompile("^[0-9a-z.]+$")
|
|
|
|
var MeWellKnown = &WellKnownOIDC{}
|
|
|
|
type Manager struct {
|
|
m map[string]*WellKnownOIDC
|
|
}
|
|
|
|
func NewManager(myNamespace string, services []SsoConfig) (*Manager, error) {
|
|
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)
|
|
}
|
|
|
|
conf, err := ssoService.FetchConfig()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// save by namespace
|
|
l.m[ssoService.Namespace] = conf
|
|
}
|
|
return l, nil
|
|
}
|
|
|
|
func (m *Manager) CheckNamespace(namespace string) bool {
|
|
_, ok := m.m[namespace]
|
|
return ok
|
|
}
|
|
|
|
func (m *Manager) GetService(namespace string) *WellKnownOIDC {
|
|
return m.m[namespace]
|
|
}
|
|
|
|
func (m *Manager) FindServiceFromLogin(login string) *WellKnownOIDC {
|
|
// @ 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 m.GetService(login[n+1:])
|
|
}
|