I create a client (plain python script) and server (flask app) in Python. I wonder how to perform simple authentication using a access key in Python. Authentication is for scripts - they are not real users (no registration required). The key will be assigned to the script during implementation.
Scenario 1:
- Client sends message to server.
- Server (flask app) reads token from Authorization header.
- Server looks for the token in the database.
- If server finds token in database it will authenticate the client.
The access token is explicitly passed to the client before sending messages and the client stores it in an environment variable.
Disadventages:
- Tokens are not stored as hashes
Scenario 2:
- Client sends message to server.
- Server (flask app) reads token (jwt) from Authorization header.
- Server decodes jwt token to:
{'accessKeyId': 'fake_id', 'accessKey': 'fake_key'}
- Server looks for the key in database by id.
- If server finds row in db and key matches to decoded server will authenticate the client.
Adventages:
- Keys are stored as hashes
The access token is explicitly passed to the client too. It requires:
- create
{'accessKeyId': 'fake_id', 'accessKey': 'fake_key'}
object, - hash
'fake_key'
(and save to server database), - generate jwt token (finally it will passed for client and it will be in environment variable).
Is the 2nd approach correct? How is this implemented in applications where the user gets an access key after registration in web app and can query the api (via REST) if the key is correct - sth like OpenWeatherMaps?
Edit: I found out that I need to implement the KEY API mechanism. Are there any guides on how to do this in Flask/Python?