API
This chapter documents the public API of the nr-vault extension.
VaultService
The main service for interacting with the vault.
- interface VaultServiceInterface
-
- Fully qualified name
-
\Netresearch\
Nr Vault\ Service\ Vault Service Interface
Main interface for vault operations.
- store ( string $identifier, string $secret, array $options = []) : void
-
Store a secret in the vault.
- param string $identifier
-
Unique identifier for the secret.
- param string $secret
-
The secret value to store.
- param array $options
-
Optional configuration (owner, groups, context, expiresAt, metadata).
- retrieve ( string $identifier)
-
Retrieve a secret from the vault.
- param string $identifier
-
The secret identifier.
- returntype
-
string|null
- throws AccessDeniedException
-
If user lacks read permission.
- throws SecretExpiredException
-
If the secret has expired.
- Returns
-
The decrypted secret value or null if not found.
- exists ( string $identifier) : bool
-
Check if a secret exists.
- param string $identifier
-
The secret identifier.
- Returns
-
True if the secret exists.
- delete ( string $identifier, string $reason = '') : void
-
Delete a secret from the vault.
- param string $identifier
-
The secret identifier.
- param string $reason
-
Optional reason for deletion (logged).
- throws SecretNotFoundException
-
If secret doesn't exist.
- throws AccessDeniedException
-
If user lacks delete permission.
- rotate ( string $identifier, string $newSecret, string $reason = '') : void
-
Rotate a secret with a new value.
- param string $identifier
-
The secret identifier.
- param string $newSecret
-
The new secret value.
- param string $reason
-
Optional reason for rotation (logged).
- list ( string $pattern = null) : array
-
List accessible secrets.
- param string|null $pattern
-
Optional pattern to filter identifiers.
- Returns
-
Array of secret metadata.
Usage examples
Storing a secret
use Netresearch\NrVault\Service\VaultServiceInterface;
class MyService
{
public function __construct(
private readonly VaultServiceInterface $vault,
) {}
public function storeApiKey(string $apiKey): void
{
$this->vault->store(
'my_extension_api_key',
$apiKey,
[
'description' => 'API key for external service',
'groups' => [1, 2], // Admin, Editor groups
'context' => 'payment',
'expiresAt' => time() + 86400 * 90, // 90 days
]
);
}
}
Retrieving a secret
public function getApiKey(): ?string
{
return $this->vault->retrieve('my_extension_api_key');
}
Vault HTTP client
The vault provides a PSR-18 compatible HTTP client that can inject secrets
into requests without exposing them to your code. Configure authentication
with
with, then use standard
send.
Direct injection (recommended)
use GuzzleHttp\Psr7\Request;
use Netresearch\NrVault\Http\SecretPlacement;
use Netresearch\NrVault\Http\VaultHttpClientInterface;
final class ExternalApiService
{
public function __construct(
private readonly VaultHttpClientInterface $httpClient,
) {}
public function fetchData(): array
{
// Configure authentication, then use standard PSR-18
$client = $this->httpClient->withAuthentication(
'api_token',
SecretPlacement::Bearer,
);
$request = new Request('GET', 'https://api.example.com/data');
$response = $client->sendRequest($request);
return json_decode($response->getBody()->getContents(), true);
}
}
Via VaultService
use GuzzleHttp\Psr7\Request;
use Netresearch\NrVault\Http\SecretPlacement;
$client = $this->vaultService->http()
->withAuthentication('stripe_api_key', SecretPlacement::Bearer);
$request = new Request(
'POST',
'https://api.stripe.com/v1/charges',
['Content-Type' => 'application/json'],
json_encode($payload),
);
$response = $client->sendRequest($request);
- interface VaultHttpClientInterface
-
- Fully qualified name
-
\Netresearch\
Nr Vault\ Service\ Vault Http Client Interface
PSR-18 compatible HTTP client with vault-based authentication. Extends
\Psr\.Http\ Client\ Client Interface - withAuthentication ( string $secretIdentifier, SecretPlacement $placement = SecretPlacement::Bearer, array $options = []) : static
-
Create a new client instance configured with authentication. Returns an immutable instance - the original is unchanged.
- param string $secretIdentifier
-
Vault identifier for the secret.
- param SecretPlacement $placement
-
How to inject the secret.
- param array $options
-
Additional options (headerName, queryParam, usernameSecret, reason).
- Returns
-
New client instance with authentication configured.
- withOAuth ( OAuthConfig $config, string $reason = 'OAuth2 API call') : static
-
Create a new client instance configured with OAuth 2.0 authentication.
- param OAuthConfig $config
-
OAuth configuration.
- param string $reason
-
Audit log reason.
- Returns
-
New client instance with OAuth configured.
Authentication options
The
with method accepts these options:
- headerName
- Custom header name (for
Secret, default:Placement:: Header X-API-Key). - queryParam
- Query parameter name (for
Secret, default:Placement:: Query Param api_key). - bodyField
- Body field name (for
Secret, default:Placement:: Body Field api_key). - usernameSecret
- Separate username secret identifier (for
Secret).Placement:: Basic Auth - reason
- Reason for access (logged in audit).
SecretPlacement enum
- placement
-
Authentication placement using
Secretenum:Placement Secret- Bearer token in Authorization header.Placement:: Bearer Secret- HTTP Basic Authentication.Placement:: Basic Auth Secret- Custom header value.Placement:: Header Secret- Query parameter.Placement:: Query Param Secret- Field in request body.Placement:: Body Field Secret- OAuth 2.0 with automatic token refresh.Placement:: OAuth2 Secret- X-API-Key header (shorthand).Placement:: Api Key
use GuzzleHttp\Psr7\Request;
use Netresearch\NrVault\Http\SecretPlacement;
// Bearer authentication
$client = $this->vault->http()
->withAuthentication('stripe_api_key', SecretPlacement::Bearer);
$response = $client->sendRequest(
new Request('POST', 'https://api.stripe.com/v1/charges', [], $body)
);
// Custom header
$client = $this->vault->http()
->withAuthentication('api_token', SecretPlacement::Header, [
'headerName' => 'X-API-Key',
]);
$response = $client->sendRequest(
new Request('GET', 'https://api.example.com/data')
);
// Basic authentication with separate credentials
$client = $this->vault->http()
->withAuthentication('service_password', SecretPlacement::BasicAuth, [
'usernameSecret' => 'service_username',
'reason' => 'Fetching secure data',
]);
$response = $client->sendRequest(
new Request('GET', 'https://api.example.com/secure')
);
// Query parameter
$client = $this->vault->http()
->withAuthentication('api_key', SecretPlacement::QueryParam, [
'queryParam' => 'key',
]);
$response = $client->sendRequest(
new Request('GET', 'https://maps.example.com/geocode')
);
PSR-14 events
The vault dispatches events during secret operations.
- class SecretCreatedEvent
-
- Fully qualified name
-
\Netresearch\
Nr Vault\ Service\ Secret Created Event
Dispatched when a new secret is created.
get: The secret identifier.Identifier () get: The Secret entity.Secret () get: User ID who created it.Actor Uid ()
- class SecretAccessedEvent
-
- Fully qualified name
-
\Netresearch\
Nr Vault\ Service\ Secret Accessed Event
Dispatched when a secret is read.
get: The secret identifier.Identifier () get: User ID who accessed it.Actor Uid () get: The secret's context.Context ()
- class SecretRotatedEvent
-
- Fully qualified name
-
\Netresearch\
Nr Vault\ Service\ Secret Rotated Event
Dispatched when a secret is rotated.
get: The secret identifier.Identifier () get: The new version number.New Version () get: User ID who rotated it.Actor Uid () get: The rotation reason.Reason ()
- class SecretDeletedEvent
-
- Fully qualified name
-
\Netresearch\
Nr Vault\ Service\ Secret Deleted Event
Dispatched when a secret is deleted.
get: The secret identifier.Identifier () get: User ID who deleted it.Actor Uid () get: The deletion reason.Reason ()
- class SecretUpdatedEvent
-
- Fully qualified name
-
\Netresearch\
Nr Vault\ Service\ Secret Updated Event
Dispatched when a secret value is updated (without rotation).
get: The secret identifier.Identifier () get: The new version number.New Version () get: User ID who updated it.Actor Uid ()
- class MasterKeyRotatedEvent
-
- Fully qualified name
-
\Netresearch\
Nr Vault\ Service\ Master Key Rotated Event
Dispatched after master key rotation completes.
get: Number of secrets re-encrypted.Secrets Re Encrypted () get: User ID who performed the rotation.Actor Uid () get: DateTimeImmutable of when rotation completed.Rotated At ()