-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
58 lines (50 loc) · 1.18 KB
/
plugin.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
package auth
import (
"context"
"github.com/go-gost/core/auth"
"github.com/go-gost/plugin/auth/proto"
xlogger "github.com/go-gost/x/logger"
)
type pluginAuthenticator struct {
client proto.AuthenticatorClient
options options
}
// NewPluginAuthenticator creates an Authenticator that authenticates client by plugin.
func NewPluginAuthenticator(opts ...Option) auth.Authenticator {
var options options
for _, opt := range opts {
opt(&options)
}
if options.logger == nil {
options.logger = xlogger.Nop()
}
p := &pluginAuthenticator{
options: options,
}
if options.client != nil {
p.client = proto.NewAuthenticatorClient(options.client)
}
return p
}
// Authenticate checks the validity of the provided user-password pair.
func (p *pluginAuthenticator) Authenticate(ctx context.Context, user, password string) bool {
if p.client == nil {
return false
}
r, err := p.client.Authenticate(ctx,
&proto.AuthenticateRequest{
Username: user,
Password: password,
})
if err != nil {
p.options.logger.Error(err)
return false
}
return r.Ok
}
func (p *pluginAuthenticator) Close() error {
if p.options.client != nil {
return p.options.client.Close()
}
return nil
}