-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_backend.go
47 lines (40 loc) · 1.09 KB
/
simple_backend.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
package login
import (
"errors"
)
const SimpleProviderName = "simple"
func init() {
RegisterProvider(
&ProviderDescription{
Name: SimpleProviderName,
},
SimpleBackendFactory)
}
func SimpleBackendFactory(config map[string]string) (Backend, error) {
userPassword := map[string]string{}
for k, v := range config {
if k != "provider" && k != "name" {
userPassword[k] = v
}
}
if len(userPassword) == 0 {
return nil, errors.New("no users provided for simple backend")
}
return NewSimpleBackend(userPassword), nil
}
// Simple backend, working on a map of username password pairs
type SimpleBackend struct {
userPassword map[string]string
}
// NewSimpleBackend creates a new SIMPLE Backend and verifies the parameters.
func NewSimpleBackend(userPassword map[string]string) *SimpleBackend {
return &SimpleBackend{
userPassword: userPassword,
}
}
func (sb *SimpleBackend) Authenticate(username, password string) (bool, UserInfo, error) {
if p, exist := sb.userPassword[username]; exist && p == password {
return true, UserInfo{Username: username}, nil
}
return false, UserInfo{}, nil
}