Skip to content

Commit 704acfd

Browse files
committed
rename Login:EMAIL to Login::Email + remove use of email prefix
1 parent 7622d68 commit 704acfd

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

crates/data-model/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ rand.workspace = true
2424
regex.workspace = true
2525
woothee.workspace = true
2626
ruma-common.workspace = true
27+
lettre.workspace = true
2728

2829
mas-iana.workspace = true
2930
mas-jose.workspace = true
3031
oauth2-types.workspace = true
31-
# Emails
32-
lettre.workspace = true
32+

crates/data-model/src/oauth2/authorization_grant.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl AuthorizationGrantStage {
144144

145145
pub 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

177177
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`
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

crates/handlers/src/views/login.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ fn handle_login_hint(
385385
site_config.login_with_email_allowed,
386386
) {
387387
LoginHint::MXID(mxid) => Some(mxid.localpart().to_owned()),
388-
LoginHint::EMAIL(email) => Some(email.to_string()),
388+
LoginHint::Email(email) => Some(email.to_string()),
389389
LoginHint::None => None,
390390
};
391391
form_state.set_value(LoginFormField::Username, value);

0 commit comments

Comments
 (0)