-
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.
- Loading branch information
Showing
7 changed files
with
197 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package gapi | ||
|
||
import ( | ||
db "github.com/aradwann/eenergy/db/store" | ||
"github.com/aradwann/eenergy/pb" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
||
func convertUser(user db.User) *pb.User { | ||
return &pb.User{ | ||
Username: user.Username, | ||
FullName: user.FullName, | ||
Email: user.Email, | ||
PasswordChangedAt: timestamppb.New(user.PasswordChangedAt), | ||
CreatedAt: timestamppb.New(user.CreatedAt), | ||
} | ||
} |
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,38 @@ | ||
package gapi | ||
|
||
import ( | ||
"context" | ||
|
||
db "github.com/aradwann/eenergy/db/store" | ||
"github.com/aradwann/eenergy/pb" | ||
"github.com/aradwann/eenergy/util" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
func (server *Server) CreateUser(ctx context.Context, req *pb.CreateUserRequest) (*pb.CreateUserResponse, error) { | ||
hashedPassword, err := util.HashPassword(req.GetPassword()) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "failed to hash password: %s", err) | ||
} | ||
|
||
arg := db.CreateUserParams{ | ||
Username: req.GetUsername(), | ||
HashedPassword: hashedPassword, | ||
FullName: req.GetFullName(), | ||
Email: req.GetEmail(), | ||
} | ||
|
||
user, err := server.store.CreateUser(ctx, arg) | ||
if err != nil { | ||
if db.ErrorCode(err) == db.UniqueViolation { | ||
return nil, status.Errorf(codes.AlreadyExists, "username already exsits") | ||
} | ||
return nil, status.Errorf(codes.Internal, "failed to create user: %s", err) | ||
} | ||
|
||
rsp := &pb.CreateUserResponse{ | ||
User: convertUser(user), | ||
} | ||
return rsp, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package gapi | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
db "github.com/aradwann/eenergy/db/store" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
|
||
"github.com/aradwann/eenergy/pb" | ||
"github.com/aradwann/eenergy/util" | ||
) | ||
|
||
func (server *Server) LoginUser(ctx context.Context, req *pb.LoginUserRequest) (*pb.LoginUserResponse, error) { | ||
user, err := server.store.GetUser(ctx, req.GetUsername()) | ||
if err != nil { | ||
if errors.Is(err, db.ErrRecordNotFound) { | ||
return nil, status.Errorf(codes.NotFound, "user not found") | ||
|
||
} | ||
return nil, status.Errorf(codes.Internal, "failed to find user") | ||
} | ||
|
||
err = util.CheckPassword(req.GetPassword(), user.HashedPassword) | ||
if err != nil { | ||
return nil, status.Errorf(codes.NotFound, "incorrect password") | ||
|
||
} | ||
|
||
accessToken, accessPayload, err := server.tokenMaker.CreateToken( | ||
user.Username, | ||
server.config.AccessTokenDuration, | ||
) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "failed to create access token") | ||
} | ||
|
||
refreshToken, refreshPayload, err := server.tokenMaker.CreateToken( | ||
user.Username, | ||
server.config.RefreshTokenDuration, | ||
) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "failed to create refresh token") | ||
} | ||
|
||
session, err := server.store.CreateSession(ctx, db.CreateSessionParams{ | ||
ID: refreshPayload.ID, | ||
Username: user.Username, | ||
RefreshToken: refreshToken, | ||
UserAgent: "ctx.Request.UserAgent()", | ||
ClientIp: "ctx.ClientIP()", | ||
IsBlocked: false, | ||
ExpiresAt: refreshPayload.ExpiredAt, | ||
}) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "failed to create session") | ||
} | ||
|
||
rsp := &pb.LoginUserResponse{ | ||
User: convertUser(user), | ||
SessionId: session.ID.String(), | ||
AccessToken: accessToken, | ||
AccessTokenExpiresAt: timestamppb.New(accessPayload.ExpiredAt), | ||
RefreshToken: refreshToken, | ||
RefreshTokenExpiresAt: timestamppb.New(refreshPayload.ExpiredAt), | ||
} | ||
return rsp, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package gapi | ||
|
||
import ( | ||
"fmt" | ||
|
||
db "github.com/aradwann/eenergy/db/store" | ||
"github.com/aradwann/eenergy/pb" | ||
"github.com/aradwann/eenergy/token" | ||
"github.com/aradwann/eenergy/util" | ||
) | ||
|
||
// Server serves gRPC requests for our banking service. | ||
type Server struct { | ||
pb.UnimplementedEenergyServiceServer | ||
config util.Config | ||
store db.Store | ||
tokenMaker token.Maker | ||
} | ||
|
||
// NewServer creates a new gRPC server. | ||
func NewServer(config util.Config, store db.Store) (*Server, error) { | ||
tokenMaker, err := token.NewPASETOMaker(config.TokenSymmetricKey) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot create token maker: %w", err) | ||
} | ||
|
||
server := &Server{ | ||
config: config, | ||
store: store, | ||
tokenMaker: tokenMaker, | ||
} | ||
|
||
return server, 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