-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcredentials.go
52 lines (46 loc) · 1.52 KB
/
credentials.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
package grpc
import (
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
)
// TokenCredential implement grpc.credentials.PerRPCCredentials
type TokenCredential struct {
token string
}
// DialWithToken used on dial server with token authorization
// which can be authorized by TokenAuthorization interceptor.
func DialWithToken(host, token string) (*grpc.ClientConn, error) {
creds := &TokenCredential{token}
return grpc.Dial(
host,
grpc.WithPerRPCCredentials(creds),
grpc.WithInsecure(),
)
}
func (this *TokenCredential) GetRequestMetadata(c context.Context, uri ...string) (map[string]string, error) {
return map[string]string{
"authorization": this.token,
}, nil
}
func (this *TokenCredential) RequireTransportSecurity() bool {
return false
}
// TokenAuthorization is an interceptor authorized request token from client.
func TokenAuthorization(token string) grpc.UnaryServerInterceptor {
return func(c context.Context, param interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
md, ok := metadata.FromIncomingContext(c)
if !ok {
err = grpc.Errorf(codes.Internal, "authorization failed: token not found")
log.Error("[authorization failed]", err)
return
}
if len(md["authorization"]) < 1 || md["authorization"][0] != token {
err = grpc.Errorf(codes.Internal, "authorization failed: invalid token %v", md)
log.Error("[authorization failed]", err)
return
}
return handler(c, param)
}
}