forked from absmach/mgate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
37 lines (31 loc) · 967 Bytes
/
session.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
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
package mproxy
import (
"context"
"crypto/x509"
)
// The sessionKey type is unexported to prevent collisions with context keys defined in
// other packages.
type sessionKey struct{}
// Session stores session data.
type Session struct {
ID string
Username string
Password []byte
Cert x509.Certificate
}
// NewContext stores Session in context.Context values.
// It uses pointer to the session so it can be modified by handler.
func NewContext(ctx context.Context, s *Session) context.Context {
return context.WithValue(ctx, sessionKey{}, s)
}
// FromContext retrieves Session from context.Context.
// Second value indicates if session is present in the context
// and if it's safe to use it (it's not nil).
func FromContext(ctx context.Context) (*Session, bool) {
if s, ok := ctx.Value(sessionKey{}).(*Session); ok && s != nil {
return s, true
}
return nil, false
}