2022-09-28 09:01:17 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
2022-10-18 14:21:23 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetSession returns the session data as JSON if authentication was successful.
|
|
|
|
//
|
|
|
|
// GET /api/v1/session/:id
|
|
|
|
func GetSession(router *gin.RouterGroup) {
|
|
|
|
router.GET("/session/:id", func(c *gin.Context) {
|
|
|
|
id := clean.ID(c.Param("id"))
|
|
|
|
|
|
|
|
if id == "" {
|
|
|
|
AbortBadRequest(c)
|
|
|
|
return
|
|
|
|
} else if id != SessionID(c) {
|
|
|
|
AbortForbidden(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-18 14:21:23 +02:00
|
|
|
conf := get.Config()
|
|
|
|
|
|
|
|
// Skip authentication if app is running in public mode.
|
|
|
|
var sess *entity.Session
|
|
|
|
if conf.Public() {
|
|
|
|
sess = get.Session().Public()
|
|
|
|
} else {
|
|
|
|
sess = Session(id)
|
|
|
|
}
|
2022-09-28 09:01:17 +02:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case sess == nil:
|
|
|
|
AbortUnauthorized(c)
|
|
|
|
return
|
|
|
|
case sess.Expired(), sess.ID == "":
|
|
|
|
AbortUnauthorized(c)
|
|
|
|
return
|
|
|
|
case sess.Invalid():
|
|
|
|
AbortForbidden(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update user information.
|
|
|
|
sess.RefreshUser()
|
|
|
|
|
|
|
|
// Add session id to response headers.
|
|
|
|
AddSessionHeader(c, sess.ID)
|
|
|
|
|
2022-10-13 22:11:02 +02:00
|
|
|
// Send JSON response with user information, session data, and client config values.
|
|
|
|
data := gin.H{
|
|
|
|
"status": "ok",
|
|
|
|
"id": sess.ID,
|
|
|
|
"user": sess.User(),
|
|
|
|
"data": sess.Data(),
|
2022-10-15 21:54:11 +02:00
|
|
|
"config": get.Config().ClientSession(sess),
|
2022-09-28 09:01:17 +02:00
|
|
|
}
|
|
|
|
|
2022-10-13 22:11:02 +02:00
|
|
|
c.JSON(http.StatusOK, data)
|
2022-09-28 09:01:17 +02:00
|
|
|
})
|
|
|
|
}
|