2022-09-28 09:01:17 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
2024-01-16 16:17:16 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
2022-10-15 21:54:11 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/get"
|
2022-09-28 09:01:17 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2024-01-14 18:28:17 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/header"
|
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
2022-09-28 09:01:17 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetSession returns the session data as JSON if authentication was successful.
|
|
|
|
//
|
2024-01-14 18:28:17 +01:00
|
|
|
// GET /api/v1/session
|
2022-09-28 09:01:17 +02:00
|
|
|
// GET /api/v1/session/:id
|
2024-01-14 18:28:17 +01:00
|
|
|
// GET /api/v1/sessions/:id
|
2022-09-28 09:01:17 +02:00
|
|
|
func GetSession(router *gin.RouterGroup) {
|
2024-01-08 14:53:39 +01:00
|
|
|
getSessionHandler := func(c *gin.Context) {
|
2024-01-16 16:17:16 +01:00
|
|
|
// Prevent CDNs from caching this endpoint.
|
|
|
|
if header.IsCdn(c.Request) {
|
2024-01-16 20:56:43 +01:00
|
|
|
AbortNotFound(c)
|
2024-01-16 16:17:16 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
id := clean.ID(c.Param("id"))
|
|
|
|
|
2024-01-14 18:28:17 +01:00
|
|
|
if id != "" && !rnd.IsSessionID(id) {
|
2024-01-16 16:17:16 +01:00
|
|
|
// Abort if session id is provided but invalid.
|
2022-09-28 09:01:17 +02:00
|
|
|
AbortBadRequest(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-18 14:21:23 +02:00
|
|
|
conf := get.Config()
|
2024-01-14 18:28:17 +01:00
|
|
|
|
2024-01-16 16:17:16 +01:00
|
|
|
// Check if the session user is allowed to manage all accounts or update his/her own account.
|
|
|
|
s := AuthAny(c, acl.ResourceSessions, acl.Permissions{acl.ActionManage, acl.ActionView})
|
2022-09-28 09:01:17 +02:00
|
|
|
|
2024-01-16 16:17:16 +01:00
|
|
|
// Check if session is valid.
|
2022-09-28 09:01:17 +02:00
|
|
|
switch {
|
2024-01-16 16:17:16 +01:00
|
|
|
case s.Abort(c):
|
2022-09-28 09:01:17 +02:00
|
|
|
return
|
2024-01-16 16:17:16 +01:00
|
|
|
case s.Expired(), s.ID == "":
|
2022-09-28 09:01:17 +02:00
|
|
|
AbortUnauthorized(c)
|
|
|
|
return
|
2024-01-16 16:17:16 +01:00
|
|
|
case s.Invalid(), id != "" && s.ID != id && !conf.Public():
|
2022-09-28 09:01:17 +02:00
|
|
|
AbortForbidden(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 16:17:16 +01:00
|
|
|
// Get auth token from headers.
|
|
|
|
authToken := AuthToken(c)
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Update user information.
|
2024-01-16 16:17:16 +01:00
|
|
|
s.RefreshUser()
|
2022-09-28 09:01:17 +02:00
|
|
|
|
2024-01-06 17:35:19 +01:00
|
|
|
// Response includes user data, session data, and client config values.
|
2024-01-16 16:17:16 +01:00
|
|
|
response := GetSessionResponse(authToken, s, get.Config().ClientSession(s))
|
2022-09-28 09:01:17 +02:00
|
|
|
|
2024-01-06 17:35:19 +01:00
|
|
|
// Return JSON response.
|
|
|
|
c.JSON(http.StatusOK, response)
|
2024-01-08 14:53:39 +01:00
|
|
|
}
|
|
|
|
|
2024-01-14 18:28:17 +01:00
|
|
|
router.GET("/session", getSessionHandler)
|
2024-01-08 14:53:39 +01:00
|
|
|
router.GET("/session/:id", getSessionHandler)
|
2024-01-14 18:28:17 +01:00
|
|
|
router.GET("/sessions/:id", getSessionHandler)
|
2022-09-28 09:01:17 +02:00
|
|
|
}
|