Skip to main content

ruma_identity_service_api/
lib.rs

1#![doc(html_favicon_url = "https://ruma.dev/favicon.ico")]
2#![doc(html_logo_url = "https://ruma.dev/images/logo.png")]
3//! (De)serializable types for the [Matrix Identity Service API][identity-api].
4//! These types can be shared by client and identity service code.
5//!
6//! [identity-api]: https://spec.matrix.org/v1.19/identity-service-api/
7
8// This crate is not useful without either of those features, so export nothing if they are not
9// enabled to avoid errors when running checks wrongly without enabling any of them.
10#![cfg(any(feature = "client", feature = "server"))]
11#![warn(missing_docs)]
12
13use ruma_common::api::auth_scheme::{
14    AuthScheme, ExtractTokenError, add_access_token_as_authorization_header,
15    extract_bearer_or_query_token,
16};
17
18pub mod association;
19pub mod authentication;
20pub mod discovery;
21pub mod invitation;
22pub mod keys;
23pub mod lookup;
24pub mod tos;
25
26ruma_common::priv_owned_str!();
27
28/// Authentication is required by including an identity server access token in the
29/// `Authentication` http header, or an `access_token` query parameter.
30///
31/// Using the query parameter is deprecated since Matrix 1.11.
32#[derive(Debug, Clone, Copy, Default)]
33#[allow(clippy::exhaustive_structs)]
34pub struct IdentityServiceToken;
35
36impl AuthScheme for IdentityServiceToken {
37    type Input<'a> = &'a str;
38    type AddAuthenticationError = http::header::InvalidHeaderValue;
39    /// The identity service token.
40    type Output = String;
41    type ExtractAuthenticationError = ExtractTokenError;
42
43    fn add_authentication<T: AsRef<[u8]>>(
44        request: &mut http::Request<T>,
45        access_token: Self::Input<'_>,
46    ) -> Result<(), Self::AddAuthenticationError> {
47        add_access_token_as_authorization_header(request.headers_mut(), access_token)
48    }
49
50    fn extract_authentication<T>(
51        request: &http::Request<T>,
52    ) -> Result<Self::Output, Self::ExtractAuthenticationError> {
53        extract_bearer_or_query_token(request)?.ok_or(ExtractTokenError::MissingAccessToken)
54    }
55}