feat: rename jwt to session & introduce upload keys
This commit is contained in:
parent
3fc792fd53
commit
3b02f4931e
7 changed files with 98 additions and 15 deletions
|
@ -18,11 +18,16 @@
|
|||
package routes
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"stereo.cat/backend/internal/auth/token"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"stereo.cat/backend/internal/auth"
|
||||
"stereo.cat/backend/internal/auth/session"
|
||||
"stereo.cat/backend/internal/auth/ukey"
|
||||
"stereo.cat/backend/internal/types"
|
||||
)
|
||||
|
||||
|
@ -42,7 +47,7 @@ func RegisterAuthRoutes(cfg *types.StereoConfig, api *gin.RouterGroup) {
|
|||
panic(err)
|
||||
}
|
||||
|
||||
jwt, err := token.GenerateJWT(cfg.JWTSecret, user, uint64(time.Now().Add(time.Second*time.Duration(t.ExpiresIn)).Unix()))
|
||||
jwt, err := session.GenerateSessionJWT(cfg.JWTSecret, user, uint64(time.Now().Add(time.Second*time.Duration(t.ExpiresIn)).Unix()))
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
@ -64,8 +69,30 @@ func RegisterAuthRoutes(cfg *types.StereoConfig, api *gin.RouterGroup) {
|
|||
c.Redirect(http.StatusTemporaryRedirect, cfg.FrontendUri+"?jwt_set=true")
|
||||
})
|
||||
|
||||
api.GET("/auth/me", token.JwtMiddleware(cfg.JWTSecret), func(c *gin.Context) {
|
||||
claims, _ := c.Get("claims")
|
||||
api.GET("/auth/me", session.SessionMiddleware(cfg.JWTSecret), func(c *gin.Context) {
|
||||
claims := c.MustGet("claims")
|
||||
c.JSON(http.StatusOK, claims)
|
||||
})
|
||||
|
||||
// Generate an API key (automatically revokes previous api key too since a user can only have one api key bound to their db entry at a given time)
|
||||
api.GET("/auth/key", session.SessionMiddleware(cfg.JWTSecret), func(c *gin.Context) {
|
||||
claims := c.MustGet("claims").(jwt.MapClaims)
|
||||
|
||||
user, ok := claims["user"].(auth.User)
|
||||
if !ok {
|
||||
types.ErrorUserNotFound.Throw(c, errors.New(fmt.Sprintf("got data with type %T but wanted claims.User", claims["user"])))
|
||||
return
|
||||
}
|
||||
|
||||
key := ukey.GenerateUploadKey(cfg, &user, c)
|
||||
|
||||
if key == nil {
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"key": key,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue