-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsessions.go
102 lines (84 loc) · 2.48 KB
/
sessions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package auth
import (
"github.com/KowalskiPiotr98/ludivault/utils"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/gorilla/sessions"
"github.com/markbates/goth/gothic"
log "github.com/sirupsen/logrus"
"net/http"
)
//todo: storing sessions in cookie only is bad as it prevents any form of server-side tracking of sessions
// but it'll have to do for now
var (
UserSessionName = "ludivault-session"
SetupSession = func(baseDomain string) sessions.Store {
key := utils.GetRequiredConfig("SESSION_KEY")
maxAge := 86400 * 30
store := sessions.NewCookieStore([]byte(key))
store.MaxAge(maxAge)
store.Options.Path = "/"
store.Options.HttpOnly = true
store.Options.Secure = gin.Mode() != gin.DebugMode
store.Options.Domain = baseDomain
store.Options.SameSite = http.SameSiteLaxMode
return store
}
authStore sessions.Store
)
// InitSessionStore initialises the store for user sessions.
//
// This function must be called before any other function from this package.
func InitSessionStore(baseDomain string) {
authStore = SetupSession(baseDomain)
gothic.Store = authStore
}
// StoreUserInSession adds the user data to session store.
func StoreUserInSession(c *gin.Context, userId uuid.UUID) error {
ensureSessionStoreInit()
session, err := authStore.Get(c.Request, UserSessionName)
if err != nil {
log.Warnf("Failed to get session: %v", err)
return err
}
session.Values["userId"] = userId
if err = session.Save(c.Request, c.Writer); err != nil {
log.Warnf("Failed to save session: %v", err)
return err
}
return nil
}
// RetrieveUserFromSession attempts to get user data from session store.
func RetrieveUserFromSession(c *gin.Context) (uuid.UUID, error) {
ensureSessionStoreInit()
session, err := authStore.Get(c.Request, UserSessionName)
if err != nil {
log.Warnf("Failed to get session: %v", err)
return uuid.Nil, err
}
id, ok := session.Values["userId"].(uuid.UUID)
if !ok {
return uuid.Nil, UserIdNotStored
}
return id, nil
}
func RemoveUserSession(c *gin.Context) error {
ensureSessionStoreInit()
session, err := authStore.Get(c.Request, UserSessionName)
if err != nil {
log.Warnf("Failed to get session: %v", err)
return err
}
// remove what's left of the session
session.Options.MaxAge = -1
if err := session.Save(c.Request, c.Writer); err != nil {
log.Warnf("Failed to save session: %v", err)
return err
}
return nil
}
func ensureSessionStoreInit() {
if authStore == nil {
log.Panic("Session store is not initialized")
}
}