From 7bd74a69d87427d58650bd1fe6e7d89f439abf90 Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Wed, 19 Jan 2022 21:16:20 +0100 Subject: [PATCH] User service for all user related functions --- controllers/balance.ctrl.go | 2 +- lib/service/balance.go | 24 ------------ lib/service/service.go | 42 -------------------- lib/service/user.go | 78 +++++++++++++++++++++++++++++++++++++ lib/tokens/jwt.go | 1 - 5 files changed, 79 insertions(+), 68 deletions(-) delete mode 100644 lib/service/balance.go create mode 100644 lib/service/user.go diff --git a/controllers/balance.ctrl.go b/controllers/balance.ctrl.go index 6c1966e..99a1f02 100644 --- a/controllers/balance.ctrl.go +++ b/controllers/balance.ctrl.go @@ -20,7 +20,7 @@ func NewBalanceController(svc *service.LndhubService) *BalanceController { // Balance : Balance Controller func (controller *BalanceController) Balance(c echo.Context) error { userId := c.Get("UserID").(int64) - balance, err := controller.svc.CurrentBalance(context.TODO(), userId) + balance, err := controller.svc.CurrentUserBalance(context.TODO(), userId) if err != nil { return err } diff --git a/lib/service/balance.go b/lib/service/balance.go deleted file mode 100644 index b6793f3..0000000 --- a/lib/service/balance.go +++ /dev/null @@ -1,24 +0,0 @@ -package service - -import ( - "context" - - "github.com/getAlby/lndhub.go/db/models" -) - -func (svc *LndhubService) CurrentBalance(ctx context.Context, userId int64) (int64, error) { - var balance int64 - - account, err := svc.AccountFor(ctx, "current", userId) - if err != nil { - return balance, err - } - err = svc.DB.NewSelect().Table("account_ledgers").ColumnExpr("sum(account_ledgers.amount) as balance").Where("account_ledgers.account_id = ?", account.ID).Scan(context.TODO(), &balance) - return balance, err -} - -func (svc *LndhubService) AccountFor(ctx context.Context, accountType string, userId int64) (models.Account, error) { - account := models.Account{} - err := svc.DB.NewSelect().Model(&account).Where("user_id = ? AND type= ?", userId, accountType).Limit(1).Scan(ctx) - return account, err -} diff --git a/lib/service/service.go b/lib/service/service.go index 74e6af7..26bd3bf 100644 --- a/lib/service/service.go +++ b/lib/service/service.go @@ -2,12 +2,9 @@ package service import ( "context" - "database/sql" "fmt" - "math/rand" "github.com/getAlby/lndhub.go/db/models" - "github.com/getAlby/lndhub.go/lib/security" "github.com/getAlby/lndhub.go/lib/tokens" "github.com/labstack/gommon/random" "github.com/lightningnetwork/lnd/lnrpc" @@ -62,42 +59,3 @@ func (svc *LndhubService) GenerateToken(login, password, inRefreshToken string) } return accessToken, refreshToken, nil } -func (svc *LndhubService) CreateUser() (user *models.User, err error) { - - user = &models.User{} - - // generate user login/password (TODO: allow the user to choose a login/password?) - user.Login = randStringBytes(8) - password := randStringBytes(15) - // we only store the hashed password but return the initial plain text password in the HTTP response - hashedPassword := security.HashPassword(password) - user.Password = hashedPassword - - // Create user and the user's accounts - // We use double-entry bookkeeping so we use 4 accounts: incoming, current, outgoing and fees - // Wrapping this in a transaction in case something fails - err = svc.DB.RunInTx(context.TODO(), &sql.TxOptions{}, func(ctx context.Context, tx bun.Tx) error { - if _, err := tx.NewInsert().Model(user).Exec(ctx); err != nil { - return err - } - accountTypes := []string{"incoming", "current", "outgoing", "fees"} - for _, accountType := range accountTypes { - account := models.Account{UserID: user.ID, Type: accountType} - if _, err := tx.NewInsert().Model(&account).Exec(ctx); err != nil { - return err - } - } - return nil - }) - //return actual password in the response, not the hashed one - user.Password = password - return user, err -} - -func randStringBytes(n int) string { - b := make([]byte, n) - for i := range b { - b[i] = alphaNumBytes[rand.Intn(len(alphaNumBytes))] - } - return string(b) -} diff --git a/lib/service/user.go b/lib/service/user.go new file mode 100644 index 0000000..a229360 --- /dev/null +++ b/lib/service/user.go @@ -0,0 +1,78 @@ +package service + +import ( + "context" + "database/sql" + "math/rand" + + "github.com/getAlby/lndhub.go/db/models" + "github.com/getAlby/lndhub.go/lib/security" + "github.com/uptrace/bun" +) + +func (svc *LndhubService) CreateUser() (user *models.User, err error) { + + user = &models.User{} + + // generate user login/password (TODO: allow the user to choose a login/password?) + user.Login = randStringBytes(8) + password := randStringBytes(15) + // we only store the hashed password but return the initial plain text password in the HTTP response + hashedPassword := security.HashPassword(password) + user.Password = hashedPassword + + // Create user and the user's accounts + // We use double-entry bookkeeping so we use 4 accounts: incoming, current, outgoing and fees + // Wrapping this in a transaction in case something fails + err = svc.DB.RunInTx(context.TODO(), &sql.TxOptions{}, func(ctx context.Context, tx bun.Tx) error { + if _, err := tx.NewInsert().Model(user).Exec(ctx); err != nil { + return err + } + accountTypes := []string{"incoming", "current", "outgoing", "fees"} + for _, accountType := range accountTypes { + account := models.Account{UserID: user.ID, Type: accountType} + if _, err := tx.NewInsert().Model(&account).Exec(ctx); err != nil { + return err + } + } + return nil + }) + //return actual password in the response, not the hashed one + user.Password = password + return user, err +} + +func (svc *LndhubService) FindUser(ctx context.Context, userId int64) (*models.User, error) { + var user models.User + + err := svc.DB.NewSelect().Model(&user).Where("id = ?", userId).Limit(1).Scan(ctx) + if err != nil { + return &user, err + } + return &user, nil +} + +func (svc *LndhubService) CurrentUserBalance(ctx context.Context, userId int64) (int64, error) { + var balance int64 + + account, err := svc.AccountFor(ctx, "current", userId) + if err != nil { + return balance, err + } + err = svc.DB.NewSelect().Table("account_ledgers").ColumnExpr("sum(account_ledgers.amount) as balance").Where("account_ledgers.account_id = ?", account.ID).Scan(context.TODO(), &balance) + return balance, err +} + +func (svc *LndhubService) AccountFor(ctx context.Context, accountType string, userId int64) (models.Account, error) { + account := models.Account{} + err := svc.DB.NewSelect().Model(&account).Where("user_id = ? AND type= ?", userId, accountType).Limit(1).Scan(ctx) + return account, err +} + +func randStringBytes(n int) string { + b := make([]byte, n) + for i := range b { + b[i] = alphaNumBytes[rand.Intn(len(alphaNumBytes))] + } + return string(b) +} diff --git a/lib/tokens/jwt.go b/lib/tokens/jwt.go index 61b4ba1..e9405e0 100644 --- a/lib/tokens/jwt.go +++ b/lib/tokens/jwt.go @@ -34,7 +34,6 @@ func Middleware(secret []byte) echo.MiddlewareFunc { token := c.Get("UserJwt").(*jwt.Token) claims := token.Claims.(*jwtCustomClaims) c.Set("UserID", claims.ID) - } return middleware.JWTWithConfig(config)