This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
/
context.go
273 lines (229 loc) · 7.17 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
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
262
263
264
265
266
267
268
269
270
271
272
273
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controller
import (
"context"
"fmt"
"firebase.google.com/go/auth"
"github.com/google/exposure-notifications-verification-server/pkg/database"
"github.com/google/exposure-notifications-verification-server/pkg/observability"
"github.com/gorilla/sessions"
)
// contextKey is a unique type to avoid clashing with other packages that use
// context's to pass data.
type contextKey string
const (
contextKeyAuthorizedApp = contextKey("authorizedApp")
contextKeyFirebaseUser = contextKey("firebaseUser")
contextKeyMembership = contextKey("membership")
contextKeyMemberships = contextKey("memberships")
contextKeyRealm = contextKey("realm")
contextKeyRequestID = contextKey("requestID")
contextKeySession = contextKey("session")
contextKeyTemplate = contextKey("template")
contextKeyUser = contextKey("user")
)
// WithAuthorizedApp stores the authorized app on the context.
func WithAuthorizedApp(ctx context.Context, app *database.AuthorizedApp) context.Context {
m := TemplateMapFromContext(ctx)
m["currentAuthorizedApp"] = app
ctx = WithTemplateMap(ctx, m)
return context.WithValue(ctx, contextKeyAuthorizedApp, app)
}
// AuthorizedAppFromContext retrieves the authorized app from the context. If no
// value exists, it returns nil.
func AuthorizedAppFromContext(ctx context.Context) *database.AuthorizedApp {
v := ctx.Value(contextKeyAuthorizedApp)
if v == nil {
return nil
}
t, ok := v.(*database.AuthorizedApp)
if !ok {
return nil
}
return t
}
// WithRealm stores the current realm on the context and the realm.ID
// on the observability context.
func WithRealm(ctx context.Context, r *database.Realm) context.Context {
m := TemplateMapFromContext(ctx)
m["currentRealm"] = r
ctx = WithTemplateMap(ctx, m)
ctx = observability.WithRealmID(ctx, uint64(r.ID))
return context.WithValue(ctx, contextKeyRealm, r)
}
// RealmFromContext retrieves the realm from the context. If no
// value exists, it returns nil.
func RealmFromContext(ctx context.Context) *database.Realm {
v := ctx.Value(contextKeyRealm)
if v == nil {
return nil
}
t, ok := v.(*database.Realm)
if !ok {
return nil
}
return t
}
// WithRequestID stores the request ID on the context.
func WithRequestID(ctx context.Context, id string) context.Context {
m := TemplateMapFromContext(ctx)
m["requestID"] = id
ctx = WithTemplateMap(ctx, m)
return context.WithValue(ctx, contextKeyRequestID, id)
}
// RequestIDFromContext retrieves the request ID from the context. If no value
// exists, it returns the empty string.
func RequestIDFromContext(ctx context.Context) string {
v := ctx.Value(contextKeyRequestID)
if v == nil {
return ""
}
t, ok := v.(string)
if !ok {
return ""
}
return t
}
// WithSession stores the session on the request's context for retrieval later.
// Use Session(r) to retrieve the session.
func WithSession(ctx context.Context, session *sessions.Session) context.Context {
return context.WithValue(ctx, contextKeySession, session)
}
// SessionFromContext retrieves the session on the provided context. If no
// session exists, or if the value in the context is not of the correct type, it
// returns nil.
func SessionFromContext(ctx context.Context) *sessions.Session {
v := ctx.Value(contextKeySession)
if v == nil {
return nil
}
t, ok := v.(*sessions.Session)
if !ok {
return nil
}
return t
}
// TemplateMap is a typemap for the HTML templates.
type TemplateMap map[string]interface{}
// Title sets the title on the template map. If a title already exists, the new
// value is prepended.
func (m TemplateMap) Title(f string, args ...interface{}) {
if f == "" {
return
}
s := f
if len(args) > 0 {
s = fmt.Sprintf(f, args...)
}
if current := m["title"]; current != "" {
m["title"] = fmt.Sprintf("%s | %s", s, current)
return
}
m["title"] = s
}
// WithTemplateMap creates a context with the given template map.
func WithTemplateMap(ctx context.Context, m TemplateMap) context.Context {
return context.WithValue(ctx, contextKeyTemplate, m)
}
// TemplateMapFromContext gets the template map on the context. If no map
// exists, it returns an empty map.
func TemplateMapFromContext(ctx context.Context) TemplateMap {
v := ctx.Value(contextKeyTemplate)
if v == nil {
return make(TemplateMap)
}
m, ok := v.(TemplateMap)
if !ok {
return make(TemplateMap)
}
return m
}
// WithUser stores the current user on the context.
func WithUser(ctx context.Context, u *database.User) context.Context {
m := TemplateMapFromContext(ctx)
m["currentUser"] = u
ctx = WithTemplateMap(ctx, m)
return context.WithValue(ctx, contextKeyUser, u)
}
// UserFromContext retrieves the user from the context. If no value exists, it
// returns nil.
func UserFromContext(ctx context.Context) *database.User {
v := ctx.Value(contextKeyUser)
if v == nil {
return nil
}
t, ok := v.(*database.User)
if !ok {
return nil
}
return t
}
// WithMemberships stores the user's available memberships on the context.
func WithMemberships(ctx context.Context, u []*database.Membership) context.Context {
m := TemplateMapFromContext(ctx)
m["currentMemberships"] = u
ctx = WithTemplateMap(ctx, m)
return context.WithValue(ctx, contextKeyMemberships, u)
}
// MembershipsFromContext retrieves the membership from the context. If no value
// exists, it returns nil.
func MembershipsFromContext(ctx context.Context) []*database.Membership {
v := ctx.Value(contextKeyMemberships)
if v == nil {
return nil
}
t, ok := v.([]*database.Membership)
if !ok {
return nil
}
return t
}
// WithMembership stores the current membership on the context.
func WithMembership(ctx context.Context, u *database.Membership) context.Context {
m := TemplateMapFromContext(ctx)
m["currentMembership"] = u
ctx = WithTemplateMap(ctx, m)
return context.WithValue(ctx, contextKeyMembership, u)
}
// MembershipFromContext retrieves the membership from the context. If no value
// exists, it returns nil.
func MembershipFromContext(ctx context.Context) *database.Membership {
v := ctx.Value(contextKeyMembership)
if v == nil {
return nil
}
t, ok := v.(*database.Membership)
if !ok {
return nil
}
return t
}
// WithFirebaseUser stores the current firebase user on the context.
func WithFirebaseUser(ctx context.Context, u *auth.UserRecord) context.Context {
return context.WithValue(ctx, contextKeyFirebaseUser, u)
}
// FirebaseUserFromContext retrieves the firebase user from the context. If no value exists, it
// returns nil.
func FirebaseUserFromContext(ctx context.Context) *auth.UserRecord {
v := ctx.Value(contextKeyFirebaseUser)
if v == nil {
return nil
}
t, ok := v.(*auth.UserRecord)
if !ok {
return nil
}
return t
}