Skip to content

Commit 620f10d

Browse files
committed
feat: allow setting redirect URI port for OAuth flow
Adds a `redirect_port` parameter to `Issuer.identity_token()` that allows callers to specify a fixed port for the local OAuth redirect server. This is needed for enterprise OIDC providers that require pre-registered redirect URIs and don't allow wildcard localhost ports. The default remains `0` (ephemeral port), preserving backward compatibility. Fixes #1029 Signed-off-by: Yash Goel <yashgoel892@gmail.com>
1 parent a5a6dea commit 620f10d

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

sigstore/_internal/oidc/oauth.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@
8787
</div>
8888
<div class="anchor">
8989
<div class="links">
90-
<a href="https://sigstore.dev/" class="link login"><span class="sigstore">sigstore</span> home <span class="arrow"></span></a>
91-
<a href="https://docs.sigstore.dev/" class="link login"><span class="sigstore">sigstore</span> documentation <span class="arrow"></span></a>
92-
<a href="https://blog.sigstore.dev/" class="link"><span class="sigstore">sigstore</span> blog <span class="arrow"></span></a>
90+
<a href="https://sigstore.dev/" class="link login"><span class="sigstore">sigstore</span> home <span class="arrow">&rarr;</span></a>
91+
<a href="https://docs.sigstore.dev/" class="link login"><span class="sigstore">sigstore</span> documentation <span class="arrow">&rarr;</span></a>
92+
<a href="https://blog.sigstore.dev/" class="link"><span class="sigstore">sigstore</span> blog <span class="arrow">&rarr;</span></a>
9393
</div>
9494
</div>
9595
</div>
@@ -102,12 +102,18 @@
102102

103103

104104
class _OAuthFlow:
105-
def __init__(self, client_id: str, client_secret: str, issuer: Issuer):
105+
def __init__(
106+
self,
107+
client_id: str,
108+
client_secret: str,
109+
issuer: Issuer,
110+
redirect_port: int = 0,
111+
):
106112
self._client_id = client_id
107113
self._client_secret = client_secret
108114
self._issuer = issuer
109115
self._server = _OAuthRedirectServer(
110-
self._client_id, self._client_secret, self._issuer
116+
self._client_id, self._client_secret, self._issuer, port=redirect_port
111117
)
112118
self._server_thread = threading.Thread(
113119
target=lambda server: server.serve_forever(),
@@ -223,8 +229,14 @@ def _auth_params(self, redirect_uri: str) -> dict[str, Any]:
223229

224230

225231
class _OAuthRedirectServer(http.server.HTTPServer):
226-
def __init__(self, client_id: str, client_secret: str, issuer: Issuer) -> None:
227-
super().__init__(("localhost", 0), _OAuthRedirectHandler)
232+
def __init__(
233+
self,
234+
client_id: str,
235+
client_secret: str,
236+
issuer: Issuer,
237+
port: int = 0,
238+
) -> None:
239+
super().__init__(("localhost", port), _OAuthRedirectHandler)
228240
self.oauth_session = _OAuthSession(client_id, client_secret, issuer)
229241
self.auth_response: dict[str, list[str]] | None = None
230242
self._is_out_of_band = False

sigstore/oidc.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ def identity_token( # nosec: B107
274274
client_id: str = _DEFAULT_CLIENT_ID,
275275
client_secret: str = "",
276276
force_oob: bool = False,
277+
redirect_port: int = 0,
277278
) -> IdentityToken:
278279
"""
279280
Retrieves and returns an `IdentityToken` from the current `Issuer`, via OAuth.
@@ -283,6 +284,11 @@ def identity_token( # nosec: B107
283284
The `force_oob` flag controls the kind of flow performed. When `False` (the default),
284285
this function attempts to open the user's web browser before falling back to
285286
an out-of-band flow. When `True`, the out-of-band flow is always used.
287+
288+
The `redirect_port` parameter controls the port used for the local redirect server
289+
during the OAuth flow. When `0` (the default), an ephemeral port is chosen by the OS.
290+
Set this to a specific port number when your OIDC provider requires a pre-registered
291+
redirect URI with a fixed port.
286292
"""
287293

288294
# This function and the components that it relies on are based off of:
@@ -291,7 +297,9 @@ def identity_token( # nosec: B107
291297
from sigstore._internal.oidc.oauth import _OAuthFlow
292298

293299
code: str
294-
with _OAuthFlow(client_id, client_secret, self) as server:
300+
with _OAuthFlow(
301+
client_id, client_secret, self, redirect_port=redirect_port
302+
) as server:
295303
# Launch web browser
296304
if not force_oob and webbrowser.open(server.base_uri):
297305
print("Waiting for browser interaction...", file=sys.stderr)

0 commit comments

Comments
 (0)