-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
146 lines (120 loc) · 3.64 KB
/
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
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
143
144
145
146
package authcontrol
import (
"context"
"github.com/0xsequence/authcontrol/proto"
)
type contextKey struct {
name string
}
func (k *contextKey) String() string {
return "quotacontrol context value " + k.name
}
var (
ctxKeySessionType = &contextKey{"SessionType"}
ctxKeyAccount = &contextKey{"Account"}
ctxKeyUser = &contextKey{"User"}
ctxKeyService = &contextKey{"Service"}
ctxKeyAccessKey = &contextKey{"AccessKey"}
ctxKeyProjectID = &contextKey{"ProjectID"}
ctxKeyProject = &contextKey{"Project"}
)
//
// Session Type
//
// WithSessionType adds the access key to the context.
func WithSessionType(ctx context.Context, accessType proto.SessionType) context.Context {
return context.WithValue(ctx, ctxKeySessionType, accessType)
}
// GetSessionType returns the access key from the context.
func GetSessionType(ctx context.Context) (proto.SessionType, bool) {
v, ok := ctx.Value(ctxKeySessionType).(proto.SessionType)
if !ok {
return proto.SessionType_Public, false
}
return v, true
}
//
// Account
//
// WithAccount adds the account to the context.
//
// TODO: Deprecate this in favor of Session middleware with a JWT token.
func WithAccount(ctx context.Context, account string) context.Context {
return context.WithValue(ctx, ctxKeyAccount, account)
}
// GetAccount returns the account from the context.
func GetAccount(ctx context.Context) (string, bool) {
v, ok := ctx.Value(ctxKeyAccount).(string)
return v, ok
}
//
// User
//
// WithUser adds the user to the context.
//
// TODO: Deprecate this in favor of Session middleware with a JWT token.
func WithUser(ctx context.Context, user any) context.Context {
return context.WithValue(ctx, ctxKeyUser, user)
}
// GetUser returns the user from the context.
func GetUser[T any](ctx context.Context) (*T, bool) {
v, ok := ctx.Value(ctxKeyUser).(*T)
return v, ok
}
//
// Service
//
// WithService adds the service to the context.
//
// TODO: Deprecate this in favor of Session middleware with a JWT token.
func WithService(ctx context.Context, service string) context.Context {
return context.WithValue(ctx, ctxKeyService, service)
}
// GetService returns the service from the context.
func GetService(ctx context.Context) (string, bool) {
v, ok := ctx.Value(ctxKeyService).(string)
return v, ok
}
//
// AccessKey
//
// WithAccessKey adds the access key to the context.
//
// TODO: Deprecate this in favor of Session middleware with a JWT token.
func WithAccessKey(ctx context.Context, accessKey string) context.Context {
return context.WithValue(ctx, ctxKeyAccessKey, accessKey)
}
// GetAccessKey returns the access key from the context.
func GetAccessKey(ctx context.Context) (string, bool) {
v, ok := ctx.Value(ctxKeyAccessKey).(string)
return v, ok
}
//
// Project ID
//
// WithProjectID adds the project to the context.
//
// TODO: Deprecate this in favor of Session middleware with a JWT token.
func WithProjectID(ctx context.Context, project uint64) context.Context {
return context.WithValue(ctx, ctxKeyProjectID, project)
}
// GetProjectID returns the project and if its active from the context.
// In case its not set, it will return 0.
func GetProjectID(ctx context.Context) (uint64, bool) {
v, ok := ctx.Value(ctxKeyProjectID).(uint64)
return v, ok
}
//
// Project
//
// WithProject adds the project to the context.
//
// TODO: Deprecate this in favor of Session middleware with a JWT token.
func WithProject(ctx context.Context, project any) context.Context {
return context.WithValue(ctx, ctxKeyProject, project)
}
// GetProject returns the project from the context.
func GetProject[T any](ctx context.Context) (*T, bool) {
v, ok := ctx.Value(ctxKeyProject).(*T)
return v, ok
}