/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { // If environment is local, pass without authenticating. if (env('APP_ENV', 'production') == 'local') { // Use the first API key to mock an API user. $firstKey = ApiKey::first(); if (is_null($firstKey)) { throw new RuntimeException('Even though you are in a testing environment, you must generate an API key.'); } app()->singleton('marius321967\\ApiAuth\\ApiClient', function () use($firstKey) { return new ApiClient($firstKey); }); return $next($request); } try { $key = $this->authenticator->parseKey(); $keyRecord = $this->authenticator->authenticate($key); } catch (\RuntimeException $e) { $key = isset($key) ? $key : null; return $this->unauthHandler->handle($key); } // Bind Client object to be global. app()->singleton('marius321967\\ApiAuth\\ApiClient', function () use($keyRecord) { return new ApiClient($keyRecord); }); return $next($request); }
/** * Execute the console command. * * @return mixed */ public function handle() { $id = $this->option('id'); $key = $this->option('key'); $name = $this->option('name'); if (is_null($id) && is_null($key) && is_null($name)) { $this->error('You must supply either an --id=, --key= or --name='); return; } if (!empty($id)) { $apiKey = ApiKey::find($id); } elseif (!empty($key)) { $apiKey = ApiKey::getByKey($key); } elseif (!empty($name)) { $apiKey = ApiKey::getByName($name); } if (is_null($apiKey)) { $this->error('Key not found.'); return; } $apiKey->delete(); $this->info('API key deleted.'); }
/** * Execute the console command. * * @return mixed */ public function handle() { $keys = ApiKey::all(['id', 'key', 'name', 'created_at']); $this->table(['id' => 'ID', 'key' => 'Key', 'name' => 'Name', 'created_at' => 'Date Created'], $keys); }