@@ -144,7 +144,7 @@ impl AuthorizationGrantStage {
144144
145145pub enum LoginHint < ' a > {
146146 MXID ( & ' a UserId ) ,
147- EMAIL ( lettre:: Address ) ,
147+ Email ( lettre:: Address ) ,
148148 None ,
149149}
150150
@@ -175,14 +175,31 @@ impl std::ops::Deref for AuthorizationGrant {
175175}
176176
177177impl AuthorizationGrant {
178+ /// Parse a `login_hint`
179+ ///
180+ /// Returns `LoginHint::MXID` for valid mxid 'mxid:@john.doe:example.com'
181+ ///
182+ /// Returns `LoginHint::Email` for valid email '[email protected] ' if email 183+ /// supports is enabled
184+ ///
185+ /// Otherwise returns `LoginHint::None`
178186 #[ must_use]
179187 pub fn parse_login_hint ( & self , homeserver : & str , login_with_email_allowed : bool ) -> LoginHint {
180188 let Some ( login_hint) = & self . login_hint else {
181189 return LoginHint :: None ;
182190 } ;
183191
184- // Return none if the format is incorrect
185192 let Some ( ( prefix, value) ) = login_hint. split_once ( ':' ) else {
193+ // If email supports for login_hint is enabled
194+ if login_with_email_allowed {
195+ // Validate the email
196+ let Ok ( address) = lettre:: Address :: from_str ( login_hint) else {
197+ return LoginHint :: None ;
198+ } ;
199+ // Return none if the format is incorrect
200+ return LoginHint :: Email ( address) ;
201+ }
202+ // Unknown hint type, treat as none
186203 return LoginHint :: None ;
187204 } ;
188205
@@ -200,16 +217,6 @@ impl AuthorizationGrant {
200217
201218 LoginHint :: MXID ( mxid)
202219 }
203- "email" => {
204- if !login_with_email_allowed {
205- return LoginHint :: None ;
206- }
207- // Validate the email
208- let Ok ( address) = lettre:: Address :: from_str ( value) else {
209- return LoginHint :: None ;
210- } ;
211- LoginHint :: EMAIL ( address)
212- }
213220 // Unknown hint type, treat as none
214221 _ => LoginHint :: None ,
215222 }
@@ -333,13 +340,13 @@ mod tests {
333340 let now = Utc :: now ( ) ;
334341
335342 let grant = AuthorizationGrant {
336- login_hint : Some ( String :: from ( "email: example@user" ) ) ,
343+ login_hint : Some ( String :: from ( "example@user" ) ) ,
337344 ..AuthorizationGrant :: sample ( now, & mut rng)
338345 } ;
339346
340347 let hint = grant. parse_login_hint ( "example.com" , true ) ;
341348
342- assert ! ( matches!( hint, LoginHint :: EMAIL ( email) if email. to_string( ) == "example@user" ) ) ;
349+ assert ! ( matches!( hint, LoginHint :: Email ( email) if email. to_string( ) == "example@user" ) ) ;
343350 }
344351
345352 #[ test]
@@ -351,7 +358,7 @@ mod tests {
351358 let now = Utc :: now ( ) ;
352359
353360 let grant = AuthorizationGrant {
354- login_hint : Some ( String :: from ( "email: example@user" ) ) ,
361+ login_hint : Some ( String :: from ( "example@user" ) ) ,
355362 ..AuthorizationGrant :: sample ( now, & mut rng)
356363 } ;
357364
0 commit comments