class ApiKeyService (View source)

Service for managing API keys

Methods

PDO
getDb()

Get the database connection for debugging

__construct(ApiKeyRepositoryInterface $apiKeyRepository, PDO $db, ConfigurationManager $config, MessageService $messageService, LoggerInterface|null $logger = null, UserContextService|null $userContextService = 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

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

Create a new API key

ApiKey|null
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

bool
deleteApiKey(int $id)

Delete an API key

ApiKey|null
regenerateSecretKey(int $id)

Regenerate the secret key for an API key

ApiKey|null
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).

Details

PDO getDb()

Get the database connection for debugging

Return Value

PDO

__construct(ApiKeyRepositoryInterface $apiKeyRepository, PDO $db, ConfigurationManager $config, MessageService $messageService, LoggerInterface|null $logger = null, UserContextService|null $userContextService = null)

ApiKeyService constructor

Parameters

ApiKeyRepositoryInterface $apiKeyRepository

The API key repository

PDO $db

The database connection

ConfigurationManager $config

The configuration manager

MessageService $messageService

The message service

LoggerInterface|null $logger
UserContextService|null $userContextService

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

ApiKey|null 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

ApiKey|null

The created API key, or null if creation failed

ApiKey|null 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

ApiKey|null

The updated API key, or null if update failed

bool deleteApiKey(int $id)

Delete an API key

Parameters

int $id

The ID of the API key to delete

Return Value

bool

True if the API key was deleted, false otherwise

ApiKey|null regenerateSecretKey(int $id)

Regenerate the secret key for an API key

Parameters

int $id

The ID of the API key

Return Value

ApiKey|null

The updated API key with new secret, or null if regeneration failed

ApiKey|null toggleApiKey(int $id, bool $disabled)

Toggle the disabled status of an API key

Parameters

int $id

The ID of the API key

bool $disabled

The new disabled status

Return Value

ApiKey|null

The updated API key, or null if update failed

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