1- // Copyright 2024 New Vector Ltd.
1+ // Copyright 2024, 2025 New Vector Ltd.
22// Copyright 2021-2024 The Matrix.org Foundation C.I.C.
33//
44// SPDX-License-Identifier: AGPL-3.0-only
@@ -22,7 +22,8 @@ use mas_data_model::{
2222 AuthorizationGrant , BrowserSession , Client , CompatSsoLogin , CompatSsoLoginState ,
2323 DeviceCodeGrant , UpstreamOAuthLink , UpstreamOAuthProvider , UpstreamOAuthProviderClaimsImports ,
2424 UpstreamOAuthProviderDiscoveryMode , UpstreamOAuthProviderPkceMode ,
25- UpstreamOAuthProviderTokenAuthMethod , User , UserAgent , UserEmail , UserRecoverySession ,
25+ UpstreamOAuthProviderTokenAuthMethod , User , UserAgent , UserEmail , UserEmailAuthenticationCode ,
26+ UserRecoverySession ,
2627} ;
2728use mas_i18n:: DataLocale ;
2829use mas_iana:: jose:: JsonWebSignatureAlg ;
@@ -877,27 +878,33 @@ impl TemplateContext for EmailRecoveryContext {
877878/// Context used by the `emails/verification.{txt,html,subject}` templates
878879#[ derive( Serialize ) ]
879880pub struct EmailVerificationContext {
880- user : User ,
881- verification : serde_json :: Value ,
881+ browser_session : Option < BrowserSession > ,
882+ authentication_code : UserEmailAuthenticationCode ,
882883}
883884
884885impl EmailVerificationContext {
885886 /// Constructs a context for the verification email
886887 #[ must_use]
887- pub fn new ( user : User , verification : serde_json:: Value ) -> Self {
888- Self { user, verification }
888+ pub fn new (
889+ authentication_code : UserEmailAuthenticationCode ,
890+ browser_session : Option < BrowserSession > ,
891+ ) -> Self {
892+ Self {
893+ browser_session,
894+ authentication_code,
895+ }
889896 }
890897
891898 /// Get the user to which this email is being sent
892899 #[ must_use]
893- pub fn user ( & self ) -> & User {
894- & self . user
900+ pub fn user ( & self ) -> Option < & User > {
901+ self . browser_session . as_ref ( ) . map ( |s| & s . user )
895902 }
896903
897904 /// Get the verification code being sent
898905 #[ must_use]
899- pub fn verification ( & self ) -> & serde_json :: Value {
900- & self . verification
906+ pub fn code ( & self ) -> & str {
907+ & self . authentication_code . code
901908 }
902909}
903910
@@ -906,11 +913,21 @@ impl TemplateContext for EmailVerificationContext {
906913 where
907914 Self : Sized ,
908915 {
909- User :: samples ( now, rng)
916+ BrowserSession :: samples ( now, rng)
910917 . into_iter ( )
911- . map ( |user| {
912- let verification = serde_json:: json!( { "code" : "123456" } ) ;
913- Self { user, verification }
918+ . map ( |browser_session| {
919+ let authentication_code = UserEmailAuthenticationCode {
920+ id : Ulid :: from_datetime_with_source ( now. into ( ) , rng) ,
921+ user_email_authentication_id : Ulid :: from_datetime_with_source ( now. into ( ) , rng) ,
922+ code : "123456" . to_owned ( ) ,
923+ created_at : now - Duration :: try_minutes ( 5 ) . unwrap ( ) ,
924+ expires_at : now + Duration :: try_minutes ( 25 ) . unwrap ( ) ,
925+ } ;
926+
927+ Self {
928+ browser_session : Some ( browser_session) ,
929+ authentication_code,
930+ }
914931 } )
915932 . collect ( )
916933 }
0 commit comments