-
Notifications
You must be signed in to change notification settings - Fork 54
Support suggesting email as login_hint
#4568
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sandhose
merged 13 commits into
element-hq:main
from
tchapgouv:feat/login_hint_with_email
Aug 18, 2025
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8af50a1
display email login_hint when login_with_email_allowed is activated
mcalinghee 3f14589
rename Login:EMAIL to Login::Email + remove use of email prefix
mcalinghee cdf53ca
use of `login_with_email_allowed` at caller level
mcalinghee c314802
move Clock/MockClock/SystemClock/BoxClock/BoxRng to mas-data-model
mcalinghee d65b70d
move Clock/MockClock/SystemClock/BoxClock/BoxRng to mas-data-model : …
mcalinghee a75ca69
move Clock/MockClock/SystemClock/BoxClock/BoxRng to mas-data-model : …
mcalinghee b189bfd
use of static RNG and MockClock in tests
mcalinghee a55f26c
Merge branch 'main' into feat/login_hint_with_email
mcalinghee ec98c35
Merge branch 'main' into feat/login_hint_with_email
mcalinghee efb15e3
Merge branch 'main' into feat/login_hint_with_email
mcalinghee 6a1d67f
Merge remote-tracking branch 'origin/main' into feat/login_hint_with_…
sandhose 576b5c6
Remove unnecessary crate-level comment
sandhose fb5c4de
Simplify handling of login hints
sandhose File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ | |
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial | ||
// Please see LICENSE files in the repository root for full details. | ||
|
||
use std::str::FromStr as _; | ||
|
||
use chrono::{DateTime, Utc}; | ||
use mas_iana::oauth::PkceCodeChallengeMethod; | ||
use oauth2_types::{ | ||
|
@@ -142,6 +144,7 @@ impl AuthorizationGrantStage { | |
|
||
pub enum LoginHint<'a> { | ||
MXID(&'a UserId), | ||
Email(lettre::Address), | ||
None, | ||
} | ||
|
||
|
@@ -172,15 +175,26 @@ impl std::ops::Deref for AuthorizationGrant { | |
} | ||
|
||
impl AuthorizationGrant { | ||
/// Parse a `login_hint` | ||
/// | ||
/// Returns `LoginHint::MXID` for valid mxid 'mxid:@john.doe:example.com' | ||
/// | ||
/// Returns `LoginHint::Email` for valid email '[email protected]' | ||
/// | ||
/// Otherwise returns `LoginHint::None` | ||
#[must_use] | ||
pub fn parse_login_hint(&self, homeserver: &str) -> LoginHint { | ||
let Some(login_hint) = &self.login_hint else { | ||
return LoginHint::None; | ||
}; | ||
|
||
// Return none if the format is incorrect | ||
let Some((prefix, value)) = login_hint.split_once(':') else { | ||
mcalinghee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return LoginHint::None; | ||
// Validate the email | ||
let Ok(address) = lettre::Address::from_str(login_hint) else { | ||
// Return none if the format is incorrect | ||
return LoginHint::None; | ||
}; | ||
return LoginHint::Email(address); | ||
}; | ||
|
||
match prefix { | ||
|
@@ -271,17 +285,15 @@ impl AuthorizationGrant { | |
|
||
#[cfg(test)] | ||
mod tests { | ||
use rand::thread_rng; | ||
use rand::SeedableRng; | ||
|
||
use super::*; | ||
use crate::clock::{Clock, MockClock}; | ||
|
||
#[test] | ||
fn no_login_hint() { | ||
#[allow(clippy::disallowed_methods)] | ||
let mut rng = thread_rng(); | ||
|
||
#[allow(clippy::disallowed_methods)] | ||
let now = Utc::now(); | ||
let now = MockClock::default().now(); | ||
let mut rng = rand_chacha::ChaChaRng::seed_from_u64(42); | ||
|
||
let grant = AuthorizationGrant { | ||
login_hint: None, | ||
|
@@ -295,11 +307,8 @@ mod tests { | |
|
||
#[test] | ||
fn valid_login_hint() { | ||
#[allow(clippy::disallowed_methods)] | ||
let mut rng = thread_rng(); | ||
|
||
#[allow(clippy::disallowed_methods)] | ||
let now = Utc::now(); | ||
let now = MockClock::default().now(); | ||
let mut rng = rand_chacha::ChaChaRng::seed_from_u64(42); | ||
|
||
let grant = AuthorizationGrant { | ||
login_hint: Some(String::from("mxid:@example-user:example.com")), | ||
|
@@ -312,12 +321,24 @@ mod tests { | |
} | ||
|
||
#[test] | ||
fn invalid_login_hint() { | ||
#[allow(clippy::disallowed_methods)] | ||
let mut rng = thread_rng(); | ||
fn valid_login_hint_with_email() { | ||
let now = MockClock::default().now(); | ||
let mut rng = rand_chacha::ChaChaRng::seed_from_u64(42); | ||
|
||
let grant = AuthorizationGrant { | ||
login_hint: Some(String::from("example@user")), | ||
..AuthorizationGrant::sample(now, &mut rng) | ||
}; | ||
|
||
let hint = grant.parse_login_hint("example.com"); | ||
|
||
assert!(matches!(hint, LoginHint::Email(email) if email.to_string() == "example@user")); | ||
} | ||
|
||
#[allow(clippy::disallowed_methods)] | ||
let now = Utc::now(); | ||
#[test] | ||
fn invalid_login_hint() { | ||
let now = MockClock::default().now(); | ||
let mut rng = rand_chacha::ChaChaRng::seed_from_u64(42); | ||
|
||
let grant = AuthorizationGrant { | ||
login_hint: Some(String::from("example-user")), | ||
|
@@ -331,11 +352,8 @@ mod tests { | |
|
||
#[test] | ||
fn valid_login_hint_for_wrong_homeserver() { | ||
#[allow(clippy::disallowed_methods)] | ||
let mut rng = thread_rng(); | ||
|
||
#[allow(clippy::disallowed_methods)] | ||
let now = Utc::now(); | ||
let now = MockClock::default().now(); | ||
let mut rng = rand_chacha::ChaChaRng::seed_from_u64(42); | ||
|
||
let grant = AuthorizationGrant { | ||
login_hint: Some(String::from("mxid:@example-user:matrix.org")), | ||
|
@@ -349,11 +367,8 @@ mod tests { | |
|
||
#[test] | ||
fn unknown_login_hint_type() { | ||
#[allow(clippy::disallowed_methods)] | ||
let mut rng = thread_rng(); | ||
|
||
#[allow(clippy::disallowed_methods)] | ||
let now = Utc::now(); | ||
let now = MockClock::default().now(); | ||
let mut rng = rand_chacha::ChaChaRng::seed_from_u64(42); | ||
|
||
let grant = AuthorizationGrant { | ||
login_hint: Some(String::from("something:anything")), | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright 2025 New Vector Ltd. | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial | ||
// Please see LICENSE files in the repository root for full details. | ||
|
||
use rand_chacha::rand_core::CryptoRngCore; | ||
|
||
use crate::clock::Clock; | ||
|
||
/// A boxed [`Clock`] | ||
pub type BoxClock = Box<dyn Clock + Send>; | ||
/// A boxed random number generator | ||
pub type BoxRng = Box<dyn CryptoRngCore + Send>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.