forked from go-gost/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
38 lines (30 loc) · 813 Bytes
/
auth.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
package auth
import "context"
type Options struct{}
type Option func(opts *Options)
// Authenticator is an interface for user authentication.
type Authenticator interface {
Authenticate(ctx context.Context, user, password string, opts ...Option) (id string, ok bool)
}
type authenticatorGroup struct {
authers []Authenticator
}
func AuthenticatorGroup(authers ...Authenticator) Authenticator {
return &authenticatorGroup{
authers: authers,
}
}
func (p *authenticatorGroup) Authenticate(ctx context.Context, user, password string, opts ...Option) (string, bool) {
if len(p.authers) == 0 {
return "", false
}
for _, auther := range p.authers {
if auther == nil {
continue
}
if id, ok := auther.Authenticate(ctx, user, password, opts...); ok {
return id, ok
}
}
return "", false
}