forked from techschool/simplebank
-
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.
add grpc service to create and login user (techschool#32)
* add grpc service to create and login user * fix indentation * update go 1.18 in github action test Co-authored-by: phamlequang <phamlequang@gmail.com>
1 parent
6d1d65f
commit 5174eb1
Showing
21 changed files
with
1,286 additions
and
291 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
DB_DRIVER=postgres | ||
DB_SOURCE=postgresql://root:secret@localhost:5432/simple_bank?sslmode=disable | ||
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=15m | ||
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/techschool/simplebank/db/sqlc" | ||
"github.com/techschool/simplebank/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,42 @@ | ||
package gapi | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/lib/pq" | ||
db "github.com/techschool/simplebank/db/sqlc" | ||
"github.com/techschool/simplebank/pb" | ||
"github.com/techschool/simplebank/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 pqErr, ok := err.(*pq.Error); ok { | ||
switch pqErr.Code.Name() { | ||
case "unique_violation": | ||
return nil, status.Errorf(codes.AlreadyExists, "username already exists: %s", err) | ||
} | ||
} | ||
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,67 @@ | ||
package gapi | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
|
||
db "github.com/techschool/simplebank/db/sqlc" | ||
"github.com/techschool/simplebank/pb" | ||
"github.com/techschool/simplebank/util" | ||
"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 err == sql.ErrNoRows { | ||
return nil, status.Errorf(codes.NotFound, "user not found") | ||
} | ||
return nil, status.Errorf(codes.Internal, "failed to find user") | ||
} | ||
|
||
err = util.CheckPassword(req.Password, 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: "", | ||
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, | ||
RefreshToken: refreshToken, | ||
AccessTokenExpiresAt: timestamppb.New(accessPayload.ExpiredAt), | ||
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/techschool/simplebank/db/sqlc" | ||
"github.com/techschool/simplebank/pb" | ||
"github.com/techschool/simplebank/token" | ||
"github.com/techschool/simplebank/util" | ||
) | ||
|
||
// Server serves gRPC requests for our banking service. | ||
type Server struct { | ||
pb.UnimplementedSimpleBankServer | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,54 @@ | ||
module github.com/techschool/simplebank | ||
|
||
go 1.16 | ||
go 1.18 | ||
|
||
require ( | ||
github.com/aead/chacha20poly1305 v0.0.0-20201124145622-1a5aba2a8b29 | ||
github.com/dgrijalva/jwt-go v3.2.0+incompatible | ||
github.com/gin-gonic/gin v1.7.7 | ||
github.com/go-playground/validator/v10 v10.10.0 | ||
github.com/go-playground/validator/v10 v10.10.1 | ||
github.com/golang/mock v1.6.0 | ||
github.com/google/uuid v1.3.0 | ||
github.com/lib/pq v1.10.4 | ||
github.com/lib/pq v1.10.5 | ||
github.com/o1egl/paseto v1.0.0 | ||
github.com/spf13/afero v1.8.1 // indirect | ||
github.com/spf13/viper v1.10.1 | ||
github.com/stretchr/testify v1.7.0 | ||
github.com/ugorji/go v1.2.6 // indirect | ||
golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838 | ||
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a // indirect | ||
gopkg.in/ini.v1 v1.66.3 // indirect | ||
github.com/stretchr/testify v1.7.1 | ||
golang.org/x/crypto v0.0.0-20220408190544-5352b0902921 | ||
google.golang.org/grpc v1.43.0 | ||
google.golang.org/protobuf v1.28.0 | ||
) | ||
|
||
require ( | ||
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect | ||
github.com/aead/poly1305 v0.0.0-20180717145839-3fee0db0b635 // indirect | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/fsnotify/fsnotify v1.5.1 // indirect | ||
github.com/gin-contrib/sse v0.1.0 // indirect | ||
github.com/go-playground/locales v0.14.0 // indirect | ||
github.com/go-playground/universal-translator v0.18.0 // indirect | ||
github.com/golang/protobuf v1.5.2 // indirect | ||
github.com/hashicorp/hcl v1.0.0 // indirect | ||
github.com/json-iterator/go v1.1.12 // indirect | ||
github.com/leodido/go-urn v1.2.1 // indirect | ||
github.com/magiconair/properties v1.8.6 // indirect | ||
github.com/mattn/go-isatty v0.0.14 // indirect | ||
github.com/mitchellh/mapstructure v1.4.3 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
github.com/modern-go/reflect2 v1.0.2 // indirect | ||
github.com/pelletier/go-toml v1.9.4 // indirect | ||
github.com/pkg/errors v0.9.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/spf13/afero v1.8.2 // indirect | ||
github.com/spf13/cast v1.4.1 // indirect | ||
github.com/spf13/jwalterweatherman v1.1.0 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
github.com/subosito/gotenv v1.2.0 // indirect | ||
github.com/ugorji/go/codec v1.2.7 // indirect | ||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect | ||
golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f // indirect | ||
golang.org/x/text v0.3.7 // indirect | ||
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa // indirect | ||
gopkg.in/ini.v1 v1.66.4 // indirect | ||
gopkg.in/yaml.v2 v2.4.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect | ||
) |
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.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
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,18 @@ | ||
syntax = "proto3"; | ||
|
||
package pb; | ||
|
||
import "user.proto"; | ||
|
||
option go_package = "github.com/techschool/simplebank/pb"; | ||
|
||
message CreateUserRequest { | ||
string username = 1; | ||
string full_name = 2; | ||
string email = 3; | ||
string password = 4; | ||
} | ||
|
||
message CreateUserResponse { | ||
User user = 1; | ||
} |
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,22 @@ | ||
syntax = "proto3"; | ||
|
||
package pb; | ||
|
||
import "user.proto"; | ||
import "google/protobuf/timestamp.proto"; | ||
|
||
option go_package = "github.com/techschool/simplebank/pb"; | ||
|
||
message LoginUserRequest { | ||
string username = 1; | ||
string password = 2; | ||
} | ||
|
||
message LoginUserResponse { | ||
User user = 1; | ||
string session_id = 2; | ||
string access_token = 3; | ||
string refresh_token = 4; | ||
google.protobuf.Timestamp access_token_expires_at = 5; | ||
google.protobuf.Timestamp refresh_token_expires_at = 6; | ||
} |
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,13 @@ | ||
syntax = "proto3"; | ||
|
||
package pb; | ||
|
||
import "rpc_create_user.proto"; | ||
import "rpc_login_user.proto"; | ||
|
||
option go_package = "github.com/techschool/simplebank/pb"; | ||
|
||
service SimpleBank { | ||
rpc CreateUser (CreateUserRequest) returns (CreateUserResponse) {} | ||
rpc LoginUser (LoginUserRequest) returns (LoginUserResponse) {} | ||
} |
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,15 @@ | ||
syntax = "proto3"; | ||
|
||
package pb; | ||
|
||
import "google/protobuf/timestamp.proto"; | ||
|
||
option go_package = "github.com/techschool/simplebank/pb"; | ||
|
||
message User { | ||
string username = 1; | ||
string full_name = 2; | ||
string email = 3; | ||
google.protobuf.Timestamp password_changed_at = 4; | ||
google.protobuf.Timestamp created_at = 5; | ||
} |
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