Simplify Session and authenticate requests when required.

This commit is contained in:
Jimmy Cuadra 2017-07-08 23:25:29 -07:00
parent feaadff8db
commit 6d1c167efb
2 changed files with 41 additions and 16 deletions

View File

@ -43,7 +43,7 @@ mod error;
mod session; mod session;
/// A client for the Matrix client-server API. /// A client for the Matrix client-server API.
#[derive(Debug)] #[derive(Clone, Debug)]
pub struct Client<C> pub struct Client<C>
where where
C: Connect, C: Connect,
@ -55,11 +55,11 @@ where
impl Client<HttpConnector> { impl Client<HttpConnector> {
/// Creates a new client for making HTTP requests to the given homeserver. /// 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<Session>) -> Self {
Client { Client {
homeserver_url, homeserver_url,
hyper: Rc::new(HyperClient::configure().keep_alive(true).build(handle)), hyper: Rc::new(HyperClient::configure().keep_alive(true).build(handle)),
session: None, session,
} }
} }
} }
@ -67,7 +67,7 @@ impl Client<HttpConnector> {
#[cfg(feature = "tls")] #[cfg(feature = "tls")]
impl Client<HttpsConnector<HttpConnector>> { impl Client<HttpsConnector<HttpConnector>> {
/// Creates a new client for making HTTPS requests to the given homeserver. /// Creates a new client for making HTTPS requests to the given homeserver.
pub fn https(handle: &Handle, homeserver_url: Url) -> Result<Self, NativeTlsError> { pub fn https(handle: &Handle, homeserver_url: Url, session: Option<Session>) -> Result<Self, NativeTlsError> {
let connector = HttpsConnector::new(4, handle)?; let connector = HttpsConnector::new(4, handle)?;
Ok(Client { Ok(Client {
@ -78,7 +78,7 @@ impl Client<HttpsConnector<HttpConnector>> {
.keep_alive(true) .keep_alive(true)
.build(handle), .build(handle),
), ),
session: None, session,
}) })
} }
} }
@ -90,11 +90,11 @@ where
/// Creates a new client using the given `hyper::Client`. /// Creates a new client using the given `hyper::Client`.
/// ///
/// This allows the user to configure the details of HTTP as desired. /// This allows the user to configure the details of HTTP as desired.
pub fn custom(hyper_client: HyperClient<C>, homeserver_url: Url) -> Self { pub fn custom(hyper_client: HyperClient<C>, homeserver_url: Url, session: Option<Session>) -> Self {
Client { Client {
homeserver_url, homeserver_url,
hyper: Rc::new(hyper_client), hyper: Rc::new(hyper_client),
session: None, session,
} }
} }
@ -119,6 +119,14 @@ where
url.set_path(uri.path()); url.set_path(uri.path());
url.set_query(uri.query()); 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()) Uri::from_str(url.as_ref())

View File

@ -1,13 +1,30 @@
use ruma_identifiers::UserId; use ruma_identifiers::UserId;
use url::Host;
/// An active user session with a Matrix homeserver, allowing authenticated requests. /// A user session, containing an access token and information about the associated user account.
#[derive(Clone, Debug)] #[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Session { pub struct Session {
/// The access token of this session /// The access token used for this session.
pub access_token: String, access_token: String,
/// The homeserver this session is associated with /// The user the access token was issued for.
pub homeserver: Host, user_id: UserId,
/// the ID of the user owning this session }
pub 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
}
} }