implemented password and token logins
This commit is contained in:
parent
7d89ed4a44
commit
1e2a00edf3
2 changed files with 114 additions and 11 deletions
|
@ -1,10 +1,10 @@
|
||||||
use crate::api::methods::{login::ValidLoginFlows, sync::SyncResponse};
|
use crate::api::methods::{login::*, sync::SyncResponse};
|
||||||
use reqwest::{
|
use reqwest::{
|
||||||
header::{HeaderMap, HeaderValue, CONTENT_TYPE, USER_AGENT},
|
header::{HeaderMap, HeaderValue, CONTENT_TYPE, USER_AGENT},
|
||||||
Client as reqwest_client, Response, StatusCode,
|
Client as reqwest_client, Response, StatusCode,
|
||||||
};
|
};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use serde_json::Value;
|
use serde_json::{json, Value};
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
error::Error,
|
error::Error,
|
||||||
|
@ -175,7 +175,7 @@ impl Client {
|
||||||
&self,
|
&self,
|
||||||
method: MatrixHTTPMethod,
|
method: MatrixHTTPMethod,
|
||||||
path: &str,
|
path: &str,
|
||||||
content: Option<String>,
|
content: Option<Value>,
|
||||||
query_params: Option<HashMap<String, String>>,
|
query_params: Option<HashMap<String, String>>,
|
||||||
headers: Option<HeaderMap>,
|
headers: Option<HeaderMap>,
|
||||||
) -> Result<Response, Box<dyn std::error::Error>> {
|
) -> Result<Response, Box<dyn std::error::Error>> {
|
||||||
|
@ -216,7 +216,7 @@ impl Client {
|
||||||
request = request.headers(headers).query(&query_params);
|
request = request.headers(headers).query(&query_params);
|
||||||
|
|
||||||
if let Some(content) = content {
|
if let Some(content) = content {
|
||||||
request = request.body(content);
|
request = request.body(content.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
@ -302,6 +302,7 @@ impl Client {
|
||||||
|
|
||||||
// Holds implementation for Section 5.4, Login
|
// Holds implementation for Section 5.4, Login
|
||||||
impl Client {
|
impl Client {
|
||||||
|
/// Implementation of [Section 5.4.1 **GET** `/_matrix/client/r0/login`](https://matrix.org/docs/spec/client_server/r0.5.0#get-matrix-client-r0-login).
|
||||||
pub fn login_flows(&self) -> Result<ValidLoginFlows, Box<dyn Error>> {
|
pub fn login_flows(&self) -> Result<ValidLoginFlows, Box<dyn Error>> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.send(
|
.send(
|
||||||
|
@ -313,6 +314,92 @@ impl Client {
|
||||||
)?
|
)?
|
||||||
.json()?)
|
.json()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Implementation of password-based login for
|
||||||
|
/// [5.4.2 **POST** `/_matrix/client/r0/login`](https://matrix.org/docs/spec/client_server/r0.5.0#post-matrix-client-r0-login)
|
||||||
|
///
|
||||||
|
/// If a specific device ID is specified, then the server will attempt to
|
||||||
|
/// use it. If an existing device ID is used, then any previously assigned
|
||||||
|
/// access tokens to that device ID may be invalidated. If the device ID
|
||||||
|
/// does not exist, then it will be created. If none is provided, then the
|
||||||
|
/// server will generate one.
|
||||||
|
///
|
||||||
|
/// You may specify a device display name if the provided device ID is not
|
||||||
|
/// known. It is ignored if the device ID already exists.
|
||||||
|
pub fn login_password(
|
||||||
|
&self,
|
||||||
|
identifier: IdentifierType,
|
||||||
|
password: &str,
|
||||||
|
device_id: Option<&str>,
|
||||||
|
initial_device_display_name: Option<&str>,
|
||||||
|
) -> Result<LoginResponse, Box<dyn Error>> {
|
||||||
|
self.login(
|
||||||
|
json!({
|
||||||
|
"type": "m.login.password",
|
||||||
|
"password": password,
|
||||||
|
}),
|
||||||
|
identifier,
|
||||||
|
device_id,
|
||||||
|
initial_device_display_name,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Implementation of token-based login for
|
||||||
|
/// [5.4.2 **POST** `/_matrix/client/r0/login`](https://matrix.org/docs/spec/client_server/r0.5.0#post-matrix-client-r0-login)
|
||||||
|
///
|
||||||
|
/// If a specific device ID is specified, then the server will attempt to
|
||||||
|
/// use it. If an existing device ID is used, then any previously assigned
|
||||||
|
/// access tokens to that device ID may be invalidated. If the device ID
|
||||||
|
/// does not exist, then it will be created. If none is provided, then the
|
||||||
|
/// server will generate one.
|
||||||
|
///
|
||||||
|
/// You may specify a device display name if the provided device ID is not
|
||||||
|
/// known. It is ignored if the device ID already exists.
|
||||||
|
pub fn login_token(
|
||||||
|
&self,
|
||||||
|
identifier: IdentifierType,
|
||||||
|
password: &str,
|
||||||
|
device_id: Option<&str>,
|
||||||
|
initial_device_display_name: Option<&str>,
|
||||||
|
) -> Result<LoginResponse, Box<dyn Error>> {
|
||||||
|
self.login(
|
||||||
|
json!({
|
||||||
|
"type": "m.login.token",
|
||||||
|
"token": password,
|
||||||
|
}),
|
||||||
|
identifier,
|
||||||
|
device_id,
|
||||||
|
initial_device_display_name,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn login(
|
||||||
|
&self,
|
||||||
|
body: Value,
|
||||||
|
identifier: IdentifierType,
|
||||||
|
device_id: Option<&str>,
|
||||||
|
initial_device_display_name: Option<&str>,
|
||||||
|
) -> Result<LoginResponse, Box<dyn Error>> {
|
||||||
|
let mut body = body;
|
||||||
|
body["identifier"] = json!(identifier);
|
||||||
|
|
||||||
|
if let Some(id) = device_id {
|
||||||
|
body["device_id"] = json!(id);
|
||||||
|
if let Some(display_name) = initial_device_display_name {
|
||||||
|
body["initial_device_display_name"] = json!(display_name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(self
|
||||||
|
.send(
|
||||||
|
MatrixHTTPMethod::Post,
|
||||||
|
"/_matrix/client/r0/login",
|
||||||
|
Some(body),
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
)?
|
||||||
|
.json()?)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -395,9 +482,10 @@ mod tests {
|
||||||
assert_eq!(dbg!(resp)["hello"], "world");
|
assert_eq!(dbg!(resp)["hello"], "world");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Override 429 response with valid response after initial request
|
||||||
|
// returns a 429. This should really be done with a synchronization
|
||||||
|
// primitive (e.g. condvar) but I don't know how to do that.
|
||||||
thread::sleep(time::Duration::from_secs(1));
|
thread::sleep(time::Duration::from_secs(1));
|
||||||
|
|
||||||
// Override 429 response with valid response.
|
|
||||||
let _m = mock("GET", "/hello")
|
let _m = mock("GET", "/hello")
|
||||||
.with_body(r#"{"hello": "world"}"#)
|
.with_body(r#"{"hello": "world"}"#)
|
||||||
.create();
|
.create();
|
||||||
|
|
|
@ -1,20 +1,34 @@
|
||||||
use serde::Deserialize;
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
// 5.4.1 GET /_matrix/client/r0/login
|
||||||
|
|
||||||
/// Response for 5.4.1: GET /_matrix/client/r0/login
|
/// Response for 5.4.1: GET /_matrix/client/r0/login
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct ValidLoginFlows {
|
pub struct ValidLoginFlows {
|
||||||
pub flows: Option<LoginFlow>,
|
pub flows: Option<LoginFlow>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct LoginFlow {
|
pub struct LoginFlow {
|
||||||
pub r#type: Option<String>,
|
pub r#type: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Request helper for 5.4.2: POST /_matrix/client/r0/login
|
// 5.4.2 POST /_matrix/client/r0/login
|
||||||
pub struct LoginRequest {}
|
|
||||||
|
|
||||||
/// Response object for a valid login from 5.4.2: POST /_matrix/client/r0/login
|
/// Known identifier types for authentication methods. See [Section 5.3.6](https://matrix.org/docs/spec/client_server/r0.5.0#identifier-types)
|
||||||
|
/// for more details.
|
||||||
|
#[derive(Deserialize, Serialize)]
|
||||||
|
pub enum IdentifierType {
|
||||||
|
#[serde(rename = "m.id.user")]
|
||||||
|
User,
|
||||||
|
#[serde(rename = "m.id.thirdparty")]
|
||||||
|
ThirdParty,
|
||||||
|
#[serde(rename = "m.id.phone")]
|
||||||
|
Phone
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Response for a valid login from 5.4.2: POST /_matrix/client/r0/login
|
||||||
|
#[derive(Deserialize)]
|
||||||
pub struct LoginResponse {
|
pub struct LoginResponse {
|
||||||
pub user_id: Option<String>,
|
pub user_id: Option<String>,
|
||||||
pub access_token: Option<String>,
|
pub access_token: Option<String>,
|
||||||
|
@ -23,4 +37,5 @@ pub struct LoginResponse {
|
||||||
pub well_known: Option<DiscoveryInformation>,
|
pub well_known: Option<DiscoveryInformation>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
pub struct DiscoveryInformation {}
|
pub struct DiscoveryInformation {}
|
||||||
|
|
Loading…
Reference in a new issue