-
Notifications
You must be signed in to change notification settings - Fork 219
/
Copy pathhooks.go
45 lines (40 loc) · 1014 Bytes
/
hooks.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
package auth
import (
"context"
"go.uber.org/zap"
"github.com/DrmagicE/gmqtt/pkg/codes"
"github.com/DrmagicE/gmqtt/pkg/packets"
"github.com/DrmagicE/gmqtt/server"
)
func (a *Auth) HookWrapper() server.HookWrapper {
return server.HookWrapper{
OnBasicAuthWrapper: a.OnBasicAuthWrapper,
}
}
func (a *Auth) OnBasicAuthWrapper(pre server.OnBasicAuth) server.OnBasicAuth {
return func(ctx context.Context, client server.Client, req *server.ConnectRequest) (err error) {
err = pre(ctx, client, req)
if err != nil {
return err
}
ok, err := a.validate(string(req.Connect.Username), string(req.Connect.Password))
if err != nil {
return err
}
if !ok {
log.Debug("authentication failed", zap.String("username", string(req.Connect.Username)))
v := client.Version()
if packets.IsVersion3X(v) {
return &codes.Error{
Code: codes.V3NotAuthorized,
}
}
if packets.IsVersion5(v) {
return &codes.Error{
Code: codes.NotAuthorized,
}
}
}
return nil
}
}