forked from grpc-ecosystem/go-grpc-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples_test.go
86 lines (67 loc) · 2.67 KB
/
examples_test.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
85
86
package grpc_auth_test
import (
"context"
"log"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
"google.golang.org/grpc/status"
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
)
func parseToken(token string) (struct{}, error) {
return struct{}{}, nil
}
func userClaimFromToken(struct{}) string {
return "foobar"
}
// exampleAuthFunc is used by a middleware to authenticate requests
func exampleAuthFunc(ctx context.Context) (context.Context, error) {
token, err := grpc_auth.AuthFromMD(ctx, "bearer")
if err != nil {
return nil, err
}
tokenInfo, err := parseToken(token)
if err != nil {
return nil, status.Errorf(codes.Unauthenticated, "invalid auth token: %v", err)
}
grpc_ctxtags.Extract(ctx).Set("auth.sub", userClaimFromToken(tokenInfo))
// WARNING: in production define your own type to avoid context collisions
newCtx := context.WithValue(ctx, "tokenInfo", tokenInfo)
return newCtx, nil
}
// Simple example of server initialization code
func Example_serverConfig() {
_ = grpc.NewServer(
grpc.StreamInterceptor(grpc_auth.StreamServerInterceptor(exampleAuthFunc)),
grpc.UnaryInterceptor(grpc_auth.UnaryServerInterceptor(exampleAuthFunc)),
)
}
type gRPCserverAuthenticated struct{}
// SayHello only can be called by client when authenticated by exampleAuthFunc
func (g gRPCserverAuthenticated) SayHello(ctx context.Context, request *pb.HelloRequest) (*pb.HelloReply, error) {
return &pb.HelloReply{Message: "pong authenticated"}, nil
}
type gRPCserverUnauthenticated struct{}
// SayHello can be called by client without being authenticated by exampleAuthFunc as AuthFuncOverride is called instead
func (g *gRPCserverUnauthenticated) SayHello(ctx context.Context, request *pb.HelloRequest) (*pb.HelloReply, error) {
return &pb.HelloReply{Message: "pong unauthenticated"}, nil
}
// AuthFuncOverride is called instead of exampleAuthFunc
func (g *gRPCserverUnauthenticated) AuthFuncOverride(ctx context.Context, fullMethodName string) (context.Context, error) {
log.Println("client is calling method:", fullMethodName)
return ctx, nil
}
// Simple example of server initialization code with AuthFuncOverride method.
func Example_serverConfigWithAuthOverride() {
server := grpc.NewServer(
grpc.StreamInterceptor(grpc_auth.StreamServerInterceptor(exampleAuthFunc)),
grpc.UnaryInterceptor(grpc_auth.UnaryServerInterceptor(exampleAuthFunc)),
)
overrideActive := true
if overrideActive {
pb.RegisterGreeterServer(server, &gRPCserverUnauthenticated{})
} else {
pb.RegisterGreeterServer(server, &gRPCserverAuthenticated{})
}
}