Skip to content

Commit 512fbca

Browse files
committed
Use ServiceX API token (JWT refresh token) instead of username/email and password for authentication
1 parent 8c4b5f4 commit 512fbca

File tree

3 files changed

+18
-29
lines changed

3 files changed

+18
-29
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ If no endpoint is specified, then the library defaults to the developer endpoint
3838
Create a `.servicex` file, in the `yaml` format, in the appropriate place for your work that contains the following:
3939

4040
```yaml
41+
<<<<<<< HEAD
4142
api_endpoints:
4243
- endpoint: <your-endpoint>
43-
email: <api-email>
44-
password: <api-password>
44+
token: <api-token>
4545
type: xaod
4646
```
4747

servicex/config_default.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
api_endpoint:
55
endpoint: http://localhost:5000
6-
# email: xxx
7-
# password: yyy
6+
# token: xxx
87

98
minio_endpoint: localhost:9000
109
# The username and password for accessing files generated by servicex.

servicex/servicex_adaptor.py

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,48 +25,38 @@ def servicex_adaptor_factory(c: ConfigView):
2525
endpoint = c['api_endpoint']['endpoint'].as_str_expanded()
2626

2727
# We can default these to "None"
28-
email = c['api_endpoint']['email'].get(str) if 'email' in c['api_endpoint'] else None
29-
password = c['api_endpoint']['password'].get(str) if 'password' in c['api_endpoint'] else None
30-
return ServiceXAdaptor(endpoint, email, password)
28+
refresh_token = c['api_endpoint']['token'].get(str) if 'token' in c['api_endpoint'] else None
29+
return ServiceXAdaptor(endpoint, refresh_token)
3130

3231

3332
# Low level routines for interacting with a ServiceX instance via the WebAPI
3433
class ServiceXAdaptor:
35-
def __init__(self, endpoint, email=None, password=None):
34+
def __init__(self, endpoint, refresh_token=None):
3635
'''
3736
Authenticated access to ServiceX
3837
'''
3938
self._endpoint = endpoint
40-
self._email = email
41-
self._password = password
42-
4339
self._token = None
44-
self._refresh_token = None
45-
46-
async def _login(self, client: aiohttp.ClientSession):
47-
url = f'{self._endpoint}/login'
48-
async with client.post(url, json={
49-
'email': self._email,
50-
'password': self._password
51-
}) as response:
40+
self._refresh_token = refresh_token
41+
42+
async def _get_token(self, client: aiohttp.ClientSession):
43+
url = f'{self._endpoint}/token/refresh'
44+
headers = {'Authorization': f'Bearer {self._refresh_token}'}
45+
async with client.post(url, headers=headers) as response:
5246
status = response.status
5347
if status == 200:
5448
j = await response.json()
5549
self._token = j['access_token']
56-
self._refresh_token = j['refresh_token']
5750
else:
5851
raise ServiceXException(f'ServiceX login request rejected: {status}')
5952

6053
async def _get_authorization(self, client: aiohttp.ClientSession):
61-
if self._email:
62-
now = datetime.utcnow().timestamp()
63-
if not self._token or jwt.decode(self._token, verify=False)['exp'] - now < 0:
64-
await self._login(client)
65-
return {
66-
'Authorization': f'Bearer {self._token}'
67-
}
68-
else:
69-
return {}
54+
now = datetime.utcnow().timestamp()
55+
if not self._token or jwt.decode(self._token, verify=False)['exp'] - now < 0:
56+
await self._get_token(client)
57+
return {
58+
'Authorization': f'Bearer {self._token}'
59+
}
7060

7161
async def submit_query(self, client: aiohttp.ClientSession,
7262
json_query: Dict[str, str]) -> Dict[str, str]:

0 commit comments

Comments
 (0)