forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
grpc.go
31 lines (26 loc) · 1.02 KB
/
grpc.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
package integration
import (
"context"
"fmt"
jwt "github.com/dgrijalva/jwt-go"
"google.golang.org/grpc/metadata"
"github.com/infobloxopen/atlas-app-toolkit/auth"
)
// AppendTokenToOutgoingContext adds an authorization token to the gRPC
// request context metadata. The user must provide a token field name like "token"
// or "bearer" to this function. It is intended specifically for gRPC testing.
func AppendTokenToOutgoingContext(ctx context.Context, fieldName, token string) context.Context {
c := metadata.AppendToOutgoingContext(
ctx, auth.AuthorizationHeader, fmt.Sprintf("%s %s", fieldName, token),
)
return c
}
// StandardTestingContext returns an outgoing request context that includes the
// standard test JWT. It is intended specifically for gRPC testing.
func StandardTestingContext() (context.Context, error) {
token, err := MakeTestJWT(jwt.SigningMethodHS256, StandardClaims)
if err != nil {
return nil, err
}
return AppendTokenToOutgoingContext(context.Background(), auth.DefaultTokenType, token), nil
}