-
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.
Showing
22 changed files
with
1,167 additions
and
51 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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
|
||
# Environment variables | ||
DB_SOURCE=postgresql://root:secret@localhost:5432/gobank_db | ||
SERVER_ADDRESS=0.0.0.0:8080 | ||
HTTP_SERVER_ADDRESS=0.0.0.0:8080 | ||
GRPC_SERVER_ADDRESS=0.0.0.0:9090 | ||
TOKEN_SYMMETRIC_KEY=12345678901234567890123456789012 | ||
ACCESS_TOKEN_DURATION=20m | ||
REFRESH_TOKEN_DURATION=24h |
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/the-eduardo/Go-Bank/db/sqlc" | ||
"github.com/the-eduardo/Go-Bank/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.Time), | ||
CreatedAt: timestamppb.New(user.CreatedAt.Time), | ||
} | ||
} |
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,43 @@ | ||
package gapi | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/jackc/pgx/v5/pgconn" | ||
db "github.com/the-eduardo/Go-Bank/db/sqlc" | ||
"github.com/the-eduardo/Go-Bank/pb" | ||
"github.com/the-eduardo/Go-Bank/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 { | ||
var pgErr *pgconn.PgError | ||
if errors.As(err, &pgErr) { | ||
switch pgErr.Code { | ||
case "23505": | ||
return nil, status.Errorf(codes.AlreadyExists, "user already exists: %s", err) | ||
case "23503": | ||
return nil, status.Errorf(codes.Unavailable, "foreign key violation: %s", err) | ||
} | ||
} | ||
return nil, status.Errorf(codes.Internal, "failed to create user: %s", err) | ||
|
||
} | ||
resp := &pb.CreateUserResponse{ | ||
User: convertUser(user), | ||
} | ||
return resp, 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,73 @@ | ||
package gapi | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/jackc/pgx/v5" | ||
"github.com/the-eduardo/Go-Bank/api" | ||
db "github.com/the-eduardo/Go-Bank/db/sqlc" | ||
"github.com/the-eduardo/Go-Bank/pb" | ||
"github.com/the-eduardo/Go-Bank/util" | ||
"golang.org/x/crypto/bcrypt" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
||
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, pgx.ErrNoRows) { | ||
return nil, status.Errorf(codes.NotFound, "user not found") | ||
} | ||
return nil, status.Errorf(codes.Internal, "failed to find user") | ||
} | ||
err = util.CheckPassword(user.HashedPassword, req.Password) | ||
if err != nil { | ||
if errors.Is(err, bcrypt.ErrMismatchedHashAndPassword) { | ||
return nil, status.Errorf(codes.Unauthenticated, "invalid password") | ||
} | ||
return nil, status.Errorf(codes.Internal, "failed to verify password") | ||
} | ||
accessToken, accessPayload, err := server.tokenMaker.CreateToken( | ||
user.Username, | ||
server.config.AccessTokenDuration, | ||
) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "cannot create access token") | ||
} | ||
refreshToken, refreshPayload, err := server.tokenMaker.CreateToken( | ||
user.Username, | ||
server.config.RefreshTokenDuration) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "cannot create refresh token") | ||
} | ||
fixedRefreshPayload, err := api.ConvertGoogleUUIDToPGTypeUUID(refreshPayload.ID) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "cannot convert refresh payload") | ||
} | ||
fixedRefreshPayloadExpiresAt := api.TimeToPGTimestamptz(refreshPayload.ExpiredAt) | ||
_, err = server.store.CreateSession(ctx, db.CreateSessionParams{ | ||
ID: fixedRefreshPayload, | ||
Username: user.Username, | ||
RefreshToken: refreshToken, | ||
UserAgent: "", | ||
ClientIp: "", | ||
IsBlocked: false, | ||
ExpiresAt: fixedRefreshPayloadExpiresAt, | ||
}) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "cannot create session") | ||
} | ||
|
||
resp := &pb.LoginUserResponse{ | ||
User: convertUser(user), | ||
SessionId: refreshPayload.ID.String(), | ||
AccessToken: accessToken, | ||
RefreshToken: refreshToken, | ||
AccessTokenExpiresAt: timestamppb.New(accessPayload.ExpiredAt), | ||
RefreshTokenExpiresAt: timestamppb.New(refreshPayload.ExpiredAt), | ||
} | ||
return resp, 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,33 @@ | ||
package gapi | ||
|
||
import ( | ||
"fmt" | ||
db "github.com/the-eduardo/Go-Bank/db/sqlc" | ||
"github.com/the-eduardo/Go-Bank/pb" | ||
"github.com/the-eduardo/Go-Bank/token" | ||
"github.com/the-eduardo/Go-Bank/util" | ||
) | ||
|
||
// Server serves gRPC requests | ||
type Server struct { | ||
pb.UnimplementedGoBankServer | ||
config util.Config | ||
tokenMaker token.Maker | ||
store db.Store | ||
} | ||
|
||
// 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
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
Oops, something went wrong.