Skip to content

Commit c642451

Browse files
author
Rasmus Oscar Welander
committed
Added possibility to set client secret and id at both runtime and in config
1 parent 745ae2e commit c642451

11 files changed

+65
-24
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,14 @@ ssl_verify = False
8888
# Optional, defaults to an empty string
8989
ssl_client_cert = test_client_cert
9090
# Optional, defaults to an empty string
91-
ssl_client_key = test_client_key
91+
ssl_client_key = test_client_key
9292
# Optional, defaults to an empty string
93-
ssl_ca_cert = test_ca_cert
93+
ssl_ca_cert = test_ca_cert
9494

9595
# Optinal, defaults to an empty string
9696
auth_client_id = einstein
97+
# Optional (can also be set when instansiating the class)
98+
auth_client_secret = relativity
9799
# Optional, defaults to basic
98100
auth_login_type = basic
99101

@@ -125,6 +127,8 @@ log = logging.getLogger(__name__)
125127

126128
client = CS3Client(config, "cs3client", log)
127129
auth = Auth(client)
130+
# Set the client id (can also be set in the config)
131+
auth.set_client_id("<your_client_id_here>")
128132
# Set client secret (can also be set in config)
129133
auth.set_client_secret("<your_client_secret_here>")
130134
# Checks if token is expired if not return ('x-access-token', <token>)

cs3client/auth.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,31 @@ def __init__(self, cs3_client: CS3Client) -> None:
3737
self._gateway: GatewayAPIStub = cs3_client._gateway
3838
self._log: logging.Logger = cs3_client._log
3939
self._config: Config = cs3_client._config
40-
# The user should be able to change the client secret (e.g. token) at runtime
41-
self._client_secret: str | None = None
40+
# The user should be able to change the client secret (e.g. token) and client id at runtime
41+
self._client_secret: str | None = self._config.auth_client_secret
42+
self._client_id: str | None = self._config.auth_client_id
4243
self._token: str | None = None
4344

4445
def set_client_secret(self, token: str) -> None:
4546
"""
4647
Sets the client secret, exists so that the user can change the client secret (e.g. token, password) at runtime,
47-
without having to create a new Auth object. NOTE that token OR the client secret has to be set when
48-
instantiating the client object.
48+
without having to create a new Auth object. Note client secret has to be set when
49+
instantiating the client object or through the configuration.
4950
5051
:param token: Auth token/password.
5152
"""
5253
self._client_secret = token
5354

55+
def set_client_id(self, id: str) -> None:
56+
"""
57+
Sets the client id, exists so that the user can change the client id at runtime, without having to create
58+
a new Auth object. Settings this (either through config or here) is optional unless you are using
59+
basic authentication.
60+
61+
:param token: id.
62+
"""
63+
self._client_id = id
64+
5465
def get_token(self) -> tuple[str, str]:
5566
"""
5667
Attempts to get a valid authentication token. If the token is not valid, a new token is requested
@@ -72,19 +83,22 @@ def get_token(self) -> tuple[str, str]:
7283
# Token has expired, obtain another one.
7384
req = AuthenticateRequest(
7485
type=self._config.auth_login_type,
75-
client_id=self._config.auth_client_id,
86+
client_id=self._client_id,
7687
client_secret=self._client_secret,
7788
)
7889
# Send the authentication request to the CS3 Gateway
7990
res = self._gateway.Authenticate(req)
8091

8192
if res.status.code != CODE_OK:
82-
self._log.error(f"Failed to authenticate user {self._config.auth_client_id}, error: {res.status}")
93+
self._log.error(f'msg="Failed to authenticate" '
94+
f'user="{self._client_id if self._client_id else "no_id_set"}" '
95+
f'error_code="{res.status}"')
8396
raise AuthenticationException(
84-
f"Failed to authenticate user {self._config.auth_client_id}, error: {res.status}"
97+
f'Failed to authenticate: user="{self._client_id if self._client_id else "no_id_set"}" '
98+
f'error_code="{res.status}"'
8599
)
86100
self._token = res.token
87-
self._log.debug(f'msg="Authenticated user" user="{self._config.auth_client_id}"')
101+
self._log.debug(f'msg="Authenticated user" user="{self._client_id if self._client_id else "no_id_set"}"')
88102
return ("x-access-token", self._token)
89103

90104
def list_auth_providers(self) -> list[str]:
@@ -97,7 +111,7 @@ def list_auth_providers(self) -> list[str]:
97111
try:
98112
res = self._gateway.ListAuthProviders(request=ListAuthProvidersRequest())
99113
if res.status.code != CODE_OK:
100-
self._log.error(f"List auth providers request failed, error: {res.status}")
114+
self._log.error(f'msg="List auth providers request failed" error_code="{res.status}"')
101115
raise Exception(res.status.message)
102116
except grpc.RpcError as e:
103117
self._log.error("List auth providers request failed")

cs3client/config.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,15 @@ def auth_client_id(self) -> str:
131131
"""
132132
return self._config.get(self._config_category, "auth_client_id", fallback=None)
133133

134+
@property
135+
def auth_client_secret(self) -> str:
136+
"""
137+
The auth_client_secret property returns the auth_client_secret value from the configuration,
138+
139+
:return: auth_client_secret
140+
"""
141+
return self._config.get(self._config_category, "auth_client_secret", fallback=None)
142+
134143
@property
135144
def tus_enabled(self) -> bool:
136145
"""

cs3client/statuscodehandler.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,44 +22,44 @@ def __init__(self, log: logging.Logger, config: Config) -> None:
2222

2323
def _log_not_found_info(self, status: cs3status.Status, operation: str, msg: str = None) -> None:
2424
self._log.info(
25-
f'msg="Not found on {operation}" {msg + " " if msg else ""}'
26-
f'userid="{self._config.auth_client_id}" trace="{status.trace}" '
27-
f'reason="{status.message.replace('"', "'")}"'
25+
f'msg="Not found on {operation}" {msg + " " if msg else ""} '
26+
f'userid="{self._config.auth_client_id if self._config.auth_client_id else "no_id_set"}" '
27+
f'trace="{status.trace}" reason="{status.message.replace('"', "'")}"'
2828
)
2929

3030
def _log_authentication_error(self, status: cs3status.Status, operation: str, msg: str = None) -> None:
3131
self._log.error(
3232
f'msg="Authentication failed on {operation}" {msg + " " if msg else ""}'
33-
f'userid="{self._config.auth_client_id}" trace="{status.trace}" '
34-
f'reason="{status.message.replace('"', "'")}"'
33+
f'userid="{self._config.auth_client_id if self._config.auth_client_id else "no_id_set"}" '
34+
f'trace="{status.trace}" reason="{status.message.replace('"', "'")}"'
3535
)
3636

3737
def _log_unknown_error(self, status: cs3status.Status, operation: str, msg: str = None) -> None:
3838
self._log.error(
3939
f'msg="Failed to {operation}, unknown error" {msg + " " if msg else ""}'
40-
f'userid="{self._config.auth_client_id}" trace="{status.trace}" '
41-
f'reason="{status.message.replace('"', "'")}"'
40+
f'userid="{self._config.auth_client_id if self._config.auth_client_id else "no_id_set"}" '
41+
f'trace="{status.trace}" reason="{status.message.replace('"', "'")}"'
4242
)
4343

4444
def _log_precondition_info(self, status: cs3status.Status, operation: str, msg: str = None) -> None:
4545
self._log.info(
4646
f'msg="Failed precondition on {operation}" {msg + " " if msg else ""}'
47-
f'userid="{self._config.auth_client_id}" trace="{status.trace}" '
48-
f'reason="{status.message.replace('"', "'")}"'
47+
f'userid="{self._config.auth_client_id if self._config.auth_client_id else "no_id_set"}" '
48+
f'trace="{status.trace}" reason="{status.message.replace('"', "'")}"'
4949
)
5050

5151
def _log_already_exists(self, status: cs3status.Status, operation: str, msg: str = None) -> None:
5252
self._log.info(
5353
f'msg="Already exists on {operation}" {msg + " " if msg else ""}'
54-
f'userid="{self._config.auth_client_id}" trace="{status.trace}" '
55-
f'reason="{status.message.replace('"', "'")}"'
54+
f'userid="{self._config.auth_client_id if self._config.auth_client_id else "no_id_set"}" '
55+
f'trace="{status.trace}" reason="{status.message.replace('"', "'")}"'
5656
)
5757

5858
def _log_unimplemented(self, status: cs3status.Status, operation: str, msg: str = None) -> None:
5959
self._log.info(
6060
f'msg="Invoked {operation} on unimplemented feature" {msg + " " if msg else ""}'
61-
f'userid="{self._config.auth_client_id}" trace="{status.trace}" '
62-
f'reason="{status.message.replace('"', "'")}"'
61+
f'userid="{self._config.auth_client_id if self._config.auth_client_id else "no_id_set"}" '
62+
f'trace="{status.trace}" reason="{status.message.replace('"', "'")}"'
6363
)
6464

6565
def handle_errors(self, status: cs3status.Status, operation: str, msg: str = None) -> None:

examples/app_api_example.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
client = CS3Client(config, "cs3client", log)
2424
auth = Auth(client)
25+
# Set the client id (can also be set in the config)
26+
auth.set_client_id("<your_client_id_here>")
2527
# Set client secret (can also be set in config)
2628
auth.set_client_secret("<your_client_secret_here>")
2729
# Checks if token is expired if not return ('x-access-token', <token>)

examples/auth_example.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
client = CS3Client(config, "cs3client", log)
2323
auth = Auth(client)
24+
# Set the client id (can also be set in the config)
25+
auth.set_client_id("<your_client_id_here>")
2426
# Set client secret (can also be set in config)
2527
auth.set_client_secret("<your_client_secret_here>")
2628
# Checks if token is expired if not return ('x-access-token', <token>)

examples/checkpoints_api_example.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
client = CS3Client(config, "cs3client", log)
2424
auth = Auth(client)
25+
# Set the client id (can also be set in the config)
26+
auth.set_client_id("<your_client_id_here>")
2527
# Set client secret (can also be set in config)
2628
auth.set_client_secret("<your_client_secret_here>")
2729
# Checks if token is expired if not return ('x-access-token', <token>)

examples/default.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ ssl_ca_cert = test_ca_cert
3333
auth_client_id = einstein
3434
# Optional, defaults to basic
3535
auth_login_type = basic
36+
# Optional (Can also be set after instantiating the Auth object)
37+
auth_client_secret = relativity
3638

3739
# For the future lock implementation
3840

examples/file_api_example.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
client = CS3Client(config, "cs3client", log)
3131
auth = Auth(client)
32+
# Set the client id (can also be set in the config)
33+
auth.set_client_id("<your_client_id_here>")
3234
# Set client secret (can also be set in config)
3335
auth.set_client_secret("<your_client_secret_here>")
3436
# Checks if token is expired if not return ('x-access-token', <token>)

examples/shares_api_example.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
client = CS3Client(config, "cs3client", log)
2424
auth = Auth(client)
25+
# Set the client id (can also be set in the config)
26+
auth.set_client_id("<your_client_id_here>")
2527
# Set client secret (can also be set in config)
2628
auth.set_client_secret("<your_client_secret_here>")
2729
# Checks if token is expired if not return ('x-access-token', <token>)

examples/user_api_example.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
client = CS3Client(config, "cs3client", log)
2323
auth = Auth(client)
24+
# Set the client id (can also be set in the config)
25+
auth.set_client_id("<your_client_id_here>")
2426
# Set client secret (can also be set in config)
2527
auth.set_client_secret("<your_client_secret_here>")
2628
# Checks if token is expired if not return ('x-access-token', <token>)

0 commit comments

Comments
 (0)