-
Notifications
You must be signed in to change notification settings - Fork 2
/
authorization.go
87 lines (65 loc) · 1.94 KB
/
authorization.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
87
package auth
import (
"context"
"github.com/dgrijalva/jwt-go"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc"
)
type AuthorizerConfig struct {
Secret string
}
type AuthorizerInterceptor struct {
logger *zap.Logger
jwtManager *JWTManager
config *AuthorizerConfig
}
type ProviderClaims struct {
jwt.StandardClaims
}
type CtxProviderClaimsKey struct{}
func NewAuthorizerInterceptor(logger *zap.Logger, jwtManager *JWTManager, config *AuthorizerConfig) *AuthorizerInterceptor {
return &AuthorizerInterceptor{
logger: logger,
jwtManager: jwtManager,
config: config,
}
}
// Unary returns a server interceptor function to authenticate and authorize unary RPC.
func (i *AuthorizerInterceptor) Unary() grpc.UnaryServerInterceptor {
return func(
ctx context.Context,
req interface{},
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (interface{}, error) {
authorize, err := i.authorize(ctx, info.FullMethod)
if err != nil {
return nil, err
}
return handler(authorize, req)
}
}
func (i *AuthorizerInterceptor) authorize(ctx context.Context, method string) (context.Context, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
i.logger.Error("metadata was not provided")
return ctx, status.Errorf(codes.Unauthenticated, "signature is required")
}
signature, ok := md["x-api-key"]
if !ok {
return ctx, status.Errorf(codes.Unauthenticated, "invalid signature")
}
var claims ProviderClaims
p := jwt.Parser{ValidMethods: []string{jwt.SigningMethodHS256.Name}}
_, err := p.ParseWithClaims(signature[0], &claims, func(token *jwt.Token) (interface{}, error) {
return []byte(i.config.Secret), nil
})
if err != nil {
return nil, status.Errorf(codes.Unauthenticated, "you are not authorized to access this resource")
}
ctx = context.WithValue(ctx, CtxProviderClaimsKey{}, claims)
return ctx, nil
}