-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
auth.go
35 lines (28 loc) · 960 Bytes
/
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
package grpc
import (
"context"
"net/http"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
v1 "github.com/dapr/dapr/pkg/messaging/v1"
)
func setAPIAuthenticationMiddlewareUnary(apiToken, authHeader string) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
err := v1.ErrorFromHTTPResponseCode(http.StatusUnauthorized, "missing metadata in request")
return nil, err
}
token := md.Get(authHeader)
if len(token) == 0 {
err := v1.ErrorFromHTTPResponseCode(http.StatusUnauthorized, "missing api token in request metadata")
return nil, err
}
if token[0] != apiToken {
err := v1.ErrorFromHTTPResponseCode(http.StatusUnauthorized, "authentication error: api token mismatch")
return nil, err
}
md.Set(authHeader, "")
return handler(ctx, req)
}
}