-
Notifications
You must be signed in to change notification settings - Fork 6
/
basic.go
55 lines (49 loc) · 1.71 KB
/
basic.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
package daemonauth
import (
"context"
"fmt"
"net/http"
"github.com/shaj13/go-guardian/v2/auth"
"github.com/shaj13/go-guardian/v2/auth/strategies/basic"
)
type (
// UserGranter is the interface for UserGrants method for user basic auth.
UserGranter interface {
UserGrants(username, password string) ([]string, error)
}
// NodeAuthenticater is the interface for AuthenticateNode method for node basic auth.
NodeAuthenticater interface {
AuthenticateNode(nodename, password string) error
}
)
func initBasicUser(i any) (string, auth.Strategy, error) {
name := "basicauth user"
userDB, ok := i.(UserGranter)
if !ok {
return name, nil, fmt.Errorf("UserGranter not implemented")
}
validateUser := func(_ context.Context, _ *http.Request, userName string, password string) (auth.Info, error) {
grants, err := userDB.UserGrants(userName, password)
if err != nil {
return nil, fmt.Errorf("invalid user %s: %w", userName, err)
}
return auth.NewUserInfo(userName, "", nil, *authenticatedExtensions("user", grants...)), nil
}
return name, basic.NewCached(validateUser, cache), nil
}
func initBasicNode(i interface{}) (string, auth.Strategy, error) {
name := "basicauth node"
n, ok := i.(NodeAuthenticater)
if !ok {
return name, nil, fmt.Errorf("missing node authenticater")
}
validate := func(_ context.Context, _ *http.Request, userName string, password string) (auth.Info, error) {
if err := n.AuthenticateNode(userName, password); err != nil {
return nil, fmt.Errorf("invalid nodename %s: %w", userName, err)
}
extensions := authenticatedExtensions("node", "root")
info := auth.NewUserInfo("node-"+userName, "", nil, *extensions)
return info, nil
}
return name, basic.NewCached(validate, cache), nil
}