github.com/prysmaticlabs/prysm@v1.4.4/validator/rpc/intercepter_test.go (about) 1 package rpc 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/form3tech-oss/jwt-go" 8 "github.com/prysmaticlabs/prysm/shared/testutil/require" 9 "github.com/prysmaticlabs/prysm/shared/timeutils" 10 "google.golang.org/grpc" 11 "google.golang.org/grpc/metadata" 12 ) 13 14 func TestServer_JWTInterceptor_Verify(t *testing.T) { 15 s := Server{ 16 jwtKey: []byte("testKey"), 17 } 18 interceptor := s.JWTInterceptor() 19 20 unaryInfo := &grpc.UnaryServerInfo{ 21 FullMethod: "Proto.CreateWallet", 22 } 23 unaryHandler := func(ctx context.Context, req interface{}) (interface{}, error) { 24 return nil, nil 25 } 26 token, _, err := s.createTokenString() 27 require.NoError(t, err) 28 ctxMD := map[string][]string{ 29 "authorization": {"Bearer " + token}, 30 } 31 ctx := context.Background() 32 ctx = metadata.NewIncomingContext(ctx, ctxMD) 33 _, err = interceptor(ctx, "xyz", unaryInfo, unaryHandler) 34 require.NoError(t, err) 35 } 36 37 func TestServer_JWTInterceptor_BadToken(t *testing.T) { 38 s := Server{ 39 jwtKey: []byte("testKey"), 40 } 41 interceptor := s.JWTInterceptor() 42 43 unaryInfo := &grpc.UnaryServerInfo{ 44 FullMethod: "Proto.CreateWallet", 45 } 46 unaryHandler := func(ctx context.Context, req interface{}) (interface{}, error) { 47 return nil, nil 48 } 49 50 badServer := Server{ 51 jwtKey: []byte("badTestKey"), 52 } 53 token, _, err := badServer.createTokenString() 54 require.NoError(t, err) 55 ctxMD := map[string][]string{ 56 "authorization": {"Bearer " + token}, 57 } 58 ctx := context.Background() 59 ctx = metadata.NewIncomingContext(ctx, ctxMD) 60 _, err = interceptor(ctx, "xyz", unaryInfo, unaryHandler) 61 require.ErrorContains(t, "signature is invalid", err) 62 } 63 64 func TestServer_JWTInterceptor_InvalidSigningType(t *testing.T) { 65 ss := &Server{jwtKey: make([]byte, 32)} 66 expirationTime := timeutils.Now().Add(tokenExpiryLength) 67 // Use a different signing type than the expected, HMAC. 68 token := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.StandardClaims{ 69 ExpiresAt: expirationTime.Unix(), 70 }) 71 _, err := ss.validateJWT(token) 72 require.ErrorContains(t, "unexpected JWT signing method", err) 73 }