-
Notifications
You must be signed in to change notification settings - Fork 226
/
communications.rs
597 lines (554 loc) · 21.3 KB
/
communications.rs
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! Server Communications.
//!
//! Handles however communication to and from the remote Push Server should be done. For Desktop
//! this will be over Websocket. For mobile, it will probably be calls into the local operating
//! system and HTTPS to the web push server.
//!
//! In the future, it could be using gRPC and QUIC, or quantum relay.
use serde_derive::*;
use serde_json::Value;
use std::collections::{HashMap, HashSet};
use std::iter::FromIterator;
use url::Url;
use viaduct::{header_names, status_codes, Headers, Request};
use crate::config::PushConfiguration;
use crate::error::{
self,
ErrorKind::{AlreadyRegisteredError, CommunicationError, CommunicationServerError},
};
use crate::storage::Store;
#[derive(Debug)]
pub struct RegisterResponse {
/// The UAID associated with the request
pub uaid: String,
/// The Channel ID associated with the request
pub channel_id: String,
/// Auth token for subsequent calls (note, only generated on new UAIDs)
pub secret: Option<String>,
/// Push endpoint for 3rd parties
pub endpoint: String,
/// The Sender/Group ID echoed back (if applicable.)
pub senderid: Option<String>,
}
#[serde(untagged)]
#[derive(Serialize, Deserialize)]
pub enum BroadcastValue {
Value(String),
Nested(HashMap<String, BroadcastValue>),
}
/// A new communication link to the Autopush server
pub trait Connection {
// get the connection UAID
// TODO [conv]: reset_uaid(). This causes all known subscriptions to be reset.
/// send a new subscription request to the server, get back the server registration response.
fn subscribe(
&mut self,
channel_id: &str,
app_server_key: Option<&str>,
) -> error::Result<RegisterResponse>;
/// Drop an endpoint
fn unsubscribe(&self, channel_id: Option<&str>) -> error::Result<bool>;
/// Update the autopush server with the new native OS Messaging authorization token
fn update(&mut self, new_token: &str) -> error::Result<bool>;
/// Get a list of server known channels.
fn channel_list(&self) -> error::Result<Vec<String>>;
/// Verify that the known channel list matches up with the server list. If this fails, regenerate endpoints.
/// This should be performed once a day.
fn verify_connection(&self, channels: &[String]) -> error::Result<bool>;
/// Add one or more new broadcast subscriptions.
fn broadcast_subscribe(&self, broadcast: BroadcastValue) -> error::Result<BroadcastValue>;
/// get the list of broadcasts
fn broadcasts(&self) -> error::Result<BroadcastValue>;
//impl TODO: Handle a Ping response with updated Broadcasts.
//impl TODO: Handle an incoming Notification
}
/// Connect to the Autopush server via the HTTP interface
pub struct ConnectHttp {
pub options: PushConfiguration,
pub uaid: Option<String>,
pub auth: Option<String>, // Server auth token
}
// Connect to the Autopush server
pub fn connect(
options: PushConfiguration,
uaid: Option<String>,
auth: Option<String>,
) -> error::Result<ConnectHttp> {
// find connection via options
if options.socket_protocol.is_some() && options.http_protocol.is_some() {
return Err(
CommunicationError("Both socket and HTTP protocols cannot be set.".to_owned()).into(),
);
};
if options.socket_protocol.is_some() {
return Err(CommunicationError("Unsupported".to_owned()).into());
};
if options.bridge_type.is_some() && options.registration_id.is_none() {
return Err(CommunicationError(
"Missing Registration ID, please register with OS first".to_owned(),
)
.into());
};
let connection = ConnectHttp {
uaid,
options,
auth,
};
Ok(connection)
}
impl ConnectHttp {
fn headers(&self) -> error::Result<Headers> {
let mut headers = Headers::new();
if self.auth.is_some() {
headers
.insert(
header_names::AUTHORIZATION,
format!("webpush {}", self.auth.clone().unwrap()),
)
.map_err(|e| {
error::ErrorKind::CommunicationError(format!("Header error: {:?}", e))
})?;
};
Ok(headers)
}
}
impl Connection for ConnectHttp {
/// send a new subscription request to the server, get back the server registration response.
fn subscribe(
&mut self,
channel_id: &str,
app_server_key: Option<&str>,
) -> error::Result<RegisterResponse> {
// check that things are set
if self.options.http_protocol.is_none() || self.options.bridge_type.is_none() {
return Err(
CommunicationError("Bridge type or application id not set.".to_owned()).into(),
);
}
let options = self.options.clone();
let bridge_type = &options.bridge_type.unwrap();
let mut url = format!(
"{}://{}/v1/{}/{}/registration",
&options.http_protocol.unwrap(),
&options.server_host,
&bridge_type,
&options.sender_id
);
// Add the Authorization header if we have a prior subscription.
if let Some(uaid) = &self.uaid {
url.push('/');
url.push_str(&uaid);
url.push_str("/subscription");
}
let mut body = HashMap::new();
body.insert("token", options.registration_id.unwrap());
body.insert("channelID", channel_id.to_owned());
if let Some(key) = app_server_key {
body.insert("key", key.to_owned());
}
// for unit tests, we shouldn't call the server. This is because we would need to create
// a valid FCM test senderid (and make sure we call it), etc. There has also been a
// history of problems where doing this tends to fail because of uncontrolled, third party
// system reliability issues making the tree turn orange.
if &self.options.sender_id == "test" {
self.uaid = Some("abad1d3a00000000aabbccdd00000000".to_owned());
self.auth = Some("LsuUOBKVQRY6-l7_Ajo-Ag".to_owned());
return Ok(RegisterResponse {
uaid: self.uaid.clone().unwrap(),
channel_id: "deadbeef00000000decafbad00000000".to_owned(),
secret: self.auth.clone(),
endpoint: "http://push.example.com/test/opaque".to_owned(),
senderid: Some(self.options.sender_id.clone()),
});
}
let url = Url::parse(&url)?;
let requested = match Request::post(url)
.headers(self.headers()?)
.json(&body)
.send()
{
Ok(v) => v,
Err(e) => {
return Err(
CommunicationServerError(format!("Could not fetch endpoint: {}", e)).into(),
);
}
};
if requested.is_server_error() {
return Err(CommunicationServerError("General Server error".to_string()).into());
}
if requested.is_client_error() {
if requested.status == status_codes::CONFLICT {
return Err(AlreadyRegisteredError.into());
}
return Err(CommunicationError(format!(
"Unhandled client error {} : {:?}",
requested.status,
String::from_utf8_lossy(&requested.body)
))
.into());
}
let response: Value = match requested.json() {
Ok(v) => v,
Err(e) => {
return Err(
CommunicationServerError(format!("Could not parse response: {:?}", e)).into(),
);
}
};
if self.uaid.is_none() {
self.uaid = response["uaid"].as_str().map(ToString::to_string);
}
if self.auth.is_none() {
self.auth = response["secret"].as_str().map(ToString::to_string);
}
let channel_id = response["channelID"].as_str().map(ToString::to_string);
let endpoint = response["endpoint"].as_str().map(ToString::to_string);
Ok(RegisterResponse {
uaid: self.uaid.clone().unwrap(),
channel_id: channel_id.unwrap(),
secret: self.auth.clone(),
endpoint: endpoint.unwrap(),
senderid: response["senderid"].as_str().map(ToString::to_string),
})
}
/// Drop a channel and stop receiving updates.
fn unsubscribe(&self, channel_id: Option<&str>) -> error::Result<bool> {
if self.auth.is_none() {
return Err(CommunicationError("Connection is unauthorized".into()).into());
}
if self.uaid.is_none() {
return Err(CommunicationError("No UAID set".into()).into());
}
let options = self.options.clone();
let mut url = format!(
"{}://{}/v1/{}/{}/registration/{}",
&options.http_protocol.unwrap(),
&options.server_host,
&options.bridge_type.unwrap(),
&options.sender_id,
&self.uaid.clone().unwrap(),
);
if let Some(channel_id) = channel_id {
url = format!("{}/subscription/{}", url, channel_id)
}
if &self.options.sender_id == "test" {
return Ok(true);
}
match Request::delete(Url::parse(&url)?)
.headers(self.headers()?)
.send()
{
Ok(_) => Ok(true),
Err(e) => Err(CommunicationServerError(format!("Could not unsubscribe: {}", e)).into()),
}
}
/// Update the push server with the new OS push authorization token
fn update(&mut self, new_token: &str) -> error::Result<bool> {
if self.options.sender_id == "test" {
self.uaid = Some("abad1d3a00000000aabbccdd00000000".to_owned());
self.auth = Some("LsuUOBKVQRY6-l7_Ajo-Ag".to_owned());
return Ok(true);
}
if self.auth.is_none() {
return Err(CommunicationError("Connection is unauthorized".into()).into());
}
if self.uaid.is_none() {
return Err(CommunicationError("No UAID set".into()).into());
}
self.options.registration_id = Some(new_token.to_owned());
let options = self.options.clone();
let url = format!(
"{}://{}/v1/{}/{}/registration/{}",
&options.http_protocol.unwrap(),
&options.server_host,
&options.bridge_type.unwrap(),
&options.sender_id,
&self.uaid.clone().unwrap()
);
let mut body = HashMap::new();
body.insert("token", new_token);
match Request::put(Url::parse(&url)?)
.json(&body)
.headers(self.headers()?)
.send()
{
Ok(_) => Ok(true),
Err(e) => {
Err(CommunicationServerError(format!("Could not update token: {}", e)).into())
}
}
}
/// Get a list of server known channels. If it differs from what we have, reset the UAID, and refresh channels.
/// Should be done once a day.
fn channel_list(&self) -> error::Result<Vec<String>> {
#[derive(Deserialize, Debug)]
struct Payload {
uaid: String,
#[serde(rename = "channelIDs")]
channel_ids: Vec<String>,
};
if self.auth.is_none() {
return Err(CommunicationError("Connection is unauthorized".into()).into());
}
if self.uaid.is_none() {
return Err(CommunicationError("No UAID set".into()).into());
}
let options = self.options.clone();
if options.bridge_type.is_none() {
return Err(CommunicationError("No Bridge Type set".into()).into());
}
let url = format!(
"{}://{}/v1/{}/{}/registration/{}",
&options.http_protocol.unwrap_or_else(|| "https".to_owned()),
&options.server_host,
&options.bridge_type.unwrap(),
&options.sender_id,
&self.uaid.clone().unwrap(),
);
let request = match Request::get(Url::parse(&url)?)
.headers(self.headers()?)
.send()
{
Ok(v) => v,
Err(e) => {
return Err(CommunicationServerError(format!(
"Could not fetch channel list: {}",
e
))
.into());
}
};
if request.is_server_error() {
return Err(CommunicationServerError("Server error".to_string()).into());
}
if request.is_client_error() {
return Err(CommunicationError(format!("Unhandled client error {:?}", request)).into());
}
let payload: Payload = match request.json() {
Ok(p) => p,
Err(e) => {
return Err(CommunicationServerError(format!(
"Could not fetch channel_list: Bad Response {:?}",
e
))
.into());
}
};
if payload.uaid != self.uaid.clone().unwrap() {
return Err(
CommunicationServerError("Invalid Response from server".to_string()).into(),
);
}
Ok(payload
.channel_ids
.iter()
.map(|s| Store::normalize_uuid(&s))
.collect())
}
// Add one or more new broadcast subscriptions.
fn broadcast_subscribe(&self, _broadcast: BroadcastValue) -> error::Result<BroadcastValue> {
Err(CommunicationError("Unsupported".to_string()).into())
}
// get the list of broadcasts
fn broadcasts(&self) -> error::Result<BroadcastValue> {
Err(CommunicationError("Unsupported".to_string()).into())
}
/// Verify that the server and client both have matching channel information. A "false"
/// should force the client to drop the old UAID, request a new UAID from the server, and
/// resubscribe all channels, resulting in new endpoints.
fn verify_connection(&self, channels: &[String]) -> error::Result<bool> {
if self.auth.is_none() {
return Err(CommunicationError("Connection uninitiated".to_owned()).into());
}
if &self.options.sender_id == "test" {
return Ok(false);
}
let local_channels: HashSet<String> = HashSet::from_iter(channels.iter().cloned());
let remote_channels: HashSet<String> = HashSet::from_iter(self.channel_list()?);
// verify both lists match. Either side could have lost it's mind.
if remote_channels != local_channels {
// Unsubscribe all the channels (just to be sure and avoid a loop).
self.unsubscribe(None)?;
return Ok(false);
}
Ok(true)
}
//impl TODO: Handle a Ping response with updated Broadcasts.
//impl TODO: Handle an incoming Notification
}
#[cfg(test)]
mod test {
use super::*;
use super::Connection;
use mockito::{mock, server_address};
use serde_json::json;
const DUMMY_CHID: &str = "deadbeef00000000decafbad00000000";
const DUMMY_UAID: &str = "abad1dea00000000aabbccdd00000000";
// Local test SENDER_ID ("test*" reserved for Kotlin testing.)
const SENDER_ID: &str = "FakeSenderID";
const SECRET: &str = "SuP3rS1kRet";
#[test]
fn test_communications() {
// FIXME: this test shouldn't make network requests.
viaduct_reqwest::use_reqwest_backend();
// mockito forces task serialization, so for now, we test everything in one go.
let config = PushConfiguration {
http_protocol: Some("http".to_owned()),
server_host: server_address().to_string(),
sender_id: SENDER_ID.to_owned(),
bridge_type: Some("test".to_owned()),
registration_id: Some("SomeRegistrationValue".to_owned()),
..Default::default()
};
// SUBSCRIPTION with secret
{
let body = json!({
"uaid": DUMMY_UAID,
"channelID": DUMMY_CHID,
"endpoint": "https://example.com/update",
"senderid": SENDER_ID,
"secret": SECRET,
})
.to_string();
let ap_mock = mock(
"POST",
format!("/v1/test/{}/registration", SENDER_ID).as_ref(),
)
.with_status(200)
.with_header("content-type", "application/json")
.with_body(body)
.create();
let mut conn = connect(config.clone(), None, None).unwrap();
let channel_id = hex::encode(crate::crypto::get_bytes(16).unwrap());
let response = conn.subscribe(&channel_id, None).unwrap();
ap_mock.assert();
assert_eq!(response.uaid, DUMMY_UAID);
// make sure we have stored the secret.
assert_eq!(conn.auth, Some(SECRET.to_owned()));
}
// SUBSCRIPTION with no secret
{
let body = json!({
"uaid": DUMMY_UAID,
"channelID": DUMMY_CHID,
"endpoint": "https://example.com/update",
"senderid": SENDER_ID,
"secret": null,
})
.to_string();
let ap_ns_mock = mock(
"POST",
format!("/v1/test/{}/registration", SENDER_ID).as_ref(),
)
.with_status(200)
.with_header("content-type", "application/json")
.with_body(body)
.create();
let mut conn = connect(config.clone(), None, None).unwrap();
let channel_id = hex::encode(crate::crypto::get_bytes(16).unwrap());
let response = conn.subscribe(&channel_id, None).unwrap();
ap_ns_mock.assert();
assert_eq!(response.uaid, DUMMY_UAID);
// make sure we have stored the secret.
assert_eq!(conn.auth, None);
}
// UNSUBSCRIBE - Single channel
{
let ap_mock = mock(
"DELETE",
format!(
"/v1/test/{}/registration/{}/subscription/{}",
SENDER_ID, DUMMY_UAID, DUMMY_CHID
)
.as_ref(),
)
.match_header("authorization", format!("webpush {}", SECRET).as_str())
.with_status(200)
.with_header("content-type", "application/json")
.with_body("{}")
.create();
let conn = connect(
config.clone(),
Some(DUMMY_UAID.to_owned()),
Some(SECRET.to_owned()),
)
.unwrap();
let response = conn.unsubscribe(Some(DUMMY_CHID)).unwrap();
ap_mock.assert();
assert!(response);
}
// UNSUBSCRIBE - All for UAID
{
let ap_mock = mock(
"DELETE",
format!("/v1/test/{}/registration/{}", SENDER_ID, DUMMY_UAID).as_ref(),
)
.match_header("authorization", format!("webpush {}", SECRET).as_str())
.with_status(200)
.with_header("content-type", "application/json")
.with_body("{}")
.create();
let conn = connect(
config.clone(),
Some(DUMMY_UAID.to_owned()),
Some(SECRET.to_owned()),
)
.unwrap();
//TODO: Add record to nuke.
let response = conn.unsubscribe(None).unwrap();
ap_mock.assert();
assert!(response);
}
// UPDATE
{
let ap_mock = mock(
"PUT",
format!("/v1/test/{}/registration/{}", SENDER_ID, DUMMY_UAID).as_ref(),
)
.match_header("authorization", format!("webpush {}", SECRET).as_str())
.with_status(200)
.with_header("content-type", "application/json")
.with_body("{}")
.create();
let mut conn = connect(
config.clone(),
Some(DUMMY_UAID.to_owned()),
Some(SECRET.to_owned()),
)
.unwrap();
let response = conn.update("NewTokenValue").unwrap();
ap_mock.assert();
assert_eq!(
conn.options.registration_id,
Some("NewTokenValue".to_owned())
);
assert!(response);
}
// CHANNEL LIST
{
let body_cl_success = json!({
"uaid": DUMMY_UAID,
"channelIDs": [DUMMY_CHID],
})
.to_string();
let ap_mock = mock(
"GET",
format!("/v1/test/{}/registration/{}", SENDER_ID, DUMMY_UAID).as_ref(),
)
.match_header("authorization", format!("webpush {}", SECRET).as_str())
.with_status(200)
.with_header("content-type", "application/json")
.with_body(body_cl_success)
.create();
let conn =
connect(config, Some(DUMMY_UAID.to_owned()), Some(SECRET.to_owned())).unwrap();
let response = conn.channel_list().unwrap();
ap_mock.assert();
assert!(response == [DUMMY_CHID.to_owned()]);
}
}
}