Switch from glide to govendor
This commit is contained in:
106
vendor/github.com/go-kit/kit/auth/jwt/middleware_test.go
generated
vendored
106
vendor/github.com/go-kit/kit/auth/jwt/middleware_test.go
generated
vendored
@@ -1,106 +0,0 @@
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
jwt "github.com/dgrijalva/jwt-go"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
var (
|
||||
kid = "kid"
|
||||
key = []byte("test_signing_key")
|
||||
method = jwt.SigningMethodHS256
|
||||
invalidMethod = jwt.SigningMethodRS256
|
||||
claims = Claims{"user": "go-kit"}
|
||||
// Signed tokens generated at https://jwt.io/
|
||||
signedKey = "eyJhbGciOiJIUzI1NiIsImtpZCI6ImtpZCIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiZ28ta2l0In0.14M2VmYyApdSlV_LZ88ajjwuaLeIFplB8JpyNy0A19E"
|
||||
invalidKey = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.e30.vKVCKto-Wn6rgz3vBdaZaCBGfCBDTXOENSo_X2Gq7qA"
|
||||
)
|
||||
|
||||
func TestSigner(t *testing.T) {
|
||||
e := func(ctx context.Context, i interface{}) (interface{}, error) { return ctx, nil }
|
||||
|
||||
signer := NewSigner(kid, key, method, claims)(e)
|
||||
ctx, err := signer(context.Background(), struct{}{})
|
||||
if err != nil {
|
||||
t.Fatalf("Signer returned error: %s", err)
|
||||
}
|
||||
|
||||
token, ok := ctx.(context.Context).Value(JWTTokenContextKey).(string)
|
||||
if !ok {
|
||||
t.Fatal("Token did not exist in context")
|
||||
}
|
||||
|
||||
if token != signedKey {
|
||||
t.Fatalf("JWT tokens did not match: expecting %s got %s", signedKey, token)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJWTParser(t *testing.T) {
|
||||
e := func(ctx context.Context, i interface{}) (interface{}, error) { return ctx, nil }
|
||||
|
||||
keys := func(token *jwt.Token) (interface{}, error) {
|
||||
return key, nil
|
||||
}
|
||||
|
||||
parser := NewParser(keys, method)(e)
|
||||
|
||||
// No Token is passed into the parser
|
||||
_, err := parser(context.Background(), struct{}{})
|
||||
if err == nil {
|
||||
t.Error("Parser should have returned an error")
|
||||
}
|
||||
|
||||
if err != ErrTokenContextMissing {
|
||||
t.Errorf("unexpected error returned, expected: %s got: %s", ErrTokenContextMissing, err)
|
||||
}
|
||||
|
||||
// Invalid Token is passed into the parser
|
||||
ctx := context.WithValue(context.Background(), JWTTokenContextKey, invalidKey)
|
||||
_, err = parser(ctx, struct{}{})
|
||||
if err == nil {
|
||||
t.Error("Parser should have returned an error")
|
||||
}
|
||||
|
||||
// Invalid Method is used in the parser
|
||||
badParser := NewParser(keys, invalidMethod)(e)
|
||||
ctx = context.WithValue(context.Background(), JWTTokenContextKey, signedKey)
|
||||
_, err = badParser(ctx, struct{}{})
|
||||
if err == nil {
|
||||
t.Error("Parser should have returned an error")
|
||||
}
|
||||
|
||||
if err != ErrUnexpectedSigningMethod {
|
||||
t.Errorf("unexpected error returned, expected: %s got: %s", ErrUnexpectedSigningMethod, err)
|
||||
}
|
||||
|
||||
// Invalid key is used in the parser
|
||||
invalidKeys := func(token *jwt.Token) (interface{}, error) {
|
||||
return []byte("bad"), nil
|
||||
}
|
||||
|
||||
badParser = NewParser(invalidKeys, method)(e)
|
||||
ctx = context.WithValue(context.Background(), JWTTokenContextKey, signedKey)
|
||||
_, err = badParser(ctx, struct{}{})
|
||||
if err == nil {
|
||||
t.Error("Parser should have returned an error")
|
||||
}
|
||||
|
||||
// Correct token is passed into the parser
|
||||
ctx = context.WithValue(context.Background(), JWTTokenContextKey, signedKey)
|
||||
ctx1, err := parser(ctx, struct{}{})
|
||||
if err != nil {
|
||||
t.Fatalf("Parser returned error: %s", err)
|
||||
}
|
||||
|
||||
cl, ok := ctx1.(context.Context).Value(JWTClaimsContextKey).(Claims)
|
||||
if !ok {
|
||||
t.Fatal("Claims were not passed into context correctly")
|
||||
}
|
||||
|
||||
if cl["user"] != claims["user"] {
|
||||
t.Fatalf("JWT Claims.user did not match: expecting %s got %s", claims["user"], cl["user"])
|
||||
}
|
||||
}
|
126
vendor/github.com/go-kit/kit/auth/jwt/transport_test.go
generated
vendored
126
vendor/github.com/go-kit/kit/auth/jwt/transport_test.go
generated
vendored
@@ -1,126 +0,0 @@
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"google.golang.org/grpc/metadata"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestToHTTPContext(t *testing.T) {
|
||||
reqFunc := ToHTTPContext()
|
||||
|
||||
// When the header doesn't exist
|
||||
ctx := reqFunc(context.Background(), &http.Request{})
|
||||
|
||||
if ctx.Value(JWTTokenContextKey) != nil {
|
||||
t.Error("Context shouldn't contain the encoded JWT")
|
||||
}
|
||||
|
||||
// Authorization header value has invalid format
|
||||
header := http.Header{}
|
||||
header.Set("Authorization", "no expected auth header format value")
|
||||
ctx = reqFunc(context.Background(), &http.Request{Header: header})
|
||||
|
||||
if ctx.Value(JWTTokenContextKey) != nil {
|
||||
t.Error("Context shouldn't contain the encoded JWT")
|
||||
}
|
||||
|
||||
// Authorization header is correct
|
||||
header.Set("Authorization", generateAuthHeaderFromToken(signedKey))
|
||||
ctx = reqFunc(context.Background(), &http.Request{Header: header})
|
||||
|
||||
token := ctx.Value(JWTTokenContextKey).(string)
|
||||
if token != signedKey {
|
||||
t.Errorf("Context doesn't contain the expected encoded token value; expected: %s, got: %s", signedKey, token)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromHTTPContext(t *testing.T) {
|
||||
reqFunc := FromHTTPContext()
|
||||
|
||||
// No JWT Token is passed in the context
|
||||
ctx := context.Background()
|
||||
r := http.Request{}
|
||||
reqFunc(ctx, &r)
|
||||
|
||||
token := r.Header.Get("Authorization")
|
||||
if token != "" {
|
||||
t.Error("authorization key should not exist in metadata")
|
||||
}
|
||||
|
||||
// Correct JWT Token is passed in the context
|
||||
ctx = context.WithValue(context.Background(), JWTTokenContextKey, signedKey)
|
||||
r = http.Request{Header: http.Header{}}
|
||||
reqFunc(ctx, &r)
|
||||
|
||||
token = r.Header.Get("Authorization")
|
||||
expected := generateAuthHeaderFromToken(signedKey)
|
||||
|
||||
if token != expected {
|
||||
t.Errorf("Authorization header does not contain the expected JWT token; expected %s, got %s", expected, token)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToGRPCContext(t *testing.T) {
|
||||
md := metadata.MD{}
|
||||
reqFunc := ToGRPCContext()
|
||||
|
||||
// No Authorization header is passed
|
||||
ctx := reqFunc(context.Background(), &md)
|
||||
token := ctx.Value(JWTTokenContextKey)
|
||||
if token != nil {
|
||||
t.Error("Context should not contain a JWT Token")
|
||||
}
|
||||
|
||||
// Invalid Authorization header is passed
|
||||
md["authorization"] = []string{fmt.Sprintf("%s", signedKey)}
|
||||
ctx = reqFunc(context.Background(), &md)
|
||||
token = ctx.Value(JWTTokenContextKey)
|
||||
if token != nil {
|
||||
t.Error("Context should not contain a JWT Token")
|
||||
}
|
||||
|
||||
// Authorization header is correct
|
||||
md["authorization"] = []string{fmt.Sprintf("Bearer %s", signedKey)}
|
||||
ctx = reqFunc(context.Background(), &md)
|
||||
token, ok := ctx.Value(JWTTokenContextKey).(string)
|
||||
if !ok {
|
||||
t.Fatal("JWT Token not passed to context correctly")
|
||||
}
|
||||
|
||||
if token != signedKey {
|
||||
t.Errorf("JWT tokens did not match: expecting %s got %s", signedKey, token)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromGRPCContext(t *testing.T) {
|
||||
reqFunc := FromGRPCContext()
|
||||
|
||||
// No JWT Token is passed in the context
|
||||
ctx := context.Background()
|
||||
md := metadata.MD{}
|
||||
reqFunc(ctx, &md)
|
||||
|
||||
_, ok := md["authorization"]
|
||||
if ok {
|
||||
t.Error("authorization key should not exist in metadata")
|
||||
}
|
||||
|
||||
// Correct JWT Token is passed in the context
|
||||
ctx = context.WithValue(context.Background(), JWTTokenContextKey, signedKey)
|
||||
md = metadata.MD{}
|
||||
reqFunc(ctx, &md)
|
||||
|
||||
token, ok := md["authorization"]
|
||||
if !ok {
|
||||
t.Fatal("JWT Token not passed to metadata correctly")
|
||||
}
|
||||
|
||||
if token[0] != generateAuthHeaderFromToken(signedKey) {
|
||||
t.Errorf("JWT tokens did not match: expecting %s got %s", signedKey, token[0])
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user