-
Notifications
You must be signed in to change notification settings - Fork 270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hash API keys #3842
base: feature/1027-reset-api-key
Are you sure you want to change the base?
Hash API keys #3842
Conversation
01ad6ca
to
05df12a
Compare
How API keys are reset and displayed has changed since the initial version of API keys: Users will be able to view an API key exactly once after it has been created/reset. This requires a slightly different user interface. We’re also planning a few more changes to API keys in the future, and these UI changes prepare for that.
The existing settings UI was a little cluttered and unstructured. We’re going to add new settings in this PR and in follow-up PRs, so I took the time to clean up the UI (both visually and implementation-wise).
This is a hacky workaround, but a proper fix would require quite some refactoring. Considering that this hack is pretty isolated and not going to affect any other parts of the UI and that we will need to upgrade to Blueprint 5 at some point anyway, I’ve opted for the quick-and-dirty solution for now.
In the future, roles won’t have an API key by default anymore. As an alternative, we generate session tokens explicitly.
Most users do not need API access so there’s no reason to generate an API key for them by default.
Previously, an API was generate automatically for new users, i.e. every user had an API key. This has now changed, and the settings UI needs to properly handle situations where a user doesn’t yet have an API key. As this increases the complexity of the UI state, I’ve refactored the component to make use of a local reducer.
This method is now also used to generate an initial key for users who do not yet have an API key.
While the logic initially was quite simply, there will be more business logic related to API keys, e.g. sending notifications ahead of and when an API key has expired.
Initially, I added this to the role model as the model to be consistent with the model's `set_password` method. However, as the logic to generate an API token has become more complex, it is clear that it shouldn't live in the model.
Aleph represents both users and groups using the role model. However, some API keys (such as `has_password` or `has_api_key` are not relevant for groups).
cb47a2f
to
fd5a17e
Compare
Aleph used to store user API keys as plaintext in the database. This commit changes that to store only a hash of the API key. API keys are generated using the built-in `secrets.token_urlsafe` method which returns a random 256 bit token. In contrast to passwords, API keys are not provided by users, have a high entropy, and need to be validated on every request. It seems to be generally accepted that, given 256 bit tokens, salting or using an expensive key derivation functions isn't necessary. For this reason, we’re storing an unsalted SHA-256 hash of the API key which also makes it easy to look up and verify a given API key. I've added a separate column for the hashed API key rather than reusing the existing column. This allows us to batch-hash all existing plaintext keys without having to differentiate between keys that have already been hashed and those that haven't. Once all existing plaintext API keys have been hashed, the old `api_key` column can simply be dropped.
Required as we do not store plaintext API keys anymore. Also, we want to remove the option to pass API keys via URL parameters in the future. This makes it impossible to use OpenRefine with non-public collections. This was never documented, and most users weren't aware that they can indeed use OpenRefine with non-public collections anyway.
05df12a
to
5f94f9b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice and straightforward, left a minor comment but this looks good to go!
for index, partition in enumerate(results.partitions()): | ||
for role in partition: | ||
role.api_key_digest = hash_api_key(role.api_key) | ||
role.api_key = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minior nitpick, but I think
role.api_key = None | |
del role.api_key |
would make sure that key is no longer in memory (as soon as the garbage collector runs)
cc19c2b
to
1cb1e20
Compare
This is based on #3094. Review and merge #3094 first!
Aleph used to store user API keys as plaintext in the database. This commit changes that to store only a hash of the API key.
API keys are generated using the built-in
secrets.token_urlsafe
method which returns a random 256 bit token. In contrast to passwords, API keys are not provided by users, have a high entropy, and need to be validated on every request. It seems to be generally accepted that, given 256 bit tokens, salting or using an expensive key derivation functions isn't necessary. (But please challenge this!) For this reason, we’re storing an unsalted SHA-256 hash of the API key which also makes it easy to look up and verify a given API key.I've added a separate column for the hashed API key rather than reusing the existing column. This allows us to batch-hash all existing plaintext keys without having to differentiate between keys that have already been hashed and those that haven't. Once all existing plaintext API keys have been hashed, the old
api_key
column can simply be dropped.This is a breaking change. After deployment, admins need to run the
aleph hash-plaintext-api-keys
CLI command to hash legacy plaintext API keys. Alternatively, users can regenerate their API key.