Skip to main content

tuwunel_api/client/admin/tokens/
create.rs

1use axum::extract::State;
2use synapse_admin_api::registration_tokens::create::v1 as create;
3use tuwunel_core::Result;
4
5use super::{database_token_response, token_expires};
6use crate::{Ruma, client::admin::require_admin};
7
8/// # `POST /_synapse/admin/v1/registration_tokens/new`
9///
10/// A duplicate token is rejected with `400`, not `409`.
11pub(crate) async fn admin_create_token_route(
12	State(services): State<crate::State>,
13	body: Ruma<create::Request>,
14) -> Result<create::Response> {
15	require_admin(&services, body.sender_user()).await?;
16
17	let expires = token_expires(body.uses_allowed, body.expiry_time)?;
18
19	let (token, info) = services
20		.registration_tokens
21		.create_token(
22			body.token.as_deref(),
23			body.length
24				.and_then(|length| length.try_into().ok()),
25			expires,
26		)
27		.await?;
28
29	Ok(create::Response {
30		token: database_token_response(token, &info),
31	})
32}