-
Notifications
You must be signed in to change notification settings - Fork 60
/
helloserver.go
77 lines (63 loc) · 1.84 KB
/
helloserver.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
package main
import (
"crypto/rsa"
"fmt"
"io/ioutil"
"log"
"github.com/dgrijalva/jwt-go"
pb "github.com/kelseyhightower/grpc-hello-service/hello"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
)
func validateToken(token string, publicKey *rsa.PublicKey) (*jwt.Token, error) {
jwtToken, err := jwt.Parse(token, func(t *jwt.Token) (interface{}, error) {
if _, ok := t.Method.(*jwt.SigningMethodRSA); !ok {
log.Printf("Unexpected signing method: %v", t.Header["alg"])
return nil, fmt.Errorf("invalid token")
}
return publicKey, nil
})
if err == nil && jwtToken.Valid {
return jwtToken, nil
}
return nil, err
}
// helloServer is used to implement hello.HelloServer.
type helloServer struct {
jwtPublicKey *rsa.PublicKey
}
func NewHelloServer(rsaPublicKey string) (*helloServer, error) {
data, err := ioutil.ReadFile(rsaPublicKey)
if err != nil {
return nil, fmt.Errorf("Error reading the jwt public key: %v", err)
}
publickey, err := jwt.ParseRSAPublicKeyFromPEM(data)
if err != nil {
return nil, fmt.Errorf("Error parsing the jwt public key: %s", err)
}
return &helloServer{publickey}, nil
}
func (hs *helloServer) Say(ctx context.Context, request *pb.Request) (*pb.Response, error) {
var (
token *jwt.Token
err error
)
md, ok := metadata.FromContext(ctx)
if !ok {
return nil, grpc.Errorf(codes.Unauthenticated, "valid token required.")
}
jwtToken, ok := md["authorization"]
if !ok {
return nil, grpc.Errorf(codes.Unauthenticated, "valid token required.")
}
token, err = validateToken(jwtToken[0], hs.jwtPublicKey)
if err != nil {
return nil, grpc.Errorf(codes.Unauthenticated, "valid token required.")
}
response := &pb.Response{
Message: fmt.Sprintf("Hello %s (%s)", request.Name, token.Claims["email"]),
}
return response, nil
}