2020-08-13 20:27:42 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2022-02-25 19:08:43 +00:00
|
|
|
"github.com/golang-jwt/jwt/v4"
|
2023-03-17 18:52:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/testing/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{
|
2021-10-28 14:24:39 +00:00
|
|
|
jwtSecret: []byte("testKey"),
|
2020-08-13 20:27:42 +00:00
|
|
|
}
|
|
|
|
interceptor := s.JWTInterceptor()
|
|
|
|
|
|
|
|
unaryInfo := &grpc.UnaryServerInfo{
|
|
|
|
FullMethod: "Proto.CreateWallet",
|
|
|
|
}
|
|
|
|
unaryHandler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2021-10-28 14:24:39 +00:00
|
|
|
token, err := createTokenString(s.jwtSecret)
|
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{
|
2021-10-28 14:24:39 +00:00
|
|
|
jwtSecret: []byte("testKey"),
|
2020-08-13 20:27:42 +00:00
|
|
|
}
|
|
|
|
interceptor := s.JWTInterceptor()
|
|
|
|
|
|
|
|
unaryInfo := &grpc.UnaryServerInfo{
|
|
|
|
FullMethod: "Proto.CreateWallet",
|
|
|
|
}
|
|
|
|
unaryHandler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
badServer := Server{
|
2021-10-28 14:24:39 +00:00
|
|
|
jwtSecret: []byte("badTestKey"),
|
2020-08-13 20:27:42 +00:00
|
|
|
}
|
2021-10-28 14:24:39 +00:00
|
|
|
token, err := createTokenString(badServer.jwtSecret)
|
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-10-01 14:38:53 +00:00
|
|
|
require.ErrorContains(t, "signature is invalid", err)
|
2020-08-13 20:27:42 +00:00
|
|
|
}
|
2020-10-20 05:37:12 +00:00
|
|
|
|
|
|
|
func TestServer_JWTInterceptor_InvalidSigningType(t *testing.T) {
|
2021-10-28 14:24:39 +00:00
|
|
|
ss := &Server{jwtSecret: make([]byte, 32)}
|
2020-10-20 05:37:12 +00:00
|
|
|
// Use a different signing type than the expected, HMAC.
|
2022-08-17 04:10:11 +00:00
|
|
|
token := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.RegisteredClaims{})
|
2020-10-20 05:37:12 +00:00
|
|
|
_, err := ss.validateJWT(token)
|
|
|
|
require.ErrorContains(t, "unexpected JWT signing method", err)
|
|
|
|
}
|