This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
/
context.go
140 lines (118 loc) · 3.78 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
// 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"
"github.com/google/exposure-notifications-verification-server/pkg/database"
"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")
contextKeyRealm = contextKey("realm")
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 {
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.
func WithRealm(ctx context.Context, r *database.Realm) context.Context {
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
}
// 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{}
// WithTemplateMap sets the user in the context.
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 {
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
}