From 6d1c167efbb2010f1bbd0559bd60d690b867591c Mon Sep 17 00:00:00 2001 From: Jimmy Cuadra Date: Sat, 8 Jul 2017 23:25:29 -0700 Subject: [PATCH] Simplify `Session` and authenticate requests when required. --- src/lib.rs | 22 +++++++++++++++------- src/session.rs | 35 ++++++++++++++++++++++++++--------- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 886321dd..efd6cf3a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,7 +43,7 @@ mod error; mod session; /// A client for the Matrix client-server API. -#[derive(Debug)] +#[derive(Clone, Debug)] pub struct Client where C: Connect, @@ -55,11 +55,11 @@ where impl Client { /// Creates a new client for making HTTP requests to the given homeserver. - pub fn new(handle: &Handle, homeserver_url: Url) -> Self { + pub fn new(handle: &Handle, homeserver_url: Url, session: Option) -> Self { Client { homeserver_url, hyper: Rc::new(HyperClient::configure().keep_alive(true).build(handle)), - session: None, + session, } } } @@ -67,7 +67,7 @@ impl Client { #[cfg(feature = "tls")] impl Client> { /// Creates a new client for making HTTPS requests to the given homeserver. - pub fn https(handle: &Handle, homeserver_url: Url) -> Result { + pub fn https(handle: &Handle, homeserver_url: Url, session: Option) -> Result { let connector = HttpsConnector::new(4, handle)?; Ok(Client { @@ -78,7 +78,7 @@ impl Client> { .keep_alive(true) .build(handle), ), - session: None, + session, }) } } @@ -90,11 +90,11 @@ where /// Creates a new client using the given `hyper::Client`. /// /// This allows the user to configure the details of HTTP as desired. - pub fn custom(hyper_client: HyperClient, homeserver_url: Url) -> Self { + pub fn custom(hyper_client: HyperClient, homeserver_url: Url, session: Option) -> Self { Client { homeserver_url, hyper: Rc::new(hyper_client), - session: None, + session, } } @@ -119,6 +119,14 @@ where url.set_path(uri.path()); url.set_query(uri.query()); + + if E::METADATA.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); + } + } } Uri::from_str(url.as_ref()) diff --git a/src/session.rs b/src/session.rs index 7e450741..388567d4 100644 --- a/src/session.rs +++ b/src/session.rs @@ -1,13 +1,30 @@ use ruma_identifiers::UserId; -use url::Host; -/// An active user session with a Matrix homeserver, allowing authenticated requests. -#[derive(Clone, Debug)] +/// A user session, containing an access token and information about the associated user account. +#[derive(Clone, Debug, Eq, Hash, PartialEq)] pub struct Session { - /// 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, + /// The access token used for this session. + access_token: String, + /// The user the access token was issued for. + user_id: UserId, +} + +impl Session { + /// Create a new user session from an access token and a user ID. + pub fn new(access_token: String, user_id: UserId) -> Self { + Session { + access_token, + user_id, + } + } + + /// Get the access token associated with this session. + pub fn access_token(&self) -> &str { + &self.access_token + } + + /// Get the ID of the user the session belongs to. + pub fn user_id(&self) -> &UserId { + &self.user_id + } }