Skip to content

Commit 57665ca

Browse files
committed
CH-226 fix cookie auth issue
1 parent 31dd519 commit 57665ca

4 files changed

Lines changed: 41 additions & 4 deletions

File tree

applications/samples/backend/samples/controllers/auth_controller.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,5 @@ def valid_cookie(): # noqa: E501
2626
:rtype: List[Valid]
2727
"""
2828
from cloudharness.middleware import get_authentication_token
29-
from cloudharness.auth import decode_token
3029
token = get_authentication_token()
31-
assert decode_token(token)
3230
return 'OK'

applications/samples/backend/samples/controllers/security_controller_.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from typing import List
2+
from cloudharness.auth import decode_token
3+
from cloudharness.middleware import set_authentication_token
24

35

46
def info_from_bearerAuth(token):
@@ -13,3 +15,31 @@ def info_from_bearerAuth(token):
1315
:rtype: dict | None
1416
"""
1517
return {'uid': 'user_id'}
18+
19+
20+
def info_from_cookieAuth(api_key):
21+
"""
22+
Check and retrieve authentication information from cookie-based API key.
23+
This function is called by Connexion when cookieAuth security is used.
24+
25+
:param api_key Token provided by the kc-access cookie
26+
:type api_key: str
27+
:return: Decoded token information or None if token is invalid
28+
:rtype: dict | None
29+
"""
30+
if not api_key:
31+
return None
32+
33+
# Set the authentication token in the middleware context
34+
# so that get_authentication_token() can access it
35+
set_authentication_token(api_key)
36+
37+
# Decode and validate the token
38+
try:
39+
decoded = decode_token(api_key)
40+
if decoded:
41+
return decoded
42+
except Exception:
43+
pass
44+
45+
return None

applications/samples/backend/samples/openapi/openapi.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,4 +336,4 @@ components:
336336
in: cookie
337337
name: kc-access
338338
type: apiKey
339-
x-apikeyInfoFunc: cloudharness.auth.decode_token
339+
x-apikeyInfoFunc: samples.controllers.security_controller_.info_from_cookieAuth

libraries/cloudharness-common/cloudharness/utils/server.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,16 @@ def handle_exception(request, exc: Exception):
170170

171171
# Register error handler with Flask app directly for better compatibility
172172
@app.errorhandler(Exception)
173-
def flask_handle_exception(exc: Exception):
173+
def flask_handle_exception(*args):
174+
# Flask error handlers can be called with different signatures
175+
# Handle both single argument (exc) and multiple arguments flexibly
176+
if len(args) == 1:
177+
exc = args[0]
178+
elif len(args) >= 2:
179+
exc = args[0] if isinstance(args[0], Exception) else args[1]
180+
else:
181+
exc = Exception("Unknown error")
182+
174183
# For Flask error handlers, we don't get the request object,
175184
# but we can access it via flask.request if needed
176185
try:

0 commit comments

Comments
 (0)