#![doc(html_favicon_url = "https://ruma.dev/favicon.ico")]
#![doc(html_logo_url = "https://ruma.dev/images/logo.png")]
#![cfg(any(feature = "client", feature = "server"))]
#![warn(missing_docs)]
use ruma_common::api::auth_scheme::{
AuthScheme, ExtractTokenError, add_access_token_as_authorization_header,
extract_bearer_or_query_token,
};
use serde::{Deserialize, Serialize};
pub mod event;
pub mod ping;
pub mod query;
pub mod thirdparty;
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub struct Namespace {
pub exclusive: bool,
pub regex: String,
}
impl Namespace {
pub fn new(exclusive: bool, regex: String) -> Self {
Namespace { exclusive, regex }
}
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub struct Namespaces {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub users: Vec<Namespace>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub aliases: Vec<Namespace>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub rooms: Vec<Namespace>,
}
impl Namespaces {
pub fn new() -> Self {
Self::default()
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub struct Registration {
pub id: String,
#[serde(deserialize_with = "Option::deserialize")]
pub url: Option<String>,
pub as_token: String,
pub hs_token: String,
pub sender_localpart: String,
pub namespaces: Namespaces,
#[serde(skip_serializing_if = "Option::is_none")]
pub rate_limited: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub protocols: Option<Vec<String>>,
#[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
pub receive_ephemeral: bool,
}
#[derive(Debug)]
#[allow(clippy::exhaustive_structs)]
pub struct RegistrationInit {
pub id: String,
pub url: Option<String>,
pub as_token: String,
pub hs_token: String,
pub sender_localpart: String,
pub namespaces: Namespaces,
pub rate_limited: Option<bool>,
pub protocols: Option<Vec<String>>,
}
impl From<RegistrationInit> for Registration {
fn from(init: RegistrationInit) -> Self {
let RegistrationInit {
id,
url,
as_token,
hs_token,
sender_localpart,
namespaces,
rate_limited,
protocols,
} = init;
Self {
id,
url,
as_token,
hs_token,
sender_localpart,
namespaces,
rate_limited,
protocols,
receive_ephemeral: false,
}
}
}
#[derive(Debug, Clone, Copy, Default)]
#[allow(clippy::exhaustive_structs)]
pub struct HomeserverToken;
impl AuthScheme for HomeserverToken {
type Input<'a> = &'a str;
type AddAuthenticationError = http::header::InvalidHeaderValue;
type Output = String;
type ExtractAuthenticationError = ExtractTokenError;
fn add_authentication<T: AsRef<[u8]>>(
request: &mut http::Request<T>,
access_token: Self::Input<'_>,
) -> Result<(), Self::AddAuthenticationError> {
add_access_token_as_authorization_header(request.headers_mut(), access_token)
}
fn extract_authentication<T>(
request: &http::Request<T>,
) -> Result<Self::Output, Self::ExtractAuthenticationError> {
extract_bearer_or_query_token(request)?.ok_or(ExtractTokenError::MissingAccessToken)
}
}