some cleaning up
This commit is contained in:
parent
e3049dddd9
commit
a6a02d3521
4 changed files with 25 additions and 27 deletions
|
@ -5,7 +5,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"stereo.cat/backend/internal/auth"
|
"stereo.cat/backend/internal/auth/token"
|
||||||
"stereo.cat/backend/internal/types"
|
"stereo.cat/backend/internal/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ func RegisterAuthRoutes(cfg *types.StereoConfig, api *gin.RouterGroup) {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
jwt, err := auth.GenerateJWT(cfg.JWTSecret, user, uint64(time.Now().Add(time.Second*time.Duration(t.ExpiresIn)).Unix()))
|
jwt, err := token.GenerateJWT(cfg.JWTSecret, user, uint64(time.Now().Add(time.Second*time.Duration(t.ExpiresIn)).Unix()))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -47,7 +47,7 @@ func RegisterAuthRoutes(cfg *types.StereoConfig, api *gin.RouterGroup) {
|
||||||
c.Redirect(http.StatusTemporaryRedirect, cfg.FrontendUri+"?jwt_set=true")
|
c.Redirect(http.StatusTemporaryRedirect, cfg.FrontendUri+"?jwt_set=true")
|
||||||
})
|
})
|
||||||
|
|
||||||
api.GET("/auth/me", auth.JwtMiddleware(cfg.JWTSecret), func(c *gin.Context) {
|
api.GET("/auth/me", token.JwtMiddleware(cfg.JWTSecret), func(c *gin.Context) {
|
||||||
claims, _ := c.Get("claims")
|
claims, _ := c.Get("claims")
|
||||||
c.JSON(http.StatusOK, claims)
|
c.JSON(http.StatusOK, claims)
|
||||||
})
|
})
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"github.com/h2non/filetype"
|
"github.com/h2non/filetype"
|
||||||
"github.com/minio/minio-go/v7"
|
"github.com/minio/minio-go/v7"
|
||||||
"stereo.cat/backend/internal/auth"
|
"stereo.cat/backend/internal/auth"
|
||||||
|
"stereo.cat/backend/internal/auth/token"
|
||||||
"stereo.cat/backend/internal/types"
|
"stereo.cat/backend/internal/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -20,7 +21,7 @@ func intoReader(buf []byte) io.Reader {
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterFileRoutes(cfg *types.StereoConfig, api *gin.RouterGroup) {
|
func RegisterFileRoutes(cfg *types.StereoConfig, api *gin.RouterGroup) {
|
||||||
api.POST("/upload", auth.JwtMiddleware(cfg.JWTSecret), func(c *gin.Context) {
|
api.POST("/upload", token.JwtMiddleware(cfg.JWTSecret), func(c *gin.Context) {
|
||||||
claims := c.MustGet("claims").(jwt.MapClaims)
|
claims := c.MustGet("claims").(jwt.MapClaims)
|
||||||
user := claims["user"].(auth.User)
|
user := claims["user"].(auth.User)
|
||||||
|
|
||||||
|
@ -88,7 +89,7 @@ func RegisterFileRoutes(cfg *types.StereoConfig, api *gin.RouterGroup) {
|
||||||
c.JSON(200, gin.H{"message": "file uploaded successfully", "id": fileMeta.ID.String()})
|
c.JSON(200, gin.H{"message": "file uploaded successfully", "id": fileMeta.ID.String()})
|
||||||
})
|
})
|
||||||
|
|
||||||
api.DELETE("/:id", auth.JwtMiddleware(cfg.JWTSecret), func(c *gin.Context) {
|
api.DELETE("/:id", token.JwtMiddleware(cfg.JWTSecret), func(c *gin.Context) {
|
||||||
claims := c.MustGet("claims").(jwt.MapClaims)
|
claims := c.MustGet("claims").(jwt.MapClaims)
|
||||||
user := claims["user"].(auth.User)
|
user := claims["user"].(auth.User)
|
||||||
|
|
||||||
|
@ -100,15 +101,10 @@ func RegisterFileRoutes(cfg *types.StereoConfig, api *gin.RouterGroup) {
|
||||||
|
|
||||||
var file *types.File
|
var file *types.File
|
||||||
|
|
||||||
cfg.Database.First(&file, fileID)
|
err = cfg.Database.First(&file, fileID).Error
|
||||||
|
|
||||||
if file == nil {
|
|
||||||
err := cfg.Database.Delete(&file).Error
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
types.ErrorDatabase.Throw(c, err)
|
types.ErrorFileNotFound.Throw(c, err)
|
||||||
return
|
|
||||||
}
|
|
||||||
types.ErrorFileNotFound.Throw(c, nil)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,7 +158,7 @@ func RegisterFileRoutes(cfg *types.StereoConfig, api *gin.RouterGroup) {
|
||||||
c.DataFromReader(200, file.Size, file.Mime, object, nil)
|
c.DataFromReader(200, file.Size, file.Mime, object, nil)
|
||||||
})
|
})
|
||||||
|
|
||||||
api.GET("/list", auth.JwtMiddleware(cfg.JWTSecret), func(c *gin.Context) {
|
api.GET("/list", token.JwtMiddleware(cfg.JWTSecret), func(c *gin.Context) {
|
||||||
claims := c.MustGet("claims").(jwt.MapClaims)
|
claims := c.MustGet("claims").(jwt.MapClaims)
|
||||||
user := claims["user"].(auth.User)
|
user := claims["user"].(auth.User)
|
||||||
|
|
||||||
|
|
|
@ -1,17 +1,18 @@
|
||||||
package auth
|
package token
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
|
"stereo.cat/backend/internal/auth"
|
||||||
|
"stereo.cat/backend/internal/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GenerateJWT(key string, user User, expiryTimestamp uint64) (string, error) {
|
func GenerateJWT(key string, user auth.User, expiryTimestamp uint64) (string, error) {
|
||||||
claims := Claims{
|
claims := auth.Claims{
|
||||||
User: user,
|
User: user,
|
||||||
Exp: expiryTimestamp,
|
Exp: expiryTimestamp,
|
||||||
}
|
}
|
||||||
|
@ -20,10 +21,6 @@ func GenerateJWT(key string, user User, expiryTimestamp uint64) (string, error)
|
||||||
return token.SignedString([]byte(key))
|
return token.SignedString([]byte(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
func invalidAuth(c *gin.Context) {
|
|
||||||
c.String(http.StatusUnauthorized, "Unauthorized.")
|
|
||||||
c.Abort()
|
|
||||||
}
|
|
||||||
|
|
||||||
func JwtMiddleware(secret string) gin.HandlerFunc {
|
func JwtMiddleware(secret string) gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
|
@ -33,7 +30,7 @@ func JwtMiddleware(secret string) gin.HandlerFunc {
|
||||||
jwtSplit := strings.Split(c.GetHeader("Authorization"), " ")
|
jwtSplit := strings.Split(c.GetHeader("Authorization"), " ")
|
||||||
|
|
||||||
if len(jwtSplit) < 2 || jwtSplit[0] != "Bearer" {
|
if len(jwtSplit) < 2 || jwtSplit[0] != "Bearer" {
|
||||||
invalidAuth(c)
|
types.ErrorUnauthorized.Throw(c, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,21 +39,21 @@ func JwtMiddleware(secret string) gin.HandlerFunc {
|
||||||
|
|
||||||
claims, err := ValidateJWT(jwt, secret)
|
claims, err := ValidateJWT(jwt, secret)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
invalidAuth(c)
|
types.ErrorUnauthorized.Throw(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if userClaims, ok := claims["user"].(map[string]interface{}); ok {
|
if userClaims, ok := claims["user"].(map[string]interface{}); ok {
|
||||||
userJSON, err := json.Marshal(userClaims) // Convert map to JSON
|
userJSON, err := json.Marshal(userClaims) // Convert map to JSON
|
||||||
if err != nil {
|
if err != nil {
|
||||||
invalidAuth(c)
|
types.ErrorUnauthorized.Throw(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var user User
|
var user auth.User
|
||||||
err = json.Unmarshal(userJSON, &user)
|
err = json.Unmarshal(userJSON, &user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
invalidAuth(c)
|
types.ErrorUserNotFound.Throw(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,3 +64,8 @@ var ErrorS3 = StereoError{
|
||||||
Code: 7,
|
Code: 7,
|
||||||
StatusCode: 500,
|
StatusCode: 500,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var ErrorUserNotFound = StereoError{
|
||||||
|
Code: 8,
|
||||||
|
StatusCode: 404,
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue