feat: auto-select browser for external auth

This commit is contained in:
Kevin Yue committed 2026-06-21 20:59:13 +08:00
1 parent b32002710b
commit ecc9c9f09c
7 files changed
+144 -47

No files matched your search

+27
View File
@@ -682,6 +682,33 @@ jobs:
run: |
cd gh-release/gp/scripts && ./gh-release.sh "$RELEASE_TAG"
update-flake-hashes:
if: ${{ startsWith(github.ref, 'refs/tags/') }}
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
needs:
- gh-release
steps:
- name: Checkout GlobalProtect-openconnect
uses: actions/checkout@v6
with:
token: ${{ secrets.GH_PAT }}
repository: yuezk/GlobalProtect-openconnect
ref: ${{ github.ref }}
- name: Install Nix
uses: cachix/install-nix-action@v31
- name: Update flake hashes
run: scripts/update-flake-hashes.sh "${{ github.ref_name }}"
- name: Open pull request
env:
GH_TOKEN: ${{ secrets.GH_PAT }}
run: scripts/open-flake-hashes-pr.sh "${{ github.ref_name }}"
publish-docker-image:
needs:
- build-docker-image
+3 -34
View File
@@ -6,9 +6,6 @@ on:
version:
description: "Release version. Defaults to Cargo.toml workspace version."
required: false
release:
types:
- published
permissions:
contents: write
@@ -26,7 +23,7 @@ jobs:
- name: Update flake hashes
env:
RELEASE_VERSION: ${{ github.event.release.tag_name || inputs.version }}
RELEASE_VERSION: ${{ inputs.version }}
run: |
if [[ -n "$RELEASE_VERSION" ]]; then
scripts/update-flake-hashes.sh "$RELEASE_VERSION"
@@ -37,33 +34,5 @@ jobs:
- name: Open pull request
env:
GH_TOKEN: ${{ github.token }}
RELEASE_VERSION: ${{ github.event.release.tag_name || inputs.version }}
run: |
if git diff --quiet -- flake.nix; then
echo "flake.nix hashes are already up to date"
exit 0
fi
version="${RELEASE_VERSION:-$(grep '^version' Cargo.toml | head -1 | sed 's/version *= *"\(.*\)"/\1/')}"
version="${version#v}"
branch="chore/update-flake-hashes-v${version}"
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git switch -c "$branch"
git add flake.nix
git commit -m "chore: update flake hashes for v${version}"
git push --force-with-lease origin "$branch"
if gh pr view "$branch" --json number > /tmp/flake-pr.json 2> /dev/null; then
pr_number="$(jq -r '.number' /tmp/flake-pr.json)"
gh pr edit "$pr_number" \
--title "chore: update flake hashes for v${version}" \
--body "Updates the Nix flake fetchzip hashes for v${version} release assets."
else
gh pr create \
--title "chore: update flake hashes for v${version}" \
--body "Updates the Nix flake fetchzip hashes for v${version} release assets." \
--base main \
--head "$branch"
fi
RELEASE_VERSION: ${{ inputs.version }}
run: scripts/open-flake-hashes-pr.sh "$RELEASE_VERSION"
+2
View File
@@ -81,6 +81,8 @@ gpauth <portal> --browser 2>/dev/null | sudo gpclient connect <portal> --cookie-
```
**Browser Options:**
- Use `--browser` to auto-select Chrome, Firefox, then the system default browser
- Use `--browser default` to use the system default browser
- Use `--browser <browser>` to specify a browser (e.g., `firefox`, `chrome`)
- Use `--browser remote` for headless servers – this provides a URL you can access from another machine to complete authentication
+19 -2
View File
@@ -69,8 +69,8 @@ struct Cli {
#[arg(
long,
help = "The browser to use for authentication, e.g., `default`, `firefox`, `chrome`, `chromium`, or the path to the browser executable",
default_missing_value = "default",
help = "Use external browser authentication. With no value, auto-select Chrome, Firefox, then system default. Use `default` for the system default browser.",
default_missing_value = "auto",
num_args=0..=1
)]
browser: Option<String>,
@@ -250,6 +250,23 @@ mod tests {
assert!(cli.external_browser_requested());
}
#[test]
fn browser_without_value_uses_auto_mode() {
let cli = Cli::try_parse_from(["gpauth", "portal.example.com", "--browser"]).expect("gpauth args should parse");
assert_eq!(cli.browser.as_deref(), Some("auto"));
assert!(cli.external_browser_requested());
}
#[test]
fn browser_default_keeps_system_default_mode() {
let cli =
Cli::try_parse_from(["gpauth", "portal.example.com", "--browser", "default"]).expect("gpauth args should parse");
assert_eq!(cli.browser.as_deref(), Some("default"));
assert!(cli.external_browser_requested());
}
#[test]
fn client_version_arg_sets_profile_client_version() {
let cli = Cli::try_parse_from(["gpauth", "portal.example.com", "--client-version", "legacy-client"])
+22 -2
View File
@@ -141,8 +141,8 @@ pub(crate) struct ConnectArgs {
#[arg(
long,
help = "Use the specified browser to authenticate, e.g., `default`, `firefox`, `chrome`, `chromium`, `remote`.\nOr the path to the browser executable.\nUse 'remote' for headless servers.",
default_missing_value = "default",
help = "Use external browser authentication. With no value, auto-select Chrome, Firefox, then system default. Use `default` for the system default browser or `remote` for headless servers.",
default_missing_value = "auto",
num_args=0..=1
)]
pub(super) browser: Option<String>,
@@ -243,6 +243,26 @@ mod tests {
assert_eq!(cli.args.gateway.as_deref(), Some("gw1"));
}
#[test]
fn browser_without_value_uses_auto_mode() {
use clap::Parser;
let cli = ConnectArgsTestCli::try_parse_from(["test", "portal.example.com", "--browser"])
.expect("--browser without value should parse");
assert_eq!(cli.args.browser.as_deref(), Some("auto"));
}
#[test]
fn browser_default_keeps_system_default_mode() {
use clap::Parser;
let cli = ConnectArgsTestCli::try_parse_from(["test", "portal.example.com", "--browser", "default"])
.expect("--browser default should parse");
assert_eq!(cli.args.browser.as_deref(), Some("default"));
}
#[test]
fn os_defaults_to_runtime_os() {
use clap::Parser;
+38 -9
View File
@@ -11,6 +11,7 @@ use tokio::{
use crate::browser::auth_server::AuthServer;
pub enum Browser<'a> {
Auto,
Default,
Chrome,
Firefox,
@@ -21,6 +22,7 @@ pub enum Browser<'a> {
impl<'a> Browser<'a> {
pub fn from_str(browser: &'a str) -> Self {
match browser.to_lowercase().as_str() {
"auto" => Browser::Auto,
"default" => Browser::Default,
"chrome" => Browser::Chrome,
"firefox" => Browser::Firefox,
@@ -31,6 +33,7 @@ impl<'a> Browser<'a> {
fn as_str(&self) -> &str {
match self {
Browser::Auto => "auto",
Browser::Default => "default",
Browser::Chrome => "chrome",
Browser::Firefox => "firefox",
@@ -87,6 +90,15 @@ Note that the URL is only valid for a single use.
info!("Launching the default browser...");
webbrowser::open(&auth_url)?;
}
Browser::Auto => {
if let Some(app) = find_auto_browser_path() {
info!("Launching browser: {}", app);
open::with_detached(&auth_url, app)?;
} else {
info!("No preferred browser found; launching the default browser...");
webbrowser::open(&auth_url)?;
}
}
_ => {
let app = find_browser_path(&self.browser);
@@ -124,19 +136,25 @@ async fn detect_local_ip() -> anyhow::Result<String> {
fn find_browser_path(browser: &Browser) -> String {
match browser {
Browser::Chrome => {
const CHROME_VARIANTS: &[&str] = &["google-chrome-stable", "google-chrome", "chromium"];
CHROME_VARIANTS
.iter()
.find_map(|&browser_name| which::which(browser_name).ok())
.map(|path| path.to_string_lossy().to_string())
.unwrap_or_else(|| browser.as_str().to_string())
}
Browser::Chrome => find_chrome_path().unwrap_or_else(|| browser.as_str().to_string()),
_ => browser.as_str().to_string(),
}
}
fn find_auto_browser_path() -> Option<String> {
find_chrome_path().or_else(|| find_program_path("firefox"))
}
fn find_chrome_path() -> Option<String> {
["google-chrome-stable", "google-chrome", "chromium"]
.iter()
.find_map(|browser_name| find_program_path(browser_name))
}
fn find_program_path(name: &str) -> Option<String> {
which::which(name).ok().map(|path| path.to_string_lossy().to_string())
}
async fn wait_auth_data() -> anyhow::Result<SamlAuthData> {
// Start a local server to receive the browser authentication data
let listener = TcpListener::bind("127.0.0.1:0").await?;
@@ -176,3 +194,14 @@ fn read_auth_data_from_stdin() -> anyhow::Result<SamlAuthData> {
let auth_data = SamlAuthData::from_gpcallback(data.trim())?;
Ok(auth_data)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn browser_auto_is_distinct_from_system_default() {
assert!(matches!(Browser::from_str("auto"), Browser::Auto));
assert!(matches!(Browser::from_str("default"), Browser::Default));
}
}
+33
View File
@@ -0,0 +1,33 @@
#!/usr/bin/env bash
set -euo pipefail
release_version="${1:-}"
if git diff --quiet -- flake.nix; then
echo "flake.nix hashes are already up to date"
exit 0
fi
if [[ -z "$release_version" ]]; then
release_version="$(grep '^version' Cargo.toml | head -1 | sed 's/version *= *"\(.*\)"/\1/')"
fi
version="${release_version#v}"
branch="chore/update-flake-hashes-v${version}"
title="chore: update flake hashes for v${version}"
body="Updates the Nix flake fetchzip hashes for v${version} release assets."
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git switch -C "$branch"
git add flake.nix
git commit -m "$title"
git push --force-with-lease origin "$branch"
if gh pr view "$branch" --json number > /tmp/flake-pr.json 2> /dev/null; then
pr_number="$(jq -r '.number' /tmp/flake-pr.json)"
gh pr edit "$pr_number" --title "$title" --body "$body"
else
gh pr create --title "$title" --body "$body" --base main --head "$branch"
fi