-
Notifications
You must be signed in to change notification settings - Fork 2
/
context.go
42 lines (33 loc) · 955 Bytes
/
context.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
package session
import (
"context"
"net/textproto"
"strings"
"github.com/google/uuid"
)
type CtxKey string
var (
HeaderAsertoSessionID = CtxKey(textproto.CanonicalMIMEHeaderKey("Aserto-Session-Id"))
HeaderAsertoSessionIDLowercase = CtxKey(strings.ToLower(string(HeaderAsertoSessionID)))
)
// NewSessionID creates a new session ID.
func NewSessionID() (string, error) {
id, err := uuid.NewUUID()
return id.String(), err
}
// CheckSessionID returns an error if the id doesn't look like an session ID.
func CheckSessionID(id string) error {
_, err := uuid.Parse(id)
return err
}
// ExtractSessionID extracts a session id from a context.
func ExtractSessionID(ctx context.Context) string {
id, ok := ctx.Value(HeaderAsertoSessionID).(string)
if !ok {
return ""
}
return id
}
func ContextWithSessionID(ctx context.Context, sessionID string) context.Context {
return context.WithValue(ctx, HeaderAsertoSessionID, sessionID)
}