diff --git a/app/Console/Commands/AuthTokenCommand.php b/app/Console/Commands/AuthTokenCommand.php new file mode 100644 index 0000000..dceff42 --- /dev/null +++ b/app/Console/Commands/AuthTokenCommand.php @@ -0,0 +1,103 @@ +choice('Subcommand', ['list', 'create', 'revoke', 'exit']); + + switch ($option) { + case 'list': + $this->listTokens(); + break; + case 'create': + $this->createToken(); + break; + case 'revoke': + $this->revokeToken(); + break; + case 'exit': + break 2; + } + } + + return 0; + } + + private function listTokens() { + $tokens = AuthToken::all(['name']); + + if (sizeof($tokens) == 0) { + $this->info("No tokens found"); + } else { + $this->table(["Token Name"], $tokens); + } + } + + private function createToken() { + $tokens = AuthToken::all(); + $name = $this->ask('name'); + + if ($tokens->where('name', $name)->first() !== null) { + $this->error('There\'s already a token by that name'); + return; + } + + $tokenString = Str::random(config('chaosapi.token-length', 60)); + AuthToken::create([ + 'name' => $name, + 'token' => $tokenString + ]); + $this->info('Token created'); + } + + private function revokeToken() { + $tokens = AuthToken::all(); + + $name = $this->anticipate('Name?', $tokens->pluck('name')->toArray()); + + $token = $tokens->where('name', $name)->first(); + if ($token === null) { + $this->warn('There\'s no token authorized by that name'); + } else { + $this->info('Token \''.$name.'\' revoked'); + $token->delete(); + } + } +}