forked from supabase/auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
261 lines (216 loc) · 6.84 KB
/
context.go
File metadata and controls
261 lines (216 loc) · 6.84 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package api
import (
"context"
"net/url"
"github.com/gofrs/uuid"
jwt "github.com/golang-jwt/jwt/v5"
"github.com/supabase/auth/internal/api/shared"
"github.com/supabase/auth/internal/models"
)
type contextKey string
func (c contextKey) String() string {
return "gotrue api context key " + string(c)
}
const (
externalProviderTypeKey = contextKey("external_provider_type")
externalProviderEmailOptionalKey = contextKey("external_provider_allow_no_email")
tokenKey = contextKey("jwt")
inviteTokenKey = contextKey("invite_token")
signatureKey = contextKey("signature")
targetUserKey = contextKey("target_user")
factorKey = contextKey("factor")
sessionKey = contextKey("session")
externalReferrerKey = contextKey("external_referrer")
functionHooksKey = contextKey("function_hooks")
adminUserKey = contextKey("admin_user")
oauthTokenKey = contextKey("oauth_token") // for OAuth1.0, also known as request token
oauthVerifierKey = contextKey("oauth_verifier")
ssoProviderKey = contextKey("sso_provider")
externalHostKey = contextKey("external_host")
flowStateKey = contextKey("flow_state_id")
oauthClientStateKey = contextKey("oauth_client_state_id")
)
// withToken adds the JWT token to the context.
func withToken(ctx context.Context, token *jwt.Token) context.Context {
return context.WithValue(ctx, tokenKey, token)
}
// getToken reads the JWT token from the context.
func getToken(ctx context.Context) *jwt.Token {
obj := ctx.Value(tokenKey)
if obj == nil {
return nil
}
return obj.(*jwt.Token)
}
func getClaims(ctx context.Context) *AccessTokenClaims {
token := getToken(ctx)
if token == nil {
return nil
}
return token.Claims.(*AccessTokenClaims)
}
// withUser adds the user to the context.
func withUser(ctx context.Context, u *models.User) context.Context {
return shared.WithUser(ctx, u)
}
// withTargetUser adds the target user for linking to the context.
func withTargetUser(ctx context.Context, u *models.User) context.Context {
return context.WithValue(ctx, targetUserKey, u)
}
// with Factor adds the factor id to the context.
func withFactor(ctx context.Context, f *models.Factor) context.Context {
return context.WithValue(ctx, factorKey, f)
}
// getUser reads the user from the context.
func getUser(ctx context.Context) *models.User {
return shared.GetUser(ctx)
}
// getTargetUser reads the user from the context.
func getTargetUser(ctx context.Context) *models.User {
if ctx == nil {
return nil
}
obj := ctx.Value(targetUserKey)
if obj == nil {
return nil
}
return obj.(*models.User)
}
// getFactor reads the factor id from the context
func getFactor(ctx context.Context) *models.Factor {
obj := ctx.Value(factorKey)
if obj == nil {
return nil
}
return obj.(*models.Factor)
}
// withSession adds the session to the context.
func withSession(ctx context.Context, s *models.Session) context.Context {
return context.WithValue(ctx, sessionKey, s)
}
// getSession reads the session from the context.
func getSession(ctx context.Context) *models.Session {
if ctx == nil {
return nil
}
obj := ctx.Value(sessionKey)
if obj == nil {
return nil
}
return obj.(*models.Session)
}
// withSignature adds the provided request ID to the context.
func withSignature(ctx context.Context, id string) context.Context {
return context.WithValue(ctx, signatureKey, id)
}
func withInviteToken(ctx context.Context, token string) context.Context {
return context.WithValue(ctx, inviteTokenKey, token)
}
func withFlowStateID(ctx context.Context, FlowStateID string) context.Context {
return context.WithValue(ctx, flowStateKey, FlowStateID)
}
func getFlowStateID(ctx context.Context) string {
obj := ctx.Value(flowStateKey)
if obj == nil {
return ""
}
return obj.(string)
}
func withOAuthClientStateID(ctx context.Context, oauthClientStateID uuid.UUID) context.Context {
return context.WithValue(ctx, oauthClientStateKey, oauthClientStateID)
}
func getOAuthClientStateID(ctx context.Context) uuid.UUID {
obj := ctx.Value(oauthClientStateKey)
if obj == nil {
return uuid.Nil
}
return obj.(uuid.UUID)
}
func getInviteToken(ctx context.Context) string {
obj := ctx.Value(inviteTokenKey)
if obj == nil {
return ""
}
return obj.(string)
}
// withExternalProviderType adds the provided request ID to the context.
func withExternalProviderType(ctx context.Context, id string, emailOptional bool) context.Context {
return context.WithValue(context.WithValue(ctx, externalProviderTypeKey, id), externalProviderEmailOptionalKey, emailOptional)
}
// getExternalProviderType returns the provider type and whether user data without email address should be allowed.
func getExternalProviderType(ctx context.Context) (string, bool) {
idValue := ctx.Value(externalProviderTypeKey)
emailOptionalValue := ctx.Value(externalProviderEmailOptionalKey)
id, okID := idValue.(string)
if !okID {
return "", false
}
emailOptional, okEmailOptional := emailOptionalValue.(bool)
if !okEmailOptional {
return "", false
}
return id, emailOptional
}
func withExternalReferrer(ctx context.Context, token string) context.Context {
return context.WithValue(ctx, externalReferrerKey, token)
}
func getExternalReferrer(ctx context.Context) string {
obj := ctx.Value(externalReferrerKey)
if obj == nil {
return ""
}
return obj.(string)
}
// withAdminUser adds the admin user to the context.
func withAdminUser(ctx context.Context, u *models.User) context.Context {
return context.WithValue(ctx, adminUserKey, u)
}
// getAdminUser reads the admin user from the context.
func getAdminUser(ctx context.Context) *models.User {
obj := ctx.Value(adminUserKey)
if obj == nil {
return nil
}
return obj.(*models.User)
}
// withRequestToken adds the request token to the context
func withRequestToken(ctx context.Context, token string) context.Context {
return context.WithValue(ctx, oauthTokenKey, token)
}
func getRequestToken(ctx context.Context) string {
obj := ctx.Value(oauthTokenKey)
if obj == nil {
return ""
}
return obj.(string)
}
func withOAuthVerifier(ctx context.Context, token string) context.Context {
return context.WithValue(ctx, oauthVerifierKey, token)
}
func getOAuthVerifier(ctx context.Context) string {
obj := ctx.Value(oauthVerifierKey)
if obj == nil {
return ""
}
return obj.(string)
}
func withSSOProvider(ctx context.Context, provider *models.SSOProvider) context.Context {
return context.WithValue(ctx, ssoProviderKey, provider)
}
func getSSOProvider(ctx context.Context) *models.SSOProvider {
obj := ctx.Value(ssoProviderKey)
if obj == nil {
return nil
}
return obj.(*models.SSOProvider)
}
func withExternalHost(ctx context.Context, u *url.URL) context.Context {
return context.WithValue(ctx, externalHostKey, u)
}
func getExternalHost(ctx context.Context) *url.URL {
obj := ctx.Value(externalHostKey)
if obj == nil {
return nil
}
return obj.(*url.URL)
}