Skip to content

Commit 46e0eaf

Browse files
author
Olivier Gintrand
committed
fix(oauth): inject stored OAuth token for authorization_code gateway manual refresh
Signed-off-by: Olivier Gintrand <olivier.gintrand@forterro.com>
1 parent a02a04b commit 46e0eaf

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,6 +2136,11 @@ OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
21362136
# TOOL_CONCURRENT_LIMIT=10
21372137
# GATEWAY_TOOL_NAME_SEPARATOR=-
21382138

2139+
# Maximum length of response text returned for non-JSON REST API responses
2140+
# Longer responses are truncated to prevent exposing excessive sensitive data
2141+
# Default: 5000 characters, Range: 1000-100000
2142+
# REST_RESPONSE_TEXT_MAX_LENGTH=5000
2143+
21392144
# Prompt Configuration
21402145
# PROMPT_CACHE_SIZE=100
21412146
# MAX_PROMPT_SIZE=102400

.secrets.baseline

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"files": "^.secrets.baseline|package-lock.json|Cargo.lock|scripts/sign_image.sh|scripts/zap|sonar-project.properties|uv.lock|go.sum|mcpgateway/sri_hashes.json|^.secrets.baseline$",
44
"lines": null
55
},
6-
"generated_at": "2026-04-14T13:09:46Z",
6+
"generated_at": "2026-04-14T14:08:10Z",
77
"plugins_used": [
88
{
99
"name": "AWSKeyDetector"
@@ -4830,7 +4830,7 @@
48304830
"hashed_secret": "ff37a98a9963d347e9749a5c1b3936a4a245a6ff",
48314831
"is_secret": false,
48324832
"is_verified": false,
4833-
"line_number": 2228,
4833+
"line_number": 2236,
48344834
"type": "Secret Keyword",
48354835
"verified_result": null
48364836
}
@@ -8624,39 +8624,39 @@
86248624
"hashed_secret": "ee977806d7286510da8b9a7492ba58e2484c0ecc",
86258625
"is_secret": false,
86268626
"is_verified": false,
8627-
"line_number": 6376,
8627+
"line_number": 6907,
86288628
"type": "Secret Keyword",
86298629
"verified_result": null
86308630
},
86318631
{
86328632
"hashed_secret": "f2e7745f43b0ef0e2c2faf61d6c6a28be2965750",
86338633
"is_secret": false,
86348634
"is_verified": false,
8635-
"line_number": 6868,
8635+
"line_number": 7399,
86368636
"type": "Secret Keyword",
86378637
"verified_result": null
86388638
},
86398639
{
86408640
"hashed_secret": "4a249743d4d2241bd2ae085b4fe654d089488295",
86418641
"is_secret": false,
86428642
"is_verified": false,
8643-
"line_number": 8215,
8643+
"line_number": 8746,
86448644
"type": "Secret Keyword",
86458645
"verified_result": null
86468646
},
86478647
{
86488648
"hashed_secret": "0c8d051d3c7eada5d31b53d9936fce6bcc232ae2",
86498649
"is_secret": false,
86508650
"is_verified": false,
8651-
"line_number": 8357,
8651+
"line_number": 8888,
86528652
"type": "Secret Keyword",
86538653
"verified_result": null
86548654
},
86558655
{
86568656
"hashed_secret": "f2b14f68eb995facb3a1c35287b778d5bd785511",
86578657
"is_secret": false,
86588658
"is_verified": false,
8659-
"line_number": 8733,
8659+
"line_number": 9264,
86608660
"type": "Secret Keyword",
86618661
"verified_result": null
86628662
}

mcpgateway/services/gateway_service.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5226,6 +5226,32 @@ async def refresh_gateway_manually(
52265226
if request_headers:
52275227
pre_auth_headers = get_passthrough_headers(request_headers, {}, db, gateway)
52285228

5229+
# For Authorization Code OAuth gateways, retrieve the stored user token
5230+
# so that _initialize_gateway() can connect instead of early-returning.
5231+
if (
5232+
user_email
5233+
and gateway.auth_type == "oauth"
5234+
and isinstance(gateway.oauth_config, dict)
5235+
and gateway.oauth_config.get("grant_type") == "authorization_code"
5236+
and "Authorization" not in pre_auth_headers
5237+
):
5238+
from mcpgateway.services.token_storage_service import TokenStorageService # pylint: disable=import-outside-toplevel
5239+
5240+
token_service = TokenStorageService(db)
5241+
access_token = await token_service.get_user_token(gateway_id, user_email)
5242+
if access_token:
5243+
pre_auth_headers["Authorization"] = f"Bearer {access_token}"
5244+
logger.debug(
5245+
"Injected stored OAuth token for auth_code gateway %s (user %s)",
5246+
gateway_name, user_email,
5247+
)
5248+
else:
5249+
logger.info(
5250+
"No stored OAuth token for auth_code gateway %s (user %s) — "
5251+
"refresh will return empty; user must complete /oauth/authorize/%s first",
5252+
gateway_name, user_email, gateway_id,
5253+
)
5254+
52295255
lock = self._get_refresh_lock(gateway_id)
52305256

52315257
# Check if lock is already held (concurrent refresh in progress)

0 commit comments

Comments
 (0)