Skip to content

Commit 322c854

Browse files
committed
Admin API to list user registration tokens
1 parent 62cad6a commit 322c854

File tree

6 files changed

+1537
-0
lines changed

6 files changed

+1537
-0
lines changed

crates/handlers/src/admin/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ fn finish(t: TransformOpenApi) -> TransformOpenApi {
7373
description: Some("Manage browser sessions of users".to_owned()),
7474
..Tag::default()
7575
})
76+
.tag(Tag {
77+
name: "user-registration-token".to_owned(),
78+
description: Some("Manage user registration tokens".to_owned()),
79+
..Tag::default()
80+
})
7681
.tag(Tag {
7782
name: "upstream-oauth-link".to_owned(),
7883
description: Some(

crates/handlers/src/admin/model.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,3 +602,83 @@ impl PolicyData {
602602
}]
603603
}
604604
}
605+
606+
/// A registration token
607+
#[derive(Serialize, JsonSchema)]
608+
pub struct UserRegistrationToken {
609+
#[serde(skip)]
610+
id: Ulid,
611+
612+
/// The token string
613+
token: String,
614+
615+
/// Maximum number of times this token can be used
616+
usage_limit: Option<u32>,
617+
618+
/// Number of times this token has been used
619+
times_used: u32,
620+
621+
/// When the token was created
622+
created_at: DateTime<Utc>,
623+
624+
/// When the token was last used. If null, the token has never been used.
625+
last_used_at: Option<DateTime<Utc>>,
626+
627+
/// When the token expires. If null, the token never expires.
628+
expires_at: Option<DateTime<Utc>>,
629+
630+
/// When the token was revoked. If null, the token is not revoked.
631+
revoked_at: Option<DateTime<Utc>>,
632+
}
633+
634+
impl From<mas_data_model::UserRegistrationToken> for UserRegistrationToken {
635+
fn from(token: mas_data_model::UserRegistrationToken) -> Self {
636+
Self {
637+
id: token.id,
638+
token: token.token,
639+
usage_limit: token.usage_limit,
640+
times_used: token.times_used,
641+
created_at: token.created_at,
642+
last_used_at: token.last_used_at,
643+
expires_at: token.expires_at,
644+
revoked_at: token.revoked_at,
645+
}
646+
}
647+
}
648+
649+
impl Resource for UserRegistrationToken {
650+
const KIND: &'static str = "user-registration_token";
651+
const PATH: &'static str = "/api/admin/v1/user-registration-tokens";
652+
653+
fn id(&self) -> Ulid {
654+
self.id
655+
}
656+
}
657+
658+
impl UserRegistrationToken {
659+
/// Samples of registration tokens
660+
pub fn samples() -> [Self; 2] {
661+
[
662+
Self {
663+
id: Ulid::from_bytes([0x01; 16]),
664+
token: "abc123def456".to_owned(),
665+
usage_limit: Some(10),
666+
times_used: 5,
667+
created_at: DateTime::default(),
668+
last_used_at: Some(DateTime::default()),
669+
expires_at: Some(DateTime::default() + chrono::Duration::days(30)),
670+
revoked_at: None,
671+
},
672+
Self {
673+
id: Ulid::from_bytes([0x02; 16]),
674+
token: "xyz789abc012".to_owned(),
675+
usage_limit: None,
676+
times_used: 0,
677+
created_at: DateTime::default(),
678+
last_used_at: None,
679+
expires_at: None,
680+
revoked_at: Some(DateTime::default()),
681+
},
682+
]
683+
}
684+
}

crates/handlers/src/admin/v1/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ mod oauth2_sessions;
2323
mod policy_data;
2424
mod upstream_oauth_links;
2525
mod user_emails;
26+
mod user_registration_tokens;
2627
mod user_sessions;
2728
mod users;
2829

@@ -119,6 +120,13 @@ where
119120
"/user-sessions/{id}",
120121
get_with(self::user_sessions::get, self::user_sessions::get_doc),
121122
)
123+
.api_route(
124+
"/user-registration-tokens",
125+
get_with(
126+
self::user_registration_tokens::list,
127+
self::user_registration_tokens::list_doc,
128+
),
129+
)
122130
.api_route(
123131
"/upstream-oauth-links",
124132
get_with(

0 commit comments

Comments
 (0)