1
- from quart_babel import _
2
1
from eve .auth import TokenAuth
3
2
4
-
5
3
from superdesk .utc import utcnow
6
- from superdesk .core .types .web import Request
7
4
from superdesk .errors import SuperdeskApiError
5
+ from superdesk .core .types .web import AuthRule , Request
8
6
from superdesk .core .auth .user_auth import UserAuthProtocol
7
+ from superdesk .core .auth .rules import endpoint_intrinsic_auth_rule
9
8
from superdesk .publish .subscriber_token import SubscriberTokenService , SubscriberToken
10
9
11
10
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 ):
15
15
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
17
28
18
29
19
30
class SubscriberTokenAuth (UserAuthProtocol ):
31
+ def get_default_auth_rules (self ) -> list [AuthRule ]:
32
+ return [endpoint_intrinsic_auth_rule ]
33
+
20
34
def get_token_from_request (self , request : Request ) -> str | None :
21
35
"""
22
36
Extracts the token from `Authorization` header. Code taken partly
@@ -36,7 +50,7 @@ async def authenticate(self, request: Request) -> None:
36
50
Tries to find the auth token in the request and if valid put subscriber id into ``g.user``.
37
51
"""
38
52
token_service = SubscriberTokenService ()
39
- token_missing_exception = SuperdeskApiError .forbiddenError (message = _ ( "Authorization token missing." ) )
53
+ token_missing_exception = SuperdeskApiError .forbiddenError (message = "Authorization token missing." )
40
54
token_id = self .get_token_from_request (request )
41
55
42
56
if token_id is None :
@@ -57,7 +71,7 @@ async def check_token_validity(self, token: SubscriberToken) -> None:
57
71
58
72
if token .expiry and token .expiry < utcnow ():
59
73
await SubscriberTokenService ().delete (token )
60
- raise SuperdeskApiError .forbiddenError (message = _ ( "Authorization token expired." ) )
74
+ raise SuperdeskApiError .forbiddenError (message = "Authorization token expired." )
61
75
62
76
async def start_session (self , request : Request , token : SubscriberToken ) -> None : # type: ignore[override]
63
77
"""
0 commit comments