diff --git a/config/laravelApiKey.php b/config/laravelApiKey.php new file mode 100644 index 0000000..b79ec69 --- /dev/null +++ b/config/laravelApiKey.php @@ -0,0 +1,9 @@ + true, + 'enable_access_events' => true, + + // The number of characters for the generation of the Api Key + 'api_key_length' => 64, +]; \ No newline at end of file diff --git a/readme.md b/readme.md index d18ef8a..04cc4c1 100644 --- a/readme.md +++ b/readme.md @@ -18,7 +18,7 @@ In your `config/app.php` file, add the Laravel API Key service provider to the e ], ``` -Publish the migration files +Publish the migration & config files $ php artisan vendor:publish @@ -32,6 +32,18 @@ Run the migrations * api_key_access_events * api_key_admin_events +And config file config/laravelApiKey.php +```php +[ + // if true the admin data will be saved in the database + 'enable_admin_events' + // if true all data request will be saved in the database + 'enable_access_events' + // The number of characters for the generation of the Api Key + 'api_key_length' +], +``` + ## Managing Keys Generate a new key using `php artisan apikey:generate {name}`. The name argument is the name of your API key. All new keys are active by default. diff --git a/src/Http/Middleware/AuthorizeApiKey.php b/src/Http/Middleware/AuthorizeApiKey.php index f428183..b739f0a 100644 --- a/src/Http/Middleware/AuthorizeApiKey.php +++ b/src/Http/Middleware/AuthorizeApiKey.php @@ -24,7 +24,9 @@ public function handle(Request $request, Closure $next) $apiKey = ApiKey::getByKey($header); if ($apiKey instanceof ApiKey) { - $this->logAccessEvent($request, $apiKey); + if (config('laravelApiKey.enable_access_events') === true){ + $this->logAccessEvent($request, $apiKey); + } return $next($request); } diff --git a/src/Models/ApiKey.php b/src/Models/ApiKey.php index 3558251..8d894c5 100644 --- a/src/Models/ApiKey.php +++ b/src/Models/ApiKey.php @@ -47,25 +47,29 @@ public static function boot() parent::boot(); static::created(function(ApiKey $apiKey) { - self::logApiKeyAdminEvent($apiKey, self::EVENT_NAME_CREATED); + if (config('laravelApiKey.enable_admin_events') === true){ + self::logApiKeyAdminEvent($apiKey, self::EVENT_NAME_CREATED); + } }); static::updated(function($apiKey) { $changed = $apiKey->getDirty(); - - if (isset($changed) && $changed['active'] === 1) { - self::logApiKeyAdminEvent($apiKey, self::EVENT_NAME_ACTIVATED); - } - - if (isset($changed) && $changed['active'] === 0) { - self::logApiKeyAdminEvent($apiKey, self::EVENT_NAME_DEACTIVATED); + if (config('laravelApiKey.enable_admin_events') === true) { + if (isset($changed) && $changed['active'] === 1) { + self::logApiKeyAdminEvent($apiKey, self::EVENT_NAME_ACTIVATED); + } + + if (isset($changed) && $changed['active'] === 0) { + self::logApiKeyAdminEvent($apiKey, self::EVENT_NAME_DEACTIVATED); + } } - }); static::deleted(function($apiKey) { - self::logApiKeyAdminEvent($apiKey, self::EVENT_NAME_DELETED); + if (config('laravelApiKey.enable_admin_events') === true) { + self::logApiKeyAdminEvent($apiKey, self::EVENT_NAME_DELETED); + } }); } @@ -76,8 +80,9 @@ public static function boot() */ public static function generate() { + $length = config('laravelApiKey.api_key_length', 64); do { - $key = Str::random(64); + $key = Str::random($length); } while (self::keyExists($key)); return $key; diff --git a/src/Providers/ApiKeyServiceProvider.php b/src/Providers/ApiKeyServiceProvider.php index 09e25a9..8f09a3e 100644 --- a/src/Providers/ApiKeyServiceProvider.php +++ b/src/Providers/ApiKeyServiceProvider.php @@ -23,6 +23,9 @@ public function boot(Router $router) { $this->registerMiddleware($router); $this->registerMigrations(__DIR__ . '/../../database/migrations'); + $this->publishes([ + __DIR__.'/../../config/laravelApiKey.php' => config_path('laravelApiKey.php'), + ], 'apikey-config'); } /** @@ -38,6 +41,10 @@ public function register() { GenerateApiKey::class, ListApiKeys::class, ]); + + $this->mergeConfigFrom( + __DIR__.'/../../config/laravelApiKey.php', 'laravelApiKey' + ); } /**