feat(web): Handle MSC4198 login hints

This commit is contained in:
Ginger committed 2026-12-03 13:48:39 -05:00
1 parent c647d9d197
commit 5339cead10
17 files changed
+472 -83

No files matched your search

+32 -1
View File
@@ -3,10 +3,11 @@ use std::{
collections::BTreeSet,
error::Error,
fmt::{Debug, Display},
str::FromStr,
};
use regex::regex;
use ruma::{OwnedDeviceId, api::OAuthClientScope};
use ruma::{OwnedDeviceId, OwnedUserId, api::OAuthClientScope};
use serde::{Deserialize, Serialize};
use url::Url;
@@ -26,6 +27,8 @@ pub struct AuthorizationCodeQuery {
pub code_challenge_method: CodeChallengeMethod,
#[serde(default)]
pub prompt: Option<Prompt>,
#[serde(default)]
pub login_hint: Option<String>,
}
#[derive(Deserialize, Serialize)]
@@ -38,6 +41,8 @@ pub struct AuthorizationCodeResponse {
pub struct DeviceCodeRequest {
pub client_id: String,
pub scope: RawScopes,
#[serde(default, rename = "org.matrix.msc4198.login_hint")]
pub login_hint: Option<String>,
}
#[derive(Deserialize, Serialize)]
@@ -53,6 +58,8 @@ pub struct DeviceCodeResponse {
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct DeviceCodeVerifyQuery {
pub user_code: Option<String>,
#[serde(default, skip_serializing_if = "core::ops::Not::not")]
pub ignore_login_hint: bool,
}
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
@@ -174,6 +181,30 @@ impl RawScopes {
}
}
pub enum LoginHint {
Mxid(OwnedUserId),
}
impl FromStr for LoginHint {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.split_once(':') {
| Some(("mxid", user_id)) if let Ok(user_id) = user_id.parse::<OwnedUserId>() =>
Ok(Self::Mxid(user_id)),
| _ => Err(()),
}
}
}
impl Display for LoginHint {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
| Self::Mxid(user_id) => write!(f, "mxid:{user_id}"),
}
}
}
#[derive(Serialize, Debug, Clone)]
pub struct OAuthError {
pub error: ErrorCode,
+11 -2
View File
@@ -135,6 +135,7 @@ struct PendingDeviceCodeGrant {
requested_scopes: RequestedScopes,
client_name: Option<String>,
client_id: String,
login_hint: Option<String>,
requested_at: SystemTime,
}
@@ -165,6 +166,7 @@ pub struct DeviceCodeGrantInfo {
pub device_code: String,
pub client_metadata: ClientMetadata,
pub requested_scopes: RequestedScopes,
pub login_hint: Option<String>,
}
/// A time-limited grant for a client to perform some sensitive action.
@@ -422,6 +424,7 @@ impl Service {
requested_scopes,
client_name: client_metadata.client_name,
client_id: query.client_id,
login_hint: query.login_hint,
requested_at: SystemTime::now(),
};
@@ -442,7 +445,7 @@ impl Service {
&self,
supplied_user_code: &str,
) -> Option<DeviceCodeGrantInfo> {
let (device_code, client_id, requested_scopes) = {
let (device_code, client_id, requested_scopes, login_hint) = {
let pending_grants = self.pending_device_code_grants.lock();
let (device_code, grant) = pending_grants
@@ -451,7 +454,12 @@ impl Service {
matches!(&grant.state, DeviceCodeGrantState::Unverified { user_code } if user_code == supplied_user_code)
})?;
(device_code.clone(), grant.client_id.clone(), grant.requested_scopes.clone())
(
device_code.clone(),
grant.client_id.clone(),
grant.requested_scopes.clone(),
grant.login_hint.clone(),
)
};
let client_metadata = self
@@ -463,6 +471,7 @@ impl Service {
device_code,
client_metadata,
requested_scopes,
login_hint,
})
}
+35 -1
View File
@@ -15,7 +15,10 @@ use futures::{FutureExt, Stream, StreamExt};
use lettre::Address;
use ruma::{
MilliSecondsSinceUnixEpoch, OwnedDeviceId, OwnedUserId, UserId,
api::client::profile::PropagateTo,
api::client::{
profile::PropagateTo,
uiaa::{EmailUserIdentifier, MatrixUserIdentifier, UserIdentifier},
},
events::{
GlobalAccountDataEventType, ignored_user_list::IgnoredUserListEvent,
invite_permission_config::InvitePermissionAction, push_rules::PushRulesEvent,
@@ -721,6 +724,37 @@ impl super::Service {
Ok(user_id)
}
/// Look up a user identifier. If a user ID is returned, it will be local to
/// this server, but may refer to a nonexistent account.
pub async fn lookup_by_identifier(&self, identifier: UserIdentifier) -> Option<OwnedUserId> {
match identifier {
| UserIdentifier::Email(EmailUserIdentifier { address, .. }) => {
let address = address.parse().ok()?;
let localpart = self
.services
.threepid
.get_localpart_for_email(&address)
.await?;
Some(
UserId::parse(format!(
"@{localpart}:{}",
self.services.globals.server_name()
))
.unwrap(),
)
},
| UserIdentifier::Matrix(MatrixUserIdentifier { user, .. }) => UserId::parse(&user)
.ok()
.filter(|user_id| self.services.globals.user_is_local(user_id))
.or_else(|| {
UserId::parse(format!("@{user}:{}", self.services.globals.server_name())).ok()
}),
| _ => None,
}
}
/// Creates an OpenID token, which can be used to prove that a user has
/// access to an account (primarily for integrations)
pub fn create_openid_token(&self, user_id: &UserId, token: &str) -> Result<u64> {