codybuell/laravault-auth
Laravel 5.4 auth provider that authenticates users against Hashicorp Vault. Stores user info in the Laravel session, tracks Vault TTL, and ends the Laravel session when the Vault token expires. Configurable as an auth driver via config/auth.php.
Install the Package
composer require codybuell/laravault-auth
Publish the config file:
php artisan vendor:publish --provider="CodyBuell\LaravaultAuth\LaravaultAuthServiceProvider" --tag="config"
Configure Vault Connection
Edit config/laravault-auth.php with your Vault server URL, token, or auth method (e.g., AppRole, AWS IAM, or Kubernetes auth):
'vault' => [
'url' => env('VAULT_ADDR', 'https://vault.example.com'),
'token' => env('VAULT_TOKEN'),
// OR use auth method (e.g., 'approle', 'aws', 'kubernetes')
'auth' => [
'method' => 'approle',
'role_id' => env('VAULT_APPROLE_ROLE_ID'),
'secret_id' => env('VAULT_APPROLE_SECRET_ID'),
],
],
First Use Case: Fetch a Secret Retrieve a secret from Vault (e.g., database credentials):
use CodyBuell\LaravaultAuth\Facades\LaravaultAuth;
$dbConfig = LaravaultAuth::get('secret/data/db/config');
// Returns decrypted data (e.g., ['username' => '...', 'password' => '...'])
refreshToken() method to rotate tokens periodically (e.g., in a scheduled job):
LaravaultAuth::refreshToken(); // Updates the current token via auth method
token, approle, aws, or kubernetes in config without code changes.$secret = LaravaultAuth::get('secret/data/app/config');
getVersioned() to retrieve a specific version:
$secret = LaravaultAuth::getVersioned('secret/data/app/config', '1');
$keys = LaravaultAuth::list('secret/data/app/');
write capability):
LaravaultAuth::set('secret/data/app/config', ['key' => 'value']);
LaravaultAuth::delete('secret/data/app/config');
$this->app->bind('vault.db', function () {
return LaravaultAuth::get('secret/data/db/config');
});
.env (use laravel/env-vault or custom logic):
$envVars = LaravaultAuth::get('secret/data/env');
putenv("DB_PASSWORD={$envVars['password']}");
try-catch for Vault failures:
try {
$secret = LaravaultAuth::get('secret/data/nonexistent');
} catch (\CodyBuell\LaravaultAuth\Exceptions\VaultException $e) {
log::error("Vault fetch failed: " . $e->getMessage());
// Fallback to local config
}
refreshToken() in long-running processes or implement a middleware to refresh tokens on demand.
// Middleware example:
public function handle($request, Closure $next) {
LaravaultAuth::refreshToken();
return $next($request);
}
read/write on paths)./data/ in paths (e.g., secret/data/app/config), while v1 omits it. Double-check your Vault setup.use Illuminate\Support\Facades\Retry;
Retry::retry(3, function () {
LaravaultAuth::get('secret/data/app/config');
});
debug to true in config to log raw Vault responses:
'debug' => env('VAULT_DEBUG', false),
404 for missing secrets and 403 for permission issues. Log the full response for debugging:
try {
LaravaultAuth::get('secret/data/app/config');
} catch (\CodyBuell\LaravaultAuth\Exceptions\VaultException $e) {
\Log::debug($e->getResponse()->getBody());
}
AuthMethod interface to support additional auth backends (e.g., LDAP, JWT).transform() method to decrypt or modify secrets before use:
$secret = LaravaultAuth::get('secret/data/app/config')->transform(function ($data) {
return base64_decode($data['encrypted_value']);
});
$cacheKey = 'vault:db:config';
$dbConfig = cache()->remember($cacheKey, now()->addHours(1), function () {
return LaravaultAuth::get('secret/data/db/config');
});
LaravaultAuth::listen('secret.read', function ($path, $data) {
\Log::info("Secret read from $path", $data);
});
How can I help you explore Laravel packages today?