Skip to main content

tuwunel_admin/token/
commands.rs

1use futures::StreamExt;
2use tuwunel_core::{Result, utils};
3use tuwunel_macros::admin_command;
4use tuwunel_service::registration_tokens::TokenExpires;
5
6#[admin_command]
7pub(super) async fn issue(
8	&self,
9	max_uses: Option<u64>,
10	max_age: Option<String>,
11	once: bool,
12) -> Result {
13	let expires = TokenExpires {
14		max_uses: max_uses.or_else(|| once.then_some(1)),
15		max_age: max_age
16			.map(|max_age| {
17				let duration = utils::time::parse_duration(&max_age)?;
18				utils::time::timepoint_from_now(duration)
19			})
20			.transpose()?,
21	};
22
23	let (token, info) = self
24		.services
25		.registration_tokens
26		.issue_token(expires)
27		.await?;
28
29	self.write_str(&format!("New registration token issued: `{token}` - {info}"))
30		.await
31}
32
33#[admin_command]
34pub(super) async fn revoke(&self, token: String) -> Result {
35	self.services
36		.registration_tokens
37		.revoke_token(&token)
38		.await?;
39
40	self.write_str("Token revoked successfully.")
41		.await
42}
43
44#[admin_command]
45pub(super) async fn list(&self) -> Result {
46	let tokens: Vec<_> = self
47		.services
48		.registration_tokens
49		.iterate_tokens()
50		.collect()
51		.await;
52
53	self.write_str(&format!("Found {} registration tokens:\n", tokens.len()))
54		.await?;
55
56	for token in tokens {
57		self.write_str(&format!("- {token}\n")).await?;
58	}
59
60	Ok(())
61}