/
grpc.go
84 lines (70 loc) · 2.12 KB
/
grpc.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package interceptor
import (
"context"
"fmt"
"github.com/infraboard/mcube/logger"
"github.com/infraboard/mcube/logger/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"github.com/infraboard/keyauth/apps/micro"
"github.com/infraboard/keyauth/client"
"github.com/infraboard/keyauth/common/header"
)
// GrpcAuthUnaryServerInterceptor returns a new unary server interceptor for auth.
func GrpcAuthUnaryServerInterceptor(c *client.Client) grpc.UnaryServerInterceptor {
return newGrpcAuther(c).Auth
}
func newGrpcAuther(c *client.Client) *grpcAuther {
return &grpcAuther{
log: zap.L().Named("Grpc Auther"),
keyauth: c,
}
}
// internal todo
type grpcAuther struct {
log logger.Logger
keyauth *client.Client
}
func (a *grpcAuther) Auth(
ctx context.Context, req interface{},
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (resp interface{}, err error) {
// 重上下文中获取认证信息
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, fmt.Errorf("ctx is not an grpc incoming context")
}
clientId, clientSecret := a.GetClientCredentialsFromMeta(md)
// 校验调用的客户端凭证是否有效
if err := a.validateServiceCredential(clientId, clientSecret); err != nil {
return nil, err
}
resp, err = handler(ctx, req)
return resp, err
}
func (a *grpcAuther) GetClientCredentialsFromMeta(md metadata.MD) (
clientId, clientSecret string) {
cids := md.Get(header.ClientHeaderKey)
sids := md.Get(header.ClientSecretKey)
if len(cids) > 0 {
clientId = cids[0]
}
if len(sids) > 0 {
clientSecret = sids[0]
}
return
}
func (a *grpcAuther) validateServiceCredential(clientId, clientSecret string) error {
if clientId == "" && clientSecret == "" {
return status.Errorf(codes.Unauthenticated, "client_id or client_secret is \"\"")
}
vsReq := micro.NewValidateClientCredentialRequest(clientId, clientSecret)
_, err := a.keyauth.Micro().ValidateClientCredential(context.Background(), vsReq)
if err != nil {
return status.Errorf(codes.Unauthenticated, "service auth error, %s", err)
}
return nil
}