63 lines
1.9 KiB
Go
63 lines
1.9 KiB
Go
|
package server
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
|
||
|
"gitea.benny.dog/torjus/ezshare/pb"
|
||
|
"gitea.benny.dog/torjus/ezshare/store"
|
||
|
"github.com/google/uuid"
|
||
|
"golang.org/x/crypto/bcrypt"
|
||
|
"google.golang.org/grpc/codes"
|
||
|
"google.golang.org/grpc/status"
|
||
|
)
|
||
|
|
||
|
type GRPCUserServiceServer struct {
|
||
|
store store.UserStore
|
||
|
pb.UnimplementedUserServiceServer
|
||
|
}
|
||
|
|
||
|
func NewGRPCUserServiceServer(store store.UserStore) *GRPCUserServiceServer {
|
||
|
return &GRPCUserServiceServer{store: store}
|
||
|
}
|
||
|
func (s *GRPCUserServiceServer) Register(ctx context.Context, req *pb.RegisterUserRequest) (*pb.RegisterUserResponse, error) {
|
||
|
// Check if user already exists
|
||
|
if _, err := s.store.GetUserByUsername(req.Username); err != store.ErrNoSuchItem {
|
||
|
return nil, fmt.Errorf("user already exists")
|
||
|
}
|
||
|
|
||
|
pw, err := hashPassword(req.Password)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("unable to hash password: %w", err)
|
||
|
}
|
||
|
user := &pb.User{
|
||
|
Id: uuid.Must(uuid.NewRandom()).String(),
|
||
|
Username: req.Username,
|
||
|
HashedPassword: pw,
|
||
|
UserRole: pb.User_USER,
|
||
|
Active: true,
|
||
|
}
|
||
|
|
||
|
if err := s.store.StoreUser(user); err != nil {
|
||
|
return nil, status.Error(codes.Internal, fmt.Sprintf("unable to store user: %s", err))
|
||
|
}
|
||
|
|
||
|
return &pb.RegisterUserResponse{Id: user.Id, Token: ""}, nil
|
||
|
}
|
||
|
|
||
|
func (s *GRPCUserServiceServer) Login(_ context.Context, _ *pb.LoginUserRequest) (*pb.LoginUserResponse, error) {
|
||
|
return nil, status.Error(codes.Unimplemented, "not yet implemented")
|
||
|
}
|
||
|
|
||
|
func (s *GRPCUserServiceServer) List(_ context.Context, _ *pb.ListUsersRequest) (*pb.ListUsersResponse, error) {
|
||
|
return nil, status.Error(codes.Unimplemented, "not yet implemented")
|
||
|
}
|
||
|
|
||
|
func (s *GRPCUserServiceServer) Approve(_ context.Context, _ *pb.ApproveUserRequest) (*pb.Empty, error) {
|
||
|
return nil, status.Error(codes.Unimplemented, "not yet implemented")
|
||
|
}
|
||
|
|
||
|
func hashPassword(password string) ([]byte, error) {
|
||
|
return bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||
|
}
|