Merge pull request #27: fix: bake Google OAuth client, clean MS login fonts, faster login grace
@@ -6,6 +6,12 @@
6
6
//! `add_fonts`. fontdb only honors `<dir>` entries from `FONTCONFIG_FILE`,
7
7
//! so this module rewrites those directories and leaves the system color
8
8
//! emoji file out. An existing `FONTCONFIG_FILE` is left alone.
9
//!
10
//! This `FONTCONFIG_FILE` is for the app process only. Chrome must not
11
//! inherit the app-generated `fontconfig-emoji` conf (`browser::launch`
12
//! strips that path on spawn) — the dirs-only file breaks Segoe/Arial
13
//! fallback on the Microsoft login page. A user-provided
14
//! `FONTCONFIG_FILE` is left alone for both the app and Chrome.
9
15
10
16
use std::collections::HashSet;
11
17
use std::fs;
@@ -6,6 +6,12 @@6
//! `add_fonts`. fontdb only honors `<dir>` entries from `FONTCONFIG_FILE`,6
//! `add_fonts`. fontdb only honors `<dir>` entries from `FONTCONFIG_FILE`,7
//! so this module rewrites those directories and leaves the system color7
//! so this module rewrites those directories and leaves the system color8
//! emoji file out. An existing `FONTCONFIG_FILE` is left alone.8
//! emoji file out. An existing `FONTCONFIG_FILE` is left alone.9
//!10
//! This `FONTCONFIG_FILE` is for the app process only. Chrome must not11
//! inherit the app-generated `fontconfig-emoji` conf (`browser::launch`12
//! strips that path on spawn) — the dirs-only file breaks Segoe/Arial13
//! fallback on the Microsoft login page. A user-provided14
//! `FONTCONFIG_FILE` is left alone for both the app and Chrome.9
15
10
use std::collections::HashSet;16
use std::collections::HashSet;11
use std::fs;17
use std::fs;@@ -88,13 +88,41 @@ pub(crate) fn major_from_version_output(output: &str) -> Option<u32> {
88
88
89
89
const FALLBACK_MAJOR: u32 = 150;
90
90
91
/// Marker directory used by `app_base::linux_fonts::hide_system_color_emoji`.
92
/// Only that app-generated conf must be cleared for Chrome; a user-provided
93
/// `FONTCONFIG_FILE` / `FONTCONFIG_PATH` stays inherited.
94
const APP_EMOJI_FONTCONFIG_MARKER: &str = "fontconfig-emoji";
95
96
fn is_app_emoji_fontconfig(value: &std::ffi::OsStr) -> bool {
97
std::path::Path::new(value)
98
.components()
99
.any(|component| component.as_os_str() == APP_EMOJI_FONTCONFIG_MARKER)
100
}
101
102
fn strip_app_fontconfig(command: &mut Command) {
103
for key in ["FONTCONFIG_FILE", "FONTCONFIG_PATH"] {
104
let Some(value) = std::env::var_os(key) else {
105
continue;
106
};
107
if is_app_emoji_fontconfig(&value) {
108
command.env_remove(key);
109
}
110
}
111
}
112
91
113
fn chrome_major(platform: Platform, executable: &Path) -> u32 {
92
114
let detected = match platform {
93
Platform::Linux | Platform::Macos => Command::new(executable)
94
.arg("--version")
95
.output()
96
.ok()
97
.and_then(|output| major_from_version_output(&String::from_utf8_lossy(&output.stdout))),
115
Platform::Linux | Platform::Macos => {
116
let mut command = Command::new(executable);
117
strip_app_fontconfig(&mut command);
118
command
119
.arg("--version")
120
.output()
121
.ok()
122
.and_then(|output| {
123
major_from_version_output(&String::from_utf8_lossy(&output.stdout))
124
})
125
}
98
126
Platform::Windows | Platform::Wsl => executable
99
127
.parent()
100
128
.and_then(|parent| std::fs::read_dir(parent).ok())
@@ -134,6 +162,7 @@ impl Launcher for SystemLauncher {
134
162
start_urls: &request.start_urls,
135
163
});
136
164
let mut command = Command::new(&executable);
165
strip_app_fontconfig(&mut command);
137
166
command
138
167
.args(arguments)
139
168
.stdin(Stdio::null())
@@ -255,4 +284,39 @@ mod tests {
255
284
);
256
285
assert_eq!(major_from_version_output("nothing"), None);
257
286
}
287
288
#[test]
289
fn chrome_drops_only_app_emoji_fontconfig() {
290
assert!(is_app_emoji_fontconfig(std::ffi::OsStr::new(
291
"/tmp/cache/fontconfig-emoji/fonts.conf"
292
)));
293
assert!(!is_app_emoji_fontconfig(std::ffi::OsStr::new(
294
"/etc/fonts/fonts.conf"
295
)));
296
297
let previous_file = std::env::var_os("FONTCONFIG_FILE");
298
let previous_path = std::env::var_os("FONTCONFIG_PATH");
299
// SAFETY: test-only env mutation around a synchronous strip call.
300
unsafe {
301
std::env::set_var(
302
"FONTCONFIG_FILE",
303
"/tmp/cache/fontconfig-emoji/fonts.conf",
304
);
305
std::env::set_var("FONTCONFIG_PATH", "/etc/fonts");
306
}
307
let mut command = Command::new("true");
308
strip_app_fontconfig(&mut command);
309
// App marker path is removed; user FONTCONFIG_PATH is left alone.
310
let _ = command;
311
unsafe {
312
match previous_file {
313
Some(value) => std::env::set_var("FONTCONFIG_FILE", value),
314
None => std::env::remove_var("FONTCONFIG_FILE"),
315
}
316
match previous_path {
317
Some(value) => std::env::set_var("FONTCONFIG_PATH", value),
318
None => std::env::remove_var("FONTCONFIG_PATH"),
319
}
320
}
321
}
258
322
}
@@ -88,13 +88,41 @@ pub(crate) fn major_from_version_output(output: &str) -> Option<u32> {88
88
89
const FALLBACK_MAJOR: u32 = 150;89
const FALLBACK_MAJOR: u32 = 150;90
90
91
/// Marker directory used by `app_base::linux_fonts::hide_system_color_emoji`.92
/// Only that app-generated conf must be cleared for Chrome; a user-provided93
/// `FONTCONFIG_FILE` / `FONTCONFIG_PATH` stays inherited.94
const APP_EMOJI_FONTCONFIG_MARKER: &str = "fontconfig-emoji";95
96
fn is_app_emoji_fontconfig(value: &std::ffi::OsStr) -> bool {97
std::path::Path::new(value)98
.components()99
.any(|component| component.as_os_str() == APP_EMOJI_FONTCONFIG_MARKER)100
}101
102
fn strip_app_fontconfig(command: &mut Command) {103
for key in ["FONTCONFIG_FILE", "FONTCONFIG_PATH"] {104
let Some(value) = std::env::var_os(key) else {105
continue;106
};107
if is_app_emoji_fontconfig(&value) {108
command.env_remove(key);109
}110
}111
}112
91
fn chrome_major(platform: Platform, executable: &Path) -> u32 {113
fn chrome_major(platform: Platform, executable: &Path) -> u32 {92
let detected = match platform {114
let detected = match platform {93
Platform::Linux | Platform::Macos => Command::new(executable)115
Platform::Linux | Platform::Macos => {94
.arg("--version")116
let mut command = Command::new(executable);95
.output()117
strip_app_fontconfig(&mut command);96
.ok()118
command97
.and_then(|output| major_from_version_output(&String::from_utf8_lossy(&output.stdout))),119
.arg("--version")120
.output()121
.ok()122
.and_then(|output| {123
major_from_version_output(&String::from_utf8_lossy(&output.stdout))124
})125
}98
Platform::Windows | Platform::Wsl => executable126
Platform::Windows | Platform::Wsl => executable99
.parent()127
.parent()100
.and_then(|parent| std::fs::read_dir(parent).ok())128
.and_then(|parent| std::fs::read_dir(parent).ok())@@ -134,6 +162,7 @@ impl Launcher for SystemLauncher {134
start_urls: &request.start_urls,162
start_urls: &request.start_urls,135
});163
});136
let mut command = Command::new(&executable);164
let mut command = Command::new(&executable);165
strip_app_fontconfig(&mut command);137
command166
command138
.args(arguments)167
.args(arguments)139
.stdin(Stdio::null())168
.stdin(Stdio::null())@@ -255,4 +284,39 @@ mod tests {255
);284
);256
assert_eq!(major_from_version_output("nothing"), None);285
assert_eq!(major_from_version_output("nothing"), None);257
}286
}287
288
#[test]289
fn chrome_drops_only_app_emoji_fontconfig() {290
assert!(is_app_emoji_fontconfig(std::ffi::OsStr::new(291
"/tmp/cache/fontconfig-emoji/fonts.conf"292
)));293
assert!(!is_app_emoji_fontconfig(std::ffi::OsStr::new(294
"/etc/fonts/fonts.conf"295
)));296
297
let previous_file = std::env::var_os("FONTCONFIG_FILE");298
let previous_path = std::env::var_os("FONTCONFIG_PATH");299
// SAFETY: test-only env mutation around a synchronous strip call.300
unsafe {301
std::env::set_var(302
"FONTCONFIG_FILE",303
"/tmp/cache/fontconfig-emoji/fonts.conf",304
);305
std::env::set_var("FONTCONFIG_PATH", "/etc/fonts");306
}307
let mut command = Command::new("true");308
strip_app_fontconfig(&mut command);309
// App marker path is removed; user FONTCONFIG_PATH is left alone.310
let _ = command;311
unsafe {312
match previous_file {313
Some(value) => std::env::set_var("FONTCONFIG_FILE", value),314
None => std::env::remove_var("FONTCONFIG_FILE"),315
}316
match previous_path {317
Some(value) => std::env::set_var("FONTCONFIG_PATH", value),318
None => std::env::remove_var("FONTCONFIG_PATH"),319
}320
}321
}258
}322
}@@ -35,7 +35,7 @@ impl Default for WatchdogConfig {
35
35
died_wait: Duration::from_millis(1000),
36
36
probe_interval: Duration::from_secs(30),
37
37
probe_failures: 2,
38
login_grace: Duration::from_secs(4),
38
login_grace: Duration::from_secs(1),
39
39
}
40
40
}
41
41
}
@@ -35,7 +35,7 @@ impl Default for WatchdogConfig {35
died_wait: Duration::from_millis(1000),35
died_wait: Duration::from_millis(1000),36
probe_interval: Duration::from_secs(30),36
probe_interval: Duration::from_secs(30),37
probe_failures: 2,37
probe_failures: 2,38
login_grace: Duration::from_secs(4),38
login_grace: Duration::from_secs(1),39
}39
}40
}40
}41
}41
}@@ -49,13 +49,14 @@ pub async fn wait_for_code(pending: &AuthPending, listener: TcpListener) -> Resu
49
49
_ => {}
50
50
}
51
51
}
52
let body = if state.as_deref() == Some(pending.state.as_str()) && code.is_some() {
53
"You can close this window and return to Rusty Teams."
52
let ok = state.as_deref() == Some(pending.state.as_str()) && code.is_some();
53
let body = if ok {
54
LOOPBACK_OK_HTML
54
55
} else {
55
"Sign-in failed. You can close this window."
56
LOOPBACK_FAIL_HTML
56
57
};
57
58
let response = format!(
58
"HTTP/1.1 200 OK\r\nContent-Type: text/plain; charset=utf-8\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
59
"HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
59
60
body.len()
60
61
);
61
62
let _ = stream.write_all(response.as_bytes()).await;
@@ -74,6 +75,32 @@ fn urlencoding_decode(value: &str) -> String {
74
75
.into_owned()
75
76
}
76
77
78
const LOOPBACK_OK_HTML: &str = r#"<!DOCTYPE html>
79
<html lang="en">
80
<head><meta charset="utf-8"><title>Rusty Teams</title>
81
<style>body{font-family:system-ui,sans-serif;max-width:32rem;margin:3rem auto;padding:0 1rem;line-height:1.5;color:#222}
82
h1{font-size:1.25rem;margin:0 0 .75rem}p{margin:.5rem 0}</style>
83
</head>
84
<body>
85
<h1>Signed in</h1>
86
<p>You can close this window and return to Rusty Teams.</p>
87
<p lang="de">Angemeldet. Sie können dieses Fenster schliessen und zu Rusty Teams zurückkehren.</p>
88
</body>
89
</html>"#;
90
91
const LOOPBACK_FAIL_HTML: &str = r#"<!DOCTYPE html>
92
<html lang="en">
93
<head><meta charset="utf-8"><title>Rusty Teams</title>
94
<style>body{font-family:system-ui,sans-serif;max-width:32rem;margin:3rem auto;padding:0 1rem;line-height:1.5;color:#222}
95
h1{font-size:1.25rem;margin:0 0 .75rem}p{margin:.5rem 0}</style>
96
</head>
97
<body>
98
<h1>Sign-in did not finish</h1>
99
<p>You can close this window and try again in Rusty Teams.</p>
100
<p lang="de">Die Anmeldung ist nicht abgeschlossen. Sie können dieses Fenster schliessen und es in Rusty Teams erneut versuchen.</p>
101
</body>
102
</html>"#;
103
77
104
#[cfg(test)]
78
105
mod tests {
79
106
use super::*;
@@ -49,13 +49,14 @@ pub async fn wait_for_code(pending: &AuthPending, listener: TcpListener) -> Resu49
_ => {}49
_ => {}50
}50
}51
}51
}52
let body = if state.as_deref() == Some(pending.state.as_str()) && code.is_some() {52
let ok = state.as_deref() == Some(pending.state.as_str()) && code.is_some();53
"You can close this window and return to Rusty Teams."53
let body = if ok {54
LOOPBACK_OK_HTML54
} else {55
} else {55
"Sign-in failed. You can close this window."56
LOOPBACK_FAIL_HTML56
};57
};57
let response = format!(58
let response = format!(58
"HTTP/1.1 200 OK\r\nContent-Type: text/plain; charset=utf-8\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",59
"HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",59
body.len()60
body.len()60
);61
);61
let _ = stream.write_all(response.as_bytes()).await;62
let _ = stream.write_all(response.as_bytes()).await;@@ -74,6 +75,32 @@ fn urlencoding_decode(value: &str) -> String {74
.into_owned()75
.into_owned()75
}76
}76
77
78
const LOOPBACK_OK_HTML: &str = r#"<!DOCTYPE html>79
<html lang="en">80
<head><meta charset="utf-8"><title>Rusty Teams</title>81
<style>body{font-family:system-ui,sans-serif;max-width:32rem;margin:3rem auto;padding:0 1rem;line-height:1.5;color:#222}82
h1{font-size:1.25rem;margin:0 0 .75rem}p{margin:.5rem 0}</style>83
</head>84
<body>85
<h1>Signed in</h1>86
<p>You can close this window and return to Rusty Teams.</p>87
<p lang="de">Angemeldet. Sie können dieses Fenster schliessen und zu Rusty Teams zurückkehren.</p>88
</body>89
</html>"#;90
91
const LOOPBACK_FAIL_HTML: &str = r#"<!DOCTYPE html>92
<html lang="en">93
<head><meta charset="utf-8"><title>Rusty Teams</title>94
<style>body{font-family:system-ui,sans-serif;max-width:32rem;margin:3rem auto;padding:0 1rem;line-height:1.5;color:#222}95
h1{font-size:1.25rem;margin:0 0 .75rem}p{margin:.5rem 0}</style>96
</head>97
<body>98
<h1>Sign-in did not finish</h1>99
<p>You can close this window and try again in Rusty Teams.</p>100
<p lang="de">Die Anmeldung ist nicht abgeschlossen. Sie können dieses Fenster schliessen und es in Rusty Teams erneut versuchen.</p>101
</body>102
</html>"#;103
77
#[cfg(test)]104
#[cfg(test)]78
mod tests {105
mod tests {79
use super::*;106
use super::*;@@ -38,18 +38,40 @@ pub struct TokenBundle {
38
38
pub token_type: String,
39
39
}
40
40
41
/// Runtime env wins; otherwise the value baked in at compile time (`option_env!`).
42
/// Empty strings count as missing. Used by Connect and token refresh.
41
43
pub fn client_id_from_env() -> Option<GoogleOAuth> {
42
let client_id = std::env::var(GOOGLE_CLIENT_ID_ENV).ok()?;
43
if client_id.trim().is_empty() {
44
return None;
45
}
46
let client_secret = std::env::var(GOOGLE_CLIENT_SECRET_ENV).unwrap_or_default();
44
resolve_oauth(
45
std::env::var(GOOGLE_CLIENT_ID_ENV).ok().as_deref(),
46
std::env::var(GOOGLE_CLIENT_SECRET_ENV).ok().as_deref(),
47
option_env!("TEAMS_GOOGLE_CLIENT_ID"),
48
option_env!("TEAMS_GOOGLE_CLIENT_SECRET"),
49
)
50
}
51
52
fn resolve_oauth(
53
runtime_id: Option<&str>,
54
runtime_secret: Option<&str>,
55
compile_id: Option<&str>,
56
compile_secret: Option<&str>,
57
) -> Option<GoogleOAuth> {
58
let client_id = first_non_empty(runtime_id).or_else(|| first_non_empty(compile_id))?;
59
let client_secret = first_non_empty(runtime_secret)
60
.or_else(|| first_non_empty(compile_secret))
61
.unwrap_or_default();
47
62
Some(GoogleOAuth {
48
63
client_id,
49
64
client_secret,
50
65
})
51
66
}
52
67
68
fn first_non_empty(value: Option<&str>) -> Option<String> {
69
value
70
.map(str::trim)
71
.filter(|text| !text.is_empty())
72
.map(str::to_owned)
73
}
74
53
75
impl GoogleOAuth {
54
76
pub fn begin(&self, redirect_uri: &str) -> AuthPending {
55
77
let state = random_url_token(16);
@@ -182,4 +204,38 @@ mod tests {
182
204
assert_eq!(bundle.refresh_token, "r");
183
205
assert_eq!(bundle.expires_in, 3600);
184
206
}
207
208
#[test]
209
fn resolve_prefers_runtime_over_compile() {
210
let oauth = resolve_oauth(
211
Some("runtime-id"),
212
Some("runtime-secret"),
213
Some("compile-id"),
214
Some("compile-secret"),
215
)
216
.unwrap();
217
assert_eq!(oauth.client_id, "runtime-id");
218
assert_eq!(oauth.client_secret, "runtime-secret");
219
}
220
221
#[test]
222
fn resolve_falls_back_to_compile_when_runtime_empty() {
223
let oauth = resolve_oauth(Some(" "), None, Some("compile-id"), Some("compile-secret"))
224
.unwrap();
225
assert_eq!(oauth.client_id, "compile-id");
226
assert_eq!(oauth.client_secret, "compile-secret");
227
}
228
229
#[test]
230
fn resolve_returns_none_when_both_missing() {
231
assert!(resolve_oauth(None, None, None, None).is_none());
232
assert!(resolve_oauth(Some(""), None, Some(" "), None).is_none());
233
}
234
235
#[test]
236
fn resolve_allows_missing_secret() {
237
let oauth = resolve_oauth(Some("cid"), None, None, None).unwrap();
238
assert_eq!(oauth.client_id, "cid");
239
assert!(oauth.client_secret.is_empty());
240
}
185
241
}
@@ -38,18 +38,40 @@ pub struct TokenBundle {38
pub token_type: String,38
pub token_type: String,39
}39
}40
40
41
/// Runtime env wins; otherwise the value baked in at compile time (`option_env!`).42
/// Empty strings count as missing. Used by Connect and token refresh.41
pub fn client_id_from_env() -> Option<GoogleOAuth> {43
pub fn client_id_from_env() -> Option<GoogleOAuth> {42
let client_id = std::env::var(GOOGLE_CLIENT_ID_ENV).ok()?;44
resolve_oauth(43
if client_id.trim().is_empty() {45
std::env::var(GOOGLE_CLIENT_ID_ENV).ok().as_deref(),44
return None;46
std::env::var(GOOGLE_CLIENT_SECRET_ENV).ok().as_deref(),45
}47
option_env!("TEAMS_GOOGLE_CLIENT_ID"),46
let client_secret = std::env::var(GOOGLE_CLIENT_SECRET_ENV).unwrap_or_default();48
option_env!("TEAMS_GOOGLE_CLIENT_SECRET"),49
)50
}51
52
fn resolve_oauth(53
runtime_id: Option<&str>,54
runtime_secret: Option<&str>,55
compile_id: Option<&str>,56
compile_secret: Option<&str>,57
) -> Option<GoogleOAuth> {58
let client_id = first_non_empty(runtime_id).or_else(|| first_non_empty(compile_id))?;59
let client_secret = first_non_empty(runtime_secret)60
.or_else(|| first_non_empty(compile_secret))61
.unwrap_or_default();47
Some(GoogleOAuth {62
Some(GoogleOAuth {48
client_id,63
client_id,49
client_secret,64
client_secret,50
})65
})51
}66
}52
67
68
fn first_non_empty(value: Option<&str>) -> Option<String> {69
value70
.map(str::trim)71
.filter(|text| !text.is_empty())72
.map(str::to_owned)73
}74
53
impl GoogleOAuth {75
impl GoogleOAuth {54
pub fn begin(&self, redirect_uri: &str) -> AuthPending {76
pub fn begin(&self, redirect_uri: &str) -> AuthPending {55
let state = random_url_token(16);77
let state = random_url_token(16);@@ -182,4 +204,38 @@ mod tests {182
assert_eq!(bundle.refresh_token, "r");204
assert_eq!(bundle.refresh_token, "r");183
assert_eq!(bundle.expires_in, 3600);205
assert_eq!(bundle.expires_in, 3600);184
}206
}207
208
#[test]209
fn resolve_prefers_runtime_over_compile() {210
let oauth = resolve_oauth(211
Some("runtime-id"),212
Some("runtime-secret"),213
Some("compile-id"),214
Some("compile-secret"),215
)216
.unwrap();217
assert_eq!(oauth.client_id, "runtime-id");218
assert_eq!(oauth.client_secret, "runtime-secret");219
}220
221
#[test]222
fn resolve_falls_back_to_compile_when_runtime_empty() {223
let oauth = resolve_oauth(Some(" "), None, Some("compile-id"), Some("compile-secret"))224
.unwrap();225
assert_eq!(oauth.client_id, "compile-id");226
assert_eq!(oauth.client_secret, "compile-secret");227
}228
229
#[test]230
fn resolve_returns_none_when_both_missing() {231
assert!(resolve_oauth(None, None, None, None).is_none());232
assert!(resolve_oauth(Some(""), None, Some(" "), None).is_none());233
}234
235
#[test]236
fn resolve_allows_missing_secret() {237
let oauth = resolve_oauth(Some("cid"), None, None, None).unwrap();238
assert_eq!(oauth.client_id, "cid");239
assert!(oauth.client_secret.is_empty());240
}185
}241
}