From aa71c988490a8585d97dd3bcca74a7902296125c Mon Sep 17 00:00:00 2001 From: Ferhat BOSTANCI Date: Mon, 7 Jun 2021 02:46:21 +0300 Subject: [PATCH] jwt manager updated & cleanup --- .kubernetes/comfigmap.yaml | 1 + handler/handler.go | 10 +- handler/room.go | 12 +- handler/user.go | 12 +- service/jwt_manager.go => jwt/manager.go | 18 ++- model/game_session.go | 22 +-- model/user.go | 10 +- proto/{auth_service.pb.go => auth.pb.go} | 133 +++++++++--------- proto/{auth_service.proto => auth.proto} | 0 ...uth_service_grpc.pb.go => auth_grpc.pb.go} | 2 +- server.go | 22 +-- 11 files changed, 127 insertions(+), 115 deletions(-) rename service/jwt_manager.go => jwt/manager.go (71%) rename proto/{auth_service.pb.go => auth.pb.go} (58%) rename proto/{auth_service.proto => auth.proto} (100%) rename proto/{auth_service_grpc.pb.go => auth_grpc.pb.go} (99%) diff --git a/.kubernetes/comfigmap.yaml b/.kubernetes/comfigmap.yaml index a63e610..c2621e2 100644 --- a/.kubernetes/comfigmap.yaml +++ b/.kubernetes/comfigmap.yaml @@ -8,6 +8,7 @@ data: Server: Port: 3000 Development: true + LogLevel: -1 MongoDB: URI: "mongodb://localhost:27017" User: "admin" diff --git a/handler/handler.go b/handler/handler.go index a3b6e14..96a17d8 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -2,30 +2,30 @@ package handler import ( "github.com/PulseyTeam/game-server/config" + "github.com/PulseyTeam/game-server/jwt" pb "github.com/PulseyTeam/game-server/proto" - "github.com/PulseyTeam/game-server/service" "go.mongodb.org/mongo-driver/mongo" ) type MultiplayerHandler struct { pb.UnimplementedMultiplayerServiceServer rooms map[string]map[string]*pb.Player - jwtManager *service.JWTManager + jwtManager *jwt.Manager mongoDB *mongo.Client cfg *config.Config } type AuthHandler struct { pb.UnimplementedAuthServiceServer - jwtManager *service.JWTManager + jwtManager *jwt.Manager mongoDB *mongo.Client cfg *config.Config } -func NewMultiplayerHandler(cfg *config.Config, mongoDB *mongo.Client, jwtManager *service.JWTManager) *MultiplayerHandler { +func NewMultiplayerHandler(cfg *config.Config, mongoDB *mongo.Client, jwtManager *jwt.Manager) *MultiplayerHandler { return &MultiplayerHandler{cfg: cfg, mongoDB: mongoDB, jwtManager: jwtManager} } -func NewAuthHandler(cfg *config.Config, mongoDB *mongo.Client, jwtManager *service.JWTManager) *AuthHandler { +func NewAuthHandler(cfg *config.Config, mongoDB *mongo.Client, jwtManager *jwt.Manager) *AuthHandler { return &AuthHandler{cfg: cfg, mongoDB: mongoDB, jwtManager: jwtManager} } diff --git a/handler/room.go b/handler/room.go index a406a04..d8ed349 100644 --- a/handler/room.go +++ b/handler/room.go @@ -14,17 +14,17 @@ import ( ) func (h *MultiplayerHandler) RoomConnect(ctx context.Context, request *pb.RoomConnectRequest) (*pb.RoomConnectResponse, error) { - collection := h.mongoDB.Database(h.cfg.MongoDB.DB).Collection("game_sessions") + collection := h.mongoDB.Database(h.cfg.MongoDB.DB).Collection(model.GameSessionCollection) gameSession := model.GameSession{} filter := bson.D{ primitive.E{Key: "map_id", Value: request.GetMapId()}, - primitive.E{Key: "status", Value: model.SessionWaiting}, + primitive.E{Key: "status", Value: model.StatusWaiting}, } err := collection.FindOne(ctx, filter).Decode(&gameSession) if err == nil { - return &pb.RoomConnectResponse{RoomId: gameSession.ID.String()}, nil + return &pb.RoomConnectResponse{RoomId: gameSession.ID.Hex()}, nil } else { //Todo refactor log.Warn().Msgf("find error: %v", err) @@ -33,7 +33,7 @@ func (h *MultiplayerHandler) RoomConnect(ctx context.Context, request *pb.RoomCo result, err := collection.InsertOne(ctx, model.GameSession{ ID: primitive.NewObjectID(), MapID: request.GetMapId(), - Status: model.SessionWaiting, + Status: model.StatusWaiting, StartedAt: time.Now(), FinishedAt: nil, }) @@ -41,9 +41,9 @@ func (h *MultiplayerHandler) RoomConnect(ctx context.Context, request *pb.RoomCo return nil, status.Errorf(codes.Internal, "failed to create resource") } - insertedID := result.InsertedID.(primitive.ObjectID).String() + insertedID := result.InsertedID.(primitive.ObjectID).Hex() - log.Trace().Msgf("game session (created): %v", insertedID) + log.Trace().Msgf("game session created: %v", insertedID) return &pb.RoomConnectResponse{RoomId: insertedID}, nil } diff --git a/handler/user.go b/handler/user.go index f7e086f..e89a2f3 100644 --- a/handler/user.go +++ b/handler/user.go @@ -12,8 +12,8 @@ import ( "google.golang.org/grpc/status" ) -func (h *AuthHandler) Login(ctx context.Context, request *pb.LoginRequest) (*pb.LoginResponse, error) { - collection := h.mongoDB.Database(h.cfg.MongoDB.DB).Collection("users") +func (a *AuthHandler) Login(ctx context.Context, request *pb.LoginRequest) (*pb.LoginResponse, error) { + collection := a.mongoDB.Database(a.cfg.MongoDB.DB).Collection(model.UserCollection) findUser := &model.User{} filter := bson.D{primitive.E{Key: "username", Value: request.GetUsername()}} @@ -29,7 +29,7 @@ func (h *AuthHandler) Login(ctx context.Context, request *pb.LoginRequest) (*pb. return nil, status.Errorf(codes.Unauthenticated, "authentication failed") } - accessToken, err := h.jwtManager.Generate(findUser) + accessToken, err := a.jwtManager.Generate(findUser) if err != nil { return nil, status.Errorf(codes.Internal, "cannot generate access token") } @@ -37,8 +37,8 @@ func (h *AuthHandler) Login(ctx context.Context, request *pb.LoginRequest) (*pb. return &pb.LoginResponse{AccessToken: accessToken}, nil } -func (h *AuthHandler) Register(ctx context.Context, request *pb.RegisterRequest) (*pb.RegisterResponse, error) { - collection := h.mongoDB.Database(h.cfg.MongoDB.DB).Collection("users") +func (a *AuthHandler) Register(ctx context.Context, request *pb.RegisterRequest) (*pb.RegisterResponse, error) { + collection := a.mongoDB.Database(a.cfg.MongoDB.DB).Collection(model.UserCollection) findUser := model.User{} filter := bson.D{primitive.E{Key: "username", Value: request.GetUsername()}} @@ -67,7 +67,7 @@ func (h *AuthHandler) Register(ctx context.Context, request *pb.RegisterRequest) return nil, status.Errorf(codes.Internal, "failed to create resource") } - accessToken, err := h.jwtManager.Generate(newUser) + accessToken, err := a.jwtManager.Generate(newUser) if err != nil { return nil, status.Errorf(codes.Internal, "cannot generate access token") } diff --git a/service/jwt_manager.go b/jwt/manager.go similarity index 71% rename from service/jwt_manager.go rename to jwt/manager.go index 2035f03..a52d202 100644 --- a/service/jwt_manager.go +++ b/jwt/manager.go @@ -1,4 +1,4 @@ -package service +package jwt import ( "fmt" @@ -7,7 +7,12 @@ import ( "time" ) -type JWTManager struct { +const ( + secretKey = "0pPeA3tn0BUesH9dtjptZsZpuYHZtCDFqUFH7EdiVw4U8APE6uTNg53LXqq1EAa" + tokenDuration = 4 * time.Hour +) + +type Manager struct { secretKey string tokenDuration time.Duration } @@ -17,14 +22,15 @@ type UserClaims struct { Username string `json:"username"` } -func NewJWTManager(secretKey string, tokenDuration time.Duration) *JWTManager { - return &JWTManager{secretKey, tokenDuration} +func NewManager() *Manager { + return &Manager{secretKey, tokenDuration} } -func (manager *JWTManager) Generate(user *model.User) (string, error) { +func (manager *Manager) Generate(user *model.User) (string, error) { claims := UserClaims{ StandardClaims: jwt.StandardClaims{ ExpiresAt: time.Now().Add(manager.tokenDuration).Unix(), + IssuedAt: time.Now().Unix(), Subject: user.ID.Hex(), }, Username: user.Username, @@ -34,7 +40,7 @@ func (manager *JWTManager) Generate(user *model.User) (string, error) { return token.SignedString([]byte(manager.secretKey)) } -func (manager *JWTManager) Verify(accessToken string) (*UserClaims, error) { +func (manager *Manager) Verify(accessToken string) (*UserClaims, error) { token, err := jwt.ParseWithClaims( accessToken, &UserClaims{}, diff --git a/model/game_session.go b/model/game_session.go index 0ea966a..85b1927 100644 --- a/model/game_session.go +++ b/model/game_session.go @@ -5,17 +5,19 @@ import ( "time" ) -const ( - SessionWaiting = "waiting" - SessionReady = "ready" - SessionPlaying = "playing" - SessionFinished = "finished" -) +const GameSessionCollection = "game_sessions" type GameSession struct { ID primitive.ObjectID `bson:"_id"` - MapID string `json:"map_id"` - Status string `json:"status"` - StartedAt time.Time `json:"started_at"` - FinishedAt *time.Time `json:"finished_at"` + MapID string `bson:"map_id"` + Status string `bson:"status"` + StartedAt time.Time `bson:"started_at"` + FinishedAt *time.Time `bson:"finished_at"` } + +const ( + StatusWaiting = "waiting" + StatusReady = "ready" + StatusPlaying = "playing" + StatusFinished = "finished" +) diff --git a/model/user.go b/model/user.go index ad13e2b..6036c45 100644 --- a/model/user.go +++ b/model/user.go @@ -6,12 +6,14 @@ import ( "golang.org/x/crypto/bcrypt" ) +const UserCollection = "users" + type User struct { ID primitive.ObjectID `bson:"_id"` - Username string `json:"username"` - Password string `json:"password"` - Settings *[]string `json:"settings"` - Cosmetics *[]string `json:"cosmetics"` + Username string `bson:"username"` + Password string `bson:"password"` + Settings *[]string `bson:"settings"` + Cosmetics *[]string `bson:"cosmetics"` } func (u *User) HashPassword() error { diff --git a/proto/auth_service.pb.go b/proto/auth.pb.go similarity index 58% rename from proto/auth_service.pb.go rename to proto/auth.pb.go index b08c245..cdae35a 100644 --- a/proto/auth_service.pb.go +++ b/proto/auth.pb.go @@ -2,7 +2,7 @@ // versions: // protoc-gen-go v1.26.0 // protoc v3.15.8 -// source: proto/auth_service.proto +// source: proto/auth.proto package proto @@ -32,7 +32,7 @@ type LoginRequest struct { func (x *LoginRequest) Reset() { *x = LoginRequest{} if protoimpl.UnsafeEnabled { - mi := &file_proto_auth_service_proto_msgTypes[0] + mi := &file_proto_auth_proto_msgTypes[0] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -45,7 +45,7 @@ func (x *LoginRequest) String() string { func (*LoginRequest) ProtoMessage() {} func (x *LoginRequest) ProtoReflect() protoreflect.Message { - mi := &file_proto_auth_service_proto_msgTypes[0] + mi := &file_proto_auth_proto_msgTypes[0] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -58,7 +58,7 @@ func (x *LoginRequest) ProtoReflect() protoreflect.Message { // Deprecated: Use LoginRequest.ProtoReflect.Descriptor instead. func (*LoginRequest) Descriptor() ([]byte, []int) { - return file_proto_auth_service_proto_rawDescGZIP(), []int{0} + return file_proto_auth_proto_rawDescGZIP(), []int{0} } func (x *LoginRequest) GetUsername() string { @@ -86,7 +86,7 @@ type LoginResponse struct { func (x *LoginResponse) Reset() { *x = LoginResponse{} if protoimpl.UnsafeEnabled { - mi := &file_proto_auth_service_proto_msgTypes[1] + mi := &file_proto_auth_proto_msgTypes[1] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -99,7 +99,7 @@ func (x *LoginResponse) String() string { func (*LoginResponse) ProtoMessage() {} func (x *LoginResponse) ProtoReflect() protoreflect.Message { - mi := &file_proto_auth_service_proto_msgTypes[1] + mi := &file_proto_auth_proto_msgTypes[1] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -112,7 +112,7 @@ func (x *LoginResponse) ProtoReflect() protoreflect.Message { // Deprecated: Use LoginResponse.ProtoReflect.Descriptor instead. func (*LoginResponse) Descriptor() ([]byte, []int) { - return file_proto_auth_service_proto_rawDescGZIP(), []int{1} + return file_proto_auth_proto_rawDescGZIP(), []int{1} } func (x *LoginResponse) GetAccessToken() string { @@ -134,7 +134,7 @@ type RegisterRequest struct { func (x *RegisterRequest) Reset() { *x = RegisterRequest{} if protoimpl.UnsafeEnabled { - mi := &file_proto_auth_service_proto_msgTypes[2] + mi := &file_proto_auth_proto_msgTypes[2] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -147,7 +147,7 @@ func (x *RegisterRequest) String() string { func (*RegisterRequest) ProtoMessage() {} func (x *RegisterRequest) ProtoReflect() protoreflect.Message { - mi := &file_proto_auth_service_proto_msgTypes[2] + mi := &file_proto_auth_proto_msgTypes[2] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -160,7 +160,7 @@ func (x *RegisterRequest) ProtoReflect() protoreflect.Message { // Deprecated: Use RegisterRequest.ProtoReflect.Descriptor instead. func (*RegisterRequest) Descriptor() ([]byte, []int) { - return file_proto_auth_service_proto_rawDescGZIP(), []int{2} + return file_proto_auth_proto_rawDescGZIP(), []int{2} } func (x *RegisterRequest) GetUsername() string { @@ -188,7 +188,7 @@ type RegisterResponse struct { func (x *RegisterResponse) Reset() { *x = RegisterResponse{} if protoimpl.UnsafeEnabled { - mi := &file_proto_auth_service_proto_msgTypes[3] + mi := &file_proto_auth_proto_msgTypes[3] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -201,7 +201,7 @@ func (x *RegisterResponse) String() string { func (*RegisterResponse) ProtoMessage() {} func (x *RegisterResponse) ProtoReflect() protoreflect.Message { - mi := &file_proto_auth_service_proto_msgTypes[3] + mi := &file_proto_auth_proto_msgTypes[3] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -214,7 +214,7 @@ func (x *RegisterResponse) ProtoReflect() protoreflect.Message { // Deprecated: Use RegisterResponse.ProtoReflect.Descriptor instead. func (*RegisterResponse) Descriptor() ([]byte, []int) { - return file_proto_auth_service_proto_rawDescGZIP(), []int{3} + return file_proto_auth_proto_rawDescGZIP(), []int{3} } func (x *RegisterResponse) GetAccessToken() string { @@ -224,58 +224,57 @@ func (x *RegisterResponse) GetAccessToken() string { return "" } -var File_proto_auth_service_proto protoreflect.FileDescriptor - -var file_proto_auth_service_proto_rawDesc = []byte{ - 0x0a, 0x18, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x73, 0x65, 0x72, - 0x76, 0x69, 0x63, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x46, 0x0a, 0x0c, 0x4c, 0x6f, - 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x75, 0x73, - 0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x75, 0x73, - 0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, - 0x72, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, - 0x72, 0x64, 0x22, 0x32, 0x0a, 0x0d, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, - 0x6e, 0x73, 0x65, 0x12, 0x21, 0x0a, 0x0c, 0x61, 0x63, 0x63, 0x65, 0x73, 0x73, 0x5f, 0x74, 0x6f, - 0x6b, 0x65, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x61, 0x63, 0x63, 0x65, 0x73, - 0x73, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x22, 0x49, 0x0a, 0x0f, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, - 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x75, 0x73, 0x65, - 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x75, 0x73, 0x65, - 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, - 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, - 0x64, 0x22, 0x35, 0x0a, 0x10, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x65, 0x73, - 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x21, 0x0a, 0x0c, 0x61, 0x63, 0x63, 0x65, 0x73, 0x73, 0x5f, - 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x61, 0x63, 0x63, - 0x65, 0x73, 0x73, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x32, 0x66, 0x0a, 0x0b, 0x41, 0x75, 0x74, 0x68, - 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x26, 0x0a, 0x05, 0x4c, 0x6f, 0x67, 0x69, 0x6e, - 0x12, 0x0d, 0x2e, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, - 0x0e, 0x2e, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, - 0x2f, 0x0a, 0x08, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x12, 0x10, 0x2e, 0x52, 0x65, - 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x11, 0x2e, - 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, - 0x42, 0x18, 0x5a, 0x08, 0x2e, 0x2f, 0x3b, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0xaa, 0x02, 0x0b, 0x41, - 0x75, 0x74, 0x68, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x33, +var File_proto_auth_proto protoreflect.FileDescriptor + +var file_proto_auth_proto_rawDesc = []byte{ + 0x0a, 0x10, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x61, 0x75, 0x74, 0x68, 0x2e, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x22, 0x46, 0x0a, 0x0c, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, + 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x75, 0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x75, 0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1a, + 0x0a, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x22, 0x32, 0x0a, 0x0d, 0x4c, 0x6f, + 0x67, 0x69, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x21, 0x0a, 0x0c, 0x61, + 0x63, 0x63, 0x65, 0x73, 0x73, 0x5f, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x0b, 0x61, 0x63, 0x63, 0x65, 0x73, 0x73, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x22, 0x49, + 0x0a, 0x0f, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, + 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x75, 0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x08, 0x75, 0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1a, 0x0a, + 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x22, 0x35, 0x0a, 0x10, 0x52, 0x65, 0x67, + 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x21, 0x0a, + 0x0c, 0x61, 0x63, 0x63, 0x65, 0x73, 0x73, 0x5f, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x0b, 0x61, 0x63, 0x63, 0x65, 0x73, 0x73, 0x54, 0x6f, 0x6b, 0x65, 0x6e, + 0x32, 0x66, 0x0a, 0x0b, 0x41, 0x75, 0x74, 0x68, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, + 0x26, 0x0a, 0x05, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x12, 0x0d, 0x2e, 0x4c, 0x6f, 0x67, 0x69, 0x6e, + 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x0e, 0x2e, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, + 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x2f, 0x0a, 0x08, 0x52, 0x65, 0x67, 0x69, 0x73, + 0x74, 0x65, 0x72, 0x12, 0x10, 0x2e, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x65, + 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x11, 0x2e, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, + 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x18, 0x5a, 0x08, 0x2e, 0x2f, 0x3b, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0xaa, 0x02, 0x0b, 0x41, 0x75, 0x74, 0x68, 0x53, 0x65, 0x72, 0x76, 0x69, + 0x63, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( - file_proto_auth_service_proto_rawDescOnce sync.Once - file_proto_auth_service_proto_rawDescData = file_proto_auth_service_proto_rawDesc + file_proto_auth_proto_rawDescOnce sync.Once + file_proto_auth_proto_rawDescData = file_proto_auth_proto_rawDesc ) -func file_proto_auth_service_proto_rawDescGZIP() []byte { - file_proto_auth_service_proto_rawDescOnce.Do(func() { - file_proto_auth_service_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_auth_service_proto_rawDescData) +func file_proto_auth_proto_rawDescGZIP() []byte { + file_proto_auth_proto_rawDescOnce.Do(func() { + file_proto_auth_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_auth_proto_rawDescData) }) - return file_proto_auth_service_proto_rawDescData + return file_proto_auth_proto_rawDescData } -var file_proto_auth_service_proto_msgTypes = make([]protoimpl.MessageInfo, 4) -var file_proto_auth_service_proto_goTypes = []interface{}{ +var file_proto_auth_proto_msgTypes = make([]protoimpl.MessageInfo, 4) +var file_proto_auth_proto_goTypes = []interface{}{ (*LoginRequest)(nil), // 0: LoginRequest (*LoginResponse)(nil), // 1: LoginResponse (*RegisterRequest)(nil), // 2: RegisterRequest (*RegisterResponse)(nil), // 3: RegisterResponse } -var file_proto_auth_service_proto_depIdxs = []int32{ +var file_proto_auth_proto_depIdxs = []int32{ 0, // 0: AuthService.Login:input_type -> LoginRequest 2, // 1: AuthService.Register:input_type -> RegisterRequest 1, // 2: AuthService.Login:output_type -> LoginResponse @@ -287,13 +286,13 @@ var file_proto_auth_service_proto_depIdxs = []int32{ 0, // [0:0] is the sub-list for field type_name } -func init() { file_proto_auth_service_proto_init() } -func file_proto_auth_service_proto_init() { - if File_proto_auth_service_proto != nil { +func init() { file_proto_auth_proto_init() } +func file_proto_auth_proto_init() { + if File_proto_auth_proto != nil { return } if !protoimpl.UnsafeEnabled { - file_proto_auth_service_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + file_proto_auth_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*LoginRequest); i { case 0: return &v.state @@ -305,7 +304,7 @@ func file_proto_auth_service_proto_init() { return nil } } - file_proto_auth_service_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + file_proto_auth_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*LoginResponse); i { case 0: return &v.state @@ -317,7 +316,7 @@ func file_proto_auth_service_proto_init() { return nil } } - file_proto_auth_service_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + file_proto_auth_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*RegisterRequest); i { case 0: return &v.state @@ -329,7 +328,7 @@ func file_proto_auth_service_proto_init() { return nil } } - file_proto_auth_service_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + file_proto_auth_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*RegisterResponse); i { case 0: return &v.state @@ -346,18 +345,18 @@ func file_proto_auth_service_proto_init() { out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), - RawDescriptor: file_proto_auth_service_proto_rawDesc, + RawDescriptor: file_proto_auth_proto_rawDesc, NumEnums: 0, NumMessages: 4, NumExtensions: 0, NumServices: 1, }, - GoTypes: file_proto_auth_service_proto_goTypes, - DependencyIndexes: file_proto_auth_service_proto_depIdxs, - MessageInfos: file_proto_auth_service_proto_msgTypes, + GoTypes: file_proto_auth_proto_goTypes, + DependencyIndexes: file_proto_auth_proto_depIdxs, + MessageInfos: file_proto_auth_proto_msgTypes, }.Build() - File_proto_auth_service_proto = out.File - file_proto_auth_service_proto_rawDesc = nil - file_proto_auth_service_proto_goTypes = nil - file_proto_auth_service_proto_depIdxs = nil + File_proto_auth_proto = out.File + file_proto_auth_proto_rawDesc = nil + file_proto_auth_proto_goTypes = nil + file_proto_auth_proto_depIdxs = nil } diff --git a/proto/auth_service.proto b/proto/auth.proto similarity index 100% rename from proto/auth_service.proto rename to proto/auth.proto diff --git a/proto/auth_service_grpc.pb.go b/proto/auth_grpc.pb.go similarity index 99% rename from proto/auth_service_grpc.pb.go rename to proto/auth_grpc.pb.go index 7efb99a..f063630 100644 --- a/proto/auth_service_grpc.pb.go +++ b/proto/auth_grpc.pb.go @@ -133,5 +133,5 @@ var AuthService_ServiceDesc = grpc.ServiceDesc{ }, }, Streams: []grpc.StreamDesc{}, - Metadata: "proto/auth_service.proto", + Metadata: "proto/auth.proto", } diff --git a/server.go b/server.go index 1ef64b5..f142abf 100644 --- a/server.go +++ b/server.go @@ -6,8 +6,8 @@ import ( "github.com/PulseyTeam/game-server/config" "github.com/PulseyTeam/game-server/database" "github.com/PulseyTeam/game-server/handler" + "github.com/PulseyTeam/game-server/jwt" pb "github.com/PulseyTeam/game-server/proto" - "github.com/PulseyTeam/game-server/service" "github.com/rs/zerolog" "github.com/rs/zerolog/log" "google.golang.org/grpc" @@ -16,15 +16,17 @@ import ( "time" ) -const ( - secretKey = "pulsey-secret" - tokenDuration = 4 * time.Hour -) - func main() { - consoleWriter := zerolog.ConsoleWriter{Out: os.Stderr} - consoleWriter.TimeFormat = time.RFC3339 - log.Logger = zerolog.New(consoleWriter).With().Timestamp().Caller().Logger() + if os.Getenv("KUBERNETES_PORT") != "" { + zerolog.LevelFieldName = "severity" + zerolog.TimestampFieldName = "timestamp" + zerolog.TimeFieldFormat = time.RFC3339Nano + log.Logger = log.With().Logger() + } else { + consoleWriter := zerolog.ConsoleWriter{Out: os.Stderr} + consoleWriter.TimeFormat = time.RFC3339 + log.Logger = zerolog.New(consoleWriter).With().Timestamp().Caller().Logger() + } log.Info().Msg("starting server...") @@ -49,7 +51,7 @@ func main() { }() log.Info().Msgf("mongodb connected: %v", mongoDBConn.NumberSessionsInProgress()) - jwtManager := service.NewJWTManager(secretKey, tokenDuration) + jwtManager := jwt.NewManager() grpcServer := grpc.NewServer(grpc.UnaryInterceptor(unaryInterceptor), grpc.StreamInterceptor(streamInterceptor)) pb.RegisterMultiplayerServiceServer(grpcServer, handler.NewMultiplayerHandler(cfg, mongoDBConn, jwtManager))