code refactoring, fix jwt

This commit is contained in:
2023-09-25 11:12:18 +02:00
parent 9bb5d7bb0d
commit 764c1f4729
4 changed files with 11 additions and 12 deletions

View File

@@ -15,10 +15,6 @@ import (
"github.com/marcopeocchi/yt-dlp-web-ui/server/utils" "github.com/marcopeocchi/yt-dlp-web-ui/server/utils"
) )
const (
TOKEN_COOKIE_NAME = "jwt"
)
type DirectoryEntry struct { type DirectoryEntry struct {
Name string `json:"name"` Name string `json:"name"`
Path string `json:"path"` Path string `json:"path"`

View File

@@ -8,6 +8,7 @@ import (
"github.com/goccy/go-json" "github.com/goccy/go-json"
"github.com/golang-jwt/jwt/v5" "github.com/golang-jwt/jwt/v5"
"github.com/marcopeocchi/yt-dlp-web-ui/server/config" "github.com/marcopeocchi/yt-dlp-web-ui/server/config"
"github.com/marcopeocchi/yt-dlp-web-ui/server/utils"
) )
type LoginRequest struct { type LoginRequest struct {
@@ -44,7 +45,7 @@ func Login(w http.ResponseWriter, r *http.Request) {
} }
cookie := &http.Cookie{ cookie := &http.Cookie{
Name: TOKEN_COOKIE_NAME, Name: utils.TOKEN_COOKIE_NAME,
HttpOnly: true, HttpOnly: true,
Secure: false, Secure: false,
Expires: expiresAt, // 30 days Expires: expiresAt, // 30 days
@@ -57,7 +58,7 @@ func Login(w http.ResponseWriter, r *http.Request) {
func Logout(w http.ResponseWriter, r *http.Request) { func Logout(w http.ResponseWriter, r *http.Request) {
cookie := &http.Cookie{ cookie := &http.Cookie{
Name: TOKEN_COOKIE_NAME, Name: utils.TOKEN_COOKIE_NAME,
HttpOnly: true, HttpOnly: true,
Secure: false, Secure: false,
Expires: time.Now(), Expires: time.Now(),

View File

@@ -8,10 +8,7 @@ import (
"github.com/golang-jwt/jwt/v5" "github.com/golang-jwt/jwt/v5"
"github.com/marcopeocchi/yt-dlp-web-ui/server/config" "github.com/marcopeocchi/yt-dlp-web-ui/server/config"
) "github.com/marcopeocchi/yt-dlp-web-ui/server/utils"
const (
TOKEN_COOKIE_NAME = "jwt"
) )
func Authenticated(next http.Handler) http.Handler { func Authenticated(next http.Handler) http.Handler {
@@ -21,7 +18,7 @@ func Authenticated(next http.Handler) http.Handler {
return return
} }
cookie, err := r.Cookie(TOKEN_COOKIE_NAME) cookie, err := r.Cookie(utils.TOKEN_COOKIE_NAME)
if err != nil { if err != nil {
http.Error(w, "invalid token", http.StatusBadRequest) http.Error(w, "invalid token", http.StatusBadRequest)
@@ -37,7 +34,7 @@ func Authenticated(next http.Handler) http.Handler {
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok { if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", t.Header["alg"]) return nil, fmt.Errorf("unexpected signing method: %v", t.Header["alg"])
} }
return []byte(os.Getenv("JWTSECRET")), nil return []byte(os.Getenv("JWT_SECRET")), nil
}) })
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid { if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {

5
server/utils/cookie.go Normal file
View File

@@ -0,0 +1,5 @@
package utils
const (
TOKEN_COOKIE_NAME = "jwt-yt-dlp-webui"
)