Moved cookies to auth module, added sessions cleanup
This commit is contained in:
parent
fe204aa77d
commit
f330279e05
|
|
@ -0,0 +1,16 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func (s *Server) StartCleaner() {
|
||||
ticker := time.NewTicker(time.Hour * 1)
|
||||
|
||||
go func() {
|
||||
for {
|
||||
s.Services.Sessions.Cleanup()
|
||||
<-ticker.C
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
|
@ -49,5 +49,9 @@ func main() {
|
|||
keyProtected := router.Group("/", middleware.KeyAuthMiddleware(server))
|
||||
keyProtected.POST("/api/sensors/:sensor/values", routes.HandlePostSensorValues(server))
|
||||
|
||||
// Starts session cleanup goroutine
|
||||
server.StartCleaner()
|
||||
|
||||
// Run the server
|
||||
router.Run(fmt.Sprintf("%s:%d", server.Config.Ip, server.Config.Port))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,27 @@
|
|||
package services
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type AuthService struct {
|
||||
ctx *Context
|
||||
}
|
||||
|
||||
const SESSION_ID string = "session.id"
|
||||
|
||||
func (s *AuthService) FromContext(ctx *gin.Context) (*SessionItem, error) {
|
||||
return s.ctx.Services.Sessions.FromContext(ctx)
|
||||
session, err := s.getSessionFromContext(ctx)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s.ctx.Services.Sessions.Extend(session)
|
||||
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func (s *AuthService) Login(ctx *gin.Context) error {
|
||||
|
|
@ -17,13 +31,13 @@ func (s *AuthService) Login(ctx *gin.Context) error {
|
|||
return err
|
||||
}
|
||||
|
||||
s.ctx.Services.Sessions.ToContext(ctx, session)
|
||||
ctx.SetCookie(SESSION_ID, session.Id, int(time.Duration(time.Hour*24).Seconds()), "/", "", true, true)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *AuthService) Logout(ctx *gin.Context) error {
|
||||
session, err := s.ctx.Services.Sessions.FromContext(ctx)
|
||||
session, err := s.getSessionFromContext(ctx)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -33,7 +47,17 @@ func (s *AuthService) Logout(ctx *gin.Context) error {
|
|||
return err
|
||||
}
|
||||
|
||||
s.ctx.Services.Sessions.ClearContext(ctx)
|
||||
ctx.SetCookie(SESSION_ID, "", 0, "/", "", true, true)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *AuthService) getSessionFromContext(ctx *gin.Context) (*SessionItem, error) {
|
||||
cookie, err := ctx.Cookie(SESSION_ID)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.ctx.Services.Sessions.GetById(cookie)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,8 +5,6 @@ import (
|
|||
"encoding/hex"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type SessionsService struct {
|
||||
|
|
@ -18,32 +16,6 @@ type SessionItem struct {
|
|||
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{}
|
||||
|
||||
|
|
@ -87,6 +59,12 @@ func (s *SessionsService) Extend(session *SessionItem) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (s *SessionsService) Cleanup() error {
|
||||
_, err := s.ctx.DB.Exec("DELETE FROM sessions WHERE expires_at < ?", time.Now().Unix())
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func generateExpiryDate() time.Time {
|
||||
return time.Now().Add(time.Hour * 24)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue