From 481531a161873822a0e9df48794d812dd0a69a88 Mon Sep 17 00:00:00 2001 From: Abhik Jain Date: Tue, 27 Apr 2021 00:30:54 +0530 Subject: [PATCH] identity-service-api: Add endpoints for email validation --- ruma-identity-service-api/CHANGELOG.md | 14 ++++- ruma-identity-service-api/Cargo.toml | 1 + ruma-identity-service-api/src/association.rs | 3 + .../src/association/email.rs | 5 ++ .../email/create_email_validation_session.rs | 3 + .../create_email_validation_session/v2.rs | 57 +++++++++++++++++++ .../src/association/email/validate_email.rs | 3 + .../association/email/validate_email/v2.rs | 44 ++++++++++++++ .../email/validate_email_by_end_user.rs | 3 + .../email/validate_email_by_end_user/v2.rs | 45 +++++++++++++++ ruma-identity-service-api/src/lib.rs | 1 + 11 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 ruma-identity-service-api/src/association.rs create mode 100644 ruma-identity-service-api/src/association/email.rs create mode 100644 ruma-identity-service-api/src/association/email/create_email_validation_session.rs create mode 100644 ruma-identity-service-api/src/association/email/create_email_validation_session/v2.rs create mode 100644 ruma-identity-service-api/src/association/email/validate_email.rs create mode 100644 ruma-identity-service-api/src/association/email/validate_email/v2.rs create mode 100644 ruma-identity-service-api/src/association/email/validate_email_by_end_user.rs create mode 100644 ruma-identity-service-api/src/association/email/validate_email_by_end_user/v2.rs diff --git a/ruma-identity-service-api/CHANGELOG.md b/ruma-identity-service-api/CHANGELOG.md index 1c490f10..84bc147a 100644 --- a/ruma-identity-service-api/CHANGELOG.md +++ b/ruma-identity-service-api/CHANGELOG.md @@ -3,4 +3,16 @@ # 0.0.1 * Add authentication endpoints: - `authentication::{get_account_information::v2, logout::v2, register::v2}` + ```rust + authentication::{get_account_information::v2, logout::v2, register::v2} + ``` +* Add email association endpoints: + ```rust + association::{ + email::{ + create_email_validation_session::v2, + validate_email::v2, + validate_email_by_end_user::v2, + } + } + ``` diff --git a/ruma-identity-service-api/Cargo.toml b/ruma-identity-service-api/Cargo.toml index 4391c062..7e069cff 100644 --- a/ruma-identity-service-api/Cargo.toml +++ b/ruma-identity-service-api/Cargo.toml @@ -11,6 +11,7 @@ repository = "https://github.com/ruma/ruma" edition = "2018" [dependencies] +js_int = { version = "0.2.0", features = ["serde"] } ruma-api = { version = "=0.17.0-alpha.4", path = "../ruma-api" } ruma-common = { version = "0.5.0", path = "../ruma-common" } ruma-identifiers = { version = "0.19.0", path = "../ruma-identifiers" } diff --git a/ruma-identity-service-api/src/association.rs b/ruma-identity-service-api/src/association.rs new file mode 100644 index 00000000..db65096b --- /dev/null +++ b/ruma-identity-service-api/src/association.rs @@ -0,0 +1,3 @@ +//! Endpoints to create associations with a Matrix ID on the identity server. + +pub mod email; diff --git a/ruma-identity-service-api/src/association/email.rs b/ruma-identity-service-api/src/association/email.rs new file mode 100644 index 00000000..e65ed555 --- /dev/null +++ b/ruma-identity-service-api/src/association/email.rs @@ -0,0 +1,5 @@ +//! Endpoints to create an association betwen a Matrix ID and an email ID on the identity server. + +pub mod create_email_validation_session; +pub mod validate_email; +pub mod validate_email_by_end_user; diff --git a/ruma-identity-service-api/src/association/email/create_email_validation_session.rs b/ruma-identity-service-api/src/association/email/create_email_validation_session.rs new file mode 100644 index 00000000..8aa4cd97 --- /dev/null +++ b/ruma-identity-service-api/src/association/email/create_email_validation_session.rs @@ -0,0 +1,3 @@ +//! Create a session for verifying an email. + +pub mod v2; diff --git a/ruma-identity-service-api/src/association/email/create_email_validation_session/v2.rs b/ruma-identity-service-api/src/association/email/create_email_validation_session/v2.rs new file mode 100644 index 00000000..813dbee4 --- /dev/null +++ b/ruma-identity-service-api/src/association/email/create_email_validation_session/v2.rs @@ -0,0 +1,57 @@ +//! [POST /_matrix/identity/v2/validate/email/requestToken](https://matrix.org/docs/spec/identity_service/r0.3.0#post-matrix-identity-v2-validate-email-requesttoken) + +use js_int::UInt; +use ruma_api::ruma_api; + +ruma_api! { + metadata: { + description: "Creates a session for validating an email address.", + method: POST, + name: "create_email_validation_session", + path: "/_matrix/identity/v2/validate/email/requestToken", + authentication: AccessToken, + rate_limited: false, + } + + request: { + /// A unique string generated by the client, and used to identify the validation attempt. + pub client_secret: &'a str, + + /// The email address to validate. + pub email: &'a str, + + /// The server will only send an email if the send_attempt is a number greater than the + /// most recent one which it has seen, scoped to that email + client_secret pair. + pub send_attempt: UInt, + + /// When the validation is completed, the identity server will redirect the user to this + /// URL. + #[serde(skip_serializing_if = "Option::is_none")] + pub next_link: Option<&'a str>, + } + + response: { + /// The session ID. Session IDs are opaque strings generated by the identity server. + pub sid: String, + } +} + +impl<'a> Request<'a> { + /// Create a new `Request` with the given client secret, email ID, `send_attempt` number, and + /// the link to redirect to after validation. + pub fn new( + client_secret: &'a str, + email: &'a str, + send_attempt: js_int::UInt, + next_link: Option<&'a str>, + ) -> Self { + Self { client_secret, email, send_attempt, next_link } + } +} + +impl Response { + /// Create a new `Response` with the given session ID. + pub fn new(sid: String) -> Self { + Self { sid } + } +} diff --git a/ruma-identity-service-api/src/association/email/validate_email.rs b/ruma-identity-service-api/src/association/email/validate_email.rs new file mode 100644 index 00000000..b17fc825 --- /dev/null +++ b/ruma-identity-service-api/src/association/email/validate_email.rs @@ -0,0 +1,3 @@ +//! Validate an email ID after creation of a session. + +pub mod v2; diff --git a/ruma-identity-service-api/src/association/email/validate_email/v2.rs b/ruma-identity-service-api/src/association/email/validate_email/v2.rs new file mode 100644 index 00000000..a95e4c92 --- /dev/null +++ b/ruma-identity-service-api/src/association/email/validate_email/v2.rs @@ -0,0 +1,44 @@ +//! [POST /_matrix/identity/v2/validate/email/submitToken](https://matrix.org/docs/spec/identity_service/r0.3.0#post-matrix-identity-v2-validate-email-submittoken) + +use ruma_api::ruma_api; + +ruma_api! { + metadata: { + description: "Validate ownership of an email address.", + method: POST, + name: "validate_email", + path: "/_matrix/identity/v2/validate/email/submitToken", + authentication: AccessToken, + rate_limited: false, + } + + request: { + /// The session ID, generated by the `requestToken` call. + pub sid: &'a str, + + /// The client secret that was supplied to the `requestToken` call. + pub client_secret: &'a str, + + /// The token generated by the `requestToken` call and emailed to the user. + pub token: &'a str, + } + + response: { + /// Whether the validation was successful or not. + pub success: bool, + } +} + +impl<'a> Request<'a> { + /// Create a new `Request` with the given session ID, client secret and token. + pub fn new(sid: &'a str, client_secret: &'a str, token: &'a str) -> Self { + Self { sid, client_secret, token } + } +} + +impl Response { + /// Create a new `Response` with the success status. + pub fn new(success: bool) -> Self { + Self { success } + } +} diff --git a/ruma-identity-service-api/src/association/email/validate_email_by_end_user.rs b/ruma-identity-service-api/src/association/email/validate_email_by_end_user.rs new file mode 100644 index 00000000..6bfc6723 --- /dev/null +++ b/ruma-identity-service-api/src/association/email/validate_email_by_end_user.rs @@ -0,0 +1,3 @@ +//! Endpoint for validation of an email ID by the end-user, after creation of a session. + +pub mod v2; diff --git a/ruma-identity-service-api/src/association/email/validate_email_by_end_user/v2.rs b/ruma-identity-service-api/src/association/email/validate_email_by_end_user/v2.rs new file mode 100644 index 00000000..1d083214 --- /dev/null +++ b/ruma-identity-service-api/src/association/email/validate_email_by_end_user/v2.rs @@ -0,0 +1,45 @@ +//! [GET /_matrix/identity/v2/validate/email/submitToken](https://matrix.org/docs/spec/identity_service/r0.3.0#get-matrix-identity-v2-validate-email-submittoken) + +use ruma_api::ruma_api; + +ruma_api! { + metadata: { + description: "Validate ownership of an email address.", + method: GET, + name: "validate_email_by_end_user", + path: "/_matrix/identity/v2/validate/email/submitToken", + authentication: AccessToken, + rate_limited: false, + } + + request: { + /// The session ID, generated by the `requestToken` call. + #[ruma_api(query)] + pub sid: &'a str, + + /// The client secret that was supplied to the `requestToken` call. + #[ruma_api(query)] + pub client_secret: &'a str, + + /// The token generated by the `requestToken` call and emailed to the user. + #[ruma_api(query)] + pub token: &'a str, + } + + #[derive(Default)] + response: {} +} + +impl<'a> Request<'a> { + /// Create a new `Request` with the given session ID, client secret and token. + pub fn new(sid: &'a str, client_secret: &'a str, token: &'a str) -> Self { + Self { sid, client_secret, token } + } +} + +impl Response { + /// Create a new empty `Response`. + pub fn new() -> Self { + Self {} + } +} diff --git a/ruma-identity-service-api/src/lib.rs b/ruma-identity-service-api/src/lib.rs index 558fc995..462cff69 100644 --- a/ruma-identity-service-api/src/lib.rs +++ b/ruma-identity-service-api/src/lib.rs @@ -7,6 +7,7 @@ #![warn(missing_docs)] +pub mod association; pub mod authentication; pub mod keys; pub mod lookup;