mirror of
https://github.com/photoprism/photoprism.git
synced 2025-12-12 00:34:13 +01:00
Backend: Add session package #169
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
@@ -4,9 +4,9 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/patrickmn/go-cache"
|
|
||||||
"github.com/photoprism/photoprism/internal/config"
|
"github.com/photoprism/photoprism/internal/config"
|
||||||
"github.com/photoprism/photoprism/internal/form"
|
"github.com/photoprism/photoprism/internal/form"
|
||||||
|
"github.com/photoprism/photoprism/internal/session"
|
||||||
"github.com/photoprism/photoprism/internal/util"
|
"github.com/photoprism/photoprism/internal/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -25,15 +25,13 @@ func CreateSession(router *gin.RouterGroup, conf *config.Config) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
token := util.RandomToken(16)
|
user := gin.H{"ID": 1, "FirstName": "Admin", "LastName": "", "Role": "admin", "Email": "photoprism@localhost"}
|
||||||
|
|
||||||
|
token := session.Create(user)
|
||||||
|
|
||||||
c.Header("X-Session-Token", token)
|
c.Header("X-Session-Token", token)
|
||||||
|
|
||||||
gc := conf.Cache()
|
s := gin.H{"token": token, "user": user}
|
||||||
|
|
||||||
gc.Set(token, 1, cache.DefaultExpiration)
|
|
||||||
|
|
||||||
s := gin.H{"token": token, "user": gin.H{"ID": 1, "FirstName": "Admin", "LastName": "", "Role": "admin", "Email": "photoprism@localhost"}}
|
|
||||||
|
|
||||||
c.JSON(http.StatusOK, s)
|
c.JSON(http.StatusOK, s)
|
||||||
})
|
})
|
||||||
@@ -44,9 +42,7 @@ func DeleteSession(router *gin.RouterGroup, conf *config.Config) {
|
|||||||
router.DELETE("/session/:token", func(c *gin.Context) {
|
router.DELETE("/session/:token", func(c *gin.Context) {
|
||||||
token := c.Param("token")
|
token := c.Param("token")
|
||||||
|
|
||||||
gc := conf.Cache()
|
session.Delete(token)
|
||||||
|
|
||||||
gc.Delete(token)
|
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{"status": "ok", "token": token})
|
c.JSON(http.StatusOK, gin.H{"status": "ok", "token": token})
|
||||||
})
|
})
|
||||||
@@ -61,11 +57,7 @@ func Unauthorized(c *gin.Context, conf *config.Config) bool {
|
|||||||
|
|
||||||
// Get session token from HTTP header
|
// Get session token from HTTP header
|
||||||
token := c.GetHeader("X-Session-Token")
|
token := c.GetHeader("X-Session-Token")
|
||||||
log.Debugf("X-Session-Token: %s", token)
|
|
||||||
|
|
||||||
// Check if session token is valid
|
// Check if session token is valid
|
||||||
gc := conf.Cache()
|
return !session.Exists(token)
|
||||||
_, found := gc.Get(token)
|
|
||||||
|
|
||||||
return !found
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -573,11 +573,11 @@ func (c *Config) ClientConfig() ClientConfig {
|
|||||||
var albums []*entity.Album
|
var albums []*entity.Album
|
||||||
|
|
||||||
var position struct {
|
var position struct {
|
||||||
PhotoUUID string `json:"photo"`
|
PhotoUUID string `json:"photo"`
|
||||||
LocationID uint64 `json:"location"`
|
LocationID uint64 `json:"location"`
|
||||||
TakenAt time.Time `json:"utc"`
|
TakenAt time.Time `json:"utc"`
|
||||||
PhotoLat float64 `json:"lat"`
|
PhotoLat float64 `json:"lat"`
|
||||||
PhotoLng float64 `json:"lng"`
|
PhotoLng float64 `json:"lng"`
|
||||||
}
|
}
|
||||||
|
|
||||||
db.Table("photos").
|
db.Table("photos").
|
||||||
|
|||||||
14
internal/session/session.go
Normal file
14
internal/session/session.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
/*
|
||||||
|
This package encapsulates session storage.
|
||||||
|
|
||||||
|
Additional information can be found in our Developer Guide:
|
||||||
|
|
||||||
|
https://github.com/photoprism/photoprism/wiki
|
||||||
|
*/
|
||||||
|
package session
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/photoprism/photoprism/internal/event"
|
||||||
|
)
|
||||||
|
|
||||||
|
var log = event.Log
|
||||||
31
internal/session/store.go
Normal file
31
internal/session/store.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package session
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
gc "github.com/patrickmn/go-cache"
|
||||||
|
)
|
||||||
|
|
||||||
|
var cache = gc.New(72*time.Hour, 30*time.Minute)
|
||||||
|
|
||||||
|
func Create(data interface{}) string {
|
||||||
|
token := Token()
|
||||||
|
cache.Set(token, data, gc.DefaultExpiration)
|
||||||
|
log.Debugf("session: created")
|
||||||
|
return token
|
||||||
|
}
|
||||||
|
|
||||||
|
func Delete(token string) {
|
||||||
|
cache.Delete(token)
|
||||||
|
log.Debugf("session: deleted")
|
||||||
|
}
|
||||||
|
|
||||||
|
func Get(token string) (data interface{}, exists bool) {
|
||||||
|
return cache.Get(token)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Exists(token string) bool {
|
||||||
|
_, found := cache.Get(token)
|
||||||
|
|
||||||
|
return found
|
||||||
|
}
|
||||||
45
internal/session/store_test.go
Normal file
45
internal/session/store_test.go
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package session
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCreate(t *testing.T) {
|
||||||
|
token := Create(23)
|
||||||
|
t.Logf("token: %s", token)
|
||||||
|
assert.Equal(t, 48, len(token))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDelete(t *testing.T) {
|
||||||
|
Delete("abc")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGet(t *testing.T) {
|
||||||
|
token := Create(42)
|
||||||
|
t.Logf("token: %s", token)
|
||||||
|
assert.Equal(t, 48, len(token))
|
||||||
|
|
||||||
|
data, exists := Get(token)
|
||||||
|
|
||||||
|
assert.Equal(t, 42, data)
|
||||||
|
assert.True(t, exists)
|
||||||
|
|
||||||
|
Delete(token)
|
||||||
|
|
||||||
|
data, exists = Get(token)
|
||||||
|
|
||||||
|
assert.Nil(t, data)
|
||||||
|
assert.False(t, Exists(token))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExists(t *testing.T) {
|
||||||
|
assert.False(t, Exists("xyz"))
|
||||||
|
token := Create(23)
|
||||||
|
t.Logf("token: %s", token)
|
||||||
|
assert.Equal(t, 48, len(token))
|
||||||
|
assert.True(t, Exists(token))
|
||||||
|
Delete(token)
|
||||||
|
assert.False(t, Exists(token))
|
||||||
|
}
|
||||||
16
internal/session/token.go
Normal file
16
internal/session/token.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package session
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Token() string {
|
||||||
|
b := make([]byte, 24)
|
||||||
|
|
||||||
|
if _, err := rand.Read(b); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("%x", b)
|
||||||
|
}
|
||||||
15
internal/session/token_test.go
Normal file
15
internal/session/token_test.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package session
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestToken(t *testing.T) {
|
||||||
|
for n := 0; n < 5; n++ {
|
||||||
|
token := Token()
|
||||||
|
t.Logf("token: %s", token)
|
||||||
|
assert.Equal(t, 48, len(token))
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user