diff --git a/README.md b/README.md index 0b0c948..546ce76 100755 --- a/README.md +++ b/README.md @@ -16,8 +16,14 @@ composer require yabhq/flightdeck ## Usage +Generate new API key for authorization ``` php -php artisan vendor:publish +php artisan flightdeck:generate app1 +``` + +List all available API keys +``` php +php artisan flightdeck:list ``` ### Testing @@ -36,11 +42,12 @@ Please see [CONTRIBUTING](CONTRIBUTING.md) for details. ### Security -If you discover any security related issues, please email chris@yabhq.com instead of using the issue tracker. +If you discover any security related issues, please email us instead of using the issue tracker. ## Credits -- [Chris Blackwell](https://github.com/yabhq) +- [Chris Blackwell](https://github.com/chrisblackwell) +- [Jim Hlad](https://github.com/jimhlad) - [All Contributors](../../contributors) ## License diff --git a/src/Commands/GenerateToken.php b/src/Commands/GenerateToken.php index d60fa9e..2b9d912 100644 --- a/src/Commands/GenerateToken.php +++ b/src/Commands/GenerateToken.php @@ -12,7 +12,7 @@ class GenerateToken extends Command * * @var string */ - protected $signature = 'flightdeck:generate {expires_at?}'; + protected $signature = 'flightdeck:generate {name} {expires_at?}'; /** * The console command description. @@ -21,16 +21,6 @@ class GenerateToken extends Command */ protected $description = 'Generate a fresh API token for authorization requests'; - /** - * Create a new command instance. - * - * @return void - */ - public function __construct() - { - parent::__construct(); - } - /** * Execute the console command. * @@ -38,7 +28,7 @@ public function __construct() */ public function handle() { - $token = FlightDeck::generate($this->option('expires_at')); + $token = FlightDeck::generate($this->argument('name'), $this->argument('expires_at')); $this->info($token); } } diff --git a/src/Commands/ListTokens.php b/src/Commands/ListTokens.php new file mode 100644 index 0000000..79f3eb6 --- /dev/null +++ b/src/Commands/ListTokens.php @@ -0,0 +1,47 @@ +get(); + if ($tokens->count() === 0) { + $this->info('There are no API keys'); + return; + } + + $headers = ['Name', 'Token', 'Expires At']; + $rows = $tokens->map(function ($token) { + return [ + $token->name, + $token->token, + $token->expires_at, + ]; + }); + $this->table($headers, $rows); + } +} diff --git a/src/FlightDeck.php b/src/FlightDeck.php index 0a48d8c..80025fe 100755 --- a/src/FlightDeck.php +++ b/src/FlightDeck.php @@ -9,14 +9,16 @@ class FlightDeck /** * Generate an authorization token * + * @param string $name * @param string $expires_at * @param integer $length * @return string */ - public static function generate(string $expires_at = null, int $length = 60) : string + public static function generate(string $name, string $expires_at = null, int $length = 60) : string { $token = str_random($length); DB::table('api_tokens')->insert([ + 'name' => $name, 'token' => $token, 'expires_at' => $expires_at ?? now()->addDays(config('flightdeck.tokens.expire_days')), ]); diff --git a/src/FlightDeckServiceProvider.php b/src/FlightDeckServiceProvider.php index 6c7efe6..eba8c3d 100755 --- a/src/FlightDeckServiceProvider.php +++ b/src/FlightDeckServiceProvider.php @@ -2,6 +2,7 @@ namespace Yab\FlightDeck; +use Yab\FlightDeck\Commands\ListTokens; use Illuminate\Support\ServiceProvider; use Yab\FlightDeck\Commands\GenerateToken; use Yab\FlightDeck\Http\Middleware\Authorization; @@ -47,6 +48,7 @@ public function register() $this->commands([ GenerateToken::class, + ListTokens::class, ]); } } diff --git a/src/database/migrations/2019_04_05_000000_create_api_tokens_table.php b/src/database/migrations/2019_04_05_000000_create_api_tokens_table.php index 64cce91..3beccb7 100644 --- a/src/database/migrations/2019_04_05_000000_create_api_tokens_table.php +++ b/src/database/migrations/2019_04_05_000000_create_api_tokens_table.php @@ -14,6 +14,7 @@ class CreateApiTokensTable extends Migration public function up() { Schema::create('api_tokens', function (Blueprint $table) { + $table->string('name'); $table->string('token', 60)->unique()->nullable()->default(null); $table->timestamp('expires_at')->default( now()->addDays(config('flightdeck.tokens.expire_days'))->toDateTimeString() diff --git a/tests/AuthorizationTest.php b/tests/AuthorizationTest.php index ce281cb..2ab424d 100644 --- a/tests/AuthorizationTest.php +++ b/tests/AuthorizationTest.php @@ -10,7 +10,7 @@ class AuthorizationTest extends TestCase /** @test */ public function valid_authorization_token_allows_request() { - $token = FlightDeck::generate(); + $token = FlightDeck::generate('app1'); Route::get('authorization-test', function () { return response()->json([ 'data' => 'this was a success', @@ -42,7 +42,7 @@ public function authorization_token_must_be_provided_on_routes() /** @test */ public function expired_token_is_unauthorized() { - $token = FlightDeck::generate(now()->subDays(2)->toDateTimeString()); + $token = FlightDeck::generate('app2', now()->subDays(2)->toDateTimeString()); Route::get('authorization-test', function () { return response()->json([ 'data' => 'this was a success', diff --git a/tests/Console/GenerateTokenCommandTest.php b/tests/Console/GenerateTokenCommandTest.php new file mode 100644 index 0000000..a7e0c85 --- /dev/null +++ b/tests/Console/GenerateTokenCommandTest.php @@ -0,0 +1,15 @@ +artisan('flightdeck:generate', ['name' => 'app1']) + ->assertExitCode(0); + } +} diff --git a/tests/ProfileRetrievalTest.php b/tests/UserRetrievalTest.php similarity index 80% rename from tests/ProfileRetrievalTest.php rename to tests/UserRetrievalTest.php index 61626d9..569d0c4 100755 --- a/tests/ProfileRetrievalTest.php +++ b/tests/UserRetrievalTest.php @@ -6,7 +6,7 @@ use Yab\FlightDeck\Models\User; use Yab\FlightDeck\Tests\TestCase; -class ProfileRetrievalTest extends TestCase +class UserRetrievalTest extends TestCase { /** @test */ public function an_authorized_user_can_retrieve_their_profile() @@ -19,10 +19,10 @@ public function an_authorized_user_can_retrieve_their_profile() $response->assertStatus(Response::HTTP_OK); $response->assertJson([ - 'data' => [ - 'name' => $user->name, - 'email' => $user->email, - ] + 'data' => [ + 'name' => $user->name, + 'email' => $user->email, + ] ]); } @@ -34,4 +34,3 @@ public function a_guest_cannot_retrieve_their_profile() $response->assertStatus(Response::HTTP_UNAUTHORIZED); } } -