We got some auth going
This commit is contained in:
@@ -33,3 +33,6 @@ test:
|
||||
|
||||
build_db_image:
|
||||
make -C ./pgImage build
|
||||
|
||||
my-test:
|
||||
@echo hello
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package api
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
const (
|
||||
APIPath = "/api/v1/"
|
||||
)
|
||||
|
||||
func APILoginHandler(c *gin.Context) {
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
exp "expenses/expenses"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func getAllCategories(c *gin.Context) {
|
||||
// TODO
|
||||
// What user is logged in?
|
||||
ctx := context.Background()
|
||||
|
||||
// TODO - only get the categories of the user
|
||||
// create roles tho, so an admin or someone can get way more categories
|
||||
categories, err := exp.App.GetAllCategories(ctx)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"categories": categories})
|
||||
}
|
||||
|
||||
func RouteCategories(router *gin.RouterGroup) {
|
||||
router.GET("categories", getAllCategories)
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
exp "expenses/expenses"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func getAllExpenses(c *gin.Context) {
|
||||
ctx := context.Background()
|
||||
// TODO - only get the expenses of the user
|
||||
// create roles tho, so an admin or someone can get way more expenses
|
||||
expenses, err := exp.App.GetAllExpenses(ctx)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"expenses": expenses})
|
||||
}
|
||||
|
||||
func RouteExpenses(router *gin.RouterGroup) {
|
||||
router.GET("expenses", getAllExpenses)
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
exp "expenses/expenses"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func getAllStores(c *gin.Context) {
|
||||
// TODO - we need to set the current user from the JWT token we got
|
||||
ctx := context.Background()
|
||||
|
||||
// TODO - only get the stores of the user
|
||||
// create roles tho, so an admin or someone can get way more stores
|
||||
stores, err := exp.App.GetAllStores(ctx)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"users": stores})
|
||||
}
|
||||
|
||||
func RouteStores(router *gin.RouterGroup) {
|
||||
router.GET("stores", getAllStores)
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
exp "expenses/expenses"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func getAllTypes(c *gin.Context) {
|
||||
// TODO - we need to set the current user from the JWT token we got
|
||||
ctx := context.Background()
|
||||
|
||||
// TODO - only get the types of the user
|
||||
// create roles tho, so an admin or someone can get way more types
|
||||
types, err := exp.App.GetAllTypes(ctx)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"users": types})
|
||||
}
|
||||
|
||||
func RouteTypes(router *gin.RouterGroup) {
|
||||
router.GET("types", getAllTypes)
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
exp "expenses/expenses"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func getAllUsers(c *gin.Context) {
|
||||
// TODO - stuff
|
||||
ctx := context.Background()
|
||||
|
||||
// TODO - only get the users of the user
|
||||
// create roles tho, so an admin or someone can get way more users
|
||||
users, err := exp.App.GetAllUsers(ctx)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"users": users})
|
||||
}
|
||||
|
||||
func RouteUsers(router *gin.RouterGroup) {
|
||||
router.GET("users", getAllUsers)
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
expauth "expenses/expenses/auth"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type TokenData struct {
|
||||
Token string `json:"JWTToken"`
|
||||
}
|
||||
|
||||
func APIAuthMiddleWare() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
auth := c.GetHeader("Authorization")
|
||||
if !strings.HasPrefix(auth, "Bearer ") {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing token"})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
tokenStr := strings.TrimPrefix(auth, "Bearer ")
|
||||
validateToken, err := expauth.ValidateToken(tokenStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
if !validateToken {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "token is not valid"})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
exp "expenses/expenses"
|
||||
"expenses/expenses/auth"
|
||||
"github.com/gin-gonic/gin"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type LoginData struct {
|
||||
Name string `json:"username"`
|
||||
Pass string `json:"password"`
|
||||
}
|
||||
|
||||
func login(c *gin.Context) {
|
||||
var loginData LoginData
|
||||
err := c.ShouldBindJSON(&loginData)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "bad request - " + err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
log.Println("user:", loginData.Name)
|
||||
log.Println("pass:", loginData.Pass)
|
||||
|
||||
user, err := exp.App.ValidateCredentials(loginData.Name, loginData.Pass)
|
||||
if err != nil {
|
||||
log.Println("Error validating user: ", err)
|
||||
// TODO - go to an error page or login failed or something
|
||||
return
|
||||
}
|
||||
|
||||
token, err := auth.GenerateToken(user.UserName)
|
||||
if err != nil {
|
||||
log.Println("Error generating token: ", err)
|
||||
// TODO - get error return
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"token": token})
|
||||
}
|
||||
|
||||
func RouteLogin(eng *gin.Engine) {
|
||||
eng.POST("login", login)
|
||||
}
|
||||
@@ -135,7 +135,7 @@
|
||||
|| evt.detail.xhr.responseType === "text") {
|
||||
try {
|
||||
const data = JSON.parse(evt.detail.xhr.responseText);
|
||||
console.log(data);
|
||||
console.log("Data: " + data);
|
||||
|
||||
document.getElementById("expense-date").value = data["date"];
|
||||
document.getElementById("expense-value").value = data["total"];
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"expenses/api"
|
||||
"expenses/config"
|
||||
"expenses/expenses/auth"
|
||||
"expenses/pages"
|
||||
@@ -67,6 +68,14 @@ func setRoutes(router *gin.RouterGroup, eng *gin.Engine) {
|
||||
pages.RouteServerError(eng)
|
||||
}
|
||||
|
||||
func setAPI(router *gin.RouterGroup) {
|
||||
api.RouteExpenses(router)
|
||||
api.RouteUsers(router)
|
||||
api.RouteStores(router)
|
||||
api.RouteTypes(router)
|
||||
api.RouteCategories(router)
|
||||
}
|
||||
|
||||
func startWebserver() {
|
||||
cfg := config.GetInstance()
|
||||
|
||||
@@ -83,6 +92,14 @@ func startWebserver() {
|
||||
setRoutes(authGroup, router)
|
||||
}
|
||||
|
||||
// TODO - this also needs authentication
|
||||
apiGroup := router.Group(api.APIPath)
|
||||
{
|
||||
apiAuth := apiGroup.Group("/")
|
||||
apiAuth.Use(api.APIAuthMiddleWare())
|
||||
setAPI(apiAuth)
|
||||
}
|
||||
|
||||
err := router.Run(":" + cfg.Port)
|
||||
if err != nil {
|
||||
log.Printf("Failed to launch application: \n %s\n", err)
|
||||
|
||||
@@ -23,6 +23,7 @@ func AuthMiddleware() gin.HandlerFunc {
|
||||
uid := session.Get("user_id")
|
||||
log.Println("UID is: ", uid)
|
||||
if uid != nil {
|
||||
log.Println("Got UUID: ", uid)
|
||||
user, err = exp.App.GetUser(uid.(int32))
|
||||
log.Println("Got User: ", user)
|
||||
if err != nil {
|
||||
@@ -31,15 +32,6 @@ func AuthMiddleware() gin.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// Check if we have an authorization header
|
||||
// if user == nil {
|
||||
// authHeader := c.GetHeader("Authorization")
|
||||
// if strings.HasPrefix(authHeader, "Bearer ") {
|
||||
// tokenStr := strings.TrimPrefix(authHeader, "Bearer ")
|
||||
// claims, err := validateJWT(tokenStr)
|
||||
// }
|
||||
// }
|
||||
|
||||
if user == nil {
|
||||
log.Println("No User: ", user)
|
||||
c.Redirect(http.StatusTemporaryRedirect, "/login")
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
// TODO - also allow asymetrical keys to be used
|
||||
func GenerateToken(sub string) (string, error) {
|
||||
// TODO - check if we can or should use JTI
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||
"iss": "expenseswebapp",
|
||||
"sub": sub,
|
||||
"aud": sub,
|
||||
// "nbf": time.Now().UTC(),
|
||||
"iat": time.Now().UTC(),
|
||||
"exp": time.Now().Add(15 * time.Minute).UTC(),
|
||||
})
|
||||
|
||||
tokenStr, err := token.SignedString([]byte("any?"))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return tokenStr, nil
|
||||
}
|
||||
|
||||
func ValidateToken(tStr string) (bool, error) {
|
||||
token, err := jwt.Parse(tStr, func(token *jwt.Token) (any, error) {
|
||||
return []byte("any?"), nil
|
||||
}, jwt.WithValidMethods([]string{jwt.SigningMethodHS256.Alg()}))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if claims, ok := token.Claims.(jwt.MapClaims); ok {
|
||||
log.Println(claims)
|
||||
return true, nil
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
@@ -5,8 +5,7 @@ import (
|
||||
mod "expenses/expenses/models"
|
||||
)
|
||||
|
||||
func (a *ExpensesApp) GetAllCategories() ([]mod.Category, error) {
|
||||
ctx := context.Background()
|
||||
func (a *ExpensesApp) GetAllCategories(ctx context.Context) ([]mod.Category, error) {
|
||||
return a.CategoryRepo.GetAll(ctx)
|
||||
}
|
||||
|
||||
|
||||
@@ -8,10 +8,10 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func (a *ExpensesApp) GetAllExpenses() ([]mod.Expense, error) {
|
||||
ctx := context.Background()
|
||||
func (a *ExpensesApp) GetAllExpenses(ctx context.Context) ([]mod.Expense, error) {
|
||||
user := *ctx.Value("user").(*mod.User)
|
||||
|
||||
expenses, err := a.ExpRepo.GetAll(ctx)
|
||||
expenses, err := a.ExpRepo.GetAll(ctx, user.UserID)
|
||||
if err != nil {
|
||||
return []mod.Expense{}, err
|
||||
}
|
||||
@@ -19,7 +19,8 @@ func (a *ExpensesApp) GetAllExpenses() ([]mod.Expense, error) {
|
||||
return expenses, nil
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) GetExpensesRanged(startDate string, endDate string) ([]mod.Expense, error) {
|
||||
func (a *ExpensesApp) GetExpensesRanged(
|
||||
ctx context.Context, startDate string, endDate string) ([]mod.Expense, error) {
|
||||
startDateTime, err := time.ParseInLocation("02-Jan-2006 15:04:05", startDate, time.UTC)
|
||||
if err != nil {
|
||||
log.Printf("error startDate: %v", err)
|
||||
@@ -32,9 +33,8 @@ func (a *ExpensesApp) GetExpensesRanged(startDate string, endDate string) ([]mod
|
||||
return []mod.Expense{}, nil
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
return a.ExpRepo.GetExpensesRange(ctx, startDateTime, endDateTime)
|
||||
user := *ctx.Value("user").(*mod.User)
|
||||
return a.ExpRepo.GetExpensesRange(ctx, startDateTime, endDateTime, user.UserID)
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) GetExpense(id int32) (mod.Expense, error) {
|
||||
@@ -137,9 +137,7 @@ func (a *ExpensesApp) analyzeExpense(e *mod.Expense) {
|
||||
a.NormalizeShares(e)
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) NewExpense(exp mod.Expense) error {
|
||||
ctx := context.Background()
|
||||
|
||||
func (a *ExpensesApp) NewExpense(ctx context.Context, exp mod.Expense) error {
|
||||
a.analyzeExpense(&exp)
|
||||
|
||||
return a.ExpRepo.Insert(ctx, exp)
|
||||
|
||||
@@ -23,7 +23,8 @@ func (a *ExpensesApp) prepareExpense(exp *mod.Expense) error {
|
||||
|
||||
func (a *ExpensesApp) luaGetAllExpenses(L *lua.LState) int {
|
||||
ctx := context.Background()
|
||||
expenses, err := a.ExpRepo.GetAll(ctx)
|
||||
// TODO - what do we do about the LUA API
|
||||
expenses, err := a.ExpRepo.GetAll(ctx, 1)
|
||||
|
||||
tbl := L.NewTable()
|
||||
for _, e := range expenses {
|
||||
|
||||
@@ -9,9 +9,9 @@ import (
|
||||
type ExpenseRepository interface {
|
||||
// Direct expense methods
|
||||
Get(ctx context.Context, id int32) (mod.Expense, error)
|
||||
GetAll(ctx context.Context) (mod.Expenses, error)
|
||||
GetAll(ctx context.Context, uId int32) (mod.Expenses, error)
|
||||
GetExpensesRange(ctx context.Context, start time.Time,
|
||||
end time.Time) (mod.Expenses, error)
|
||||
end time.Time, uId int32) (mod.Expenses, error)
|
||||
Update(ctx context.Context, exp mod.Expense) error
|
||||
Insert(ctx context.Context, exp mod.Expense) error
|
||||
Delete(ctx context.Context, id int32) error
|
||||
|
||||
@@ -45,7 +45,8 @@ func withTx(
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (p *PgExpRepo) getAll(ctx context.Context, q *pgsqlc.Queries) (mod.Expenses, error) {
|
||||
func (p *PgExpRepo) getAll(
|
||||
ctx context.Context, q *pgsqlc.Queries, uId int32) (mod.Expenses, error) {
|
||||
// We want to select all here, no timestamps
|
||||
var start pgtype.Timestamp
|
||||
start.Valid = false
|
||||
@@ -55,6 +56,7 @@ func (p *PgExpRepo) getAll(ctx context.Context, q *pgsqlc.Queries) (mod.Expenses
|
||||
expenses, err := q.GetExpenses(ctx, pgsqlc.GetExpensesParams{
|
||||
Startdate: start,
|
||||
Enddate: end,
|
||||
UserID: uId,
|
||||
})
|
||||
if err != nil {
|
||||
return []mod.Expense{}, err
|
||||
@@ -63,8 +65,8 @@ func (p *PgExpRepo) getAll(ctx context.Context, q *pgsqlc.Queries) (mod.Expenses
|
||||
return mapRepoGetExpensesRows(expenses), err
|
||||
}
|
||||
|
||||
func (p PgExpRepo) GetAll(ctx context.Context) (mod.Expenses, error) {
|
||||
return p.getAll(ctx, pgsqlc.New(p.DB))
|
||||
func (p PgExpRepo) GetAll(ctx context.Context, uId int32) (mod.Expenses, error) {
|
||||
return p.getAll(ctx, pgsqlc.New(p.DB), uId)
|
||||
}
|
||||
|
||||
func (p *PgExpRepo) get(ctx context.Context, q *pgsqlc.Queries, id int32) (mod.Expense, error) {
|
||||
@@ -237,7 +239,10 @@ func (p PgExpRepo) Delete(ctx context.Context, id int32) error {
|
||||
}
|
||||
|
||||
func (p PgExpRepo) GetExpensesRange(
|
||||
ctx context.Context, start time.Time, end time.Time) (mod.Expenses, error) {
|
||||
ctx context.Context,
|
||||
start time.Time,
|
||||
end time.Time,
|
||||
uId int32) (mod.Expenses, error) {
|
||||
|
||||
startPg, err := timeToTimestamp(start)
|
||||
if err != nil {
|
||||
@@ -253,6 +258,7 @@ func (p PgExpRepo) GetExpensesRange(
|
||||
expenses, err := queries.GetExpenses(ctx, pgsqlc.GetExpensesParams{
|
||||
Startdate: startPg,
|
||||
Enddate: endPg,
|
||||
UserID: uId,
|
||||
})
|
||||
if err != nil {
|
||||
return mod.Expenses{}, err
|
||||
|
||||
@@ -101,12 +101,15 @@ JOIN
|
||||
JOIN
|
||||
"expenseTypes" as types ON types."TypeID" = expenses."TypeID"
|
||||
WHERE
|
||||
($1::timestamp IS NULL OR expenses."ExpDate" >= $1::timestamp)
|
||||
expenses."ExpID" IN (SELECT "ExpID" from "expensesShares" WHERE "expensesShares"."UserID" = $1)
|
||||
AND
|
||||
($2::timestamp IS NULL OR expenses."ExpDate" >= $2::timestamp)
|
||||
AND
|
||||
($2::timestamp IS NULL OR expenses."ExpDate" <= $2::timestamp)
|
||||
($3::timestamp IS NULL OR expenses."ExpDate" <= $3::timestamp)
|
||||
`
|
||||
|
||||
type GetExpensesParams struct {
|
||||
UserID int32
|
||||
Startdate pgtype.Timestamp
|
||||
Enddate pgtype.Timestamp
|
||||
}
|
||||
@@ -120,7 +123,7 @@ type GetExpensesRow struct {
|
||||
}
|
||||
|
||||
func (q *Queries) GetExpenses(ctx context.Context, arg GetExpensesParams) ([]GetExpensesRow, error) {
|
||||
rows, err := q.db.Query(ctx, getExpenses, arg.Startdate, arg.Enddate)
|
||||
rows, err := q.db.Query(ctx, getExpenses, arg.UserID, arg.Startdate, arg.Enddate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@ JOIN
|
||||
JOIN
|
||||
"expenseTypes" as types ON types."TypeID" = expenses."TypeID"
|
||||
WHERE
|
||||
expenses."ExpID" IN (SELECT "ExpID" from "expensesShares" WHERE "expensesShares"."UserID" = $1)
|
||||
AND
|
||||
(sqlc.narg(StartDate)::timestamp IS NULL OR expenses."ExpDate" >= sqlc.narg(StartDate)::timestamp)
|
||||
AND
|
||||
(sqlc.narg(EndDate)::timestamp IS NULL OR expenses."ExpDate" <= sqlc.narg(EndDate)::timestamp);
|
||||
|
||||
@@ -5,8 +5,7 @@ import (
|
||||
mod "expenses/expenses/models"
|
||||
)
|
||||
|
||||
func (a *ExpensesApp) GetAllStores() ([]mod.Store, error) {
|
||||
ctx := context.Background()
|
||||
func (a *ExpensesApp) GetAllStores(ctx context.Context) ([]mod.Store, error) {
|
||||
return a.StoreRepo.GetAll(ctx)
|
||||
}
|
||||
|
||||
|
||||
@@ -5,8 +5,7 @@ import (
|
||||
mod "expenses/expenses/models"
|
||||
)
|
||||
|
||||
func (a *ExpensesApp) GetAllTypes() (mod.Types, error) {
|
||||
ctx := context.Background()
|
||||
func (a *ExpensesApp) GetAllTypes(ctx context.Context) (mod.Types, error) {
|
||||
return a.TypeRepo.GetAll(ctx)
|
||||
}
|
||||
|
||||
|
||||
+18
-2
@@ -3,6 +3,7 @@ package expenses
|
||||
import (
|
||||
"context"
|
||||
mod "expenses/expenses/models"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
func (a *ExpensesApp) GetUser(id int32) (*mod.User, error) {
|
||||
@@ -15,7 +16,22 @@ func (a *ExpensesApp) GetUserByName(name string) (*mod.User, error) {
|
||||
return a.UserRepo.GetByName(ctx, name)
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) GetAllUsers() ([]mod.User, error) {
|
||||
ctx := context.Background()
|
||||
func (a *ExpensesApp) GetAllUsers(ctx context.Context) ([]mod.User, error) {
|
||||
return a.UserRepo.GetAll(ctx)
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) ValidateCredentials(name string, pass string) (*mod.User, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
user, err := a.UserRepo.GetByName(ctx, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(pass))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
+2
-1
@@ -6,12 +6,14 @@ require (
|
||||
github.com/ESilva15/gofatqr v1.0.0
|
||||
github.com/gin-contrib/sessions v1.0.4
|
||||
github.com/gin-gonic/gin v1.10.1
|
||||
github.com/golang-jwt/jwt/v5 v5.3.0
|
||||
github.com/golang-migrate/migrate/v4 v4.18.3
|
||||
github.com/jackc/pgx/v5 v5.5.4
|
||||
github.com/layeh/gopher-json v0.0.0-20201124131017-552bb3c4c3bf
|
||||
github.com/shopspring/decimal v1.4.0
|
||||
github.com/spf13/cobra v1.9.1
|
||||
github.com/yuin/gopher-lua v1.1.1
|
||||
golang.org/x/crypto v0.37.0
|
||||
gopkg.in/yaml.v2 v2.4.0
|
||||
)
|
||||
|
||||
@@ -47,7 +49,6 @@ require (
|
||||
github.com/ugorji/go/codec v1.2.12 // indirect
|
||||
go.uber.org/atomic v1.7.0 // indirect
|
||||
golang.org/x/arch v0.16.0 // indirect
|
||||
golang.org/x/crypto v0.37.0 // indirect
|
||||
golang.org/x/net v0.38.0 // indirect
|
||||
golang.org/x/sync v0.13.0 // indirect
|
||||
golang.org/x/sys v0.32.0 // indirect
|
||||
|
||||
@@ -52,6 +52,8 @@ github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
|
||||
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
|
||||
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
|
||||
github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo=
|
||||
github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
|
||||
github.com/golang-migrate/migrate/v4 v4.18.3 h1:EYGkoOsvgHHfm5U/naS1RP/6PL/Xv3S4B/swMiAmDLs=
|
||||
github.com/golang-migrate/migrate/v4 v4.18.3/go.mod h1:99BKpIi6ruaaXRM1A77eqZ+FWPQ3cfRa+ZVy5bmWMaY=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
|
||||
@@ -1,12 +1,23 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"expenses/api"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func NotFoundView(c *gin.Context, msg string) {
|
||||
if strings.HasPrefix(c.Request.URL.Path, api.APIPath) {
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
"error": "not_found",
|
||||
"message": "endpoint doesn't exist",
|
||||
"path": c.Param("path"),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.HTML(404, "terminal", gin.H{
|
||||
"warning": true,
|
||||
"renderNavBar": false,
|
||||
|
||||
+11
-2
@@ -2,11 +2,13 @@ package pages
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
exp "expenses/expenses"
|
||||
experr "expenses/expenses/errors"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@@ -15,7 +17,14 @@ const (
|
||||
)
|
||||
|
||||
func categoriesGlobalPage(c *gin.Context) {
|
||||
categories, err := exp.App.GetAllCategories()
|
||||
ctx, err := getLoggedInUserCTX(c)
|
||||
if err != nil {
|
||||
log.Println("failed to fetch logged in user -", err.Error())
|
||||
ServerErrorView(c, "The server too makes mistakes")
|
||||
return
|
||||
}
|
||||
|
||||
categories, err := exp.App.GetAllCategories(ctx)
|
||||
if err != nil {
|
||||
ServerErrorView(c, "The server too makes mistakes")
|
||||
return
|
||||
|
||||
+63
-23
@@ -6,10 +6,11 @@ import (
|
||||
experr "expenses/expenses/errors"
|
||||
mod "expenses/expenses/models"
|
||||
"fmt"
|
||||
fatqr "github.com/ESilva15/gofatqr"
|
||||
"github.com/gin-gonic/gin"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
fatqr "github.com/ESilva15/gofatqr"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -17,7 +18,13 @@ const (
|
||||
)
|
||||
|
||||
func expensesPartial(c *gin.Context) {
|
||||
expenses, err := exp.App.GetAllExpenses()
|
||||
ctx, err := getLoggedInUserCTX(c)
|
||||
if err != nil {
|
||||
ServerErrorView(c, "Could not get logged in user")
|
||||
return
|
||||
}
|
||||
|
||||
expenses, err := exp.App.GetAllExpenses(ctx)
|
||||
if err != nil {
|
||||
c.Header("HX-Redirect", "/500")
|
||||
return
|
||||
@@ -29,7 +36,13 @@ func expensesPartial(c *gin.Context) {
|
||||
}
|
||||
|
||||
func ExpensesGlobalPage(c *gin.Context) {
|
||||
expenses, err := exp.App.GetAllExpenses()
|
||||
ctx, err := getLoggedInUserCTX(c)
|
||||
if err != nil {
|
||||
ServerErrorView(c, "Could not get logged in user")
|
||||
return
|
||||
}
|
||||
|
||||
expenses, err := exp.App.GetAllExpenses(ctx)
|
||||
if err != nil {
|
||||
log.Panicln("error:", err)
|
||||
c.Header("HX-Redirect", "/500")
|
||||
@@ -45,6 +58,12 @@ func ExpensesGlobalPage(c *gin.Context) {
|
||||
}
|
||||
|
||||
func expensePage(c *gin.Context) {
|
||||
ctx, err := getLoggedInUserCTX(c)
|
||||
if err != nil {
|
||||
ServerErrorView(c, "Could not get logged in user")
|
||||
return
|
||||
}
|
||||
|
||||
expenseID, err := exp.ParseID(c.Param("id"))
|
||||
if err != nil {
|
||||
ServerErrorView(c, "failed to parse requested expenses id")
|
||||
@@ -77,36 +96,30 @@ func expensePage(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
categories, err := exp.App.GetAllCategories()
|
||||
categories, err := exp.App.GetAllCategories(ctx)
|
||||
if err != nil {
|
||||
ServerErrorView(c, "failed to fetch categories")
|
||||
return
|
||||
}
|
||||
|
||||
stores, err := exp.App.GetAllStores()
|
||||
stores, err := exp.App.GetAllStores(ctx)
|
||||
if err != nil {
|
||||
ServerErrorView(c, "failed to fetch stores")
|
||||
return
|
||||
}
|
||||
|
||||
types, err := exp.App.GetAllTypes()
|
||||
types, err := exp.App.GetAllTypes(ctx)
|
||||
if err != nil {
|
||||
ServerErrorView(c, "failed to fetch types")
|
||||
return
|
||||
}
|
||||
|
||||
users, err := exp.App.GetAllUsers()
|
||||
users, err := exp.App.GetAllUsers(ctx)
|
||||
if err != nil {
|
||||
ServerErrorView(c, "failed to fetch users")
|
||||
return
|
||||
}
|
||||
|
||||
// summary, err := expense.GetSummary()
|
||||
// if err != nil {
|
||||
// ServerErrorView(c, "failed to get summary")
|
||||
// return
|
||||
// }
|
||||
|
||||
c.HTML(http.StatusOK, "terminal", gin.H{
|
||||
"page": "expense",
|
||||
"renderNavBar": false,
|
||||
@@ -122,25 +135,32 @@ func expensePage(c *gin.Context) {
|
||||
}
|
||||
|
||||
func newExpensePage(c *gin.Context) {
|
||||
categories, err := exp.App.GetAllCategories()
|
||||
ctx, err := getLoggedInUserCTX(c)
|
||||
if err != nil {
|
||||
log.Println("failed to fetch logged in user -", err.Error())
|
||||
ServerErrorView(c, "The server too makes mistakes")
|
||||
return
|
||||
}
|
||||
|
||||
categories, err := exp.App.GetAllCategories(ctx)
|
||||
if err != nil {
|
||||
ServerErrorView(c, "failed to fetch categories")
|
||||
return
|
||||
}
|
||||
|
||||
stores, err := exp.App.GetAllStores()
|
||||
stores, err := exp.App.GetAllStores(ctx)
|
||||
if err != nil {
|
||||
ServerErrorView(c, "failed to fetch stores")
|
||||
return
|
||||
}
|
||||
|
||||
types, err := exp.App.GetAllTypes()
|
||||
types, err := exp.App.GetAllTypes(ctx)
|
||||
if err != nil {
|
||||
ServerErrorView(c, "failed to fetch types")
|
||||
return
|
||||
}
|
||||
|
||||
users, err := exp.App.GetAllUsers()
|
||||
users, err := exp.App.GetAllUsers(ctx)
|
||||
if err != nil {
|
||||
ServerErrorView(c, "failed to fetch users")
|
||||
return
|
||||
@@ -160,7 +180,14 @@ func newExpensePage(c *gin.Context) {
|
||||
}
|
||||
|
||||
func createExpense(c *gin.Context) {
|
||||
newExp, err := expenseFromForm(c)
|
||||
ctx, err := getLoggedInUserCTX(c)
|
||||
if err != nil {
|
||||
log.Println("failed to fetch logged in user -", err.Error())
|
||||
ServerErrorView(c, "The server too makes mistakes")
|
||||
return
|
||||
}
|
||||
|
||||
newExp, err := expenseFromForm(c, ctx)
|
||||
if err != nil {
|
||||
// TODO
|
||||
// Change this to something the user can see
|
||||
@@ -168,7 +195,7 @@ func createExpense(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
err = exp.App.NewExpense(*newExp)
|
||||
err = exp.App.NewExpense(ctx, *newExp)
|
||||
if err != nil {
|
||||
c.Header("HX-Trigger", fmt.Sprintf("{\"formState\":\"%s\"}", err.Error()))
|
||||
return
|
||||
@@ -178,13 +205,20 @@ func createExpense(c *gin.Context) {
|
||||
}
|
||||
|
||||
func updateExpense(c *gin.Context) {
|
||||
ctx, err := getLoggedInUserCTX(c)
|
||||
if err != nil {
|
||||
log.Println("failed to fetch logged in user -", err.Error())
|
||||
ServerErrorView(c, "The server too makes mistakes")
|
||||
return
|
||||
}
|
||||
|
||||
expenseID, err := exp.ParseID(c.Param("id"))
|
||||
if err != nil {
|
||||
c.Header("HX-Redirect", "/404")
|
||||
return
|
||||
}
|
||||
|
||||
newExp, err := expenseFromForm(c)
|
||||
newExp, err := expenseFromForm(c, ctx)
|
||||
if err != nil {
|
||||
// TODO
|
||||
// Change this to something the user can see
|
||||
@@ -235,14 +269,20 @@ func qrRequest(c *gin.Context) {
|
||||
var fat fatqr.FatQR
|
||||
err := fat.Scan(qr, 0)
|
||||
if err != nil {
|
||||
// TODO
|
||||
// handle the error here for the user
|
||||
log.Println("error scanning QR code:", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// try to find storeID
|
||||
storeID, err := exp.App.GetStoreIDFromNIF(fat.TaxRegistrationNumber)
|
||||
if err != nil {
|
||||
log.Println("error getting storeID:", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"context"
|
||||
"expenses/expenses"
|
||||
mod "expenses/expenses/models"
|
||||
"time"
|
||||
@@ -25,7 +26,7 @@ func processFormPayments(c *gin.Context) ([]mod.Payment, error) {
|
||||
return expenses.ParseFormPayments(userIDs, paymentIDs, values)
|
||||
}
|
||||
|
||||
func expenseFromForm(c *gin.Context) (*mod.Expense, error) {
|
||||
func expenseFromForm(c *gin.Context, ctx context.Context) (*mod.Expense, error) {
|
||||
newDescription := c.PostForm("expense-desc")
|
||||
newDate := c.PostForm("expense-date")
|
||||
newQR := c.PostForm("expense-qr")
|
||||
@@ -68,7 +69,8 @@ func expenseFromForm(c *gin.Context) (*mod.Expense, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
newExp := mod.Expense{
|
||||
creator := *ctx.Value("user").(*mod.User)
|
||||
return &mod.Expense{
|
||||
Description: newDescription,
|
||||
Date: formattedDate,
|
||||
Value: value,
|
||||
@@ -81,15 +83,10 @@ func expenseFromForm(c *gin.Context) (*mod.Expense, error) {
|
||||
Store: mod.Store{
|
||||
StoreID: storeID,
|
||||
},
|
||||
Owner: mod.User{
|
||||
// TODO
|
||||
// This needs to be dynamic - to be added once we have logins and whatnot
|
||||
UserID: 1,
|
||||
},
|
||||
Owner: creator,
|
||||
CreationDate: time.Now(),
|
||||
Shares: shares,
|
||||
Payments: payments,
|
||||
QRString: newQR,
|
||||
}
|
||||
return &newExp, nil
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func getLoggedInUserCTX(c *gin.Context) (context.Context, error) {
|
||||
if _, ok := c.Get("user"); !ok {
|
||||
return nil, fmt.Errorf("unable to get user from gin context")
|
||||
}
|
||||
|
||||
user, _ := c.Get("user")
|
||||
ctx := context.WithValue(c.Request.Context(), "user", user)
|
||||
|
||||
return ctx, nil
|
||||
}
|
||||
+3
-11
@@ -6,7 +6,6 @@ import (
|
||||
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-gonic/gin"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -25,16 +24,9 @@ func loginUser(c *gin.Context) {
|
||||
username := c.PostForm("login-user")
|
||||
password := c.PostForm("login-pass")
|
||||
|
||||
user, err := exp.App.GetUserByName(username)
|
||||
if err != nil {
|
||||
// TODO - go to an error page here
|
||||
return
|
||||
}
|
||||
|
||||
// Check password
|
||||
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
|
||||
if err != nil {
|
||||
// TODO - wrong password
|
||||
user, err := exp.App.ValidateCredentials(username, password)
|
||||
if err != nil {
|
||||
// TODO - go to an error page or login failed or something
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -34,10 +34,16 @@ func overviewPage(c *gin.Context) {
|
||||
}
|
||||
|
||||
func getResults(c *gin.Context) {
|
||||
ctx, err := getLoggedInUserCTX(c)
|
||||
if err != nil {
|
||||
ServerErrorView(c, "Could not get logged in user")
|
||||
return
|
||||
}
|
||||
|
||||
startDate := c.PostForm("range-start") + " 00:00:00"
|
||||
endDate := c.PostForm("range-end") + " 23:59:59"
|
||||
|
||||
queriedExpenses, err := exp.App.GetExpensesRanged(startDate, endDate)
|
||||
queriedExpenses, err := exp.App.GetExpensesRanged(ctx, startDate, endDate)
|
||||
if err != nil {
|
||||
log.Printf("getting expenses: %v", err)
|
||||
return
|
||||
|
||||
+8
-1
@@ -17,7 +17,14 @@ const (
|
||||
)
|
||||
|
||||
func storesGlobalPage(c *gin.Context) {
|
||||
stores, err := exp.App.GetAllStores()
|
||||
ctx, err := getLoggedInUserCTX(c)
|
||||
if err != nil {
|
||||
log.Println("failed to fetch logged in user -", err.Error())
|
||||
ServerErrorView(c, "The server too makes mistakes")
|
||||
return
|
||||
}
|
||||
|
||||
stores, err := exp.App.GetAllStores(ctx)
|
||||
if err != nil {
|
||||
ServerErrorView(c, "Failed to fetch stores content")
|
||||
return
|
||||
|
||||
+8
-2
@@ -5,8 +5,8 @@ import (
|
||||
exp "expenses/expenses"
|
||||
experr "expenses/expenses/errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -14,7 +14,13 @@ const (
|
||||
)
|
||||
|
||||
func typesGlobalPage(c *gin.Context) {
|
||||
types, err := exp.App.GetAllTypes()
|
||||
ctx, err := getLoggedInUserCTX(c)
|
||||
if err != nil {
|
||||
ServerErrorView(c, "could not get logged in user")
|
||||
return
|
||||
}
|
||||
|
||||
types, err := exp.App.GetAllTypes(ctx)
|
||||
if err != nil {
|
||||
ServerErrorView(c, "the server failed to get the types")
|
||||
return
|
||||
|
||||
+7
-2
@@ -1,3 +1,8 @@
|
||||
registry = dir.esilva.org
|
||||
repo = esilva
|
||||
include .env
|
||||
|
||||
REQUIRED_VARS := registry repo
|
||||
|
||||
$(foreach v,$(REQUIRED_VARS),\
|
||||
$(if $(value $(v)),,$(error Variable '$(v)' is required but empty)))
|
||||
|
||||
tag = v0.0.83-alpha
|
||||
|
||||
Reference in New Issue
Block a user