2023-06-15 20:32:18 +00:00
|
|
|
package main
|
|
|
|
|
2023-10-03 08:44:05 +00:00
|
|
|
import (
|
2024-08-01 19:17:05 +00:00
|
|
|
"fmt"
|
2023-10-03 08:44:05 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2024-08-01 19:17:05 +00:00
|
|
|
lm "github.com/hrfee/jfa-go/logmessages"
|
2023-10-03 08:44:05 +00:00
|
|
|
)
|
2023-06-15 20:32:18 +00:00
|
|
|
|
|
|
|
func (app *appContext) userAuth() gin.HandlerFunc {
|
|
|
|
return app.userAuthenticate
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *appContext) userAuthenticate(gc *gin.Context) {
|
|
|
|
jellyfinLogin := app.config.Section("ui").Key("jellyfin_login").MustBool(true)
|
|
|
|
if !jellyfinLogin {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Printf(lm.FailedAuthRequest, lm.UserPageRequiresJellyfinAuth)
|
2023-06-15 20:32:18 +00:00
|
|
|
respond(500, "Contact Admin", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
claims, ok := app.decodeValidateAuthHeader(gc)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// user id can be nil for all we care, we just want the Jellyfin ID
|
|
|
|
jfID := claims["jfid"].(string)
|
|
|
|
|
|
|
|
gc.Set("jfId", jfID)
|
|
|
|
gc.Set("userMode", true)
|
|
|
|
gc.Next()
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Grabs an user-access token using username & password.
|
|
|
|
// @description Has limited access to API routes, used to display the user's personal page.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} getTokenDTO
|
|
|
|
// @Failure 401 {object} stringResponse
|
|
|
|
// @Router /my/token/login [get]
|
|
|
|
// @tags Auth
|
|
|
|
// @Security getUserTokenAuth
|
|
|
|
func (app *appContext) getUserTokenLogin(gc *gin.Context) {
|
|
|
|
if !app.config.Section("ui").Key("jellyfin_login").MustBool(true) {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Printf(lm.FailedAuthRequest, lm.UserPageRequiresJellyfinAuth)
|
2023-06-15 20:32:18 +00:00
|
|
|
respond(500, "Contact Admin", gc)
|
|
|
|
return
|
|
|
|
}
|
2024-08-01 19:17:05 +00:00
|
|
|
app.logIpInfo(gc, true, fmt.Sprintf(lm.RequestingToken, lm.UserTokenLoginAttempt))
|
2023-12-23 20:54:55 +00:00
|
|
|
username, password, ok := app.decodeValidateLoginHeader(gc, true)
|
2023-06-15 20:32:18 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-23 20:54:55 +00:00
|
|
|
user, ok := app.validateJellyfinCredentials(username, password, gc, true)
|
2023-06-15 20:32:18 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
token, refresh, err := CreateToken(user.ID, user.ID, false)
|
|
|
|
if err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Printf(lm.FailedGenerateToken, err)
|
2023-06-15 20:32:18 +00:00
|
|
|
respond(500, "Couldn't generate user token", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-13 19:39:06 +00:00
|
|
|
// host := gc.Request.URL.Hostname()
|
|
|
|
host := app.ExternalDomain
|
2023-10-03 08:44:05 +00:00
|
|
|
uri := "/my"
|
|
|
|
if strings.HasPrefix(gc.Request.RequestURI, app.URLBase) {
|
|
|
|
uri = "/accounts/my"
|
|
|
|
}
|
2024-08-13 19:39:06 +00:00
|
|
|
gc.SetCookie("user-refresh", refresh, REFRESH_TOKEN_VALIDITY_SEC, uri, host, true, true)
|
2023-06-15 20:32:18 +00:00
|
|
|
gc.JSON(200, getTokenDTO{token})
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Grabs an user-access token using a refresh token from cookies.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} getTokenDTO
|
|
|
|
// @Failure 401 {object} stringResponse
|
|
|
|
// @Router /my/token/refresh [get]
|
|
|
|
// @tags Auth
|
|
|
|
func (app *appContext) getUserTokenRefresh(gc *gin.Context) {
|
|
|
|
jellyfinLogin := app.config.Section("ui").Key("jellyfin_login").MustBool(true)
|
|
|
|
if !jellyfinLogin {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Printf(lm.FailedAuthRequest, lm.UserPageRequiresJellyfinAuth)
|
2023-06-15 20:32:18 +00:00
|
|
|
respond(500, "Contact Admin", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-01 19:17:05 +00:00
|
|
|
app.logIpInfo(gc, true, fmt.Sprintf(lm.RequestingToken, lm.UserTokenRefresh))
|
2023-06-18 11:30:23 +00:00
|
|
|
claims, ok := app.decodeValidateRefreshCookie(gc, "user-refresh")
|
2023-06-15 20:32:18 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jfID := claims["jfid"].(string)
|
|
|
|
|
|
|
|
jwt, refresh, err := CreateToken(jfID, jfID, false)
|
|
|
|
if err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Printf(lm.FailedGenerateToken, err)
|
2023-06-15 20:32:18 +00:00
|
|
|
respond(500, "Couldn't generate user token", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-13 19:39:06 +00:00
|
|
|
// host := gc.Request.URL.Hostname()
|
|
|
|
host := app.ExternalDomain
|
|
|
|
gc.SetCookie("user-refresh", refresh, REFRESH_TOKEN_VALIDITY_SEC, "/my", host, true, true)
|
2023-06-15 20:32:18 +00:00
|
|
|
gc.JSON(200, getTokenDTO{jwt})
|
|
|
|
}
|