10
"xelbot.com/reprogl/container"
11
"xelbot.com/reprogl/security"
15
CookieName = "session"
16
CtxKey = "session.ctx.key"
17
CsrfCookie = "csrf_token"
18
RefererCookie = "after_login"
21
FlashSuccessKey = "fs"
23
OAuthStateKey = "ostk"
24
OAuthVerifierKey = "ofk"
26
varnishSessionHeader = "X-Varnish-Session"
28
maxAge = 14 * 24 * time.Hour
32
DecodeError = errors.New("session: decode error")
33
EncodedValueTooLong = errors.New("session: the encoded value is too long")
34
ErrMacInvalid = errors.New("session: the HMAC is not valid")
35
Expired = errors.New("session: expired")
36
EncryptionError = errors.New("session: encryption error")
37
DecryptionError = errors.New("session: decryption failed")
40
type CookieInterface interface {
47
func FromRequest(r *http.Request, logger *log.Logger) *Store {
49
requestData := r.Header.Get(varnishSessionHeader)
50
if len(requestData) > 0 {
51
secureCookie := NewSecureCookie(
52
container.GetConfig().SessionHashKey,
53
container.GetConfig().SessionBlockKey,
55
data, err := secureCookie.decode(requestData)
57
return newStoreWithData(data)
60
logger.Printf("[AUTH] session: %s error: %s\n", requestData, err.Error())
67
store.status = Destroyed
74
func FromContext(ctx context.Context) *Store {
75
c, ok := ctx.Value(CtxKey).(*Store)
77
panic("session: no data in context")
83
func Put(ctx context.Context, key string, value any) {
84
store := FromContext(ctx)
87
store.data.values[key] = value
92
func Has(ctx context.Context, key string) bool {
93
store := FromContext(ctx)
96
_, exists := store.data.values[key]
102
func Get[T any](ctx context.Context, key string) (T, bool) {
103
store := FromContext(ctx)
106
defer store.mu.RUnlock()
110
if raw, exists := store.data.values[key]; exists {
111
if result, ok = raw.(T); ok {
119
func Pop[T any](ctx context.Context, key string) (T, bool) {
120
store := FromContext(ctx)
123
defer store.mu.Unlock()
127
if raw, exists := store.data.values[key]; exists {
128
if result, ok = raw.(T); ok {
129
delete(store.data.values, key)
139
func Remove(ctx context.Context, key string) {
140
store := FromContext(ctx)
143
defer store.mu.Unlock()
145
_, exists := store.data.values[key]
150
delete(store.data.values, key)
154
func Destroy(ctx context.Context) {
155
store := FromContext(ctx)
158
store.status = Destroyed
162
func HasIdentity(ctx context.Context) (result bool) {
163
store := FromContext(ctx)
166
result = !store.data.identity.IsZero()
172
func GetIdentity(ctx context.Context) (security.Identity, bool) {
173
store := FromContext(ctx)
176
defer store.mu.RUnlock()
178
return store.data.identity, !store.data.identity.IsZero()
181
func SetIdentity(ctx context.Context, identity security.Identity) {
182
store := FromContext(ctx)
185
store.data.identity = identity
190
func ClearIdentity(ctx context.Context) {
191
store := FromContext(ctx)
194
defer store.mu.Unlock()
196
if !store.data.identity.IsZero() {
200
store.data.identity = security.Identity{}