Skip to content

Commit 90129e7

Browse files
author
Rasmus Oscar Welander
committed
Authentication to be handled externally to allow parallelism
1 parent afe7044 commit 90129e7

23 files changed

+504
-408
lines changed

README.md

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ Alternatively, you can clone this repository and install manually:
3737
```bash
3838
git clone [email protected]:cs3org/cs3-python-client.git
3939
cd cs3-python-client
40-
pip install -e .
41-
export PYTHONPATH="path/to/cs3-python-client/:$PYTHONPATH"
40+
pip install .
4241
```
4342

4443

@@ -112,129 +111,138 @@ lock_expiration = 1800
112111

113112
To use `cs3client`, you first need to import and configure it. Here's a simple example of how to set up and start using the client. For configuration see [Configuration](#configuration). For more in depth examples see `cs3-python-client/examples/`.
114113

115-
### Initilization
114+
### Initilization and Authentication
116115
```python
117116
import logging
118117
import configparser
119118
from cs3client import CS3Client
120-
from cs3resource import Resource
119+
from auth import Auth
121120

122121
config = configparser.ConfigParser()
123122
with open("default.conf") as fdef:
124123
config.read_file(fdef)
125-
126124
log = logging.getLogger(__name__)
127125

128126
client = CS3Client(config, "cs3client", log)
129-
# client.auth.set_token("<your_token_here>")
130-
# OR
131-
client.auth.set_client_secret("<your_client_secret_here>")
127+
auth = Auth(client)
128+
# Set client secret (can also be set in config)
129+
auth.set_client_secret("<your_client_secret_here>")
130+
# Checks if token is expired if not return ('x-access-token', <token>)
131+
# if expired, request a new token from reva
132+
auth_token = auth.get_token()
133+
134+
# OR if you already have a reva token
135+
# Checks if token is expired if not return (x-access-token', <token>)
136+
# if expired, throws an AuthenticationException (so you can refresh your reva token)
137+
token = "<your_reva_token>"
138+
auth_token = Auth.check_token(token)
139+
132140
```
133141

134142
### File Example
135143
```python
136144
# mkdir
137145
directory_resource = Resource.from_file_ref_and_endpoint(f"/eos/user/r/rwelande/test_directory")
138-
res = client.file.make_dir(directory_resource)
146+
res = client.file.make_dir(client.auth.get_token(), directory_resource)
139147

140148
# touchfile
141149
touch_resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/touch_file.txt")
142-
res = client.file.touch_file(touch_resource)
150+
res = client.file.touch_file(client.auth.get_token(), touch_resource)
143151

144152
# setxattr
145153
resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/text_file.txt")
146-
res = client.file.set_xattr(resource, "iop.wopi.lastwritetime", str(1720696124))
154+
res = client.file.set_xattr(client.auth.get_token(), resource, "iop.wopi.lastwritetime", str(1720696124))
147155

148156
# rmxattr
149-
res = client.file.remove_xattr(resource, "iop.wopi.lastwritetime")
157+
res = client.file.remove_xattr(client.auth.get_token(), resource, "iop.wopi.lastwritetime")
150158

151159
# stat
152-
res = client.file.stat(resource)
160+
res = client.file.stat(client.auth.get_token(), resource)
153161

154162
# removefile
155-
res = client.file.remove_file(touch_resource)
163+
res = client.file.remove_file(client.auth.get_token(), touch_resource)
156164

157165
# rename
158166
rename_resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/rename_file.txt")
159-
res = client.file.rename_file(resource, rename_resource)
167+
res = client.file.rename_file(client.auth.get_token(), resource, rename_resource)
160168

161169
# writefile
162170
content = b"Hello World"
163171
size = len(content)
164-
res = client.file.write_file(rename_resource, content, size)
172+
res = client.file.write_file(client.auth.get_token(), rename_resource, content, size)
165173

166174
# listdir
167175
list_directory_resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande")
168-
res = client.file.list_dir(list_directory_resource)
176+
res = client.file.list_dir(client.auth.get_token(), list_directory_resource)
169177

170178

171179
# readfile
172-
file_res = client.file.read_file(rename_resource)
180+
file_res = client.file.read_file(client.auth.get_token(), rename_resource)
173181
```
174182

175183
### Share Example
176184
```python
177185
# Create share #
178186
resource = Resource.from_file_ref_and_endpoint("/eos/user/r/<some_username>/text.txt")
179-
resource_info = client.file.stat(resource)
187+
resource_info = client.file.stat(client.auth.get_token(), resource)
180188
user = client.user.get_user_by_claim("username", "<some_username>")
181-
res = client.share.create_share(resource_info, user.id.opaque_id, user.id.idp, "EDITOR", "USER")
189+
res = client.share.create_share(client.auth.get_token(), resource_info, user.id.opaque_id, user.id.idp, "EDITOR", "USER")
182190

183191
# List existing shares #
184192
filter_list = []
185193
filter = client.share.create_share_filter(resource_id=resource_info.id, filter_type="TYPE_RESOURCE_ID")
186194
filter_list.append(filter)
187195
filter = client.share.create_share_filter(share_state="SHARE_STATE_PENDING", filter_type="TYPE_STATE")
188196
filter_list.append(filter)
189-
res, _ = client.share.list_existing_shares()
197+
res, _ = client.share.list_existing_shares(client.auth.get_token(), )
190198

191199
# Get share #
192200
share_id = "58"
193-
res = client.share.get_share(opaque_id=share_id)
201+
res = client.share.get_share(client.auth.get_token(), opaque_id=share_id)
194202

195203
# update share #
196-
res = client.share.update_share(opaque_id=share_id, role="VIEWER")
204+
res = client.share.update_share(client.auth.get_token(), opaque_id=share_id, role="VIEWER")
197205

198206
# remove share #
199-
res = client.share.remove_share(opaque_id=share_id)
207+
res = client.share.remove_share(client.auth.get_token(), opaque_id=share_id)
200208

201209
# List existing received shares #
202210
filter_list = []
203211
filter = client.share.create_share_filter(share_state="SHARE_STATE_ACCEPTED", filter_type="TYPE_STATE")
204212
filter_list.append(filter)
205-
res, _ = client.share.list_received_existing_shares()
213+
res, _ = client.share.list_received_existing_shares(client.auth.get_token())
206214

207215
# get received share #
208-
received_share = client.share.get_received_share(opaque_id=share_id)
216+
received_share = client.share.get_received_share(client.auth.get_token(), opaque_id=share_id)
209217

210218
# update recieved share #
211-
res = client.share.update_received_share(received_share=received_share, state="SHARE_STATE_ACCEPTED")
219+
res = client.share.update_received_share(client.auth.get_token(), received_share=received_share, state="SHARE_STATE_ACCEPTED")
212220

213221
# create public share #
214-
res = client.share.create_public_share(resource_info, role="VIEWER")
222+
res = client.share.create_public_share(client.auth.get_token(), resource_info, role="VIEWER")
215223

216224
# list existing public shares #
217225
filter_list = []
218226
filter = client.share.create_public_share_filter(resource_id=resource_info.id, filter_type="TYPE_RESOURCE_ID")
219227
filter_list.append(filter)
220228
res, _ = client.share.list_existing_public_shares(filter_list=filter_list)
221229

222-
res = client.share.get_public_share(opaque_id=share_id, sign=True)
230+
res = client.share.get_public_share(client.auth.get_token(), opaque_id=share_id, sign=True)
223231
# OR token = "<token>"
224232
# res = client.share.get_public_share(token=token, sign=True)
225233

226234
# update public share #
227-
res = client.share.update_public_share(type="TYPE_PASSWORD", token=token, role="VIEWER", password="hello")
235+
res = client.share.update_public_share(client.auth.get_token(), type="TYPE_PASSWORD", token=token, role="VIEWER", password="hello")
228236

229237
# remove public share #
230-
res = client.share.remove_public_share(token=token)
238+
res = client.share.remove_public_share(client.auth.get_token(), token=token)
231239

232240
```
233241

234242
### User Example
235243
```python
236244
# find_user
237-
res = client.user.find_users("rwel")
245+
res = client.user.find_users(client.auth.get_token(), "rwel")
238246

239247
# get_user
240248
res = client.user.get_user("https://auth.cern.ch/auth/realms/cern", "asdoiqwe")
@@ -253,21 +261,21 @@ res = client.user.get_user_by_claim("username", "rwelande")
253261
### App Example
254262
```python
255263
# list_app_providers
256-
res = client.app.list_app_providers()
264+
res = client.app.list_app_providers(client.auth.get_token())
257265

258266
# open_in_app
259267
resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/collabora.odt")
260-
res = client.app.open_in_app(resource)
268+
res = client.app.open_in_app(client.auth.get_token(), resource)
261269
```
262270

263271
### Checkpoint Example
264272
```python
265273
# list file versions
266274
resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/test.md")
267-
res = client.checkpoint.list_file_versions(resource)
275+
res = client.checkpoint.list_file_versions(client.auth.get_token(), resource)
268276

269277
# restore file version
270-
res = client.checkpoint.restore_file_version(resource, "1722936250.0569fa2f")
278+
res = client.checkpoint.restore_file_version(client.auth.get_token(), resource, "1722936250.0569fa2f")
271279
```
272280

273281
## Documentation

examples/app_api_example.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,41 @@
66
77
Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti.
88
9-
Last updated: 19/08/2024
9+
Last updated: 30/08/2024
1010
"""
1111

1212
import logging
1313
import configparser
14-
from cs3client import CS3Client
1514
from cs3resource import Resource
15+
from cs3client import CS3Client
16+
from auth import Auth
1617

1718
config = configparser.ConfigParser()
1819
with open("default.conf") as fdef:
1920
config.read_file(fdef)
20-
# log
2121
log = logging.getLogger(__name__)
2222

23-
2423
client = CS3Client(config, "cs3client", log)
25-
# client.auth.set_token("<your_token_here>")
26-
# OR
27-
client.auth.set_client_secret("<your_client_secret_here>")
28-
29-
print(client.auth.get_token())
24+
auth = Auth(client)
25+
# Set client secret (can also be set in config)
26+
auth.set_client_secret("<your_client_secret_here>")
27+
# Checks if token is expired if not return ('x-access-token', <token>)
28+
# if expired, request a new token from reva
29+
auth_token = auth.get_token()
30+
31+
# OR if you already have a reva token
32+
# Checks if token is expired if not return (x-access-token', <token>)
33+
# if expired, throws an AuthenticationException (so you can refresh your reva token)
34+
token = "<your_reva_token>"
35+
auth_token = Auth.check_token(token)
3036

3137
# list_app_providers
32-
res = client.app.list_app_providers()
38+
res = client.app.list_app_providers(client.auth.get_token())
3339
if res is not None:
3440
print(res)
3541

3642
# open_in_app
3743
resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/collabora.odt")
38-
res = client.app.open_in_app(resource)
44+
res = client.app.open_in_app(client.auth.get_token(), resource)
3945
if res is not None:
4046
print(res)

examples/auth_example.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
auth_example.py
3+
4+
Example script to demonstrate the usage of the app API in the CS3Client class.
5+
note that these are examples, and is not meant to be run as a script.
6+
7+
Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti.
8+
9+
Last updated: 30/08/2024
10+
"""
11+
12+
import logging
13+
import configparser
14+
from cs3client import CS3Client
15+
from auth import Auth
16+
17+
config = configparser.ConfigParser()
18+
with open("default.conf") as fdef:
19+
config.read_file(fdef)
20+
log = logging.getLogger(__name__)
21+
22+
client = CS3Client(config, "cs3client", log)
23+
auth = Auth(client)
24+
# Set client secret (can also be set in config)
25+
auth.set_client_secret("<your_client_secret_here>")
26+
# Checks if token is expired if not return ('x-access-token', <token>)
27+
# if expired, request a new token from reva
28+
auth_token = auth.get_token()
29+
30+
# OR if you already have a reva token
31+
# Checks if token is expired if not return (x-access-token', <token>)
32+
# if expired, throws an AuthenticationException (so you can refresh your reva token)
33+
token = "<your_reva_token>"
34+
auth_token = Auth.check_token(token)

examples/checkpoints_api_example.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,45 @@
66
77
Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti.
88
9-
Last updated: 19/08/2024
9+
Last updated: 30/08/2024
1010
"""
1111

1212
import logging
1313
import configparser
14-
from cs3client import CS3Client
1514
from cs3resource import Resource
15+
from cs3client import CS3Client
16+
from auth import Auth
1617

1718
config = configparser.ConfigParser()
1819
with open("default.conf") as fdef:
1920
config.read_file(fdef)
20-
# log
2121
log = logging.getLogger(__name__)
2222

2323
client = CS3Client(config, "cs3client", log)
24-
# client.auth.set_token("<your_token_here>")
25-
# OR
26-
client.auth.set_client_secret("<your_client_secret_here>")
24+
auth = Auth(client)
25+
# Set client secret (can also be set in config)
26+
auth.set_client_secret("<your_client_secret_here>")
27+
# Checks if token is expired if not return ('x-access-token', <token>)
28+
# if expired, request a new token from reva
29+
auth_token = auth.get_token()
30+
31+
# OR if you already have a reva token
32+
# Checks if token is expired if not return (x-access-token', <token>)
33+
# if expired, throws an AuthenticationException (so you can refresh your reva token)
34+
token = "<your_reva_token>"
35+
auth_token = Auth.check_token(token)
2736

2837
res = None
2938

3039
markdown_resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/test.md")
3140

32-
res = client.checkpoint.list_file_versions(markdown_resource)
41+
res = client.checkpoint.list_file_versions(client.auth.get_token(), markdown_resource)
3342

3443
if res is not None:
3544
for ver in res:
3645
print(ver)
3746

38-
res = client.checkpoint.restore_file_version(markdown_resource, "1722936250.0569fa2f")
47+
res = client.checkpoint.restore_file_version(client.auth.get_token(), markdown_resource, "1722936250.0569fa2f")
3948
if res is not None:
4049
for ver in res:
4150
print(ver)

0 commit comments

Comments
 (0)