From ef7dd6dd84bf48fc2af4ed69d639a8483da37d65 Mon Sep 17 00:00:00 2001 From: Adam <13720823+Frinksy@users.noreply.github.com> Date: Thu, 10 Jun 2021 18:59:37 +0100 Subject: [PATCH] identity-service-api: Add 3PID bind endpoint --- crates/ruma-identifiers/src/signatures.rs | 5 ++ crates/ruma-identity-service-api/CHANGELOG.md | 11 +++ .../src/association.rs | 1 + .../src/association/bind_3pid.rs | 3 + .../src/association/bind_3pid/v2.rs | 75 +++++++++++++++++++ 5 files changed, 95 insertions(+) create mode 100644 crates/ruma-identity-service-api/src/association/bind_3pid.rs create mode 100644 crates/ruma-identity-service-api/src/association/bind_3pid/v2.rs diff --git a/crates/ruma-identifiers/src/signatures.rs b/crates/ruma-identifiers/src/signatures.rs index b04d2fe4..ec8cb5bf 100644 --- a/crates/ruma-identifiers/src/signatures.rs +++ b/crates/ruma-identifiers/src/signatures.rs @@ -16,6 +16,11 @@ pub type EntitySignatures = BTreeMap, String>; /// signatures.insert(server_name, key_identifier, signature.into()); /// ``` #[derive(Clone, Debug, Default)] +#[cfg_attr( + feature = "serde", + derive(serde::Serialize, serde::Deserialize), + serde(transparent, crate = "serde") +)] pub struct Signatures(BTreeMap>); impl Signatures { diff --git a/crates/ruma-identity-service-api/CHANGELOG.md b/crates/ruma-identity-service-api/CHANGELOG.md index 5f1cf61c..0d00ab24 100644 --- a/crates/ruma-identity-service-api/CHANGELOG.md +++ b/crates/ruma-identity-service-api/CHANGELOG.md @@ -1,5 +1,16 @@ # [unreleased] +Improvements: + +* Add more endpoints: + + ```rust + association::{ + check_3pid_validity::v2, + bind_3pid::v2, + } + ``` + # 0.1.0 Breaking changes: diff --git a/crates/ruma-identity-service-api/src/association.rs b/crates/ruma-identity-service-api/src/association.rs index 13fd38b2..477aaf60 100644 --- a/crates/ruma-identity-service-api/src/association.rs +++ b/crates/ruma-identity-service-api/src/association.rs @@ -1,5 +1,6 @@ //! Endpoints to create associations with a Matrix ID on the identity server. +pub mod bind_3pid; pub mod check_3pid_validity; pub mod email; pub mod msisdn; diff --git a/crates/ruma-identity-service-api/src/association/bind_3pid.rs b/crates/ruma-identity-service-api/src/association/bind_3pid.rs new file mode 100644 index 00000000..8aadab0c --- /dev/null +++ b/crates/ruma-identity-service-api/src/association/bind_3pid.rs @@ -0,0 +1,3 @@ +//! Endpoint to bind a session to a Matrix user ID. + +pub mod v2; diff --git a/crates/ruma-identity-service-api/src/association/bind_3pid/v2.rs b/crates/ruma-identity-service-api/src/association/bind_3pid/v2.rs new file mode 100644 index 00000000..f3770bd2 --- /dev/null +++ b/crates/ruma-identity-service-api/src/association/bind_3pid/v2.rs @@ -0,0 +1,75 @@ +//! [POST /_matrix/identity/v2/3pid/bind](https://matrix.org/docs/spec/identity_service/r0.3.0#post-matrix-identity-v2-3pid-bind) + +use ruma_api::ruma_api; +use ruma_common::{thirdparty::Medium, MilliSecondsSinceUnixEpoch}; +use ruma_identifiers::{ServerSignatures, UserId}; + +ruma_api! { + metadata: { + description: "Publish an association between a session and a Matrix user ID.", + method: POST, + name: "bind_3pid", + path: "/_matrix/identity/v2/3pid/bind", + rate_limited: false, + authentication: AccessToken, + } + + request: { + /// The session ID generated by the `requestToken` call. + pub sid: &'a str, + + /// The client secret passed to the `requestToken` call. + pub client_secret: &'a str, + + /// The Matrix user ID to associate with the 3PIDs. + pub mxid: &'a UserId, + } + + response: { + /// The 3PID address of the user being looked up. + pub address: String, + + /// The medium type of the 3PID. + pub medium: Medium, + + /// The Matrix user ID associated with the 3PID. + pub mxid: UserId, + + /// A UNIX timestamp before which the association is not known to be valid. + pub not_before: MilliSecondsSinceUnixEpoch, + + /// A UNIX timestamp after which the association is not known to be valid. + pub not_after: MilliSecondsSinceUnixEpoch, + + /// The UNIX timestamp at which the association was verified. + pub ts: MilliSecondsSinceUnixEpoch, + + /// The signatures of the verifiying identity servers which show that the + /// association should be trusted, if you trust the verifying identity services. + pub signatures: ServerSignatures, + } + +} + +impl<'a> Request<'a> { + /// Creates a `Request` with the given session ID, client secret and Matrix user ID. + pub fn new(sid: &'a str, client_secret: &'a str, mxid: &'a UserId) -> Self { + Self { sid, client_secret, mxid } + } +} + +impl Response { + /// Creates a `Response` with the given 3PID address, medium, Matrix user ID, timestamps and + /// signatures. + pub fn new( + address: String, + medium: Medium, + mxid: UserId, + not_before: MilliSecondsSinceUnixEpoch, + not_after: MilliSecondsSinceUnixEpoch, + ts: MilliSecondsSinceUnixEpoch, + signatures: ServerSignatures, + ) -> Self { + Self { address, medium, mxid, not_before, not_after, ts, signatures } + } +}