-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
mfa.go
80 lines (64 loc) · 1.76 KB
/
mfa.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
package main
import (
"fmt"
"net/http"
"sync"
log "github.com/sirupsen/logrus"
"github.com/Luzifer/nginx-sso/plugins"
)
var mfaLoginField = plugins.LoginField{
Label: "MFA Token",
Name: plugins.MFALoginFieldName,
Placeholder: "(optional)",
Type: "text",
}
var (
mfaRegistry = []plugins.MFAProvider{}
mfaRegistryMutex sync.RWMutex
activeMFAProviders = []plugins.MFAProvider{}
)
func registerMFAProvider(m plugins.MFAProvider) {
mfaRegistryMutex.Lock()
defer mfaRegistryMutex.Unlock()
mfaRegistry = append(mfaRegistry, m)
}
func initializeMFAProviders(yamlSource []byte) error {
mfaRegistryMutex.Lock()
defer mfaRegistryMutex.Unlock()
for _, m := range mfaRegistry {
err := m.Configure(yamlSource)
switch err {
case nil:
activeMFAProviders = append(activeMFAProviders, m)
log.WithFields(log.Fields{"mfa_provider": m.ProviderID()}).Debug("Activated MFA provider")
case plugins.ErrProviderUnconfigured:
log.WithFields(log.Fields{"mfa_provider": m.ProviderID()}).Debug("MFA provider unconfigured")
// This is okay.
default:
return fmt.Errorf("MFA provider configuration caused an error: %s", err)
}
}
return nil
}
func validateMFA(res http.ResponseWriter, r *http.Request, user string, mfaCfgs []plugins.MFAConfig) error {
if len(mfaCfgs) == 0 {
// User has no configured MFA devices, their MFA is automatically valid
return nil
}
mfaRegistryMutex.RLock()
defer mfaRegistryMutex.RUnlock()
for _, m := range activeMFAProviders {
err := m.ValidateMFA(res, r, user, mfaCfgs)
switch err {
case nil:
// Validated successfully
return nil
case plugins.ErrNoValidUserFound:
// This is fine for now
default:
return err
}
}
// No method could verify the user
return plugins.ErrNoValidUserFound
}