Skip to content

Commit 50ac90b

Browse files
author
Rasmus Oscar Welander
committed
Adapted tokens to be handled externally to allow parallelism
1 parent afe7044 commit 50ac90b

File tree

13 files changed

+167
-145
lines changed

13 files changed

+167
-145
lines changed
File renamed without changes.

src/app.py renamed to cs3client/app.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
44
Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti.
55
6-
Last updated: 19/08/2024
6+
Last updated: 28/08/2024
77
"""
88

99
import logging
10-
from auth import Auth
11-
from cs3resource import Resource
1210
import cs3.app.registry.v1beta1.registry_api_pb2 as cs3arreg
1311
import cs3.app.registry.v1beta1.resources_pb2 as cs3arres
1412
import cs3.gateway.v1beta1.gateway_api_pb2 as cs3gw
1513
import cs3.app.provider.v1beta1.resources_pb2 as cs3apr
1614
from cs3.gateway.v1beta1.gateway_api_pb2_grpc import GatewayAPIStub
17-
from statuscodehandler import StatusCodeHandler
18-
from config import Config
15+
16+
from cs3client.cs3resource import Resource
17+
from cs3client.statuscodehandler import StatusCodeHandler
18+
from cs3client.config import Config
1919

2020

2121
class App:
@@ -28,7 +28,6 @@ def __init__(
2828
config: Config,
2929
log: logging.Logger,
3030
gateway: GatewayAPIStub,
31-
auth: Auth,
3231
status_code_handler: StatusCodeHandler,
3332
) -> None:
3433
"""
@@ -37,20 +36,21 @@ def __init__(
3736
:param config: Config object containing the configuration parameters.
3837
:param log: Logger instance for logging.
3938
:param gateway: GatewayAPIStub instance for interacting with CS3 Gateway.
40-
:param auth: An instance of the auth class.
4139
:param status_code_handler: An instance of the StatusCodeHandler class.
4240
"""
4341
self._status_code_handler: StatusCodeHandler = status_code_handler
4442
self._gateway: GatewayAPIStub = gateway
4543
self._log: logging.Logger = log
4644
self._config: Config = config
47-
self._auth: Auth = auth
4845

49-
def open_in_app(self, resource: Resource, view_mode: str = None, app: str = None) -> cs3apr.OpenInAppURL:
46+
def open_in_app(
47+
self, auth_token: tuple, resource: Resource, view_mode: str = None, app: str = None
48+
) -> cs3apr.OpenInAppURL:
5049
"""
5150
Open a file in an app, given the resource, view mode (VIEW_MODE_VIEW_ONLY, VIEW_MODE_READ_ONLY,
5251
VIEW_MODE_READ_WRITE, VIEW_MODE_PREVIEW), and app name.
5352
53+
:param auth_token: tuple in the form ('x-access-token', <token> (see auth.get_token/auth.check_token)
5454
:param resource: Resource object containing the resource information.
5555
:param view_mode: View mode of the app.
5656
:param app: App name.
@@ -63,21 +63,22 @@ def open_in_app(self, resource: Resource, view_mode: str = None, app: str = None
6363
if view_mode:
6464
view_mode_type = cs3gw.OpenInAppRequest.ViewMode.Value(view_mode)
6565
req = cs3gw.OpenInAppRequest(ref=resource.ref, view_mode=view_mode_type, app=app)
66-
res = self._gateway.OpenInApp(request=req, metadata=[self._auth.get_token()])
66+
res = self._gateway.OpenInApp(request=req, metadata=[auth_token])
6767
self._status_code_handler.handle_errors(res.status, "open in app", f"{resource.get_file_ref_str()}")
6868
self._log.debug(f'msg="Invoked OpenInApp" {resource.get_file_ref_str()} trace="{res.status.trace}"')
6969
return res.OpenInAppURL
7070

71-
def list_app_providers(self) -> list[cs3arres.ProviderInfo]:
71+
def list_app_providers(self, auth_token: dict) -> list[cs3arres.ProviderInfo]:
7272
"""
7373
list_app_providers lists all the app providers.
7474
75+
:param auth_token: tuple in the form ('x-access-token', <token> (see auth.get_token/auth.check_token)
7576
:return: List of app providers.
7677
:raises: AuthenticationException (Operation not permitted)
7778
:raises: UnknownException (Unknown error)
7879
"""
7980
req = cs3arreg.ListAppProvidersRequest()
80-
res = self._gateway.ListAppProviders(request=req, metadata=[self._auth.get_token()])
81+
res = self._gateway.ListAppProviders(request=req, metadata=[auth_token])
8182
self._status_code_handler.handle_errors(res.status, "list app providers")
8283
self._log.debug(f'msg="Invoked ListAppProviders" res_count="{len(res.providers)}" trace="{res.status.trace}"')
8384
return res.providers

src/auth.py renamed to cs3client/auth.py

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti.
55
6-
Last updated: 19/08/2024
6+
Last updated: 28/08/2024
77
"""
88

99
import grpc
@@ -15,8 +15,8 @@
1515
from cs3.gateway.v1beta1.gateway_api_pb2_grpc import GatewayAPIStub
1616
from cs3.rpc.v1beta1.code_pb2 import CODE_OK
1717

18-
from exceptions.exceptions import AuthenticationException, SecretNotSetException
19-
from config import Config
18+
from cs3client.exceptions.exceptions import AuthenticationException, SecretNotSetException
19+
from cs3client.config import Config
2020

2121

2222
class Auth:
@@ -40,16 +40,6 @@ def __init__(self, config: Config, log: logging.Logger, gateway: GatewayAPIStub)
4040
self._client_secret: str | None = None
4141
self._token: str | None = None
4242

43-
def set_token(self, token: str) -> None:
44-
"""
45-
Should be used if the user wishes to set the reva token directly, instead of letting the client
46-
exchange credentials for the token. NOTE that token OR the client secret has to be set when
47-
instantiating the client object.
48-
49-
:param token: The reva token.
50-
"""
51-
self._token = token
52-
5343
def set_client_secret(self, token: str) -> None:
5444
"""
5545
Sets the client secret, exists so that the user can change the client secret (e.g. token, password) at runtime,
@@ -71,16 +61,13 @@ def get_token(self) -> tuple[str, str]:
7161
:raises: SecretNotSetException (neither token or client secret was set)
7262
"""
7363

74-
if not Auth._check_token(self._token):
75-
# Check that client secret or token is set
76-
if not self._client_secret and not self._token:
77-
self._log.error("Attempted to authenticate, neither client secret or token was set.")
78-
raise SecretNotSetException("The client secret (e.g. token, passowrd) is not set")
79-
elif not self._client_secret and self._token:
80-
# Case where ONLY a token is provided but it has expired
81-
self._log.error("The provided token have expired")
82-
raise AuthenticationException("The credentials have expired")
83-
# Create an authentication request
64+
try:
65+
Auth.check_token(self._token)
66+
except ValueError:
67+
self._log.error("Attempted to authenticate, neither client secret or token was set.")
68+
raise SecretNotSetException("The client secret (e.g. token, passowrd) is not set")
69+
except AuthenticationException:
70+
# Token has expired, obtain another one.
8471
req = AuthenticateRequest(
8572
type=self._config.auth_login_type,
8673
client_id=self._config.auth_client_id,
@@ -116,20 +103,22 @@ def list_auth_providers(self) -> list[str]:
116103
return res.types
117104

118105
@classmethod
119-
def _check_token(cls, token: str) -> bool:
106+
def check_token(cls, token: str) -> str:
120107
"""
121108
Checks if the given token is set and valid.
122109
123110
:param token: JWT token as a string.
124-
:return: True if the token is valid, False otherwise.
111+
:return tuple: A tuple containing the header key and the token.
112+
:raises: ValueError (Token missing)
113+
:raises: AuthenticationException (Token is expired)
125114
"""
126115
if not token:
127-
return False
116+
raise ValueError("A token is required")
128117
# Decode the token without verifying the signature
129118
decoded_token = jwt.decode(jwt=token, algorithms=["HS256"], options={"verify_signature": False})
130119
now = datetime.datetime.now().timestamp()
131120
token_expiration = decoded_token.get("exp")
132121
if token_expiration and now > token_expiration:
133-
return False
122+
raise AuthenticationException("Token has expired")
134123

135-
return True
124+
return ("x-access-token", token)

src/checkpoint.py renamed to cs3client/checkpoint.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
44
Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti.
55
6-
Last updated: 19/08/2024
6+
Last updated: 28/08/2024
77
"""
88

99
from typing import Generator
1010
import logging
11-
from auth import Auth
1211
import cs3.storage.provider.v1beta1.resources_pb2 as cs3spr
1312
import cs3.storage.provider.v1beta1.provider_api_pb2 as cs3spp
1413
from cs3.gateway.v1beta1.gateway_api_pb2_grpc import GatewayAPIStub
15-
from config import Config
16-
from statuscodehandler import StatusCodeHandler
17-
from cs3resource import Resource
14+
15+
from cs3client.config import Config
16+
from cs3client.statuscodehandler import StatusCodeHandler
17+
from cs3client.cs3resource import Resource
1818

1919

2020
class Checkpoint:
@@ -27,7 +27,6 @@ def __init__(
2727
config: Config,
2828
log: logging.Logger,
2929
gateway: GatewayAPIStub,
30-
auth: Auth,
3130
status_code_handler: StatusCodeHandler,
3231
) -> None:
3332
"""
@@ -36,21 +35,20 @@ def __init__(
3635
:param config: Config object containing the configuration parameters.
3736
:param log: Logger instance for logging.
3837
:param gateway: GatewayAPIStub instance for interacting with CS3 Gateway.
39-
:param auth: An instance of the auth class.
4038
:param status_code_handler: An instance of the StatusCodeHandler class.
4139
"""
4240
self._gateway: GatewayAPIStub = gateway
4341
self._log: logging.Logger = log
4442
self._config: Config = config
45-
self._auth: Auth = auth
4643
self._status_code_handler: StatusCodeHandler = status_code_handler
4744

4845
def list_file_versions(
49-
self, resource: Resource, page_token: str = "", page_size: int = 0
46+
self, auth_token: tuple, resource: Resource, page_token: str = "", page_size: int = 0
5047
) -> Generator[cs3spr.FileVersion, any, any]:
5148
"""
5249
List all versions of a file.
5350
51+
:param auth_token: tuple in the form ('x-access-token', <token> (see auth.get_token/auth.check_token)
5452
:param resource: Resource object containing the resource information.
5553
:param page_token: Token for pagination.
5654
:param page_size: Number of file versions to return.
@@ -61,15 +59,18 @@ def list_file_versions(
6159
:raises: UnknownException (Unknown error)
6260
"""
6361
req = cs3spp.ListFileVersionsRequest(ref=resource.ref, page_token=page_token, page_size=page_size)
64-
res = self._gateway.ListFileVersions(request=req, metadata=[self._auth.get_token()])
62+
res = self._gateway.ListFileVersions(request=req, metadata=[auth_token])
6563
self._status_code_handler.handle_errors(res.status, "list file versions", f"{resource.get_file_ref_str()}")
6664
self._log.debug(f'msg="list file versions" {resource.get_file_ref_str()} trace="{res.status.trace}"')
6765
return res.versions
6866

69-
def restore_file_version(self, resource: Resource, version_key: str, lock_id: str = None) -> None:
67+
def restore_file_version(
68+
self, auth_token: tuple, resource: Resource, version_key: str, lock_id: str = None
69+
) -> None:
7070
"""
7171
Restore a file to a previous version.
7272
73+
:param auth_token: tuple in the form ('x-access-token', <token> (see auth.get_token/auth.check_token)
7374
:param resource: Resource object containing the resource information.
7475
:param version_key: Key of the version to restore.
7576
:param lock_id: Lock ID of the file (OPTIONAL).
@@ -79,7 +80,7 @@ def restore_file_version(self, resource: Resource, version_key: str, lock_id: st
7980
:raises: UnknownException (Unknown error)
8081
"""
8182
req = cs3spp.RestoreFileVersionRequest(ref=resource.ref, key=version_key, lock_id=lock_id)
82-
res = self._gateway.RestoreFileVersion(request=req, metadata=[self._auth.get_token()])
83+
res = self._gateway.RestoreFileVersion(request=req, metadata=[auth_token])
8384
self._status_code_handler.handle_errors(res.status, "restore file version", f"{resource.get_file_ref_str()}")
8485
self._log.debug(f'msg="restore file version" {resource.get_file_ref_str()} trace="{res.status.trace}"')
8586
return
File renamed without changes.

src/cs3client.py renamed to cs3client/cs3client.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
44
Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti.
55
6-
Last updated: 19/08/2024
6+
Last updated: 28/08/2024
77
"""
88

99
import grpc
1010
import logging
1111
import cs3.gateway.v1beta1.gateway_api_pb2_grpc as cs3gw_grpc
12-
1312
from configparser import ConfigParser
14-
from auth import Auth
15-
from file import File
16-
from user import User
17-
from share import Share
18-
from statuscodehandler import StatusCodeHandler
19-
from app import App
20-
from checkpoint import Checkpoint
21-
from config import Config
13+
14+
from cs3client.auth import Auth
15+
from cs3client.file import File
16+
from cs3client.user import User
17+
from cs3client.share import Share
18+
from cs3client.statuscodehandler import StatusCodeHandler
19+
from cs3client.app import App
20+
from cs3client.checkpoint import Checkpoint
21+
from cs3client.config import Config
2222

2323

2424
class CS3Client:
@@ -47,13 +47,13 @@ def __init__(self, config: ConfigParser, config_category: str, log: logging.Logg
4747
self._gateway: cs3gw_grpc.GatewayAPIStub = cs3gw_grpc.GatewayAPIStub(self.channel)
4848
self._status_code_handler: StatusCodeHandler = StatusCodeHandler(self._log, self._config)
4949
self.auth: Auth = Auth(self._config, self._log, self._gateway)
50-
self.file: File = File(self._config, self._log, self._gateway, self.auth, self._status_code_handler)
51-
self.user: User = User(self._config, self._log, self._gateway, self.auth, self._status_code_handler)
52-
self.app: App = App(self._config, self._log, self._gateway, self.auth, self._status_code_handler)
50+
self.file: File = File(self._config, self._log, self._gateway, self._status_code_handler)
51+
self.user: User = User(self._config, self._log, self._gateway, self._status_code_handler)
52+
self.app: App = App(self._config, self._log, self._gateway, self._status_code_handler)
5353
self.checkpoint: Checkpoint = Checkpoint(
54-
self._config, self._log, self._gateway, self.auth, self._status_code_handler
54+
self._config, self._log, self._gateway, self._status_code_handler
5555
)
56-
self.share = Share(self._config, self._log, self._gateway, self.auth, self._status_code_handler)
56+
self.share = Share(self._config, self._log, self._gateway, self._status_code_handler)
5757

5858
def _create_channel(self) -> grpc.Channel:
5959
"""
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)