Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
3995c25
feat(auth): support a redirect-port range for U2M browser flow (PECOB…
eric-wang-1990 Aug 18, 2026
5fd4b41
refactor(auth): model U2M redirect port as a single Option<Vec<u16>>
eric-wang-1990 Aug 18, 2026
0caf453
test(auth): update U2M integration tests for redirect_ports list
eric-wang-1990 Aug 18, 2026
7b3e416
ai: apply changes for #257 (1 review thread)
Aug 18, 2026
f5f88b5
ai: apply changes for #257 (1 review thread)
Aug 18, 2026
9b85bc4
ai: apply changes for #257 (1 review thread)
Aug 18, 2026
652ccdd
refactor(auth): drop redundant single-port knobs (review feedback)
eric-wang-1990 Aug 18, 2026
4edb6c3
test(auth): add live U2M busy-port fallback E2E test (PECOBLR-4144)
eric-wang-1990 Aug 18, 2026
ba96295
ai: apply changes for #257 (1 review thread)
Aug 18, 2026
5852dd0
Merge branch 'main' into eric-wang/pecoblr-4144-u2m-port-range
eric-wang-1990 Aug 18, 2026
aaf60ec
ai: apply changes for #257 (1 review thread)
Aug 18, 2026
a31df85
refactor(auth): new_first_free rename + drop pyo3 redirect_port kwarg
eric-wang-1990 Aug 18, 2026
23c8566
ai: apply changes for #257 (1 review thread)
Aug 18, 2026
9c3f717
ai: apply changes for #257 (1 review thread)
Aug 18, 2026
cdea913
ai: apply changes for #257 (1 review thread)
Aug 18, 2026
7c2ddaf
feat(auth): default kernel U2M to a candidate port window (8030-8034)
eric-wang-1990 Aug 18, 2026
db57abf
ai: apply changes for #257 (1 review thread)
Aug 18, 2026
bb6ca48
ai: apply changes for #257 (1 review thread)
Aug 18, 2026
b5a3255
ai: apply changes for #257 (1 review thread)
Aug 18, 2026
6b0bc8a
docs(auth): finish redirect_port -> redirect_ports doc sweep
eric-wang-1990 Aug 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONNECTION_PARAMETERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ An enum; pick one variant. Use the validating constructors
| Field | Description | Default |
|-------|-------------|---------|
| `client_id` | OAuth client id. | `"databricks-sql-connector"` |
| `redirect_port` | Localhost port for the OAuth callback listener. The default app's redirect URI is registered for `http://localhost:8030`. | `8030` |
| `redirect_ports` | Ordered candidate localhost ports for the OAuth callback listener; the first free port is bound (busy-port fallback). `None`/empty selects the default window. The default app (`databricks-sql-connector`) accepts any `http://localhost:{port}` because its in-house OAuth normalizes loopback redirect URIs by dropping the port (RFC 8252 §7.3); for other apps every port must be a registered redirect URI. | `[8030, 8031, 8032, 8033, 8034]` |
| `scopes` | OAuth scopes. `offline_access` is required to receive a refresh token (cached and refreshed transparently). | `["sql", "offline_access"]` |
| `token_url_override` | Token endpoint override. | OIDC discovery |
| `callback_timeout` | How long to wait for the user to complete the browser flow. | `120s` |
Expand Down
21 changes: 15 additions & 6 deletions napi/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,15 @@ fn build_auth_config(options: &ConnectionOptions) -> napi::Result<AuthConfig> {
}
Ok(AuthConfig::oauth_u2m(
options.oauth_client_id.clone(),
options.oauth_redirect_port,
// Node exposes a single redirect port; forward it as a
// one-element candidate list (kernel binds the first free one).
// A `0` port would bind an OS-assigned ephemeral port that can
// never match the IdP's static redirect_uri, so map `0 -> None`
// to fall back to the kernel default (matches C ABI + pyo3).
options
.oauth_redirect_port
.filter(|&p| p != 0)
.map(|p| vec![p]),
options.oauth_scopes.clone(),
None,
None,
Expand Down Expand Up @@ -722,29 +730,30 @@ mod tests {
match build_auth_config(&o).unwrap() {
AuthConfig::OAuthU2M {
client_id,
redirect_port,
redirect_ports,
scopes,
..
} => {
assert!(client_id.is_none());
assert!(redirect_port.is_none());
assert!(redirect_ports.is_none());
assert!(scopes.is_none());
}
other => panic!("expected OAuthU2M, got {other:?}"),
}
// Explicit overrides thread through.
// Explicit overrides thread through. Node's single redirect port is
// forwarded as a one-element candidate list.
o.oauth_client_id = Some("custom-cli".to_owned());
o.oauth_redirect_port = Some(9000);
o.identity_federation_client_id = Some("federation-client".to_owned());
match build_auth_config(&o).unwrap() {
AuthConfig::OAuthU2M {
client_id,
redirect_port,
redirect_ports,
identity_federation_client_id,
..
} => {
assert_eq!(client_id.as_deref(), Some("custom-cli"));
assert_eq!(redirect_port, Some(9000));
assert_eq!(redirect_ports, Some(vec![9000]));
assert_eq!(
identity_federation_client_id.as_deref(),
Some("federation-client")
Expand Down
148 changes: 109 additions & 39 deletions pyo3/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
//! - `"oauth-m2m"` — OAuth 2.0 client-credentials (service principal):
//! `client_id` + `client_secret`, optional `scopes` / `token_url`.
//! - `"oauth-u2m"` — OAuth 2.0 authorization-code + PKCE browser flow:
//! optional `client_id` / `redirect_port` / `scopes` / `token_url` /
//! optional `client_id` / `redirect_ports` / `scopes` / `token_url` /
//! `oauth_callback_timeout_secs`.
//!
//! OAuth is **kernel-owned**: the kernel acquires and refreshes tokens
Expand Down Expand Up @@ -50,7 +50,16 @@ pub(crate) struct OAuthParams {
pub client_secret: Option<String>,
pub scopes: Option<Vec<String>>,
pub token_url: Option<String>,
pub redirect_port: Option<u16>,
/// Candidate U2M redirect ports, tried in order (first free binds). A
/// single fixed port is just a one-element list (`vec![port]`); a
/// multi-port list gives the busy-port fallback the Thrift connector has
/// across its registered ports (`databricks-sql-python` → the discrete
/// set `8020`/`8025`, not a contiguous range).
/// `None` → the databricks-sql-python default (8020) when the
/// default client is in use, else the kernel default. At the pyo3
/// boundary an explicit *empty* list is rejected (see
/// [`build_auth_config`]), not defaulted.
pub redirect_ports: Option<Vec<u16>>,
pub callback_timeout_secs: Option<u64>,
pub identity_federation_client_id: Option<String>,
}
Expand Down Expand Up @@ -167,14 +176,15 @@ fn build_auth_config_inner(
"oauth-u2m" => {
// Browser flow: every field is optional and `None` lets the
// kernel apply its defaults (client_id "databricks-sql-connector",
// redirect_port 8030, scopes ["sql", "offline_access"],
// redirect port 8030, scopes ["sql", "offline_access"],
// callback timeout 120s). But a *present-yet-degenerate*
// value is a footgun the kernel can't distinguish from an
// intentional setting, so reject those here:
// - client_id="" would bypass the default and bind an
// empty client id;
// - redirect_port=0 binds an OS-assigned ephemeral port,
// which can't match the IdP's static redirect_uri;
// - an empty redirect_ports list, or a 0 entry (which binds an
// OS-assigned ephemeral port that can't match the IdP's
// static redirect_uri);
// - callback_timeout_secs=0 means "time out immediately",
// not "wait forever" (a common cross-system confusion).
if matches!(oauth.client_id.as_deref(), Some("")) {
Expand All @@ -184,20 +194,33 @@ fn build_auth_config_inner(
.to_owned(),
);
}
if oauth.redirect_port == Some(0) {
return Err(
"auth_type='oauth-u2m' redirect_port must be non-zero when set \
(a static port the IdP redirect_uri can match); omit it for the default"
.to_owned(),
);
}
if oauth.callback_timeout_secs == Some(0) {
return Err(
"auth_type='oauth-u2m' oauth_callback_timeout_secs must be > 0 when set \
(0 would time out the browser flow immediately); omit it for the default"
.to_owned(),
);
}
// `redirect_ports` is the ordered list of candidate callback ports
// (a single fixed port is just `[port]`). Reject the degenerate
// shapes: an empty list carries no port, and a 0 entry binds an
// ephemeral port that can't match the IdP's static redirect_uri.
if let Some(ports) = oauth.redirect_ports.as_deref() {
if ports.is_empty() {
return Err(
"auth_type='oauth-u2m' redirect_ports must be non-empty when set; \
omit it for the default"
.to_owned(),
);
}
if ports.contains(&0) {
return Err(
"auth_type='oauth-u2m' redirect_ports entries must be non-zero \
(each must be a static port the IdP redirect_uri can match)"
.to_owned(),
);
}
}
// Default the U2M OAuth client to the Python connector's
// own registered client (`databricks-sql-python`), NOT the
// kernel's bare-Rust fallback (`databricks-sql-connector`). This
Expand Down Expand Up @@ -234,15 +257,20 @@ fn build_auth_config_inner(
};
// Pin the databricks-sql-python app's own redirect port when
// relying on the default client_id — the kernel default (8030) is
// for the databricks-sql-connector app and would mismatch.
let redirect_port = match oauth.redirect_port {
Some(p) => Some(p),
None if !caller_set_client_id => Some(PYSQL_OAUTH_REDIRECT_PORT),
// for the databricks-sql-connector app and would mismatch. An
// explicit caller list (e.g. the connector's registered range for
// busy-port fallback) is used as-is; otherwise, under the default
// client, forward the pinned databricks-sql-python port as a
// one-element list. A custom client_id with no ports defers to the
// kernel (None) — that app owns its own redirect port.
let redirect_ports = match oauth.redirect_ports {
Some(ports) => Some(ports),
None if !caller_set_client_id => Some(vec![PYSQL_OAUTH_REDIRECT_PORT]),
None => None,
};
Ok(AuthConfig::oauth_u2m(
client_id,
redirect_port,
redirect_ports,
scopes,
oauth.token_url,
oauth.callback_timeout_secs.map(Duration::from_secs),
Expand Down Expand Up @@ -395,7 +423,7 @@ mod tests {
match cfg {
AuthConfig::OAuthU2M {
client_id,
redirect_port,
redirect_ports,
scopes,
token_url_override,
callback_timeout,
Expand All @@ -404,8 +432,10 @@ mod tests {
} => {
assert_eq!(client_id.as_deref(), Some("databricks-sql-python"));
// Python bundle pins its own app's registered port (8020),
// NOT the kernel default 8030 (databricks-sql-connector).
assert_eq!(redirect_port, Some(8020));
// NOT the kernel default 8030 (databricks-sql-connector). With
// no explicit range, the single pinned port is forwarded as a
// one-element candidate list.
assert_eq!(redirect_ports, Some(vec![8020]));
assert_eq!(
scopes,
Some(vec!["sql".to_string(), "offline_access".to_string()])
Expand All @@ -426,6 +456,59 @@ mod tests {
}
}

#[test]
fn u2m_redirect_ports_are_forwarded() {
// A caller-supplied port range (the connector's registered redirect
// URIs) is carried through onto the config for busy-port fallback.
let cfg = build_auth_config_inner(
"oauth-u2m",
None,
OAuthParams {
client_id: Some("databricks-sql-python".to_string()),
redirect_ports: Some(vec![8020, 8021, 8022, 8023, 8024]),
..Default::default()
},
)
.unwrap();
match cfg {
AuthConfig::OAuthU2M { redirect_ports, .. } => {
assert_eq!(redirect_ports, Some(vec![8020, 8021, 8022, 8023, 8024]));
}
other => panic!("expected OAuthU2M, got {other:?}"),
}
}

#[test]
fn u2m_empty_redirect_ports_rejected() {
let err = build_auth_config_inner(
"oauth-u2m",
None,
OAuthParams {
redirect_ports: Some(vec![]),
..Default::default()
},
)
.unwrap_err();
assert!(
err.contains("redirect_ports must be non-empty"),
"got: {err}"
);
}

#[test]
fn u2m_zero_in_redirect_ports_rejected() {
let err = build_auth_config_inner(
"oauth-u2m",
None,
OAuthParams {
redirect_ports: Some(vec![8020, 0]),
..Default::default()
},
)
.unwrap_err();
assert!(err.contains("non-zero"), "got: {err}");
}

#[test]
fn u2m_explicit_client_id_overrides_default() {
let cfg = build_auth_config_inner(
Expand All @@ -441,15 +524,15 @@ mod tests {
AuthConfig::OAuthU2M {
client_id,
scopes,
redirect_port,
redirect_ports,
..
} => {
assert_eq!(client_id.as_deref(), Some("my-custom-app"));
// Custom client_id without scopes/port: don't pin the Python
// bundle's defaults — a custom app owns its own scopes and
// redirect port, so both defer to the kernel (None).
assert!(scopes.is_none());
assert!(redirect_port.is_none());
assert!(redirect_ports.is_none());
}
other => panic!("expected OAuthU2M, got {other:?}"),
}
Expand Down Expand Up @@ -486,7 +569,7 @@ mod tests {
"oauth-u2m",
None,
OAuthParams {
redirect_port: Some(9999),
redirect_ports: Some(vec![9999]),
callback_timeout_secs: Some(300),
identity_federation_client_id: Some("federation-client".into()),
..Default::default()
Expand All @@ -495,12 +578,13 @@ mod tests {
.unwrap();
match cfg {
AuthConfig::OAuthU2M {
redirect_port,
redirect_ports,
callback_timeout,
identity_federation_client_id,
..
} => {
assert_eq!(redirect_port, Some(9999));
// The single caller port is forwarded as a one-element list.
assert_eq!(redirect_ports, Some(vec![9999]));
assert_eq!(callback_timeout, Some(Duration::from_secs(300)));
assert_eq!(
identity_federation_client_id.as_deref(),
Expand All @@ -527,20 +611,6 @@ mod tests {
assert!(err.contains("client_id must be non-empty"), "{err}");
}

#[test]
fn u2m_rejects_zero_redirect_port() {
let err = build_auth_config_inner(
"oauth-u2m",
None,
OAuthParams {
redirect_port: Some(0),
..Default::default()
},
)
.unwrap_err();
assert!(err.contains("redirect_port must be non-zero"), "{err}");
}

#[test]
fn u2m_rejects_zero_callback_timeout() {
let err = build_auth_config_inner(
Expand Down
8 changes: 4 additions & 4 deletions pyo3/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Session {
/// `client_id` + `client_secret` (optional `oauth_scopes`,
/// `token_url`).
/// - `"oauth-u2m"`: browser authorization-code + PKCE flow
/// (optional `client_id`, `redirect_port`, `oauth_scopes`,
/// (optional `client_id`, `redirect_ports`, `oauth_scopes`,
/// `token_url`, `oauth_callback_timeout_secs`).
///
/// OAuth is kernel-owned: the kernel acquires and transparently
Expand Down Expand Up @@ -120,7 +120,7 @@ impl Session {
client_secret=None,
oauth_scopes=None,
token_url=None,
redirect_port=None,
redirect_ports=None,
Comment thread
oss-reviewer-bot[bot] marked this conversation as resolved.
Resolved
oauth_callback_timeout_secs=None,
identity_federation_client_id=None,
tls_ca_cert=None,
Expand Down Expand Up @@ -150,7 +150,7 @@ impl Session {
client_secret: Option<String>,
oauth_scopes: Option<Vec<String>>,
token_url: Option<String>,
redirect_port: Option<u16>,
redirect_ports: Option<Vec<u16>>,
oauth_callback_timeout_secs: Option<u64>,
identity_federation_client_id: Option<String>,
tls_ca_cert: Option<Vec<u8>>,
Expand Down Expand Up @@ -197,7 +197,7 @@ impl Session {
client_secret,
scopes: oauth_scopes,
token_url,
redirect_port,
redirect_ports,
callback_timeout_secs: oauth_callback_timeout_secs,
identity_federation_client_id,
};
Expand Down
Loading
Loading