class ApiKeyService (View source)

Creates, regenerates, disables and deletes API keys and enforces the per-user key limit.

Methods

__construct(ApiKeyRepositoryInterface $apiKeyRepository, UserLookupInterface $users, ConfigurationInterface $config, PermissionService $permissionService, ActorInterface $actor, LoggerInterface|null $logger = null)

ApiKeyService constructor

array
getAllApiKeys()

Get all API keys the current user has access to with creator usernames

ApiKey|null
getApiKey(int $id)

Get a specific API key if the current user has access to it

createApiKey(string $name, DateTime|null $expiresAt = null, bool $isReadonly = false, array|null $allowedOperations = null, array $zoneIds = [])

Create a new API key

updateApiKey(int $id, string $name, DateTime|null $expiresAt = null, bool $disabled = false, bool $isReadonly = false, array|null $allowedOperations = null, array|null $zoneIds = null)

Update an existing API key

deleteApiKey(int $id)

Delete an API key

regenerateSecretKey(int $id)

Regenerate the secret key for an API key

toggleApiKey(int $id, bool $disabled)

Toggle the disabled status of an API key

bool
authenticate(string $secretKey)

Authenticate a request using an API key

int
getUserIdFromApiKey(string $secretKey)

Get user ID from API key without setting session (stateless).

int|null
getIdFromApiKey(string $secretKey)

Get the api_keys row id from a raw secret key without setting session (stateless).

getScopeFromApiKey(string $secretKey)

Resolve the permission scope of an API key (stateless, no session writes).

bool
zoneScopesReady()

Whether zone restrictions can be shown and changed; false until the one-time scope move has run (see ApiKeyRepositoryInterface::zoneScopesReady()).

static bool
hasUnmappedZoneScope(ApiKey $apiKey)

Whether the key keeps a zone restriction the zone id upgrade could not map; it is stored as zone id 0, which matches no zone, and the edit form cannot show it as selected.

Details

__construct(ApiKeyRepositoryInterface $apiKeyRepository, UserLookupInterface $users, ConfigurationInterface $config, PermissionService $permissionService, ActorInterface $actor, LoggerInterface|null $logger = null)

ApiKeyService constructor

Parameters

ApiKeyRepositoryInterface $apiKeyRepository

The API key repository

UserLookupInterface $users

Resolves key creators and owners

ConfigurationInterface $config

The configuration manager

PermissionService $permissionService

Decides who may see and manage other users' keys

ActorInterface $actor

The user the owner checks are about

LoggerInterface|null $logger

array getAllApiKeys()

Get all API keys the current user has access to with creator usernames

Return Value

array

Array of API keys with creator usernames

ApiKey|null getApiKey(int $id)

Get a specific API key if the current user has access to it

Parameters

int $id

The ID of the API key

Return Value

ApiKey|null

The API key, or null if not found

ApiKeyWriteResult createApiKey(string $name, DateTime|null $expiresAt = null, bool $isReadonly = false, array|null $allowedOperations = null, array $zoneIds = [])

Create a new API key

Parameters

string $name

The name of the API key

DateTime|null $expiresAt

Optional expiration date

bool $isReadonly

Whether the key is restricted to read-only requests

array|null $allowedOperations

Operations the key may perform; null/empty means all

array $zoneIds

Zones the key is restricted to; empty means no restriction

Return Value

ApiKeyWriteResult

ApiKeyWriteResult updateApiKey(int $id, string $name, DateTime|null $expiresAt = null, bool $disabled = false, bool $isReadonly = false, array|null $allowedOperations = null, array|null $zoneIds = null)

Update an existing API key

Parameters

int $id

The ID of the API key to update

string $name

The new name for the API key

DateTime|null $expiresAt

The new expiration date

bool $disabled

Whether the API key should be disabled

bool $isReadonly

Whether the key is restricted to read-only requests

array|null $allowedOperations

Operations the key may perform; null/empty means all

array|null $zoneIds

Zones the key is restricted to; null leaves them unchanged, [] clears

Return Value

ApiKeyWriteResult

ApiKeyWriteResult deleteApiKey(int $id)

Delete an API key

Parameters

int $id

Return Value

ApiKeyWriteResult

ApiKeyWriteResult regenerateSecretKey(int $id)

Regenerate the secret key for an API key

Parameters

int $id

Return Value

ApiKeyWriteResult

ApiKeyWriteResult toggleApiKey(int $id, bool $disabled)

Toggle the disabled status of an API key

Parameters

int $id
bool $disabled

Return Value

ApiKeyWriteResult

bool authenticate(string $secretKey)

Authenticate a request using an API key

Parameters

string $secretKey

The secret key from the request

Return Value

bool

True if authentication succeeded, false otherwise

int getUserIdFromApiKey(string $secretKey)

Get user ID from API key without setting session (stateless).

Parameters

string $secretKey

Return Value

int

int|null getIdFromApiKey(string $secretKey)

Get the api_keys row id from a raw secret key without setting session (stateless).

Used for audit logging so a request can be traced back to a specific key.

Parameters

string $secretKey

Return Value

int|null

ApiKeyScope|null getScopeFromApiKey(string $secretKey)

Resolve the permission scope of an API key (stateless, no session writes).

Returns null when the key is missing/invalid or the API is disabled, in which case the caller should fall back to an unrestricted scope (the key is already rejected by authentication). A key with no restrictions yields a scope where every zone and operation is allowed.

Parameters

string $secretKey

The raw secret key from the request

Return Value

ApiKeyScope|null

The resolved scope, or null if it cannot be resolved

bool zoneScopesReady()

Whether zone restrictions can be shown and changed; false until the one-time scope move has run (see ApiKeyRepositoryInterface::zoneScopesReady()).

Return Value

bool

static bool hasUnmappedZoneScope(ApiKey $apiKey)

Whether the key keeps a zone restriction the zone id upgrade could not map; it is stored as zone id 0, which matches no zone, and the edit form cannot show it as selected.

Parameters

ApiKey $apiKey

Return Value

bool