Skip to content

A secure and extensible token manager for Laravel, designed to store, encrypt, and decrypt tokens or API keys.

License

Notifications You must be signed in to change notification settings

cleaniquecoders/token-vault

Repository files navigation

Laravel Token Vault

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

A secure and extensible token manager for Laravel, designed to store, encrypt, and decrypt tokens or API keys. This is useful when you are building an application that require to store sensitive information.

Installation

You can install the package via composer:

composer require cleaniquecoders/token-vault

You can publish and run the migrations with:

php artisan vendor:publish --tag="token-vault-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="token-vault-config"

Here’s the updated Usage guide for your TokenVault package, incorporating the Provider enum and clarifying token types:

✅ Usage

🧩 Setup Model

To allow a model (e.g. User) to have tokens:

use CleaniqueCoders\TokenVault\Traits\InteractsWithTokenVault;

class User extends Authenticatable
{
    use InteractsWithTokenVault;
}

🔐 Storing a Token

use CleaniqueCoders\TokenVault\Enums\Provider;

$user = User::find(1);

$user->tokens()->create([
    'provider' => Provider::GitHub, // enum usage
    'type' => 'access_token',       // e.g., access_token, refresh_token
    'token' => 'ghp_xxxx',          // will be encrypted automatically
    'meta' => ['note' => 'GitHub Deploy Token'],
    'expires_at' => now()->addDays(30),
]);

🔓 Decrypting a Token (when needed)

$token = $user->tokens()->first();

$plainToken = $token->getDecryptedToken();

⚠️ Only use this when absolutely necessary — avoid exposing raw tokens.

👁️ Token Masking (Safe Display)

$token->getMaskedToken(); // e.g., "ghp_****abcd"

Use this for logs, audit trails, or safe UI display.

📂 Retrieve Tokens by Provider

use CleaniqueCoders\TokenVault\Enums\Provider;

$githubToken = $user->tokens()
    ->where('provider', Provider::GitHub)
    ->latest()
    ->first();

🧹 Cleaning Expired Tokens

$user->tokens()
    ->where('expires_at', '<', now())
    ->delete();

Encryption Drivers (Optional)

To use a custom encryption method:

'token-vault.encryptor' => \App\Drivers\OpenSslEncryptor::class,

And the class need to implements the \CleaniqueCoders\TokenVault\Contracts\Encryptor interface.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

A secure and extensible token manager for Laravel, designed to store, encrypt, and decrypt tokens or API keys.

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages