/
middleware.go
57 lines (53 loc) · 1.56 KB
/
middleware.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
package middleware
import (
"context"
"encoding/base64"
"errors"
"fmt"
"strings"
"github.com/go-kit/kit/endpoint"
"github.com/nightsilvertech/clockwerk/gvar"
"golang.org/x/crypto/bcrypt"
"google.golang.org/grpc/metadata"
)
func BasicAuthMiddleware() endpoint.Middleware {
return func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, errors.New("authentication required")
}
authData, ok := md["authorization"]
if !ok {
return nil, errors.New("authentication required")
}
var username, password string
var basicAuth, data []string
if basicAuth = strings.Split(authData[0], " "); len(basicAuth) != 2 {
return nil, errors.New("authentication required")
}
if data = strings.Split(basicAuth[1], ":"); len(basicAuth) != 2 {
return nil, errors.New("authentication required")
}
decoded, err := base64.StdEncoding.DecodeString(data[0])
if err != nil {
username = data[0]
password = data[1]
} else {
decodedDataSplit := strings.Split(string(decoded), ":")
username = decodedDataSplit[0]
password = decodedDataSplit[1]
}
key := fmt.Sprintf("%s_%s", gvar.HashKeyMap, username)
hashedPassword, ok := gvar.SyncMapHashStorage.Load(key)
if !ok {
return nil, errors.New("username not found")
}
err = bcrypt.CompareHashAndPassword([]byte(hashedPassword.(string)), []byte(password))
if err != nil {
return nil, err
}
return next(ctx, request)
}
}
}