@@ -24,8 +24,8 @@ use mas_matrix::BoxHomeserverConnection;
24
24
use mas_policy:: Policy ;
25
25
use mas_router:: UrlBuilder ;
26
26
use mas_storage:: {
27
- queue:: { ProvisionUserJob , QueueJobRepositoryExt as _} ,
28
- user:: { BrowserSessionRepository , UserEmailRepository , UserPasswordRepository , UserRepository } ,
27
+ queue:: { QueueJobRepositoryExt as _, SendEmailAuthenticationCodeJob } ,
28
+ user:: { UserEmailRepository , UserRepository } ,
29
29
BoxClock , BoxRepository , BoxRng , RepositoryAccess ,
30
30
} ;
31
31
use mas_templates:: {
@@ -141,6 +141,8 @@ pub(crate) async fn post(
141
141
Form ( form) : Form < ProtectedForm < RegisterForm > > ,
142
142
) -> Result < Response , FancyError > {
143
143
let user_agent = user_agent. map ( |ua| UserAgent :: parse ( ua. as_str ( ) . to_owned ( ) ) ) ;
144
+
145
+ let ip_address = activity_tracker. ip ( ) ;
144
146
if !site_config. password_registration_enabled {
145
147
return Ok ( StatusCode :: METHOD_NOT_ALLOWED . into_response ( ) ) ;
146
148
}
@@ -296,49 +298,64 @@ pub(crate) async fn post(
296
298
return Ok ( ( cookie_jar, Html ( content) ) . into_response ( ) ) ;
297
299
}
298
300
299
- let user = repo. user ( ) . add ( & mut rng, & clock, form. username ) . await ?;
300
-
301
- if let Some ( tos_uri) = & site_config. tos_uri {
302
- repo. user_terms ( )
303
- . accept_terms ( & mut rng, & clock, & user, tos_uri. clone ( ) )
304
- . await ?;
305
- }
306
-
307
- let password = Zeroizing :: new ( form. password . into_bytes ( ) ) ;
308
- let ( version, hashed_password) = password_manager. hash ( & mut rng, password) . await ?;
309
- let user_password = repo
310
- . user_password ( )
311
- . add ( & mut rng, & clock, & user, version, hashed_password, None )
301
+ let post_auth_action = query
302
+ . post_auth_action
303
+ . map ( serde_json:: to_value)
304
+ . transpose ( ) ?;
305
+ let registration = repo
306
+ . user_registration ( )
307
+ . add (
308
+ & mut rng,
309
+ & clock,
310
+ form. username ,
311
+ ip_address,
312
+ user_agent,
313
+ post_auth_action,
314
+ )
312
315
. await ?;
313
316
314
- let user_email = repo
317
+ let registration = if let Some ( tos_uri) = & site_config. tos_uri {
318
+ repo. user_registration ( )
319
+ . set_terms_url ( registration, tos_uri. clone ( ) )
320
+ . await ?
321
+ } else {
322
+ registration
323
+ } ;
324
+
325
+ // Create a new user email authentication session
326
+ let user_email_authentication = repo
315
327
. user_email ( )
316
- . add ( & mut rng, & clock, & user , form. email )
328
+ . add_authentication_for_registration ( & mut rng, & clock, form. email , & registration )
317
329
. await ?;
318
330
319
- let next = mas_router:: AccountVerifyEmail :: new ( user_email. id ) . and_maybe ( query. post_auth_action ) ;
320
-
321
- let session = repo
322
- . browser_session ( )
323
- . add ( & mut rng, & clock, & user, user_agent)
331
+ // Schedule a job to verify the email
332
+ repo. queue_job ( )
333
+ . schedule_job (
334
+ & mut rng,
335
+ & clock,
336
+ SendEmailAuthenticationCodeJob :: new ( & user_email_authentication, locale. to_string ( ) ) ,
337
+ )
324
338
. await ?;
325
339
326
- repo. browser_session ( )
327
- . authenticate_with_password ( & mut rng, & clock, & session, & user_password)
340
+ let registration = repo
341
+ . user_registration ( )
342
+ . set_email_authentication ( registration, & user_email_authentication)
328
343
. await ?;
329
344
330
- repo. queue_job ( )
331
- . schedule_job ( & mut rng, & clock, ProvisionUserJob :: new ( & user) )
345
+ // Hash the password
346
+ let password = Zeroizing :: new ( form. password . into_bytes ( ) ) ;
347
+ let ( version, hashed_password) = password_manager. hash ( & mut rng, password) . await ?;
348
+
349
+ // Add the password to the registration
350
+ let registration = repo
351
+ . user_registration ( )
352
+ . set_password ( registration, hashed_password, version)
332
353
. await ?;
333
354
334
355
repo. save ( ) . await ?;
335
356
336
- activity_tracker
337
- . record_browser_session ( & clock, & session)
338
- . await ;
339
-
340
- let cookie_jar = cookie_jar. set_session ( & session) ;
341
- Ok ( ( cookie_jar, url_builder. redirect ( & next) ) . into_response ( ) )
357
+ // TODO: redirect to the next step on the registration
358
+ Ok ( format ! ( "{}" , registration. id) . into_response ( ) )
342
359
}
343
360
344
361
async fn render (
@@ -451,16 +468,23 @@ mod tests {
451
468
let request = cookies. with_cookies ( request) ;
452
469
let response = state. request ( request) . await ;
453
470
cookies. save_cookies ( & response) ;
454
- response. assert_status ( StatusCode :: SEE_OTHER ) ;
455
-
456
- // Now if we get to the home page, we should see the user's username
457
- let request = Request :: get ( "/" ) . empty ( ) ;
458
- let request = cookies. with_cookies ( request) ;
459
- let response = state. request ( request) . await ;
460
- cookies. save_cookies ( & response) ;
461
471
response. assert_status ( StatusCode :: OK ) ;
462
- response. assert_header_value ( CONTENT_TYPE , "text/html; charset=utf-8" ) ;
463
- assert ! ( response. body( ) . contains( "john" ) ) ;
472
+
473
+ // The handler gives us the registration ID in the body for now
474
+ let id = response. body ( ) . parse ( ) . unwrap ( ) ;
475
+ // There should be a new registration in the database
476
+ let mut repo = state. repository ( ) . await . unwrap ( ) ;
477
+ let registration = repo. user_registration ( ) . lookup ( id) . await . unwrap ( ) . unwrap ( ) ;
478
+ assert_eq ! ( registration. username, "john" . to_owned( ) ) ;
479
+ assert ! ( registration. password. is_some( ) ) ;
480
+
481
+ let email_authentication = repo
482
+ . user_email ( )
483
+ . lookup_authentication ( registration. email_authentication_id . unwrap ( ) )
484
+ . await
485
+ . unwrap ( )
486
+ . unwrap ( ) ;
487
+ assert_eq ! ( email_authentication
. email
, "[email protected] " ) ;
464
488
}
465
489
466
490
/// When the two password fields mismatch, it should give an error
0 commit comments