forked from qvest-digital/loginsrv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.go
68 lines (57 loc) · 1.51 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package htpasswd
import (
"errors"
"github.com/tarent/loginsrv/login"
"github.com/tarent/loginsrv/model"
"strings"
)
// ProviderName const
const ProviderName = "htpasswd"
func init() {
login.RegisterProvider(
&login.ProviderDescription{
Name: ProviderName,
HelpText: "Htpasswd login backend opts: files=/path/to/pwdfile,/path/to/additionalfile",
},
BackendFactory)
}
// BackendFactory creates a htpasswd backend
func BackendFactory(config map[string]string) (login.Backend, error) {
var files []string
if f, exist := config["files"]; exist {
for _, file := range strings.Split(f, ";") {
files = append(files, file)
}
}
if f, exist := config["file"]; exist {
for _, file := range strings.Split(f, ";") {
files = append(files, file)
}
}
if len(files) == 0 {
return nil, errors.New(`missing parameter "file" for htpasswd provider`)
}
return NewBackend(files)
}
// Backend is a htpasswd based authentication backend.
type Backend struct {
auth *Auth
}
// NewBackend creates a new Backend and verifies the parameters.
func NewBackend(filenames []string) (*Backend, error) {
auth, err := NewAuth(filenames)
return &Backend{
auth,
}, err
}
// Authenticate the user
func (sb *Backend) Authenticate(username, password string) (bool, model.UserInfo, error) {
authenticated, err := sb.auth.Authenticate(username, password)
if authenticated && err == nil {
return authenticated, model.UserInfo{
Origin: ProviderName,
Sub: username,
}, err
}
return false, model.UserInfo{}, err
}