Skip to content

Commit

Permalink
Matrix API finished
Browse files Browse the repository at this point in the history
  • Loading branch information
avdb13 committed Feb 27, 2024
1 parent c7cd019 commit 87f60df
Show file tree
Hide file tree
Showing 18 changed files with 177 additions and 680 deletions.
10 changes: 7 additions & 3 deletions crates/matrix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@ publish = false

[dependencies]
ruma-events = { version = "0.27.11", features = ["html", "markdown"] }
ruma-common = { version = "0.12.0", default_features = false, features = ["api", "rand"] }
ruma-common = { version = "0.12.0", default_features = false, features = [
"api",
"rand",
] }
ruma-macros = "0.12.0"

# Workspace Dependencies
anyhow = { workspace = true }
chrono = { workspace = true, features = ["serde"] }
mime = { workspace = true }
reqwest = { workspace = true, features = ["json"] }
serde = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
sha1 = { workspace = true }
url = { workspace = true, features = ["serde"] }
hex = { workspace = true }
hmac = { workspace = true }

[features]
client = []
Expand Down
8 changes: 6 additions & 2 deletions crates/matrix/src/admin/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
mod user;
//! This module is the root of the admin API.
//!
//! reference: https://matrix-org.github.io/synapse/latest/usage/administration/admin_api/index.html
mod room;
// mod room;
mod session;
mod user;
4 changes: 2 additions & 2 deletions crates/matrix/src/admin/room.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! reference: https://matrix-org.github.io/synapse/latest/admin_api/rooms.html
//!
//! This module contains handlers for managing rooms.
//!
//! reference: https://matrix-org.github.io/synapse/latest/admin_api/rooms.html
use ruma_common::{
room::RoomType, EventEncryptionAlgorithm, OwnedMxcUri, OwnedRoomAliasId, OwnedRoomId,
Expand Down
45 changes: 45 additions & 0 deletions crates/matrix/src/admin/session.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//! This module contains handlers for user registration.
//!
//! reference: https://matrix-org.github.io/synapse/latest/admin_api/register_api.html
use hmac::Mac;

mod get_nonce;
mod register;

#[derive(Clone, Debug)]
pub struct Hmac {
inner: Vec<u8>,
}

impl Hmac {
fn new(
shared_secret: &str,
nonce: &str,
username: &str,
password: &str,
admin: bool,
) -> Result<Self, hmac::digest::InvalidLength> {
let mut mac = hmac::Hmac::<sha1::Sha1>::new_from_slice(shared_secret.as_bytes())?;
let admin = match admin {
true => "admin",
false => "notadmin",
};

mac.update(
&[nonce, username, password, admin]
.map(str::as_bytes)
.join(&0x00),
);

let result = mac.finalize().into_bytes();

Ok(Self {
inner: result.to_vec(),
})
}

fn get(&self) -> String {
hex::encode(&self.inner)
}
}
22 changes: 22 additions & 0 deletions crates/matrix/src/admin/session/get_nonce.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use ruma_common::{
api::{request, response, Metadata},
metadata,
};

#[allow(dead_code)]
const METADATA: Metadata = metadata! {
method: GET,
rate_limited: false,
authentication: AccessToken,
history: {
unstable => "/_synapse/admin/v1/register",
}
};

#[request(error = crate::Error)]
pub struct Request {}

#[response(error = crate::Error)]
pub struct Response {
nonce: String,
}
43 changes: 43 additions & 0 deletions crates/matrix/src/admin/session/register.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use ruma_common::{
api::{request, response, Metadata},
metadata, OwnedDeviceId, OwnedServerName, OwnedUserId,
};

use super::Hmac;

#[allow(dead_code)]
const METADATA: Metadata = metadata! {
method: GET,
rate_limited: false,
authentication: AccessToken,
history: {
unstable => "/_synapse/admin/v1/register",
}
};

#[request(error = crate::Error)]
pub struct Request {
pub nonce: String,

pub username: String,

pub password: String,

#[serde(skip_deserializing_if = "String::is_empty")]
pub displayname: String,

pub admin: bool,

pub hmac: Hmac,
}

#[response(error = crate::Error)]
pub struct Response {
pub access_token: String,

pub user_id: OwnedUserId,

pub home_server: OwnedServerName,

pub device_id: OwnedDeviceId,
}
4 changes: 2 additions & 2 deletions crates/matrix/src/admin/user.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! reference: https://matrix-org.github.io/synapse/latest/admin_api/user_admin_api.html
//! This module contains handlers for managing users.
//!
//! This module contains handlers for managing user accounts.
//! reference: https://matrix-org.github.io/synapse/latest/admin_api/user_admin_api.html
use ruma_common::{thirdparty::ThirdPartyIdentifier, OwnedMxcUri, OwnedUserId};
use serde::{Deserialize, Serialize};
Expand Down
9 changes: 0 additions & 9 deletions crates/matrix/src/client/error.rs

This file was deleted.

Loading

0 comments on commit 87f60df

Please sign in to comment.