-
Notifications
You must be signed in to change notification settings - Fork 1
/
gRPC_test.go
102 lines (94 loc) · 2.86 KB
/
gRPC_test.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package bearerware
import (
"errors"
"reflect"
"testing"
"google.golang.org/grpc/metadata"
"github.com/dgrijalva/jwt-go"
"golang.org/x/net/context"
)
func TestJWTAccessCredentials(t *testing.T) {
var tests = []struct {
token string
md map[string]string
}{
{
token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIiLCJpYXQiOm51bGwsImV4cCI6bnVsbCwiYXVkIjoiIiwic3ViIjoiIn0.IlffGJz3IyFX1ADQ6-jOTQ_0D-K0kuKq5SpB_oirCrk",
md: map[string]string{authHeader: "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIiLCJpYXQiOm51bGwsImV4cCI6bnVsbCwiYXVkIjoiIiwic3ViIjoiIn0.IlffGJz3IyFX1ADQ6-jOTQ_0D-K0kuKq5SpB_oirCrk"},
},
}
for _, test := range tests {
cred, err := NewJWTAccessFromJWT(test.token)
if err != nil {
t.Error(err)
}
md, err := cred.GetRequestMetadata(context.Background())
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(md, test.md) {
t.Errorf("Expected: %+v, Got: %+v", test.md, md)
}
require := cred.RequireTransportSecurity()
if !require {
t.Error("Expected RequireTransportSecurity to be true but got false")
}
}
}
func TestJWTFromContext(t *testing.T) {
var (
jwtKeyFunc = func(token *jwt.Token) (interface{}, error) {
return []byte(" "), nil
}
tests = []struct {
ctx context.Context
signingMethod jwt.SigningMethod
err error
}{
{
ctx: metadata.NewIncomingContext(
context.Background(),
metadata.New(map[string]string{authHeader: "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIiLCJpYXQiOm51bGwsImV4cCI6bnVsbCwiYXVkIjoiIiwic3ViIjoiIn0.IlffGJz3IyFX1ADQ6-jOTQ_0D-K0kuKq5SpB_oirCrk"}),
),
signingMethod: jwt.SigningMethodHS256,
err: nil,
},
{
ctx: context.Background(),
signingMethod: jwt.SigningMethodHS256,
err: errors.New("Bearer realm=Restricted"),
},
{
ctx: metadata.NewIncomingContext(
context.Background(),
metadata.New(map[string]string{}),
),
signingMethod: jwt.SigningMethodHS256,
err: errors.New("Bearer realm=Restricted"),
},
{
ctx: metadata.NewIncomingContext(
context.Background(),
metadata.New(map[string]string{authHeader: "Beare eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIiLCJpYXQiOm51bGwsImV4cCI6bnVsbCwiYXVkIjoiIiwic3ViIjoiIn0.IlffGJz3IyFX1ADQ6-jOTQ_0D-K0kuKq5SpB_oirCrk"}),
),
signingMethod: jwt.SigningMethodHS256,
err: errors.New("Authorization header format must be Bearer {token}"),
},
}
)
for _, test := range tests {
token, err := JWTFromContext(test.ctx, jwtKeyFunc, test.signingMethod)
if e := testJWTFrom(token, err, test.err); e != nil {
t.Error(e)
}
}
}
func testJWTFrom(token *jwt.Token, err, testErr error) error {
if err != nil && err.Error() != testErr.Error() {
return err
}
if testErr == nil && token == nil {
return errors.New("Token not valid")
}
return nil
}