final class DbApiKeyRepository implements ApiKeyRepositoryInterface (View source)

SQL persistence for API keys in the api_keys table and their zone scopes in api_key_zones.

Constants

private HASH_PREFIX

Methods

__construct(PDO $db, ConfigurationInterface $config, LoggerInterface|null $logger = null, bool $zoneScopesReady = true, bool $isApiBackend = false)

No description

bool
zoneScopesReady()

Whether stored zone scopes hold the canonical zone ids the checks compare. While they do not, a restricted key matches no zone and new restrictions must not be saved.

array
getAll(int|null $userId = null)

No description

ApiKey|null
findById(int $id)

No description

ApiKey|null
findBySecretKey(string $secretKey)

No description

static string
hashSecretKey(string $secretKey)

One-way hash applied to API keys before persistence. SHA-256 is sufficient because keys are 256+ bits of entropy from random_bytes(32); brute force across the keyspace is infeasible without a salt. The sha256$ prefix makes hashed entries syntactically distinct from any legacy plaintext key so the plaintext-fallback path can refuse pass-the-hash candidates without also rejecting unusual legacy key formats.

save(ApiKey $apiKey)

No description

bool
delete(int $id)

No description

bool
updateLastUsed(int $id)

No description

bool
disable(int $id)

No description

bool
enable(int $id)

No description

int
countByUser(int $userId)

No description

array
getZoneIds(int $apiKeyId)

No description

void
saveZoneIds(int $apiKeyId, array $zoneIds)

No description

Details

__construct(PDO $db, ConfigurationInterface $config, LoggerInterface|null $logger = null, bool $zoneScopesReady = true, bool $isApiBackend = false)

No description

Parameters

PDO $db
ConfigurationInterface $config
LoggerInterface|null $logger
bool $zoneScopesReady

False while the one-time scope migration has not run (see ApiKeyZoneScopeMigration)

bool $isApiBackend

Whether zones are served by the PowerDNS API; under the SQL backend a scoped zone gets a named zones row

bool zoneScopesReady()

Whether stored zone scopes hold the canonical zone ids the checks compare. While they do not, a restricted key matches no zone and new restrictions must not be saved.

Return Value

bool

array getAll(int|null $userId = null)

No description

Parameters

int|null $userId

Optional user ID to filter by creator

Return Value

array

Array of API keys

ApiKey|null findById(int $id)

No description

Parameters

int $id

The ID of the API key to find

Return Value

ApiKey|null

The API key, or null if not found

ApiKey|null findBySecretKey(string $secretKey)

No description

Parameters

string $secretKey

The secret key to find

Return Value

ApiKey|null

The API key, or null if not found

static string hashSecretKey(string $secretKey)

One-way hash applied to API keys before persistence. SHA-256 is sufficient because keys are 256+ bits of entropy from random_bytes(32); brute force across the keyspace is infeasible without a salt. The sha256$ prefix makes hashed entries syntactically distinct from any legacy plaintext key so the plaintext-fallback path can refuse pass-the-hash candidates without also rejecting unusual legacy key formats.

Parameters

string $secretKey

Return Value

string

ApiKey save(ApiKey $apiKey)

No description

Parameters

ApiKey $apiKey

The API key to save

Return Value

ApiKey

The saved API key with ID updated if it was a new key

bool delete(int $id)

No description

Parameters

int $id

The ID of the API key to delete

Return Value

bool

True if the API key was deleted, false otherwise

bool updateLastUsed(int $id)

No description

Parameters

int $id

The ID of the API key to update

Return Value

bool

True if the API key was updated, false otherwise

bool disable(int $id)

No description

Parameters

int $id

The ID of the API key to disable

Return Value

bool

True if the API key was disabled, false otherwise

bool enable(int $id)

No description

Parameters

int $id

The ID of the API key to enable

Return Value

bool

True if the API key was enabled, false otherwise

int countByUser(int $userId)

No description

Parameters

int $userId

The user ID

Return Value

int

Number of API keys

array getZoneIds(int $apiKeyId)

No description

Parameters

int $apiKeyId

The ID of the API key

Return Value

array

The zone IDs (empty array means no restriction)

void saveZoneIds(int $apiKeyId, array $zoneIds)

No description

Parameters

int $apiKeyId

The ID of the API key

array $zoneIds

The zone IDs to store (empty array clears the restriction)

Return Value

void