Skip to content

Commit d41fed4

Browse files
committed
Fix content_api.items which depends on legacy auth
SDESK-7484
1 parent b6350da commit d41fed4

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

content_api/app/__init__.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@
2424
from eve.io.mongo.mongo import MongoJSONEncoder
2525

2626
from superdesk.flask import Config
27+
from content_api.tokens.auth import LegacyTokenAuth
2728
from superdesk.datalayer import SuperdeskDataLayer
2829
from superdesk.factory.elastic_apm import setup_apm
2930
from superdesk.validator import SuperdeskValidator
3031
from superdesk.factory.app import SuperdeskEve, set_error_handlers, get_media_storage_class
3132

32-
from content_api.tokens.auth import SyncMockTokenAuth
33-
3433

3534
def get_app(config=None):
3635
"""
@@ -64,7 +63,7 @@ def get_app(config=None):
6463
media_storage = get_media_storage_class(app_config)
6564

6665
app = SuperdeskEve(
67-
auth=SyncMockTokenAuth,
66+
auth=LegacyTokenAuth,
6867
settings=app_config,
6968
data=SuperdeskDataLayer,
7069
media=media_storage,
@@ -82,6 +81,8 @@ def get_app(config=None):
8281
except AttributeError:
8382
pass
8483

84+
app.async_app.start()
85+
8586
return app
8687

8788

content_api/tokens/auth.py

+23-9
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
1-
from quart_babel import _
21
from eve.auth import TokenAuth
32

4-
53
from superdesk.utc import utcnow
6-
from superdesk.core.types.web import Request
74
from superdesk.errors import SuperdeskApiError
5+
from superdesk.core.types.web import AuthRule, Request
86
from superdesk.core.auth.user_auth import UserAuthProtocol
7+
from superdesk.core.auth.rules import endpoint_intrinsic_auth_rule
98
from superdesk.publish.subscriber_token import SubscriberTokenService, SubscriberToken
109

1110

12-
# TODO-ASYNC: remove this once everything is async in `content_api`
13-
# This is just a mock auth to avoid the app from breaking
14-
class SyncMockTokenAuth(TokenAuth):
11+
# TODO-ASYNC: Needed to avoid the content_api items endpoint from crashing
12+
# as it relies on the `user` stored in the `g` object. Once items are migrated
13+
# we should remove this
14+
class LegacyTokenAuth(TokenAuth):
1515
def check_auth(self, token, allowed_roles, resource, method):
16-
return True
16+
"""Try to find auth token and if valid put subscriber id into ``g.user``."""
17+
from superdesk.flask import g
18+
19+
data = SubscriberTokenService().mongo.find_one(token)
20+
if not data:
21+
return False
22+
now = utcnow()
23+
if data.get("expiry") and data.get("expiry") < now:
24+
SubscriberTokenService().mongo.delete_one({"_id": token})
25+
return False
26+
g.user = str(data.get("subscriber"))
27+
return g.user
1728

1829

1930
class SubscriberTokenAuth(UserAuthProtocol):
31+
def get_default_auth_rules(self) -> list[AuthRule]:
32+
return [endpoint_intrinsic_auth_rule]
33+
2034
def get_token_from_request(self, request: Request) -> str | None:
2135
"""
2236
Extracts the token from `Authorization` header. Code taken partly
@@ -36,7 +50,7 @@ async def authenticate(self, request: Request) -> None:
3650
Tries to find the auth token in the request and if valid put subscriber id into ``g.user``.
3751
"""
3852
token_service = SubscriberTokenService()
39-
token_missing_exception = SuperdeskApiError.forbiddenError(message=_("Authorization token missing."))
53+
token_missing_exception = SuperdeskApiError.forbiddenError(message="Authorization token missing.")
4054
token_id = self.get_token_from_request(request)
4155

4256
if token_id is None:
@@ -57,7 +71,7 @@ async def check_token_validity(self, token: SubscriberToken) -> None:
5771

5872
if token.expiry and token.expiry < utcnow():
5973
await SubscriberTokenService().delete(token)
60-
raise SuperdeskApiError.forbiddenError(message=_("Authorization token expired."))
74+
raise SuperdeskApiError.forbiddenError(message="Authorization token expired.")
6175

6276
async def start_session(self, request: Request, token: SubscriberToken) -> None: # type: ignore[override]
6377
"""

superdesk/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ def get_cors_headers(methods="*"):
361361

362362
return [
363363
("Access-Control-Allow-Origin", get_app_config("CLIENT_URL")),
364-
("Access-Control-Allow-Headers", ",".join(get_app_config("X_HEADERS"))),
364+
("Access-Control-Allow-Headers", ",".join(get_app_config("X_HEADERS") or [])),
365365
("Access-Control-Allow-Credentials", "true"),
366366
("Access-Control-Allow-Methods", methods),
367367
]

0 commit comments

Comments
 (0)