Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a config file for configure data in tables #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions config/laravelApiKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

return [
'enable_admin_events' => true,
'enable_access_events' => true,

// The number of characters for the generation of the Api Key
'api_key_length' => 64,
];
14 changes: 13 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion src/Http/Middleware/AuthorizeApiKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
27 changes: 16 additions & 11 deletions src/Models/ApiKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}

Expand All @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions src/Providers/ApiKeyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand All @@ -38,6 +41,10 @@ public function register() {
GenerateApiKey::class,
ListApiKeys::class,
]);

$this->mergeConfigFrom(
__DIR__.'/../../config/laravelApiKey.php', 'laravelApiKey'
);
}

/**
Expand Down