-
Notifications
You must be signed in to change notification settings - Fork 1
/
connection.go
622 lines (564 loc) · 16.5 KB
/
connection.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/* ****************************************************************************
* Copyright 2020 51 Degrees Mobile Experts Limited (51degrees.com)
*
* 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 swan
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"github.com/SWAN-community/swift-go"
"github.com/SWAN-community/owid-go"
)
// SWAN is the base structure for all actions. It includes the scheme for the
// SWAN Operator URLs, the Operator domain and the access key needed by the
// SWAN Operator.
type SWAN struct {
Scheme string // The HTTP or HTTPS scheme to use for SWAN requests
Operator string // Domain name of the SWAN Operator access node
AccessKey string // SWAN access key provided by the SWAN Operator
}
// Decrypt contains the string to be decrypted via the call to SWAN.
type Decrypt struct {
SWAN
Encrypted string // The encrypted string to be decrypted by SWAN
}
// Client is used for actions where a request from a web browser is available.
// It is mainly used to set the home node from the public IP address of the web
// browser.
type Client struct {
SWAN
Request *http.Request // The HTTP request from the web browser
}
// Operation has members for all the parameters for a storage operation
// involving a URL that is requested by the web browser.
type Operation struct {
Client
// The URL to return to with the encrypted data appended to it.
ReturnUrl string
// The access node that will be used to decrypt the result of the storage
// operation. Defaults to the access node that started the storage
// operation.
AccessNode string
Title string // The title of the progress UI page.
Message string // The text of the message in the progress UI.
ProgressColor string // The HTML color for the progress indicator.
BackgroundColor string // The HTML color for the progress UI background.
MessageColor string // The HTML color for the message text.
NodeCount int // Number of storage nodes to use for operations.
// DisplayUserInterface true if a progress UI should be displayed during the
// storage operation, otherwise false.
DisplayUserInterface bool
// PostMessageOnComplete true if at the end of the operation the resulting
// data should be returned to the parent using JavaScript postMessage,
// otherwise false. Default false.
PostMessageOnComplete bool
// UseHomeNode true if the home node can be used if it contains current
// data. False if the SWAN network should be consulted irrespective of the
// state of data held on the home node. Default true.
UseHomeNode bool
// JavaScript true if the response for storage operations should be
// JavaScript include that will continue the operation. This feature
// requires cookies to be sent for DOM inserted JavaScript elements. Default
// false.
JavaScript bool
// Optional array of strings that can be used to pass state information to
// the party that retrieves the results of the storage operation. For
// example; passing information between a Publisher and User Interface
// Provider such as a CMP in the storage operation.
State []string
}
// Update operation from a User Interface Provider where the preferences, email
// and salt have been captured. The SWID is returned from a previous call to
// swan.CreateSWID.
type Update struct {
Operation
swid *owid.OWID
pref *owid.OWID
email *owid.OWID
salt *owid.OWID
}
// Fetch operation to retrieve the SWAN data for use with a call to Decrypt or
// DecryptRaw.
type Fetch struct {
Operation
Existing []*Pair // Existing SWAN data pairs
}
// Stop operation to block an advert domain or identifier.
type Stop struct {
Operation
Host string // Advert host to block
}
// Connection stores the static details that are used when creating a new swan
// request.
type Connection struct {
operation Operation
}
// NewConnection creates a new SWAN connection based on the operation provided.
func NewConnection(operation Operation) *Connection {
return &Connection{operation: operation}
}
// NewFetch creates a new fetch operation using the default in the connection.
//
// request http request from a web browser
//
// returnUrl return URL after the operation completes
//
// existing if any values already exist then use these if none are available in
// SWAN
func (c *Connection) NewFetch(
request *http.Request,
returnUrl string,
existing []*Pair) *Fetch {
f := Fetch{}
f.Operation = c.operation
f.Request = request
f.ReturnUrl = returnUrl
f.Existing = existing
return &f
}
// NewUpdate creates a new fetch operation using the default in the connection.
//
// request http request from a web browser
//
// returnUrl return URL after the operation completes
func (c *Connection) NewUpdate(
request *http.Request,
returnUrl string) *Update {
p := Update{}
p.Operation = c.operation
p.Request = request
p.ReturnUrl = returnUrl
return &p
}
// NewStop creates a new stop operation using the default in the connection.
//
// request http request from a web browser
//
// returnUrl return URL after the operation completes
//
// host associated with the advert to stop
func (c *Connection) NewStop(
request *http.Request,
returnUrl string,
host string) *Stop {
s := Stop{}
s.Operation = c.operation
s.Request = request
s.ReturnUrl = returnUrl
s.Host = host
return &s
}
// NewClient creates a new request.
//
// request http request from a web browser
func (c *Connection) NewClient(request *http.Request) *Client {
l := Client{}
l.SWAN = c.operation.SWAN
l.Request = request
return &l
}
// NewDecrypt creates a new decrypt request using the default in the
// connection.
//
// encrypted the base 64 encoded SWAN data to be decrypted
func (c *Connection) NewDecrypt(encrypted string) *Decrypt {
e := Decrypt{}
e.SWAN = c.operation.SWAN
e.Encrypted = encrypted
return &e
}
// NewSWAN creates a new request using the default in the connection.
func (c *Connection) NewSWAN() *SWAN {
s := c.operation.SWAN
return &s
}
// GetURL contacts the SWAN operator domain with the access key and returns a
// URL string that the web browser should be immediately directed to.
func (f *Fetch) GetURL() (string, *Error) {
q := url.Values{}
err := f.setData(&q)
if err != nil {
return "", &Error{Err: err}
}
return requestAsString(&f.SWAN, "fetch", q)
}
// SetSWID verifies that the base 64 SWID string is an OWID and sets the value.
//
// swid base 64 encoded SWID
func (u *Update) SetSWID(swid string) error {
var err error
u.swid, err = owid.FromBase64(swid)
return err
}
// SWID gets the SWID if previously provided via SetSWID.
func (u *Update) SWID() *owid.OWID { return u.swid }
// SetEmail turns the email provided into an OWID using the creator.
//
// creator register OWID creator for the User Interface Provider
//
// email provided by the user
func (u *Update) SetEmail(creator *owid.Creator, email string) error {
var err error
u.email, err = creator.CreateOWIDandSign([]byte(email))
return err
}
// SetEmailFromOWID passed a base 64 encoded OWID as the email.
func (u *Update) SetEmailFromOWID(emailOWID string) error {
var err error
u.email, err = owid.FromBase64(emailOWID)
return err
}
// Email gets the Email if previously provided via SetEmail.
func (u *Update) Email() *owid.OWID { return u.email }
// SetSalt turns the salt provided into an OWID using the creator.
//
// creator register OWID creator for the User Interface Provider
//
// salt base 64 encoded salt string from salt-js
func (u *Update) SetSalt(creator *owid.Creator, salt string) error {
var err error
u.salt, err = creator.CreateOWIDandSign([]byte(salt))
return err
}
// SetSaltFromOWID passed a base 64 encoded OWID as the salt.
func (u *Update) SetSaltFromOWID(saltOWID string) error {
var err error
u.salt, err = owid.FromBase64(saltOWID)
return err
}
// Salt gets the Salt if previously provided via SetSalt.
func (u *Update) Salt() *owid.OWID { return u.salt }
// SetPref turns the preference flag provided into an OWID using the creator.
//
// creator register OWID creator for the User Interface Provider
//
// pref indicator of personalized marketing
func (u *Update) SetPref(creator *owid.Creator, pref bool) error {
var err error
var s string
if pref == true {
s = "on"
} else {
s = "off"
}
u.pref, err = creator.CreateOWIDandSign([]byte(s))
return err
}
// SetPrefFromOWID passed a base 64 encoded OWID as the preference.
func (u *Update) SetPrefFromOWID(prefOWID string) error {
var err error
u.pref, err = owid.FromBase64(prefOWID)
return err
}
// Pref gets the Pref if previously provided via SetPref.
func (u *Update) Pref() *owid.OWID { return u.pref }
// GetURL contacts the SWAN operator domain with the access key and returns a
// URL string that the web browser should be directed to.
func (u *Update) GetURL() (string, *Error) {
q := url.Values{}
err := u.setData(&q)
if err != nil {
return "", &Error{Err: err}
}
return requestAsString(&u.SWAN, "update", q)
}
// GetValues returns the values that can be used to configure a web browser with
// the information contained in the Update operation. Ensure the access key and
// other values that are specific to an operation are not included in the
// resulting values.
func (u *Update) GetValues() (url.Values, error) {
q := url.Values{}
err := u.setData(&q)
if err != nil {
return nil, err
}
q.Del("accessKey") // Known only to this party and must never be shared
q.Del("swid") // Not to be shared with other browsers
// Used for home node operations that depend on the specific browser
q.Del("remoteAddr")
q.Del("X-Forwarded-For")
return q, nil
}
// GetURL contacts the SWAN operator domain with the access key and returns a
// URL string that the web browser should be directed to.
func (s *Stop) GetURL() (string, *Error) {
q := url.Values{}
err := s.setData(&q)
if err != nil {
return "", &Error{Err: err}
}
return requestAsString(&s.SWAN, "stop", q)
}
// Decrypt returns SWAN key value pairs for the data contained in the encrypted
// string.
func (c *Connection) Decrypt(encrypted string) ([]*Pair, *Error) {
return c.NewDecrypt(encrypted).decrypt()
}
// DecryptRaw returns key value pairs for the raw SWAN data contained in the
// encrypted string. Must only be used by User Interface Providers.
func (c *Connection) DecryptRaw(
encrypted string) (map[string]interface{}, *Error) {
return c.NewDecrypt(encrypted).decryptRaw()
}
// CreateSWID returns a new SWID in OWID format from the SWAN Operator. Only
// SWAN Operators can create legitimate SWIDs.
func (c *Connection) CreateSWID() (*owid.OWID, *Error) {
return c.NewSWAN().createSWID()
}
// HomeNode returns the SWAN home node associated with the web browser.
func (c *Connection) HomeNode(r *http.Request) (string, *Error) {
return c.NewClient(r).homeNode()
}
func (c *Client) homeNode() (string, *Error) {
q := url.Values{}
err := c.setData(&q)
if err != nil {
return "", &Error{Err: err}
}
return requestAsString(&c.SWAN, "home-node", q)
}
func (e *Decrypt) decrypt() ([]*Pair, *Error) {
var p []*Pair
q := url.Values{}
err := e.setData(&q)
if err != nil {
return nil, &Error{Err: err}
}
b, se := requestAsByteArray(&e.SWAN, "decrypt", q)
if se != nil {
return nil, se
}
err = json.Unmarshal(b, &p)
if err != nil {
return nil, &Error{Err: err}
}
return p, nil
}
func (e *Decrypt) decryptRaw() (map[string]interface{}, *Error) {
r := make(map[string]interface{})
q := url.Values{}
err := e.setData(&q)
if err != nil {
return nil, &Error{Err: err}
}
b, se := requestAsByteArray(&e.SWAN, "decrypt-raw", q)
if se != nil {
return nil, se
}
err = json.Unmarshal(b, &r)
if err != nil {
return nil, &Error{Err: err}
}
return r, nil
}
func (s *SWAN) createSWID() (*owid.OWID, *Error) {
b, se := requestAsByteArray(s, "create-swid", url.Values{})
if se != nil {
return nil, se
}
o, err := owid.FromByteArray(b)
if err != nil {
return nil, &Error{Err: err}
}
return o, nil
}
func requestAsByteArray(
s *SWAN,
a string,
q url.Values) ([]byte, *Error) {
// Verify the provided parameters.
if s.Scheme == "" {
return nil, &Error{Err: fmt.Errorf("scheme must be provided")}
}
if s.Operator == "" {
return nil, &Error{Err: fmt.Errorf("operator must be provided")}
}
if s.AccessKey == "" {
return nil, &Error{Err: fmt.Errorf("accessKey must be provided")}
}
// Construct the SWAN URL.
var u url.URL
u.Scheme = s.Scheme
u.Host = s.Operator
u.Path = "/swan/api/v1/" + a
// Add the access key to the data.
q.Set("accessKey", s.AccessKey)
// Post the parameters to the SWAN url.
res, err := http.PostForm(u.String(), q)
if err != nil {
return nil, &Error{Err: err}
}
// Read the response.
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, &Error{Err: err, Response: res}
}
// If the status code is not OK then return the response and status code
// as an error message.
if res.StatusCode != http.StatusOK {
return nil, &Error{Err: fmt.Errorf(string(b)), Response: res}
}
return b, nil
}
func requestAsString(
s *SWAN,
a string,
q url.Values) (string, *Error) {
b, err := requestAsByteArray(s, a, q)
if err != nil {
return "", err
}
return string(b), nil
}
func (c *Client) setData(q *url.Values) error {
if c.Request == nil {
return fmt.Errorf("Request required")
}
swift.SetHomeNodeHeaders(c.Request, q)
return nil
}
func (e *Decrypt) setData(q *url.Values) error {
if e.Encrypted == "" {
return fmt.Errorf("Encrypted required")
}
q.Set("encrypted", e.Encrypted)
return nil
}
func (s *Stop) setData(q *url.Values) error {
err := s.Operation.setData(q)
if err != nil {
return err
}
if s.Host == "" {
return fmt.Errorf("host required")
}
q.Set("host", s.Host)
return nil
}
func (o *Operation) setData(q *url.Values) error {
err := o.Client.setData(q)
if err != nil {
return err
}
if o.ReturnUrl == "" {
return fmt.Errorf("ReturnURL required")
}
_, err = url.Parse(o.ReturnUrl)
if err != nil {
return err
}
q.Set("returnUrl", o.ReturnUrl)
if o.AccessNode != "" {
q.Set("accessNode", o.AccessNode)
}
if o.Title != "" {
q.Set("title", o.Title)
}
if o.Message != "" {
q.Set("message", o.Message)
}
if o.ProgressColor != "" {
q.Set("progressColor", o.ProgressColor)
}
if o.BackgroundColor != "" {
q.Set("backgroundColor", o.BackgroundColor)
}
if o.MessageColor != "" {
q.Set("messageColor", o.MessageColor)
}
if o.NodeCount != 0 {
q.Set("nodeCount", fmt.Sprintf("%d", o.NodeCount))
}
q.Set("displayUserInterface", fmt.Sprintf("%t",
o.DisplayUserInterface))
q.Set("postMessageOnComplete", fmt.Sprintf("%t",
o.PostMessageOnComplete))
q.Set("useHomeNode", fmt.Sprintf("%t", o.UseHomeNode))
q.Set("javaScript", fmt.Sprintf("%t", o.JavaScript))
for _, s := range o.State {
q.Add("state", s)
}
return nil
}
func (f *Fetch) setData(q *url.Values) error {
err := f.Operation.setData(q)
if err != nil {
return err
}
if f.Existing != nil {
for _, v := range f.Existing {
if v.Key == "swid" || v.Key == "pref" {
_, err := owid.FromBase64(v.Value)
if err != nil {
return err
}
q.Set(v.Key, v.Value)
}
}
}
return nil
}
func (u *Update) setData(q *url.Values) error {
var s string
err := u.Operation.setData(q)
if err != nil {
return err
}
if u.swid != nil {
s, err = u.swid.AsBase64()
if err != nil {
return err
}
q.Set("swid", s)
}
if u.pref != nil {
s, err = u.pref.AsBase64()
if err != nil {
return err
}
q.Set("pref", s)
}
if u.email != nil {
s, err = u.email.AsBase64()
if err != nil {
return err
}
q.Set("email", s)
}
if u.salt != nil {
s, err = u.salt.AsBase64()
if err != nil {
return err
}
q.Set("salt", s)
}
return nil
}
// setSWANData uses the creator to turn the value v into an OWID before setting
// that OWID as a base 64 string in the query values q against the key k.
// c owid creator for the User Interface Provider
// q collection of key value pairs
// k the key for the SWAN value
// v the raw value to be used as the payload for the OWID
func setSWANData(c *owid.Creator, q *url.Values, k string, v []byte) error {
o, err := c.CreateOWIDandSign(v)
if err != nil {
return err
}
q.Set(k, o.AsString())
return nil
}