/**
  * 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
         $apiKey = ApiKey::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 = new 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);
 }
Example #2
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $email = $this->argument('email');
     if (is_null($email)) {
         $this->error('No e-email address given! Aborting');
         return false;
     }
     while (empty($name)) {
         $name = $this->ask('What is your name?');
     }
     $password = $this->secret('Enter a password');
     $confirm = $this->secret('Confirm the password');
     if ($password !== $confirm) {
         $this->error('The passwords do not match. Aborting');
         return false;
     }
     $admin = $this->confirm('Is this user an admin? [y/N]');
     //Actually create the user
     $user = User::create(['name' => $name, 'email' => $email, 'password' => bcrypt($password), 'admin' => $admin]);
     $apiKey = new ApiKey();
     $apiKey->key = $apiKey->generateKey();
     $apiKey->user_id = $user->id;
     $apiKey->save();
     $this->info("User successfully created with ID {$user->id}");
 }
Example #3
0
 /**
  * Handle an incoming request.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  \Closure  $next
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
     $key = $request->header('token');
     if (empty($key)) {
         return response('Unauthorized.', 401);
     }
     $apiKeyModel = new ApiKey();
     $apiKey = $apiKeyModel->getByKey($key);
     if (empty($apiKey)) {
         return response('Unauthorized.', 401);
     }
     return $next($request);
 }
 /**
  * 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') {
             $apiKey = ApiKey::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");
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     $userId = $this->getOption('user-id', null);
     if (!empty($userId)) {
         // check whether this user already has an API key
         $apiKeyModel = App::make(Config::get('apiguard.models.apiKey', 'Chrisbjr\\ApiGuard\\Models\\ApiKey'));
         $apiKey = $apiKeyModel->where('user_id', '=', $userId)->first();
         if ($apiKey) {
             $overwrite = $this->ask("This user already has an existing API key. Do you want to create another one? [y/n]");
             if ($overwrite == 'n') {
                 return;
             }
         }
     }
     $apiKey = ApiKey::make($this->getOption('user-id', null), $this->getOption('level', 10), $this->getOption('ignore-limits', 1));
     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);
 }
 /**
  * @param User $user
  *
  * @return mixed
  */
 private function createUserApiKey(User $user)
 {
     $apiKey = ApiKey::make($user->id);
     $user->apiKey()->save($apiKey);
 }
Example #7
0
 /**
  * Get the api key that belongs to user
  *
  * @return ApiKey
  */
 public function apiKey()
 {
     $apiKey = ApiKey::where('user_id', '=', $this->getKey())->first();
     if (isset($apiKey)) {
         return $apiKey;
     } else {
         $apiKey = new ApiKey();
         $apiKey->key = $apiKey->generateKey();
         $apiKey->user_id = $this->getKey();
         $apiKey->level = 10;
         $apiKey->ignore_limits = 0;
         //False
         $apiKey->save();
         return $apiKey;
     }
 }
Example #8
0
 protected function authenticated($request, $user)
 {
     $apiKey = \Chrisbjr\ApiGuard\Models\ApiKey::where('user_id', $user->id)->firstOrFail();
     return ['key' => $apiKey->key, 'admin' => $user->admin == '1' ? true : false];
 }
 /**
  * Display api key
  *
  * @return Response
  */
 public function apiKey(Request $request)
 {
     return ApiKey::where('user_id', $request->user()->id)->first();
 }
Example #10
0
 protected function checkAdmin(Request $request)
 {
     $key = $request->header('token');
     if (empty($key)) {
         abort(401, 'Ej, ej, nisi admin!');
     }
     $apiKeyModel = new ApiKey();
     $apiKey = $apiKeyModel->getByKey($key);
     if (empty($apiKey)) {
         abort(401, 'Ej, ej, nisi admin!');
     }
     $user = User::find($apiKey->user_id);
     if (empty($user)) {
         abort(401, 'Ej, ej, nisi admin!');
     }
     if (!$user->admin) {
         abort(401, 'Ej, ej, nisi admin!');
     }
 }
 /**
  * Log the user out
  * 
  * @param  string $apiKey
  * @return \Illuminate\Http\JsonResponse
  */
 public function deauthenticate($apiKey)
 {
     $this->apiKey = ApiKey::where('key', $apiKey)->first();
     if (empty($this->apiKey)) {
         return $this->response->errorUnauthorized("There is no such user to deauthenticate.");
     }
     $this->apiKey->delete();
     return $this->response->withArray(['ok' => ['code' => 'SUCCESSFUL', 'http_code' => 200, 'message' => 'User was successfuly deauthenticated']]);
 }