From bafc48292ec68696bbca92ae8ea4f1a0ab5785e5 Mon Sep 17 00:00:00 2001 From: Victor Berger Date: Sun, 8 Jan 2017 15:21:33 +0100 Subject: [PATCH] Add support for authenticated endpoints. --- src/error.rs | 2 ++ src/lib.rs | 8 ++++++++ src/session.rs | 9 ++++++--- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/error.rs b/src/error.rs index 1b30cb6a..ee2f1483 100644 --- a/src/error.rs +++ b/src/error.rs @@ -14,6 +14,8 @@ pub enum Error { SerdeJson(SerdeJsonError), /// An error when serializing a query string value. SerdeUrlEncodedSerialize(SerdeUrlEncodedSerializeError), + /// Queried endpoint requires authentication but was called on an anonymous client + AuthenticationRequired } impl From for Error { diff --git a/src/lib.rs b/src/lib.rs index b336661c..f627ae26 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -84,6 +84,14 @@ impl Client { url.set_query(Some(&serde_urlencoded::to_string(&query_params)?)); + if E::requires_authentication() { + if let Some(ref session) = self.session { + url.query_pairs_mut().append_pair("access_token", &session.access_token); + } else { + return Err(Error::AuthenticationRequired) + } + } + let mut request = HyperRequest::new(E::method().into_hyper(), url); match E::method() { diff --git a/src/session.rs b/src/session.rs index c050021f..7e450741 100644 --- a/src/session.rs +++ b/src/session.rs @@ -4,7 +4,10 @@ use url::Host; /// An active user session with a Matrix homeserver, allowing authenticated requests. #[derive(Clone, Debug)] pub struct Session { - access_token: String, - homeserver: Host, - user_id: UserId, + /// The access token of this session + pub access_token: String, + /// The homeserver this session is associated with + pub homeserver: Host, + /// the ID of the user owning this session + pub user_id: UserId, }