-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
47 lines (41 loc) · 1.05 KB
/
auth.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
package main
import (
"context"
"fmt"
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
pb "github.com/morix1500/sample-go-grpc-middleware/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"net"
)
type HelloService struct{}
func (h HelloService) Hello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloResponse, error) {
return &pb.HelloResponse{
Message: "Hello, " + in.Name,
}, nil
}
func main() {
s := grpc.NewServer(
grpc.UnaryInterceptor(grpc_auth.UnaryServerInterceptor(authFunc)),
)
pb.RegisterHelloServiceServer(s, HelloService{})
lis, err := net.Listen("tcp", ":5000")
if err != nil {
panic(err)
}
if err := s.Serve(lis); err != nil {
panic(err)
}
}
func authFunc(ctx context.Context) (context.Context, error) {
token, err := grpc_auth.AuthFromMD(ctx, "bearer")
if err != nil {
return nil, err
}
fmt.Printf("receive token: %s\n", token)
if token != "hoge" {
return nil, grpc.Errorf(codes.Unauthenticated, "invalid token")
}
newCtx := context.WithValue(ctx, "result", "ok")
return newCtx, nil
}