2020-08-13 20:27:42 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2020-08-25 15:23:06 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2020-08-13 20:27:42 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestServer_JWTInterceptor_Verify(t *testing.T) {
|
|
|
|
s := Server{
|
|
|
|
jwtKey: []byte("testKey"),
|
|
|
|
}
|
|
|
|
interceptor := s.JWTInterceptor()
|
|
|
|
|
|
|
|
unaryInfo := &grpc.UnaryServerInfo{
|
|
|
|
FullMethod: "Proto.CreateWallet",
|
|
|
|
}
|
|
|
|
unaryHandler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
token, _, err := s.createTokenString()
|
2020-08-25 15:23:06 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-13 20:27:42 +00:00
|
|
|
ctxMD := map[string][]string{
|
2020-09-03 23:25:56 +00:00
|
|
|
"authorization": {"Bearer " + token},
|
2020-08-13 20:27:42 +00:00
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
ctx = metadata.NewIncomingContext(ctx, ctxMD)
|
|
|
|
_, err = interceptor(ctx, "xyz", unaryInfo, unaryHandler)
|
2020-08-25 15:23:06 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-13 20:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_JWTInterceptor_BadToken(t *testing.T) {
|
|
|
|
s := Server{
|
|
|
|
jwtKey: []byte("testKey"),
|
|
|
|
}
|
|
|
|
interceptor := s.JWTInterceptor()
|
|
|
|
|
|
|
|
unaryInfo := &grpc.UnaryServerInfo{
|
|
|
|
FullMethod: "Proto.CreateWallet",
|
|
|
|
}
|
|
|
|
unaryHandler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
badServer := Server{
|
|
|
|
jwtKey: []byte("badTestKey"),
|
|
|
|
}
|
|
|
|
token, _, err := badServer.createTokenString()
|
2020-08-25 15:23:06 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-13 20:27:42 +00:00
|
|
|
ctxMD := map[string][]string{
|
2020-09-03 23:25:56 +00:00
|
|
|
"authorization": {"Bearer " + token},
|
2020-08-13 20:27:42 +00:00
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
ctx = metadata.NewIncomingContext(ctx, ctxMD)
|
|
|
|
_, err = interceptor(ctx, "xyz", unaryInfo, unaryHandler)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Unexpected success processing token %v", err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "signature is invalid") {
|
|
|
|
t.Fatalf("Expected error validating signature, received %v", err)
|
|
|
|
}
|
|
|
|
}
|