Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalidate JWT in Backend Logout #7574

Merged
merged 15 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
271 changes: 154 additions & 117 deletions proto/validator/accounts/v2/web_api.pb.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions proto/validator/accounts/v2/web_api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ service Auth {
body: "*"
};
}
rpc Logout(google.protobuf.Empty) returns (google.protobuf.Empty) {
option (google.api.http) = {
post: "/v2/validator/logout",
body: "*"
};
}
}

// Type of key manager for the wallet, either direct, derived, or remote.
Expand Down
269 changes: 153 additions & 116 deletions proto/validator/accounts/v2_gateway/web_api.pb.go

Large diffs are not rendered by default.

78 changes: 78 additions & 0 deletions proto/validator/accounts/v2_gateway/web_api.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions validator/rpc/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ func (s *Server) Login(ctx context.Context, req *pb.AuthRequest) (*pb.AuthRespon
return s.sendAuthResponse()
}

// Logout a user by invalidating their JWT key.
func (s *Server) Logout(ctx context.Context, _ *ptypes.Empty) (*ptypes.Empty, error) {
// Invalidate the old JWT key, making all requests done with its token fail.
jwtKey, err := createRandomJWTKey()
if err != nil {
return nil, status.Error(codes.Internal, "Could not invalidate JWT key")
}
s.jwtKey = jwtKey
return &ptypes.Empty{}, nil
}

// Sends an auth response via gRPC containing a new JWT token.
func (s *Server) sendAuthResponse() (*pb.AuthResponse, error) {
// If everything is fine here, construct the auth token.
Expand Down
24 changes: 24 additions & 0 deletions validator/rpc/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"path/filepath"
"testing"

"github.com/dgrijalva/jwt-go"
ptypes "github.com/gogo/protobuf/types"
pb "github.com/prysmaticlabs/prysm/proto/validator/accounts/v2"
"github.com/prysmaticlabs/prysm/shared/event"
"github.com/prysmaticlabs/prysm/shared/fileutil"
Expand Down Expand Up @@ -79,6 +81,28 @@ func TestServer_SignupAndLogin_RoundTrip(t *testing.T) {
require.NoError(t, err)
}

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)
}

func TestServer_ChangePassword_Preconditions(t *testing.T) {
localWalletDir := setupWalletDir(t)
defaultWalletPath = localWalletDir
Expand Down
21 changes: 15 additions & 6 deletions validator/rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rpc

import (
"context"
"errors"
"fmt"
"net"

Expand Down Expand Up @@ -130,15 +131,10 @@ func (s *Server) Start() {
s.grpcServer = grpc.NewServer(opts...)

// We create a new, random JWT key upon validator startup.
r := rand.NewGenerator()
jwtKey := make([]byte, 32)
n, err := r.Read(jwtKey)
jwtKey, err := createRandomJWTKey()
if err != nil {
log.WithError(err).Fatal("Could not initialize validator jwt key")
}
if n != len(jwtKey) {
log.WithError(err).Fatal("Could not create random jwt key for validator")
}
s.jwtKey = jwtKey

// Register services available for the gRPC server.
Expand Down Expand Up @@ -172,3 +168,16 @@ func (s *Server) Stop() error {
func (s *Server) Status() error {
return s.credentialError
}

func createRandomJWTKey() ([]byte, error) {
r := rand.NewGenerator()
jwtKey := make([]byte, 32)
n, err := r.Read(jwtKey)
if err != nil {
return nil, err
}
if n != len(jwtKey) {
return nil, errors.New("could not create appropriately sized random JWT key")
}
return jwtKey, nil
}