1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-08-07 17:03:01 +03:00

Add Electron user-agent parsing for Element Desktop/Nightly (#2461)

This commit is contained in:
Michael Telatynski
2024-03-18 17:42:31 +00:00
committed by GitHub
parent 894602bca1
commit 70d688913f

View File

@@ -80,6 +80,15 @@ impl UserAgent {
}
}
fn parse_electron(user_agent: &str) -> Option<(&str, &str)> {
let regex = regex::Regex::new(r"(?m)\w+/[\w.]+").unwrap();
let omit_keys = ["Mozilla", "AppleWebKit", "Chrome", "Electron", "Safari"];
return regex
.find_iter(user_agent)
.map(|caps| caps.as_str().split_once('/').unwrap())
.find(|pair| !omit_keys.contains(&pair.0));
}
#[must_use]
pub fn parse(user_agent: String) -> Self {
if !user_agent.contains("Mozilla/") {
@@ -203,6 +212,14 @@ impl UserAgent {
result.os_version = version.into();
}
// Special handling for Electron applications e.g. Element Desktop
if user_agent.contains("Electron/") {
if let Some(app) = UserAgent::parse_electron(&user_agent) {
result.name = app.0;
result.version = app.1;
}
}
Self {
name: (result.name != VALUE_UNKNOWN).then(|| result.name.to_owned()),
version: (result.version != VALUE_UNKNOWN).then(|| result.version.to_owned()),