mirror of
https://github.com/truewhile/MeBox.git
synced 2026-09-28 11:16:37 +08:00
fix: refresh emby sessions in realtime
This commit is contained in:
@@ -2,9 +2,12 @@ package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
|
||||
"github.com/ShukeBta/MediaStationGo/internal/middleware"
|
||||
"github.com/ShukeBta/MediaStationGo/internal/service"
|
||||
)
|
||||
|
||||
@@ -47,13 +50,58 @@ func embySessionsHandler(svc *service.Container) gin.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func embySessionLogoutHandler(svc *service.Container) gin.HandlerFunc {
|
||||
func embySessionLogoutHandler(svc *service.Container, jwtSecret string) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if svc.Sessions != nil {
|
||||
uid := embyUserID(c)
|
||||
uid, _ := embyPublicSessionIdentity(c, svc, jwtSecret)
|
||||
clientInfo := embyClientInfoFromRequest(c)
|
||||
svc.Sessions.Logout(c.Request.Context(), uid, clientInfo.DeviceID, c.ClientIP())
|
||||
}
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
}
|
||||
|
||||
func embySessionCapabilitiesHandler(svc *service.Container, jwtSecret string) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
recordEmbyPublicSessionActivity(c, svc, jwtSecret)
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
}
|
||||
|
||||
func recordEmbyPublicSessionActivity(c *gin.Context, svc *service.Container, jwtSecret string) {
|
||||
uid, username := embyPublicSessionIdentity(c, svc, jwtSecret)
|
||||
recordEmbySessionActivity(c, svc, uid, username)
|
||||
}
|
||||
|
||||
func embyPublicSessionIdentity(c *gin.Context, svc *service.Container, jwtSecret string) (string, string) {
|
||||
if uid := embyUserID(c); uid != "" {
|
||||
return uid, embyContextUserName(c)
|
||||
}
|
||||
token := embyRequestToken(c)
|
||||
if strings.TrimSpace(token) == "" || strings.TrimSpace(jwtSecret) == "" {
|
||||
return "", ""
|
||||
}
|
||||
claims := &middleware.Claims{}
|
||||
parsed, err := jwt.ParseWithClaims(token, claims, func(t *jwt.Token) (interface{}, error) {
|
||||
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, jwt.ErrTokenSignatureInvalid
|
||||
}
|
||||
return []byte(jwtSecret), nil
|
||||
})
|
||||
if err != nil || !parsed.Valid {
|
||||
return "", ""
|
||||
}
|
||||
if strings.TrimSpace(claims.Purpose) != "" {
|
||||
return "", ""
|
||||
}
|
||||
uid := strings.TrimSpace(claims.UserID)
|
||||
if uid == "" {
|
||||
return "", ""
|
||||
}
|
||||
if svc != nil && svc.Repo != nil && svc.Repo.User != nil {
|
||||
if user, err := svc.Repo.User.FindByID(c.Request.Context(), uid); err == nil && user != nil {
|
||||
return uid, user.Username
|
||||
}
|
||||
}
|
||||
return uid, ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user