2022-08-21 20:51:14 +02:00
|
|
|
package services
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"crypto/rand"
|
|
|
|
|
"encoding/hex"
|
|
|
|
|
"io"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type SessionsService struct {
|
|
|
|
|
ctx *Context
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SessionItem struct {
|
|
|
|
|
Id string `json:"id"`
|
|
|
|
|
ExpiresAt int64 `json:"expiresAt"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *SessionsService) FromContext(ctx *gin.Context) (*SessionItem, error) {
|
|
|
|
|
cookie, err := ctx.Cookie("session.id")
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
session, err := s.GetById(cookie)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s.Extend(session)
|
|
|
|
|
|
|
|
|
|
return session, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *SessionsService) ToContext(ctx *gin.Context, session *SessionItem) {
|
|
|
|
|
ctx.SetCookie("session.id", session.Id, int(time.Duration(time.Hour*24).Seconds()), "/", "", true, true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *SessionsService) ClearContext(ctx *gin.Context) {
|
|
|
|
|
ctx.SetCookie("session.id", "", int(time.Duration(time.Hour*24).Seconds()), "/", "", true, true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *SessionsService) GetById(id string) (*SessionItem, error) {
|
|
|
|
|
item := SessionItem{}
|
|
|
|
|
|
|
|
|
|
row := s.ctx.DB.QueryRow("SELECT id, expires_at FROM sessions WHERE id = ? AND expires_at > ?", id, time.Now().Unix())
|
|
|
|
|
|
|
|
|
|
err := row.Scan(&item.Id, &item.ExpiresAt)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &item, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *SessionsService) Create() (*SessionItem, error) {
|
|
|
|
|
item := SessionItem{
|
|
|
|
|
// TODO: The key is not guaranteed to be unique, how do we guarantee that?
|
|
|
|
|
Id: hex.EncodeToString(generateRandomKey(128)),
|
|
|
|
|
ExpiresAt: generateExpiryDate().Unix(),
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-21 22:27:09 +02:00
|
|
|
_, err := s.ctx.DB.Exec("INSERT INTO sessions (id, expires_at) VALUES (?, ?)", item.Id, item.ExpiresAt)
|
2022-08-21 20:51:14 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &item, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *SessionsService) Destroy(sessionId string) error {
|
|
|
|
|
_, err := s.ctx.DB.Exec("DELETE FROM sessions WHERE id = ?", sessionId)
|
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *SessionsService) Extend(session *SessionItem) error {
|
|
|
|
|
session.ExpiresAt = generateExpiryDate().Unix()
|
|
|
|
|
|
|
|
|
|
_, err := s.ctx.DB.Exec("UPDATE sessions SET expires_at = ? WHERE id = ?", session.ExpiresAt, session.Id)
|
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func generateExpiryDate() time.Time {
|
|
|
|
|
return time.Now().Add(time.Hour * 24)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func generateRandomKey(length int) []byte {
|
|
|
|
|
k := make([]byte, length)
|
|
|
|
|
if _, err := io.ReadFull(rand.Reader, k); err != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return k
|
|
|
|
|
}
|