feat: init api, db, app

This commit is contained in:
2024-11-07 19:07:41 +00:00
parent ab9b3368d1
commit 11b063c2fa
43 changed files with 1487 additions and 137 deletions
+50
View File
@@ -0,0 +1,50 @@
package auth
import (
"gorm.io/gorm"
"rul.sh/vaulterm/db"
"rul.sh/vaulterm/lib"
"rul.sh/vaulterm/models"
)
type Auth struct{ db *gorm.DB }
func NewAuthRepository() *Auth {
return &Auth{db: db.Get()}
}
func (r *Auth) FindUser(username string) (*models.User, error) {
var user models.User
ret := r.db.Where("username = ? OR email = ?", username, username).First(&user)
return &user, ret.Error
}
func (r *Auth) CreateUserSession(user *models.User) (string, error) {
sessionId, err := lib.GenerateSessionID(20)
if err != nil {
return "", err
}
if ret := r.db.Create(&models.UserSession{ID: sessionId, UserID: user.ID}); ret.Error != nil {
return "", ret.Error
}
return sessionId, nil
}
func (r *Auth) GetSession(sessionId string) (*models.UserSession, error) {
var session models.UserSession
res := r.db.Joins("User").Where(&models.UserSession{ID: sessionId}).First(&session)
return &session, res.Error
}
func (r *Auth) RemoveUserSession(sessionId string, force bool) error {
db := r.db
if force {
db = db.Unscoped()
}
res := db.Delete(&models.UserSession{ID: sessionId})
return res.Error
}
+93
View File
@@ -0,0 +1,93 @@
package auth
import (
"strings"
"github.com/gofiber/fiber/v2"
"rul.sh/vaulterm/lib"
"rul.sh/vaulterm/utils"
)
func Router(app *fiber.App) {
router := app.Group("/auth")
router.Post("/login", login)
router.Get("/user", getUser)
router.Post("/logout", logout)
}
func login(c *fiber.Ctx) error {
repo := NewAuthRepository()
var body LoginSchema
if err := c.BodyParser(&body); err != nil {
return &fiber.Error{
Code: fiber.StatusBadRequest,
Message: err.Error(),
}
}
user, err := repo.FindUser(body.Username)
if err != nil {
return &fiber.Error{
Code: fiber.StatusUnauthorized,
Message: "Username or password is invalid",
}
}
if valid := lib.VerifyPassword(body.Password, user.Password); !valid {
return &fiber.Error{
Code: fiber.StatusUnauthorized,
Message: "Username or password is invalid",
}
}
sessionId, err := repo.CreateUserSession(user)
if err != nil {
return utils.ResponseError(c, err, 500)
}
return c.JSON(fiber.Map{
"user": user,
"sessionId": sessionId,
})
}
func getUser(c *fiber.Ctx) error {
auth := c.Get("Authorization")
var sessionId string
if auth != "" {
sessionId = strings.Split(auth, " ")[1]
}
repo := NewAuthRepository()
session, err := repo.GetSession(sessionId)
if err != nil {
return utils.ResponseError(c, err, 500)
}
return c.JSON(session)
}
func logout(c *fiber.Ctx) error {
auth := c.Get("Authorization")
force := c.Query("force")
var sessionId string
if auth != "" {
sessionId = strings.Split(auth, " ")[1]
}
repo := NewAuthRepository()
err := repo.RemoveUserSession(sessionId, force == "true")
if err != nil {
return utils.ResponseError(c, err, 500)
}
return c.JSON(fiber.Map{
"status": "ok",
"message": "Successfully logged out",
})
}
+6
View File
@@ -0,0 +1,6 @@
package auth
type LoginSchema struct {
Username string `json:"username"`
Password string `json:"password"`
}