-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManager.go
54 lines (44 loc) · 1.18 KB
/
Manager.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
package session
import (
"fmt"
"net/http"
"time"
)
// Manager generates new sessions with the specified
// duration and within the given storage backend.
type Manager struct {
Store Store
// Session duration in seconds
Duration int
// SameSite attribute for cookies
SameSite http.SameSite
}
// New creates a new session.
func (manager *Manager) New() *Session {
sessionID := GenerateID()
// Session data is not allowed to be an empty map.
// Therefore we add the session ID itself as predefined data in "sid".
// Additionally we'll save the creation date (UTC) in "created".
sessionData := map[string]interface{}{
"sid": sessionID,
"created": time.Now().UTC().Format(time.RFC3339),
}
session := New(sessionID, sessionData)
err := manager.Store.Set(session.id, session)
if err != nil {
fmt.Println("Error saving session in the session store:", err)
}
return session
}
// Cookie creates a cookie for the given session.
func (manager *Manager) Cookie(session *Session) *http.Cookie {
return &http.Cookie{
Name: "sid",
Value: session.ID(),
HttpOnly: true,
Secure: true,
MaxAge: manager.Duration,
Path: "/",
SameSite: manager.SameSite,
}
}