-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
registry.go
124 lines (98 loc) · 2.88 KB
/
registry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"fmt"
"net/http"
"sync"
log "github.com/sirupsen/logrus"
"github.com/Luzifer/nginx-sso/plugins"
)
var (
authenticatorRegistry = []plugins.Authenticator{}
authenticatorRegistryMutex sync.RWMutex
activeAuthenticators = []plugins.Authenticator{}
)
func registerAuthenticator(a plugins.Authenticator) {
authenticatorRegistryMutex.Lock()
defer authenticatorRegistryMutex.Unlock()
authenticatorRegistry = append(authenticatorRegistry, a)
}
func initializeAuthenticators(yamlSource []byte) error {
authenticatorRegistryMutex.Lock()
defer authenticatorRegistryMutex.Unlock()
tmp := []plugins.Authenticator{}
for _, a := range authenticatorRegistry {
err := a.Configure(yamlSource)
switch err {
case nil:
tmp = append(tmp, a)
log.WithFields(log.Fields{"authenticator": a.AuthenticatorID()}).Debug("Activated authenticator")
case plugins.ErrProviderUnconfigured:
log.WithFields(log.Fields{"authenticator": a.AuthenticatorID()}).Debug("Authenticator unconfigured")
// This is okay.
default:
return fmt.Errorf("Authenticator configuration caused an error: %s", err)
}
}
if len(tmp) == 0 {
return fmt.Errorf("No authenticator configurations supplied")
}
activeAuthenticators = tmp
return nil
}
func detectUser(res http.ResponseWriter, r *http.Request) (string, []string, error) {
authenticatorRegistryMutex.RLock()
defer authenticatorRegistryMutex.RUnlock()
for _, a := range activeAuthenticators {
user, groups, err := a.DetectUser(res, r)
switch err {
case nil:
return user, groups, err
case plugins.ErrNoValidUserFound:
// This is okay.
default:
return "", nil, err
}
}
return "", nil, plugins.ErrNoValidUserFound
}
func loginUser(res http.ResponseWriter, r *http.Request) (string, []plugins.MFAConfig, error) {
authenticatorRegistryMutex.RLock()
defer authenticatorRegistryMutex.RUnlock()
for _, a := range activeAuthenticators {
user, mfaCfgs, err := a.Login(res, r)
switch err {
case nil:
return user, mfaCfgs, nil
case plugins.ErrNoValidUserFound:
// This is okay.
default:
return "", nil, err
}
}
return "", nil, plugins.ErrNoValidUserFound
}
func logoutUser(res http.ResponseWriter, r *http.Request) error {
authenticatorRegistryMutex.RLock()
defer authenticatorRegistryMutex.RUnlock()
for _, a := range activeAuthenticators {
if err := a.Logout(res, r); err != nil {
return err
}
}
return nil
}
func getFrontendAuthenticators() map[string][]plugins.LoginField {
authenticatorRegistryMutex.RLock()
defer authenticatorRegistryMutex.RUnlock()
output := map[string][]plugins.LoginField{}
for _, a := range activeAuthenticators {
if len(a.LoginFields()) == 0 {
continue
}
output[a.AuthenticatorID()] = a.LoginFields()
if a.SupportsMFA() && !mainCfg.Login.HideMFAField {
output[a.AuthenticatorID()] = append(output[a.AuthenticatorID()], mfaLoginField)
}
}
return output
}