-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.go
More file actions
142 lines (117 loc) · 3.18 KB
/
session.go
File metadata and controls
142 lines (117 loc) · 3.18 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package session
import (
"context"
"crypto/rand"
"encoding/base64"
"encoding/json"
"io"
"net/http"
"sync"
"time"
"github.com/google/uuid"
"github.com/pkg/errors"
)
type HttpSessionInterface[T any] interface {
NewSession(ctx context.Context, t T) (*Session[T], error)
GetSession(ctx context.Context, session *Session[T]) (*Session[T], error)
PutSession(ctx context.Context, session *Session[T]) error
EndSession(ctx context.Context, session *Session[T]) (*Session[T], error)
}
type HttpSession[T any] struct {
lock sync.Mutex
store Store
cookie http.Cookie
generate func() (string, error)
regenerate bool
}
type Session[T any] struct {
Object *T
Cookie *http.Cookie
}
type GeneratorFunc func() (string, error)
type Options struct {
GeneratorFunc func() (string, error)
RegenerateIDs bool
}
func NewHttpSession[T any](store Store, cookie http.Cookie, options Options) (*HttpSession[T], error) {
httpSession := &HttpSession[T]{
lock: sync.Mutex{},
store: store,
cookie: cookie,
generate: options.GeneratorFunc,
regenerate: options.RegenerateIDs,
}
return httpSession, nil
}
func (service *HttpSession[T]) NewSession(ctx context.Context, t T) (*Session[T], error) {
var sessionID string
if service.generate != nil {
id, err := service.generate()
if err != nil {
return nil, errors.Wrap(err, "failed to generate session ID")
}
sessionID = id
}
err := service.store.New(ctx, sessionID, t)
if err != nil {
return nil, errors.Wrap(err, "failed to store new session")
}
cookie := service.cookie
cookie.Value = sessionID
return &Session[T]{
Object: &t,
Cookie: &cookie,
}, nil
}
func (service *HttpSession[T]) GetSession(ctx context.Context, session *Session[T]) (*Session[T], error) {
service.lock.Lock()
defer service.lock.Unlock()
sessionData, err := service.store.Get(ctx, session.Cookie.Value)
if err != nil {
return nil, errors.Wrap(err, "failed to get Data from session")
}
t := new(T)
err = json.Unmarshal(sessionData, t)
if err != nil {
return nil, errors.Wrap(err, "failed to unmarshal session Data")
}
session.Object = t
return session, nil
}
func (service *HttpSession[T]) PutSession(ctx context.Context, session *Session[T]) error {
service.lock.Lock()
defer service.lock.Unlock()
err := service.store.Put(ctx, session.Cookie.Value, session.Object)
if err != nil {
return errors.Wrap(err, "failed to put session in storage")
}
return nil
}
func (service *HttpSession[T]) EndSession(ctx context.Context, session *Session[T]) (*Session[T], error) {
service.lock.Lock()
defer service.lock.Unlock()
err := service.store.End(ctx, session.Cookie.Value)
if err != nil {
return nil, errors.Wrap(err, "failed to delete session")
}
session.Cookie.MaxAge = -1
session.Cookie.Expires = time.Now()
session.Cookie.Value = ""
session.Object = nil
return session, nil
}
func WithGenerateUUID() GeneratorFunc {
return func() (string, error) {
return uuid.New().String(), nil
}
}
func WithGenerateRBID() GeneratorFunc {
return func() (string, error) {
b := make([]byte, 16)
_, err := io.ReadFull(rand.Reader, b)
if err != nil {
return "", err
}
return base64.URLEncoding.EncodeToString(b), nil
}
}