Skip to content

Commit 12e9d02

Browse files
committed
Add whether the registration is valid or not in the admin API
1 parent 5d13691 commit 12e9d02

File tree

6 files changed

+71
-19
lines changed

6 files changed

+71
-19
lines changed

crates/handlers/src/admin/model.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,9 @@ pub struct UserRegistrationToken {
612612
/// The token string
613613
token: String,
614614

615+
/// Whether the token is valid
616+
valid: bool,
617+
615618
/// Maximum number of times this token can be used
616619
usage_limit: Option<u32>,
617620

@@ -631,10 +634,11 @@ pub struct UserRegistrationToken {
631634
revoked_at: Option<DateTime<Utc>>,
632635
}
633636

634-
impl From<mas_data_model::UserRegistrationToken> for UserRegistrationToken {
635-
fn from(token: mas_data_model::UserRegistrationToken) -> Self {
637+
impl UserRegistrationToken {
638+
pub fn new(token: mas_data_model::UserRegistrationToken, now: DateTime<Utc>) -> Self {
636639
Self {
637640
id: token.id,
641+
valid: token.is_valid(now),
638642
token: token.token,
639643
usage_limit: token.usage_limit,
640644
times_used: token.times_used,
@@ -662,6 +666,7 @@ impl UserRegistrationToken {
662666
Self {
663667
id: Ulid::from_bytes([0x01; 16]),
664668
token: "abc123def456".to_owned(),
669+
valid: true,
665670
usage_limit: Some(10),
666671
times_used: 5,
667672
created_at: DateTime::default(),
@@ -672,6 +677,7 @@ impl UserRegistrationToken {
672677
Self {
673678
id: Ulid::from_bytes([0x02; 16]),
674679
token: "xyz789abc012".to_owned(),
680+
valid: false,
675681
usage_limit: None,
676682
times_used: 0,
677683
created_at: DateTime::default(),

crates/handlers/src/admin/v1/user_registration_tokens/add.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use chrono::{DateTime, Utc};
99
use hyper::StatusCode;
1010
use mas_axum_utils::record_error;
1111
use mas_storage::BoxRng;
12-
use rand::{Rng, distributions::Alphanumeric};
12+
use rand::distributions::{Alphanumeric, DistString};
1313
use schemars::JsonSchema;
1414
use serde::Deserialize;
1515

@@ -79,13 +79,9 @@ pub async fn handler(
7979
Json(params): Json<Request>,
8080
) -> Result<(StatusCode, Json<SingleResponse<UserRegistrationToken>>), RouteError> {
8181
// Generate a random token if none was provided
82-
let token = params.token.unwrap_or_else(|| {
83-
(&mut rng)
84-
.sample_iter(&Alphanumeric)
85-
.take(12)
86-
.map(char::from)
87-
.collect()
88-
});
82+
let token = params
83+
.token
84+
.unwrap_or_else(|| Alphanumeric.sample_string(&mut rng, 12));
8985

9086
let registration_token = repo
9187
.user_registration_token()
@@ -102,7 +98,10 @@ pub async fn handler(
10298

10399
Ok((
104100
StatusCode::CREATED,
105-
Json(SingleResponse::new_canonical(registration_token.into())),
101+
Json(SingleResponse::new_canonical(UserRegistrationToken::new(
102+
registration_token,
103+
clock.now(),
104+
))),
106105
))
107106
}
108107

@@ -137,6 +136,7 @@ mod tests {
137136
"id": "01FSHN9AG0MZAA6S4AF7CTV32E",
138137
"attributes": {
139138
"token": "test_token_123",
139+
"valid": true,
140140
"usage_limit": 5,
141141
"times_used": 0,
142142
"created_at": "2022-01-16T14:40:00Z",
@@ -178,6 +178,7 @@ mod tests {
178178
"id": "01FSHN9AG0QMGC989M0XSFVF2X",
179179
"attributes": {
180180
"token": "42oTpLoieH5I",
181+
"valid": true,
181182
"usage_limit": 1,
182183
"times_used": 0,
183184
"created_at": "2022-01-16T14:40:00Z",

crates/handlers/src/admin/v1/user_registration_tokens/get.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ pub fn doc(operation: TransformOperation) -> TransformOperation {
6363

6464
#[tracing::instrument(name = "handler.admin.v1.user_registration_tokens.get", skip_all)]
6565
pub async fn handler(
66-
CallContext { mut repo, .. }: CallContext,
66+
CallContext {
67+
mut repo, clock, ..
68+
}: CallContext,
6769
id: UlidPathParam,
6870
) -> Result<Json<SingleResponse<UserRegistrationToken>>, RouteError> {
6971
let token = repo
@@ -73,7 +75,7 @@ pub async fn handler(
7375
.ok_or(RouteError::NotFound(*id))?;
7476

7577
Ok(Json(SingleResponse::new_canonical(
76-
UserRegistrationToken::from(token),
78+
UserRegistrationToken::new(token, clock.now()),
7779
)))
7880
}
7981

@@ -116,13 +118,14 @@ mod tests {
116118
response.assert_status(StatusCode::OK);
117119
let body: serde_json::Value = response.json();
118120

119-
assert_json_snapshot!(body, @r###"
121+
assert_json_snapshot!(body, @r#"
120122
{
121123
"data": {
122124
"type": "user-registration_token",
123125
"id": "01FSHN9AG0MZAA6S4AF7CTV32E",
124126
"attributes": {
125127
"token": "test_token_123",
128+
"valid": true,
126129
"usage_limit": 5,
127130
"times_used": 0,
128131
"created_at": "2022-01-16T14:40:00Z",
@@ -138,7 +141,7 @@ mod tests {
138141
"self": "/api/admin/v1/user-registration-tokens/01FSHN9AG0MZAA6S4AF7CTV32E"
139142
}
140143
}
141-
"###);
144+
"#);
142145
}
143146

144147
#[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")]

0 commit comments

Comments
 (0)