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
|
package discovery
import (
"encoding/json"
"fmt"
"time"
"github.com/eduvpn/eduvpn-common/internal/http"
"github.com/eduvpn/eduvpn-common/internal/util"
"github.com/eduvpn/eduvpn-common/internal/verify"
"github.com/eduvpn/eduvpn-common/types"
)
type Discovery struct {
Organizations types.DiscoveryOrganizations
Servers types.DiscoveryServers
}
// Helper function that gets a disco json and fills the structure with it
func getDiscoFile(jsonFile string, previousVersion uint64, structure interface{}) error {
errorMessage := fmt.Sprintf("failed getting file: %s from the Discovery server", jsonFile)
// Get json data
discoURL := "https://disco.eduvpn.org/v2/"
fileURL := discoURL + jsonFile
_, fileBody, fileErr := http.HTTPGet(fileURL)
if fileErr != nil {
return types.NewWrappedError(errorMessage, fileErr)
}
// Get signature
sigFile := jsonFile + ".minisig"
sigURL := discoURL + sigFile
_, sigBody, sigFileErr := http.HTTPGet(sigURL)
if sigFileErr != nil {
return types.NewWrappedError(errorMessage, sigFileErr)
}
// Verify signature
// Set this to true when we want to force prehash
forcePrehash := false
verifySuccess, verifyErr := verify.Verify(
string(sigBody),
fileBody,
jsonFile,
previousVersion,
forcePrehash,
)
if !verifySuccess || verifyErr != nil {
return types.NewWrappedError(errorMessage, verifyErr)
}
// Parse JSON to extract version and list
jsonErr := json.Unmarshal(fileBody, structure)
if jsonErr != nil {
return types.NewWrappedError(errorMessage, jsonErr)
}
return nil
}
// FIXME: Implement based on
// https://github.com/eduvpn/documentation/blob/v3/SERVER_DISCOVERY.md
// - [IMPLEMENTED] on "first launch" when offering the search for "Institute Access" and "Organizations";
// - [TODO] when the user tries to add new server AND the user did NOT yet choose an organization before;
// - [TODO] when the authorization for the server associated with an already chosen organization is triggered, e.g. after expiry or revocation.
// - [IMPLEMENTED using a custom error message] NOTE: when the org_id that the user chose previously is no longer available in organization_list.json the application should ask the user to choose their organization (again). This can occur for example when the organization replaced their identity provider, uses a different domain after rebranding or simply ceased to exist.
func (discovery *Discovery) DetermineOrganizationsUpdate() bool {
return discovery.Organizations.Timestamp.IsZero()
}
func (discovery *Discovery) GetSecureLocationList() []string {
var locations []string
for _, server := range discovery.Servers.List {
if server.Type == "secure_internet" {
locations = append(locations, server.CountryCode)
}
}
return locations
}
func (discovery *Discovery) GetServerByURL(
url string,
_type string,
) (*types.DiscoveryServer, error) {
for _, server := range discovery.Servers.List {
if server.BaseURL == url && server.Type == _type {
return &server, nil
}
}
return nil, types.NewWrappedError(
"failed getting server by URL from discovery",
&GetServerByURLNotFoundError{URL: url, Type: _type},
)
}
func (discovery *Discovery) GetServerByCountryCode(
code string,
_type string,
) (*types.DiscoveryServer, error) {
for _, server := range discovery.Servers.List {
if server.CountryCode == code && server.Type == _type {
return &server, nil
}
}
return nil, types.NewWrappedError(
"failed getting server by country code from discovery",
&GetServerByCountryCodeNotFoundError{CountryCode: code, Type: _type},
)
}
func (discovery *Discovery) getOrgByID(orgID string) (*types.DiscoveryOrganization, error) {
for _, organization := range discovery.Organizations.List {
if organization.OrgId == orgID {
return &organization, nil
}
}
return nil, types.NewWrappedError(
"failed getting Secure Internet Home URL from discovery",
&GetOrgByIDNotFoundError{ID: orgID},
)
}
func (discovery *Discovery) GetSecureHomeArgs(
orgID string,
) (*types.DiscoveryOrganization, *types.DiscoveryServer, error) {
errorMessage := "failed getting Secure Internet Home arguments from discovery"
org, orgErr := discovery.getOrgByID(orgID)
if orgErr != nil {
return nil, nil, types.NewWrappedError(errorMessage, orgErr)
}
// Get a server with the base url
url := org.SecureInternetHome
server, serverErr := discovery.GetServerByURL(url, "secure_internet")
if serverErr != nil {
return nil, nil, types.NewWrappedError(errorMessage, serverErr)
}
return org, server, nil
}
// https://github.com/eduvpn/documentation/blob/v3/SERVER_DISCOVERY.md
// - [Implemented] The application MUST always fetch the server_list.json at application start.
// - The application MAY refresh the server_list.json periodically, e.g. once every hour.
func (discovery *Discovery) DetermineServersUpdate() bool {
// No servers, we should update
if discovery.Servers.Timestamp.IsZero() {
return true
}
// 1 hour from the last update
should_update_time := discovery.Servers.Timestamp.Add(1 * time.Hour)
now := util.GetCurrentTime()
return !now.Before(should_update_time)
}
// Get the organization list
func (discovery *Discovery) GetOrganizationsList() (*types.DiscoveryOrganizations, error) {
if !discovery.DetermineOrganizationsUpdate() {
return &discovery.Organizations, nil
}
file := "organization_list.json"
bodyErr := getDiscoFile(file, discovery.Organizations.Version, &discovery.Organizations)
if bodyErr != nil {
// Return previous with an error
return &discovery.Organizations, types.NewWrappedError(
"failed getting organizations in Discovery",
bodyErr,
)
}
discovery.Organizations.Timestamp = util.GetCurrentTime()
return &discovery.Organizations, nil
}
// Get the server list
func (discovery *Discovery) GetServersList() (*types.DiscoveryServers, error) {
if !discovery.DetermineServersUpdate() {
return &discovery.Servers, nil
}
file := "server_list.json"
bodyErr := getDiscoFile(file, discovery.Servers.Version, &discovery.Servers)
if bodyErr != nil {
// Return previous with an error
return &discovery.Servers, types.NewWrappedError(
"failed getting servers in Discovery",
bodyErr,
)
}
// Update servers timestamp
discovery.Servers.Timestamp = util.GetCurrentTime()
return &discovery.Servers, nil
}
type GetOrgByIDNotFoundError struct {
ID string
}
func (e GetOrgByIDNotFoundError) Error() string {
return fmt.Sprintf(
"No Secure Internet Home found in organizations with ID %s. Please choose your server again",
e.ID,
)
}
type GetServerByURLNotFoundError struct {
URL string
Type string
}
func (e GetServerByURLNotFoundError) Error() string {
return fmt.Sprintf(
"No institute access server found in organizations with URL %s and type %s. Please choose your server again",
e.URL,
e.Type,
)
}
type GetServerByCountryCodeNotFoundError struct {
CountryCode string
Type string
}
func (e GetServerByCountryCodeNotFoundError) Error() string {
return fmt.Sprintf(
"No institute access server found in organizations with country code %s and type %s",
e.CountryCode,
e.Type,
)
}
type GetSecureHomeArgsNotFoundError struct {
URL string
}
func (e GetSecureHomeArgsNotFoundError) Error() string {
return fmt.Sprintf(
"No Secure Internet Home found with URL: %s. Please choose your server again",
e.URL,
)
}
|