@@ -13,6 +13,7 @@ use axum::{
13
13
use axum_extra:: TypedHeader ;
14
14
use chrono:: Duration ;
15
15
use mas_axum_utils:: { InternalError , SessionInfoExt as _, cookies:: CookieJar } ;
16
+ use mas_data_model:: SiteConfig ;
16
17
use mas_matrix:: HomeserverConnection ;
17
18
use mas_router:: { PostAuthAction , UrlBuilder } ;
18
19
use mas_storage:: {
@@ -51,6 +52,7 @@ pub(crate) async fn get(
51
52
State ( url_builder) : State < UrlBuilder > ,
52
53
State ( homeserver) : State < Arc < dyn HomeserverConnection > > ,
53
54
State ( templates) : State < Templates > ,
55
+ State ( site_config) : State < SiteConfig > ,
54
56
PreferredLanguage ( lang) : PreferredLanguage ,
55
57
cookie_jar : CookieJar ,
56
58
Path ( id) : Path < Ulid > ,
@@ -118,6 +120,37 @@ pub(crate) async fn get(
118
120
) ) ) ;
119
121
}
120
122
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
+
121
154
// For now, we require an email address on the registration, but this might
122
155
// change in the future
123
156
let email_authentication_id = registration
@@ -174,12 +207,19 @@ pub(crate) async fn get(
174
207
. into_response ( ) ) ;
175
208
}
176
209
177
- // Everuthing is good, let's complete the registration
210
+ // Everything is good, let's complete the registration
178
211
let registration = repo
179
212
. user_registration ( )
180
213
. complete ( & clock, registration)
181
214
. await ?;
182
215
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
+
183
223
// Consume the registration session
184
224
let cookie_jar = registrations
185
225
. consume_session ( & registration) ?
0 commit comments