162 lines
5.7 KiB
Rust
162 lines
5.7 KiB
Rust
//! The native driver (docs/V1_1B.md §1): [`Client`] is the [`Core`] over `ureq` and the app's [`Subkey`],
|
|
//! with the blocking API apps have used since v0.2. Neither the transport nor the signer ever waits, so each
|
|
//! operation completes the first time its future is polled: no executor, no thread.
|
|
|
|
use crate::{
|
|
AppEvent, Core, Device, GroupId, HttpRequest, HttpResponse, Result, StateStore, SyncReport, Transport,
|
|
VerifiedEvent,
|
|
};
|
|
use std::future::Future;
|
|
use std::ops::{Deref, DerefMut};
|
|
use std::sync::Arc;
|
|
use std::task::{Context, Poll, Waker};
|
|
use std::time::Duration;
|
|
use tessera::{BackupSecret, Revocation, Subkey, SubkeyCert};
|
|
|
|
/// The blocking HTTP transport: no redirects, 15 s for a whole request.
|
|
pub struct Ureq(ureq::Agent);
|
|
|
|
impl Default for Ureq {
|
|
fn default() -> Self {
|
|
Ureq(
|
|
ureq::Agent::config_builder()
|
|
.max_redirects(0)
|
|
.http_status_as_error(false)
|
|
.timeout_global(Some(Duration::from_secs(15)))
|
|
.build()
|
|
.into(),
|
|
)
|
|
}
|
|
}
|
|
|
|
impl Transport for Ureq {
|
|
async fn send(&self, req: HttpRequest) -> std::result::Result<HttpResponse, String> {
|
|
fn with<B>(mut r: ureq::RequestBuilder<B>, headers: &[(&'static str, String)]) -> ureq::RequestBuilder<B> {
|
|
for (n, v) in headers {
|
|
r = r.header(*n, v);
|
|
}
|
|
r
|
|
}
|
|
let resp = match req.method.as_str() {
|
|
"GET" => with(self.0.get(&req.url), &req.headers).call(),
|
|
"DELETE" => with(self.0.delete(&req.url), &req.headers).call(),
|
|
"PUT" => with(self.0.put(&req.url), &req.headers).send(&req.body[..]),
|
|
_ => with(self.0.post(&req.url), &req.headers).send(&req.body[..]),
|
|
};
|
|
let mut resp = resp.map_err(|e| e.to_string())?;
|
|
let status = resp.status().as_u16();
|
|
let body = resp.body_mut().read_to_vec().unwrap_or_default();
|
|
Ok(HttpResponse { status, body })
|
|
}
|
|
}
|
|
|
|
/// Runs a core operation over the native transport and signer: it is ready on the first poll.
|
|
fn now<F: Future>(f: F) -> F::Output {
|
|
let mut f = std::pin::pin!(f);
|
|
match f.as_mut().poll(&mut Context::from_waker(Waker::noop())) {
|
|
Poll::Ready(v) => v,
|
|
// Unreachable: `Ureq` and `Subkey` never return Pending, and the core awaits nothing else.
|
|
Poll::Pending => unreachable!("a native operation waited"),
|
|
}
|
|
}
|
|
|
|
fn system_ms() -> u64 {
|
|
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_millis() as u64).unwrap_or(0)
|
|
}
|
|
|
|
/// The native client. Every operation that does not talk to the relay or sign (`groups`, `log`, `members`,
|
|
/// `import`, …) is the [`Core`]'s, reached through `Deref`.
|
|
pub struct Client(Core<Ureq, Subkey>);
|
|
|
|
impl Deref for Client {
|
|
type Target = Core<Ureq, Subkey>;
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.0
|
|
}
|
|
}
|
|
|
|
impl DerefMut for Client {
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
&mut self.0
|
|
}
|
|
}
|
|
|
|
impl Client {
|
|
/// Opens the client for `device`'s app, with its state from `store` (or empty).
|
|
pub fn open(relay: &str, device: Device, store: Box<dyn StateStore>) -> Result<Client> {
|
|
Core::open(relay, device.cert, device.subkey, Ureq::default(), store, Arc::new(system_ms)).map(Client)
|
|
}
|
|
|
|
/// The group of this person's own devices for this app; created on first use.
|
|
pub fn self_group(&mut self) -> Result<GroupId> {
|
|
now(self.0.self_group())
|
|
}
|
|
|
|
/// A new group shared with people you will invite.
|
|
pub fn open_group(&mut self, name: &str) -> Result<GroupId> {
|
|
now(self.0.open_group(name))
|
|
}
|
|
|
|
/// Signs `events`, applies them to the local log at once, and queues them for the group. Never needs the
|
|
/// relay.
|
|
pub fn send(&mut self, g: &GroupId, events: Vec<AppEvent>) -> Result<()> {
|
|
now(self.0.send(g, events))
|
|
}
|
|
|
|
/// An invite token (`tab1:…`). Needs the relay. See [`Core::invite`].
|
|
pub fn invite(&mut self, g: &GroupId) -> Result<String> {
|
|
now(self.0.invite(g))
|
|
}
|
|
|
|
/// Asks to join with an invite token. Needs the relay. See [`Core::join`].
|
|
pub fn join(&mut self, token: &str) -> Result<()> {
|
|
now(self.0.join(token))
|
|
}
|
|
|
|
/// Removes a member. Needs the relay.
|
|
pub fn remove(&mut self, g: &GroupId, member: u32) -> Result<()> {
|
|
now(self.0.remove(g, member))
|
|
}
|
|
|
|
/// See [`Core::apply_revocation`].
|
|
pub fn apply_revocation(&mut self, statements: &[Revocation]) -> Result<usize> {
|
|
now(self.0.apply_revocation(statements))
|
|
}
|
|
|
|
/// See [`Core::announce_revocation`].
|
|
pub fn announce_revocation(&mut self, g: &GroupId, statements: Vec<Revocation>) -> Result<()> {
|
|
now(self.0.announce_revocation(g, statements))
|
|
}
|
|
|
|
/// See [`Core::revoke`].
|
|
pub fn revoke(&mut self, g: &GroupId, statements: Vec<Revocation>) -> Result<usize> {
|
|
now(self.0.revoke(g, statements))
|
|
}
|
|
|
|
/// See [`Core::renew`].
|
|
pub fn renew(&mut self, cert: SubkeyCert) -> Result<()> {
|
|
now(self.0.renew(cert))
|
|
}
|
|
|
|
/// See [`Core::backup`].
|
|
pub fn backup(&mut self, secret: &BackupSecret) -> Result<bool> {
|
|
now(self.0.backup(secret))
|
|
}
|
|
|
|
/// See [`Core::restore`].
|
|
pub fn restore(&self, secret: &BackupSecret) -> Result<Vec<Vec<u8>>> {
|
|
now(self.0.restore(secret))
|
|
}
|
|
|
|
/// Uploads what is queued, downloads and verifies what is new, admits invited devices, picks up groups this
|
|
/// device was admitted to. An unreachable relay is `relay_ok: false`, not an error.
|
|
pub fn sync(&mut self) -> Result<SyncReport> {
|
|
now(self.0.sync())
|
|
}
|
|
|
|
/// `sync`, then the group's log.
|
|
pub fn pull(&mut self, g: &GroupId) -> Result<Vec<VerifiedEvent>> {
|
|
now(self.0.pull(g))
|
|
}
|
|
}
|