Skip to content

Conversation

@dereuromark
Copy link
Member

Summary

PrimaryKeySessionAuthenticator now provides a default TokenIdentifier that works out of the box, eliminating the need for explicit configuration in most cases.

The Problem

Currently, using PrimaryKeySessionAuthenticator requires understanding the internal field mapping between the authenticator and identifier:

$service->loadAuthenticator('Authentication.PrimaryKeySession', [
    'identifier' => [
        'className' => 'Authentication.Token',
        'tokenField' => 'id',
        'dataField' => 'key',
    ],
]);

The field names don't align intuitively:

  • identifierKey (authenticator) ↔ dataField (identifier)
  • idField (authenticator) ↔ tokenField (identifier)

The Solution (Option 1 - Implemented)

Override getIdentifier() to lazily create a default TokenIdentifier with matching configuration:

// Now works without any configuration!
$service->loadAuthenticator('Authentication.PrimaryKeySession');

Custom configuration is still fully supported when needed.

Alternative Approaches Considered

Option 2: Align the naming

Rename the config keys to use consistent terminology across authenticator and identifier. This would improve discoverability but still require explicit configuration.

Option 3: Change defaults on PrimaryKeySession side

Change identifierKey default from 'key' to 'id', reducing the configuration needed. However, this still requires understanding which fields to configure.

Why Option 1?

  • Zero configuration for the common case (looking up users by id)
  • Follows the existing pattern in SessionAuthenticator (which provides a default PasswordIdentifier)
  • Custom identifiers still work exactly as before
  • The idField and identifierKey config options automatically propagate to the default identifier

Test Plan

  • Existing tests still pass
  • New test: testAuthenticateSuccessWithDefaultIdentifier - verifies authentication works without explicit identifier
  • New test: testGetIdentifierReturnsDefaultWhenNotConfigured - verifies default TokenIdentifier is created
  • New test: testGetIdentifierUsesCustomConfig - verifies custom idField/identifierKey propagates correctly

PrimaryKeySessionAuthenticator now works out of the box without
requiring explicit identifier configuration. When no identifier is
provided, it lazily creates a TokenIdentifier configured to look up
users by their `id` field.

Before:
```php
$service->loadAuthenticator('Authentication.PrimaryKeySession', [
    'identifier' => [
        'className' => 'Authentication.Token',
        'tokenField' => 'id',
        'dataField' => 'key',
    ],
]);
```

After:
```php
$service->loadAuthenticator('Authentication.PrimaryKeySession');
```

Custom configuration is still supported by passing an explicit
identifier or by using the `idField` and `identifierKey` config
options which propagate to the default TokenIdentifier.
@dereuromark dereuromark requested a review from markstory January 14, 2026 17:23
@dereuromark dereuromark added this to the 4.x milestone Jan 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants