-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- log level enabled - login & register added - jwt auth implemented
- Loading branch information
1 parent
b739229
commit acd6f98
Showing
19 changed files
with
864 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package handler | ||
|
||
import ( | ||
"context" | ||
"github.com/PulseyTeam/game-server/model" | ||
pb "github.com/PulseyTeam/game-server/proto" | ||
"github.com/rs/zerolog/log" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"google.golang.org/grpc/codes" | ||
"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") | ||
|
||
findUser := &model.User{} | ||
filter := bson.D{primitive.E{Key: "username", Value: request.GetUsername()}} | ||
|
||
err := collection.FindOne(ctx, filter).Decode(&findUser) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Unauthenticated, "authentication failed") | ||
} | ||
|
||
passwordCheck := findUser.CheckPassword(request.GetPassword()) | ||
|
||
if !passwordCheck { | ||
return nil, status.Errorf(codes.Unauthenticated, "authentication failed") | ||
} | ||
|
||
accessToken, err := h.jwtManager.Generate(findUser) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "cannot generate access token") | ||
} | ||
|
||
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") | ||
|
||
findUser := model.User{} | ||
filter := bson.D{primitive.E{Key: "username", Value: request.GetUsername()}} | ||
|
||
err := collection.FindOne(ctx, filter).Decode(&findUser) | ||
if err == nil { | ||
return nil, status.Errorf(codes.AlreadyExists, "user already registered") | ||
} else if err != mongo.ErrNoDocuments { | ||
log.Fatal().Err(err).Send() | ||
return nil, status.Errorf(codes.Internal, "internal error") | ||
} | ||
|
||
newUser := &model.User{ | ||
ID: primitive.NewObjectID(), | ||
Username: request.GetUsername(), | ||
Password: request.GetPassword(), | ||
} | ||
|
||
err = newUser.HashPassword() | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "could not hash password") | ||
} | ||
|
||
_, err = collection.InsertOne(ctx, newUser) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "failed to create resource") | ||
} | ||
|
||
accessToken, err := h.jwtManager.Generate(newUser) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "cannot generate access token") | ||
} | ||
|
||
return &pb.RegisterResponse{AccessToken: accessToken}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package model | ||
|
||
import ( | ||
"errors" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
"golang.org/x/crypto/bcrypt" | ||
) | ||
|
||
type User struct { | ||
ID primitive.ObjectID `bson:"_id"` | ||
Username string `json:"username"` | ||
Password string `json:"password"` | ||
Settings *[]string `json:"settings"` | ||
Cosmetics *[]string `json:"cosmetics"` | ||
} | ||
|
||
func (u *User) HashPassword() error { | ||
if len(u.Password) == 0 { | ||
return errors.New("password should not be empty") | ||
} | ||
|
||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
u.Password = string(hashedPassword) | ||
|
||
return nil | ||
} | ||
|
||
func (u *User) CheckPassword(plain string) bool { | ||
err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(plain)) | ||
return err == nil | ||
} |
Oops, something went wrong.