Skip to content

Commit

Permalink
Added commands to generate and delete API keys
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbjr committed Mar 27, 2015
1 parent f4f9966 commit e784147
Show file tree
Hide file tree
Showing 3 changed files with 210 additions and 0 deletions.
89 changes: 89 additions & 0 deletions src/Console/Commands/DeleteApiKeyCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php namespace Chrisbjr\ApiGuard\Console\Commands;

use App;
use Chrisbjr\ApiGuard\Models\ApiKey;
use Config;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;

class DeleteApiKeyCommand extends Command
{

/**
* The console command name.
*
* @var string
*/
protected $name = 'api-key:delete';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Revoke/delete an API key';

/**
* Create a new command instance.
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
$key = $this->option('api-key');

if ( ! is_null($key)) {
// we delete a specific API key
$confirmation = $this->ask("Are you sure you want to delete this API key? [y/n]");

if ($confirmation == 'y') {
$apiKeyModel = App::make(Config::get('apiguard.model', 'Chrisbjr\ApiGuard\Models\ApiKey'));
$apiKey = $apiKeyModel->where('key', '=', $key)->first();

if (empty($apiKey) || $apiKey->exists == false) {
$this->info("The API key you specified does not exist.");
return;
}

$this->info("The API key {$key} was deleted.");

return;
}

return;
}

$this->error("Specify an API key to delete using the --api-key option. Example: --api-key=xxxxxxxxx");
}

/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return array();
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return array(
array('api-key', null, InputOption::VALUE_REQUIRED, 'The API key to delete.', null),
);
}

}
112 changes: 112 additions & 0 deletions src/Console/Commands/GenerateApiKeyCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php namespace Chrisbjr\ApiGuard\Console\Commands;

use App;
use Chrisbjr\ApiGuard\Models\ApiKey;
use Config;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;

class GenerateApiKeyCommand extends Command
{

/**
* The console command name.
*
* @var string
*/
protected $name = 'api-key:generate';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate an API key';

/**
* Create a new command instance.
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
$userId = $this->getOption('user-id', 0);

if ( ! empty($userId)) {

// check whether this user already has an API key
$apiKeyModel = App::make(Config::get('apiguard.model', 'Chrisbjr\ApiGuard\Models\ApiKey'));

$apiKey = $apiKeyModel->where('user_id', '=', $userId)->first();

if ( ! empty($apiKey) || $apiKey->exists) {
$overwrite = $this->ask("This user already has an existing API key. Do you want to overwrite it? [y/n]");
if ($overwrite == 'n') {
return;
}
}
}

$apiKey = App::make(Config::get('apiguard.model', 'Chrisbjr\ApiGuard\Models\ApiKey'));
$apiKey->key = $apiKey->generateKey();
$apiKey->user_id = $this->getOption('user-id', 0);
$apiKey->level = $this->getOption('level', 10);
$apiKey->ignore_limits = $this->getOption('ignore-limits', 1);

if ($apiKey->save() === false) {
$this->error("Failed to save API key to the database.");
return;
}

if (empty($apiKey->user_id)) {
$this->info("You have successfully generated an API key:");
} else {
$this->info("You have successfully generated an API key for user ID#{$apiKey->user_id}:");
}
$this->info($apiKey->key);
}

/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return array();
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return array(
array('user-id', null, InputOption::VALUE_OPTIONAL, 'Link the generated API key to a user ID', null),
array('level', null, InputOption::VALUE_OPTIONAL, 'Permission level of the generated API key', null),
array('ignore-limits', null, InputOption::VALUE_OPTIONAL, 'Specify whether this API key will ignore limits or not', null),
);
}

protected function getOption($name, $defaultValue)
{
$var = $this->option($name);

if (is_null($var)) {
return $defaultValue;
}

return $var;
}

}
9 changes: 9 additions & 0 deletions src/Providers/ApiGuardServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,19 @@ public function boot()
{
$this->app->register('EllipseSynergie\ApiResponse\Laravel\ResponseServiceProvider');

$this->commands([
'Chrisbjr\ApiGuard\Console\Commands\GenerateApiKeyCommand',
'Chrisbjr\ApiGuard\Console\Commands\DeleteApiKeyCommand',
]);

// Publish your migrations
$this->publishes([
__DIR__ . '/../../database/migrations/' => base_path('/database/migrations')
], 'migrations');

$this->publishes([
__DIR__ . '/../../config/apiguard.php' => config_path('apiguard.php'),
]);
}

/**
Expand Down

0 comments on commit e784147

Please sign in to comment.