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

Command icon 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

tibezh created an issue. See original summary.

tibezh’s picture

Status: Active » Needs review

Merge request is added.

tibezh’s picture

Issue summary: View changes
klausi’s picture

Category: Feature request » Bug report
Status: Needs review » Reviewed & tested by the community

Thanks, phpunit tests are failing but look unrelated to this issue.

Otherwise looks good to me!

tibezh’s picture

Status: Reviewed & tested by the community » Needs review

I'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.

klausi’s picture

Status: Needs review » Reviewed & tested by the community

Looks good, thanks!

solideogloria made their first commit to this issue’s fork.

solideogloria’s picture

Assigned: Unassigned » solideogloria
solideogloria’s picture

Assigned: solideogloria » Unassigned
Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

solideogloria’s picture

solideogloria’s picture

Title: API key lookup uses LIKE instead of = operator, causing performance issues » Query uses LIKE instead of = operator, add index on api_key

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.