-
Notifications
You must be signed in to change notification settings - Fork 0
/
credential_provider.go
45 lines (37 loc) · 1.3 KB
/
credential_provider.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
package server
import "sync"
// interface for user credential provider
// hint: can be extended for more functionality
// =================================IMPORTANT NOTE===============================
// if the password in a third-party credential provider could be updated at runtime, we have to invalidate the caching
// for 'caching_sha2_password' by calling 'func (s *Server)InvalidateCache(string, string)'.
type CredentialProvider interface {
// check if the user exists
CheckUsername(username string) (bool, error)
// get user credential
GetCredential(username string) (password string, found bool, err error)
}
func NewInMemoryProvider() *InMemoryProvider {
return &InMemoryProvider{
userPool: sync.Map{},
}
}
// implements a in memory credential provider
type InMemoryProvider struct {
userPool sync.Map // username -> password
}
func (m *InMemoryProvider) CheckUsername(username string) (found bool, err error) {
_, ok := m.userPool.Load(username)
return ok, nil
}
func (m *InMemoryProvider) GetCredential(username string) (password string, found bool, err error) {
v, ok := m.userPool.Load(username)
if !ok {
return "", false, nil
}
return v.(string), true, nil
}
func (m *InMemoryProvider) AddUser(username, password string) {
m.userPool.Store(username, password)
}
type Provider InMemoryProvider