2020-08-13 20:27:42 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-09-03 15:11:17 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-08-13 20:27:42 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-10-20 11:41:19 +00:00
|
|
|
"github.com/dgrijalva/jwt-go"
|
|
|
|
ptypes "github.com/gogo/protobuf/types"
|
2020-08-13 20:27:42 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/validator/accounts/v2"
|
2020-09-03 15:11:17 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/event"
|
2020-09-22 14:49:07 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/fileutil"
|
2020-08-13 20:27:42 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2020-10-15 22:31:52 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/accounts"
|
|
|
|
"github.com/prysmaticlabs/prysm/validator/accounts/wallet"
|
2020-08-13 20:27:42 +00:00
|
|
|
dbtest "github.com/prysmaticlabs/prysm/validator/db/testing"
|
2020-10-15 22:31:52 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager"
|
2020-08-13 20:27:42 +00:00
|
|
|
)
|
|
|
|
|
2020-09-03 15:11:17 +00:00
|
|
|
func setupWalletDir(t testing.TB) string {
|
2020-11-10 22:45:17 +00:00
|
|
|
walletDir := filepath.Join(t.TempDir(), "wallet")
|
2020-09-22 14:49:07 +00:00
|
|
|
require.NoError(t, os.MkdirAll(walletDir, os.ModePerm))
|
2020-09-03 15:11:17 +00:00
|
|
|
return walletDir
|
|
|
|
}
|
|
|
|
|
2020-08-13 20:27:42 +00:00
|
|
|
func TestServer_SignupAndLogin_RoundTrip(t *testing.T) {
|
|
|
|
valDB := dbtest.SetupDB(t, [][48]byte{})
|
|
|
|
ctx := context.Background()
|
2020-09-03 15:11:17 +00:00
|
|
|
|
|
|
|
localWalletDir := setupWalletDir(t)
|
|
|
|
defaultWalletPath = localWalletDir
|
|
|
|
strongPass := "29384283xasjasd32%%&*@*#*"
|
|
|
|
|
2020-08-13 20:27:42 +00:00
|
|
|
ss := &Server{
|
2020-09-03 15:11:17 +00:00
|
|
|
valDB: valDB,
|
|
|
|
walletInitializedFeed: new(event.Feed),
|
2020-10-10 02:07:28 +00:00
|
|
|
walletDir: defaultWalletPath,
|
2020-08-13 20:27:42 +00:00
|
|
|
}
|
|
|
|
weakPass := "password"
|
2020-09-22 14:49:07 +00:00
|
|
|
_, err := ss.Signup(ctx, &pb.AuthRequest{
|
2020-11-23 06:29:58 +00:00
|
|
|
Password: weakPass,
|
|
|
|
PasswordConfirmation: weakPass,
|
2020-08-13 20:27:42 +00:00
|
|
|
})
|
2020-10-20 02:26:31 +00:00
|
|
|
require.ErrorContains(t, "Could not validate RPC password input", err)
|
2020-08-13 20:27:42 +00:00
|
|
|
|
|
|
|
// We assert we are able to signup with a strong password.
|
|
|
|
_, err = ss.Signup(ctx, &pb.AuthRequest{
|
2020-11-23 06:29:58 +00:00
|
|
|
Password: strongPass,
|
|
|
|
PasswordConfirmation: strongPass,
|
2020-08-13 20:27:42 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Assert we stored the hashed password.
|
2020-10-20 02:26:31 +00:00
|
|
|
passwordHashExists := fileutil.FileExists(filepath.Join(defaultWalletPath, HashedRPCPassword))
|
2020-09-22 14:49:07 +00:00
|
|
|
assert.Equal(t, true, passwordHashExists)
|
|
|
|
|
|
|
|
// We attempt to create the wallet.
|
2020-10-15 22:31:52 +00:00
|
|
|
_, err = accounts.CreateWalletWithKeymanager(ctx, &accounts.CreateWalletConfig{
|
2020-09-22 14:49:07 +00:00
|
|
|
WalletCfg: &wallet.Config{
|
|
|
|
WalletDir: defaultWalletPath,
|
2020-10-15 22:31:52 +00:00
|
|
|
KeymanagerKind: keymanager.Derived,
|
2020-09-22 14:49:07 +00:00
|
|
|
WalletPassword: strongPass,
|
|
|
|
},
|
|
|
|
SkipMnemonicConfirm: true,
|
|
|
|
})
|
2020-08-13 20:27:42 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// We assert we are able to login.
|
|
|
|
_, err = ss.Login(ctx, &pb.AuthRequest{
|
|
|
|
Password: strongPass,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2020-10-20 02:26:31 +00:00
|
|
|
|
2020-10-20 11:41:19 +00:00
|
|
|
func TestServer_Logout(t *testing.T) {
|
|
|
|
key, err := createRandomJWTKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
ss := &Server{
|
|
|
|
jwtKey: key,
|
|
|
|
}
|
|
|
|
tokenString, _, err := ss.createTokenString()
|
|
|
|
require.NoError(t, err)
|
|
|
|
checkParsedKey := func(*jwt.Token) (interface{}, error) {
|
|
|
|
return ss.jwtKey, nil
|
|
|
|
}
|
|
|
|
_, err = jwt.Parse(tokenString, checkParsedKey)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = ss.Logout(context.Background(), &ptypes.Empty{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Attempting to validate the same token string after logout should fail.
|
|
|
|
_, err = jwt.Parse(tokenString, checkParsedKey)
|
|
|
|
assert.ErrorContains(t, "signature is invalid", err)
|
|
|
|
}
|
|
|
|
|
2020-10-20 02:26:31 +00:00
|
|
|
func TestServer_ChangePassword_Preconditions(t *testing.T) {
|
|
|
|
localWalletDir := setupWalletDir(t)
|
|
|
|
defaultWalletPath = localWalletDir
|
|
|
|
ctx := context.Background()
|
|
|
|
strongPass := "29384283xasjasd32%%&*@*#*"
|
|
|
|
ss := &Server{
|
|
|
|
walletDir: defaultWalletPath,
|
|
|
|
}
|
|
|
|
require.NoError(t, ss.SaveHashedPassword(strongPass))
|
|
|
|
_, err := ss.ChangePassword(ctx, &pb.ChangePasswordRequest{
|
|
|
|
CurrentPassword: strongPass,
|
|
|
|
Password: "",
|
|
|
|
})
|
|
|
|
assert.ErrorContains(t, "Could not validate password input", err)
|
|
|
|
_, err = ss.ChangePassword(ctx, &pb.ChangePasswordRequest{
|
|
|
|
CurrentPassword: strongPass,
|
|
|
|
Password: "abc",
|
|
|
|
PasswordConfirmation: "def",
|
|
|
|
})
|
|
|
|
assert.ErrorContains(t, "does not match", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_ChangePassword_OK(t *testing.T) {
|
|
|
|
localWalletDir := setupWalletDir(t)
|
|
|
|
defaultWalletPath = localWalletDir
|
|
|
|
ss := &Server{
|
|
|
|
walletDir: defaultWalletPath,
|
|
|
|
}
|
|
|
|
password := "Passw0rdz%%%%pass"
|
|
|
|
newPassword := "NewPassw0rdz%%%%pass"
|
|
|
|
ctx := context.Background()
|
|
|
|
require.NoError(t, ss.SaveHashedPassword(password))
|
|
|
|
_, err := ss.ChangePassword(ctx, &pb.ChangePasswordRequest{
|
|
|
|
CurrentPassword: password,
|
|
|
|
Password: newPassword,
|
|
|
|
PasswordConfirmation: newPassword,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = ss.Login(ctx, &pb.AuthRequest{
|
|
|
|
Password: newPassword,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|