This repository has been archived by the owner on Apr 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
opts.go
372 lines (313 loc) · 9.79 KB
/
opts.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package firebase
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"strconv"
"time"
"cloud.google.com/go/compute/metadata"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"github.com/knq/jwt/gserviceaccount"
)
const (
// DefaultTokenExpiration is the default expiration for generated OAuth2
// tokens.
DefaultTokenExpiration = 1 * time.Hour
)
// requiredScopes are the oauth2 scopes required when using Google service
// accounts with firebase.
var requiredScopes = []string{
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/firebase.database",
// will this be required in the future?
//"https://www.googleapis.com/auth/identitytoolkit",
}
// Option is an option to modify a Firebase database ref.
type Option func(r *DatabaseRef) error
// URL is an option to set Firebase database base ref (ie, URL) to urlstr.
func URL(urlstr string) Option {
return func(r *DatabaseRef) error {
u, err := url.Parse(urlstr)
if err != nil {
return fmt.Errorf("could not parse url: %v", err)
}
r.url = u
return nil
}
}
// ProjectID is an option that sets the Firebase database base ref (ie, URL) as
// https://<projectID>.firebaseio.com/.
func ProjectID(projectID string) Option {
return func(r *DatabaseRef) error {
if projectID == "" {
return errors.New("project id cannot be empty")
}
// set url
err := URL("https://" + projectID + ".firebaseio.com/")(r)
if err != nil {
return errors.New("invalid project id")
}
return nil
}
}
// Transport is an option to set the underlying HTTP transport used when making
// requests against a Firebase database ref.
func Transport(roundTripper http.RoundTripper) Option {
return func(r *DatabaseRef) error {
r.transport = roundTripper
return nil
}
}
// WatchBufferLen is an option that sets the channel buffer size for the
// returned event channels from Watch and Listen.
func WatchBufferLen(len int) Option {
return func(r *DatabaseRef) error {
r.watchBufLen = len
return nil
}
}
// GoogleServiceAccountCredentialsJSON is an option that loads Google Service
// Account credentials for use with the Firebase database ref from a JSON
// encoded buf.
//
// Google Service Account credentials can be downloaded from the Google Cloud
// console: https://console.cloud.google.com/iam-admin/serviceaccounts/
func GoogleServiceAccountCredentialsJSON(buf []byte) Option {
return func(r *DatabaseRef) error {
var err error
// load service account credentials
gsa, err := gserviceaccount.FromJSON(buf)
if err != nil {
return err
}
// simple check
if gsa.ProjectID == "" || gsa.ClientEmail == "" || gsa.PrivateKey == "" {
return errors.New("google service account credentials missing project_id, client_email or private_key")
}
// set ref url
err = ProjectID(gsa.ProjectID)(r)
if err != nil {
return err
}
// create token source
ts, err := gsa.TokenSource(nil, requiredScopes...)
if err != nil {
return err
}
// as of v4 it appears that including the subject with the token is
// longer necessary, and will cause a 401 unauthorized error with newly
// created firebase databases.
//
// add subject
/*err = bearer.Claim("sub", gsa.ClientEmail)(ts)
if err != nil {
return err
}*/
// wrap with a reusable token source
r.source = oauth2.ReuseTokenSource(nil, ts)
return nil
}
}
// GoogleServiceAccountCredentialsFile is an option that loads Google Service
// Account credentials for use with the Firebase database ref from the
// specified file.
//
// Google Service Account credentials can be downloaded from the Google Cloud
// console: https://console.cloud.google.com/iam-admin/serviceaccounts/
func GoogleServiceAccountCredentialsFile(path string) Option {
return func(r *DatabaseRef) error {
buf, err := ioutil.ReadFile(path)
if err != nil {
return fmt.Errorf("could not read google service account credentials file: %v", err)
}
return GoogleServiceAccountCredentialsJSON(buf)(r)
}
}
// GoogleComputeCredentials is an option that loads the Google Service Account
// credentials from the GCE metadata associated with the GCE compute instance.
// If serviceAccount is empty, then the default service account credentials
// associated with the GCE instance will be used.
func GoogleComputeCredentials(serviceAccount string) Option {
return func(r *DatabaseRef) error {
var err error
// get compute metadata scopes associated with the service account
scopes, err := metadata.Scopes(serviceAccount)
if err != nil {
return err
}
// check if all the necessary scopes are provided
for _, s := range requiredScopes {
if !sliceContains(scopes, s) {
// NOTE: if you are seeing this error, you probably need to
// recreate your compute instance with the correct scope
//
// as of August 2016, there is not a way to add a scope to an
// existing compute instance
return fmt.Errorf("missing required scope %s in compute metadata", s)
}
}
// get compute metadata project id
projectID, err := metadata.ProjectID()
if err != nil {
return err
}
if projectID == "" {
return errors.New("could not retrieve project id from compute metadata service")
}
// set ref url
err = ProjectID(projectID)(r)
if err != nil {
return err
}
// set transport as the oauth2.Transport
return Transport(&oauth2.Transport{
Source: google.ComputeTokenSource(serviceAccount),
Base: r.transport,
})(r)
}
}
// DefaultQueryOptions is an option that sets the default query options on the
// database ref.
func DefaultQueryOptions(opts ...QueryOption) Option {
return func(r *DatabaseRef) error {
r.rw.Lock()
defer r.rw.Unlock()
r.queryOpts = opts
return nil
}
}
// DefaultAuthOverride is an option that sets the default
// auth_variable_override variable on the database ref.
func DefaultAuthOverride(val interface{}) Option {
return func(r *DatabaseRef) error {
return DefaultQueryOptions(AuthOverride(val))(r)
}
}
// DefaultAuthUID is an option that sets the default auth user id ("uid") via
// the auth_variable_override on the database ref.
func DefaultAuthUID(uid string) Option {
return func(r *DatabaseRef) error {
return DefaultQueryOptions(AuthUID(uid))(r)
}
}
// httpLogger handles logging http requests and responses.
type httpLogger struct {
transport http.RoundTripper
requestLogf, responseLogf Logf
}
// RoundTrip satisfies the http.RoundTripper interface.
func (hl *httpLogger) RoundTrip(req *http.Request) (*http.Response, error) {
trans := hl.transport
if trans == nil {
trans = http.DefaultTransport
}
reqBody, _ := httputil.DumpRequestOut(req, true)
res, err := trans.RoundTrip(req)
resBody, _ := httputil.DumpResponse(res, true)
hl.requestLogf("%s", reqBody)
hl.responseLogf("%s", resBody)
return res, err
}
// Logf is a logging func.
type Logf func(string, ...interface{})
// Log is an option that writes all HTTP request and response data to the
// respective logger.
//
// NOTE: this Option will not work with Watch/Listen.
func Log(requestLogf, responseLogf Logf) Option {
return func(r *DatabaseRef) error {
return Transport(&httpLogger{
transport: r.transport,
requestLogf: requestLogf,
responseLogf: responseLogf,
})(r)
}
}
// QueryOption is an option used to modify the underlying http.Request for
// Firebase.
type QueryOption func(url.Values) error
// Shallow is a query option that toggles a query to return shallow result (ie, the keys only).
func Shallow(v url.Values) error {
v.Add("shallow", "true")
return nil
}
// PrintPretty is a query option that toggles pretty formatting for query
// results.
func PrintPretty(v url.Values) error {
v.Add("print", "pretty")
return nil
}
// jsonQuery returns a QueryOption for a field and json encodes the val.
func jsonQuery(field string, val interface{}) QueryOption {
// json encode
buf, err := json.Marshal(val)
if err != nil {
err = fmt.Errorf("could not marshal query option: %v", err)
}
return func(v url.Values) error {
if err != nil {
return err
}
v.Add(field, string(buf))
return nil
}
}
// uintQuery returns a QueryOption for a field that converts n into a string.
func uintQuery(field string, n uint) QueryOption {
val := strconv.FormatUint(uint64(n), 10)
return func(v url.Values) error {
v.Add(field, val)
return nil
}
}
// OrderBy is a query option that sets Firebase's returned result order.
func OrderBy(field string) QueryOption {
return jsonQuery("orderBy", field)
}
// EqualTo is a query option that sets the order by filter to equalTo val.
func EqualTo(val interface{}) QueryOption {
return jsonQuery("equalTo", val)
}
// StartAt is a query option that sets the order by filter to startAt val.
func StartAt(val interface{}) QueryOption {
return jsonQuery("startAt", val)
}
// EndAt is a query option that sets the order by filter to endAt val.
func EndAt(val interface{}) QueryOption {
return jsonQuery("endAt", val)
}
// AuthOverride is a query option that sets the auth_variable_override.
func AuthOverride(val interface{}) QueryOption {
return jsonQuery("auth_variable_override", val)
}
// AuthUID is a query option that sets the auth user id ("uid") via the
// auth_variable_override for a single query.
func AuthUID(uid string) QueryOption {
return AuthOverride(map[string]interface{}{
"uid": uid,
})
}
// LimitToFirst is a query option that limit's Firebase's returned results to
// the first n items.
func LimitToFirst(n uint) QueryOption {
return uintQuery("limitToFirst", n)
}
// LimitToLast is a query option that limit's Firebase's returned results to
// the last n items.
func LimitToLast(n uint) QueryOption {
return uintQuery("limitToLast", n)
}
// sliceContains returns true if haystack contains needle.
func sliceContains(haystack []string, needle string) bool {
for _, s := range haystack {
if s == needle {
return true
}
}
return false
}