forked from chrisbjr/api-guard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added commands to generate and delete API keys
- Loading branch information
Showing
3 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters