@@ -144,7 +144,7 @@ impl AuthorizationGrantStage {
144
144
145
145
pub enum LoginHint < ' a > {
146
146
MXID ( & ' a UserId ) ,
147
- EMAIL ( lettre:: Address ) ,
147
+ Email ( lettre:: Address ) ,
148
148
None ,
149
149
}
150
150
@@ -175,14 +175,31 @@ impl std::ops::Deref for AuthorizationGrant {
175
175
}
176
176
177
177
impl 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 'john.doe@example.com' if
183
+ /// email supports is enabled
184
+ ///
185
+ /// Otherwise returns `LoginHint::None`
178
186
#[ must_use]
179
187
pub fn parse_login_hint ( & self , homeserver : & str , login_with_email_allowed : bool ) -> LoginHint {
180
188
let Some ( login_hint) = & self . login_hint else {
181
189
return LoginHint :: None ;
182
190
} ;
183
191
184
- // Return none if the format is incorrect
185
192
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
186
203
return LoginHint :: None ;
187
204
} ;
188
205
@@ -200,16 +217,6 @@ impl AuthorizationGrant {
200
217
201
218
LoginHint :: MXID ( mxid)
202
219
}
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
- }
213
220
// Unknown hint type, treat as none
214
221
_ => LoginHint :: None ,
215
222
}
@@ -333,13 +340,13 @@ mod tests {
333
340
let now = Utc :: now ( ) ;
334
341
335
342
let grant = AuthorizationGrant {
336
- login_hint : Some ( String :: from ( "email: example@user" ) ) ,
343
+ login_hint : Some ( String :: from ( "example@user" ) ) ,
337
344
..AuthorizationGrant :: sample ( now, & mut rng)
338
345
} ;
339
346
340
347
let hint = grant. parse_login_hint ( "example.com" , true ) ;
341
348
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" ) ) ;
343
350
}
344
351
345
352
#[ test]
@@ -351,7 +358,7 @@ mod tests {
351
358
let now = Utc :: now ( ) ;
352
359
353
360
let grant = AuthorizationGrant {
354
- login_hint : Some ( String :: from ( "email: example@user" ) ) ,
361
+ login_hint : Some ( String :: from ( "example@user" ) ) ,
355
362
..AuthorizationGrant :: sample ( now, & mut rng)
356
363
} ;
357
364
0 commit comments