Problem
The api_key field is defined as a string field without 'case_sensitive' => TRUE setting. This causes Drupal's Entity Query to use LIKE operator instead of = for exact matches in the getUserByKey() method.
This results in:
- Slow query performance (700+ ms on very large user tables)
- Inability to use database indexes effectively
- Unnecessary case-insensitive matching for an exact-match field
Steps to reproduce
1. Install key_auth module
2. Create a user with API key
3. Monitor MySQL queries during authentication:
-- Current behavior (slow):
WHERE users_field_data.api_key LIKE 'xxx' ESCAPE '\\'
-- Expected behavior (fast):
WHERE users_field_data.api_key = 'xxx'
Here is an Entity SQL Condition class where we have LIKE instead of '=' when we don't have case_sensetive option enabled - https://git.drupalcode.org/project/drupal/-/blob/11.x/core/lib/Drupal/Co...
Proposed solution
Add case_sensitive => TRUE to the field definition in key_auth.module:
$fields['api_key'] = BaseFieldDefinition::create('string')
->setLabel(t('API key'))
->setDescription(t('The API key used for authentication.'))
->addConstraint('UniqueField')
->setSettings([
'max_length' => 255,
'text_processing' => 0,
'case_sensitive' => TRUE, // Add this line
]);
Also, add an update hook in key_auth.install to update existing installations.
Issue fork key_auth-3559783
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
tibezh commentedMerge request is added.
Comment #4
tibezh commentedComment #5
klausiThanks, phpunit tests are failing but look unrelated to this issue.
Otherwise looks good to me!
Comment #6
tibezh commentedI've updated the issue patch and added an index to the api_key column.
The addition improves the module's functionality and speeds it up.
Comment #7
klausiLooks good, thanks!
Comment #9
solideogloria commentedComment #10
solideogloria commentedComment #12
solideogloria commentedComment #13
solideogloria commented