1package v2
2
3import "context"
4
5// Define the key type to avoid key collisions in context
6type key int
7
8const (
9	// AccessTokenString is the key name used to retrieve the access token string
10	AccessTokenString key = iota
11
12	// AccessTokenClaims contains the key name to retrieve the access token claims
13	AccessTokenClaims
14
15	// ClaimsKey contains key name to retrieve the jwt claims from context
16	ClaimsKey
17
18	// NamespaceKey contains the key name to retrieve the namespace from context
19	NamespaceKey
20
21	// RefreshTokenClaims contains the key name to retrieve the refresh token claims
22	RefreshTokenClaims
23
24	// RefreshTokenString contains the key name to retrieve the refresh token string
25	RefreshTokenString
26
27	// AuthorizationAttributesKey is the key name used to store authorization
28	// attributes extracted from a request
29	AuthorizationAttributesKey
30
31	// StoreKey contains the key name to retrieve the etcd store from within a context
32	StoreKey
33
34	// PageContinueKey contains the continue token used in pagination
35	PageContinueKey
36
37	// PageSizeKey contains the page size used in pagination
38	PageSizeKey
39)
40
41// ContextNamespace returns the namespace injected in the context
42func ContextNamespace(ctx context.Context) string {
43	if value := ctx.Value(NamespaceKey); value != nil {
44		return value.(string)
45	}
46	return ""
47}
48
49// PageSizeFromContext returns the page size stored in the given context, if
50// any. Returns 0 if none is found, typically meaning "unlimited" page size.
51func PageSizeFromContext(ctx context.Context) int {
52	if value := ctx.Value(PageSizeKey); value != nil {
53		return value.(int)
54	}
55	return 0
56}
57
58// PageContinueFromContext returns the continue token stored in the given
59// context, if any. Returns "" if none is found.
60func PageContinueFromContext(ctx context.Context) string {
61	if value := ctx.Value(PageContinueKey); value != nil {
62		return value.(string)
63	}
64	return ""
65}
66