Skip to content

Commit 455e778

Browse files
committed
Enforce registration token on registration
1 parent d4e2d06 commit 455e778

File tree

1 file changed

+41
-1
lines changed
  • crates/handlers/src/views/register/steps

1 file changed

+41
-1
lines changed

crates/handlers/src/views/register/steps/finish.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use axum::{
1313
use axum_extra::TypedHeader;
1414
use chrono::Duration;
1515
use mas_axum_utils::{InternalError, SessionInfoExt as _, cookies::CookieJar};
16+
use mas_data_model::SiteConfig;
1617
use mas_matrix::HomeserverConnection;
1718
use mas_router::{PostAuthAction, UrlBuilder};
1819
use mas_storage::{
@@ -51,6 +52,7 @@ pub(crate) async fn get(
5152
State(url_builder): State<UrlBuilder>,
5253
State(homeserver): State<Arc<dyn HomeserverConnection>>,
5354
State(templates): State<Templates>,
55+
State(site_config): State<SiteConfig>,
5456
PreferredLanguage(lang): PreferredLanguage,
5557
cookie_jar: CookieJar,
5658
Path(id): Path<Ulid>,
@@ -118,6 +120,37 @@ pub(crate) async fn get(
118120
)));
119121
}
120122

123+
// Check if the registration token is required and was provided
124+
let registration_token = if site_config.registration_token_required {
125+
if let Some(registration_token_id) = registration.user_registration_token_id {
126+
let registration_token = repo
127+
.user_registration_token()
128+
.lookup(registration_token_id)
129+
.await?
130+
.context("Could not load the registration token")
131+
.map_err(InternalError::from_anyhow)?;
132+
133+
if !registration_token.is_valid(clock.now()) {
134+
// XXX: the registration token isn't valid anymore, we should
135+
// have a better error in this case?
136+
return Err(InternalError::from_anyhow(anyhow::anyhow!(
137+
"Registration token used is no longer valid"
138+
)));
139+
}
140+
141+
Some(registration_token)
142+
} else {
143+
// Else redirect to the registration token page
144+
return Ok((
145+
cookie_jar,
146+
url_builder.redirect(&mas_router::RegisterToken::new(registration.id)),
147+
)
148+
.into_response());
149+
}
150+
} else {
151+
None
152+
};
153+
121154
// For now, we require an email address on the registration, but this might
122155
// change in the future
123156
let email_authentication_id = registration
@@ -174,12 +207,19 @@ pub(crate) async fn get(
174207
.into_response());
175208
}
176209

177-
// Everuthing is good, let's complete the registration
210+
// Everything is good, let's complete the registration
178211
let registration = repo
179212
.user_registration()
180213
.complete(&clock, registration)
181214
.await?;
182215

216+
// If we used a registration token, we need to mark it as used
217+
if let Some(registration_token) = registration_token {
218+
repo.user_registration_token()
219+
.use_token(&clock, registration_token)
220+
.await?;
221+
}
222+
183223
// Consume the registration session
184224
let cookie_jar = registrations
185225
.consume_session(&registration)?

0 commit comments

Comments
 (0)