no10ds / rapid

Project rAPId
https://rapid.readthedocs.io/en/latest/
MIT License
23 stars 6 forks source link

Problems when authenticating with bad users but valid url #52

Closed gillespied closed 11 months ago

gillespied commented 11 months ago

rapid version 7.0.4, rapid-sdk version 0.1.4

In the current version of the sdk I think there is a big in the code which checks if the credentials can be validated. Below is a reprex which used some bogus details for ID and SECRET but uses the correct URL for our rapid instance.

from rapid import Rapid
from rapid import RapidAuth

from dotenv import load_dotenv
import os

load_dotenv(dotenv_path="./broken.env")

rapid_authentication = RapidAuth()

if rapid_authentication.validate_credentials() is None:
    print("returned None so assum credentials are good.")

rapid = Rapid(rapid_authentication)

rapid.list_datasets()

The output from rapid_authentication.validate_credentials() is returned as None, which according to the documents should mean the connection is okay. However, list_datasets() gives the below error:

id-testing/test.py
returned None so assum credentials are good.
Traceback (most recent call last):
  File "/Users/david.gillespie/Code/rapid-testing/test.py", line 17, in <module>
    rapid.list_datasets()
  File "/Users/david.gillespie/mambaforge/envs/rapid-testing/lib/python3.10/site-packages/rapid/rapid.py", line 56, in list_datasets
    headers=self.generate_headers(),
  File "/Users/david.gillespie/mambaforge/envs/rapid-testing/lib/python3.10/site-packages/rapid/rapid.py", line 45, in generate_headers
    return {"Authorization": "Bearer " + self.auth.fetch_token()}
  File "/Users/david.gillespie/mambaforge/envs/rapid-testing/lib/python3.10/site-packages/rapid/auth.py", line 83, in fetch_token
    return json.loads(self.request_token().content.decode("utf-8"))["access_token"]
KeyError: 'access_token'

I think I would expect the error to be thrown earlier, perhaps by validate_credentials? Or maybe an exception added for an invalid_client? Current validate_credentials only checks for status=200 for it to pass.

TobyDrane commented 11 months ago

@gillespied I cannot re-create this unfortunately. I created the following 3 scenarios and the function works as stated:

  1. No client details and no url passed to the RapidAuth class raises the following CannotFindCredentialException: No value passed for RAPID_CLIENT_ID, could not authenticate to rAPId
  2. Just a url passed into RapidAuth raises the same CannotFindCredentialException: No value passed for RAPID_CLIENT_ID, could not authenticate to rAPId
  3. Invalid client details and a valid url raises AuthenticationErrorException: Auth not configured, could not connect to instance of rAPId

If you are running a Jupyter Notebook ensure you have correctly loaded in your environment variables and likewise you restart the kernel when these values change.