mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-23 03:51:29 +00:00
d17996f8b0
* Update V3 from V4 * Fix build v3 -> v4 * Update ssz * Update beacon_chain.pb.go * Fix formatter import * Update update-mockgen.sh comment to v4 * Fix conflicts. Pass build and tests * Fix test
70 lines
1.8 KiB
Go
70 lines
1.8 KiB
Go
package rpc
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/golang-jwt/jwt/v4"
|
|
"github.com/prysmaticlabs/prysm/v4/testing/require"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/metadata"
|
|
)
|
|
|
|
func TestServer_JWTInterceptor_Verify(t *testing.T) {
|
|
s := Server{
|
|
jwtSecret: []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 := createTokenString(s.jwtSecret)
|
|
require.NoError(t, err)
|
|
ctxMD := map[string][]string{
|
|
"authorization": {"Bearer " + token},
|
|
}
|
|
ctx := context.Background()
|
|
ctx = metadata.NewIncomingContext(ctx, ctxMD)
|
|
_, err = interceptor(ctx, "xyz", unaryInfo, unaryHandler)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
func TestServer_JWTInterceptor_BadToken(t *testing.T) {
|
|
s := Server{
|
|
jwtSecret: []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{
|
|
jwtSecret: []byte("badTestKey"),
|
|
}
|
|
token, err := createTokenString(badServer.jwtSecret)
|
|
require.NoError(t, err)
|
|
ctxMD := map[string][]string{
|
|
"authorization": {"Bearer " + token},
|
|
}
|
|
ctx := context.Background()
|
|
ctx = metadata.NewIncomingContext(ctx, ctxMD)
|
|
_, err = interceptor(ctx, "xyz", unaryInfo, unaryHandler)
|
|
require.ErrorContains(t, "signature is invalid", err)
|
|
}
|
|
|
|
func TestServer_JWTInterceptor_InvalidSigningType(t *testing.T) {
|
|
ss := &Server{jwtSecret: make([]byte, 32)}
|
|
// Use a different signing type than the expected, HMAC.
|
|
token := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.RegisteredClaims{})
|
|
_, err := ss.validateJWT(token)
|
|
require.ErrorContains(t, "unexpected JWT signing method", err)
|
|
}
|