-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
linkedCloud.go
102 lines (90 loc) · 2.41 KB
/
linkedCloud.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
package store
import (
"context"
"crypto/tls"
"crypto/x509"
"net/http"
"github.com/plgd-dev/cloud/authorization/oauth"
"github.com/plgd-dev/cloud/cloud2cloud-connector/events"
"github.com/plgd-dev/kit/net/http/transport"
"golang.org/x/oauth2"
)
type Events struct {
Devices []events.EventType `json:"Devices"`
Device []events.EventType `json:"Device"`
Resource []events.EventType `json:"Resource"`
StaticDeviceEvents bool `json:"StaticDeviceEvents"`
}
func makeMap(evs ...events.EventType) map[events.EventType]bool {
m := make(map[events.EventType]bool)
for _, e := range evs {
m[e] = true
}
return m
}
func (e Events) NeedPullDevices() bool {
set := makeMap(events.AllDevicesEvents...)
for _, v := range e.Devices {
delete(set, v)
}
if len(set) != 0 {
return true
}
return false
}
func (e Events) NeedPullDevice() bool {
set := makeMap(events.AllDeviceEvents...)
for _, v := range e.Device {
delete(set, v)
}
if len(set) != 0 {
return true && !e.StaticDeviceEvents
}
return false
}
func (e Events) NeedPullResources() bool {
set := makeMap(events.AllResourceEvents...)
for _, v := range e.Resource {
delete(set, v)
}
if len(set) != 0 {
return true
}
return false
}
type Endpoint struct {
URL string `json:"URL"`
RootCAs []string `json:"RootCAs"`
InsecureSkipVerify bool `json:"InsecureSkipVerify"`
UseSystemCAs bool `json:"UseSystemCAs"`
}
type LinkedCloud struct {
ID string `json:"Id" bson:"_id"`
Name string `json:"Name"`
OAuth oauth.Config `json:"OAuth"`
SupportedSubscriptionsEvents Events `json:"SupportedSubscriptionEvents"`
Endpoint Endpoint `json:"Endpoint"`
}
func (l LinkedCloud) GetHTTPClient() *http.Client {
var pool *x509.CertPool
if l.Endpoint.UseSystemCAs {
pool, _ = x509.SystemCertPool()
}
if pool == nil {
pool = x509.NewCertPool()
}
for _, ca := range l.Endpoint.RootCAs {
pool.AppendCertsFromPEM([]byte(ca))
}
t := transport.NewDefaultTransport()
t.TLSClientConfig = &tls.Config{
RootCAs: pool,
InsecureSkipVerify: l.Endpoint.InsecureSkipVerify,
}
return &http.Client{
Transport: t,
}
}
func (l LinkedCloud) CtxWithHTTPClient(ctx context.Context) context.Context {
return context.WithValue(ctx, oauth2.HTTPClient, l.GetHTTPClient())
}