public static function generate() { if (!\Auth::check()) { return NULL; } $apiKey = new ApiKey(); $apiKey->user_id = \Auth::user()->id; $apiKey->api_key = \App\Helpers\Text::randomCryptoString(); $apiKey->save(); return $apiKey; }
public function getDeactivateApiKey($key) { $apiKey = \App\Models\ApiKey::find($key); $apiKey->active = 0; $apiKey->save(); return \Redirect::route('settings.apiKeys.list'); }
public function getStatusBoardGraph(\Illuminate\Http\Request $request, $statName) { // Validate API Key $apiKey = \App\Models\ApiKey::where('api_key', $request->get('apiKey'))->where('active', 1)->first(); if (!$apiKey) { return \Response::json(['status' => 'Error', 'error' => 'Invalid API Key.'], 401); } $user = $apiKey->user; $counter = $user->counters()->where('name', $statName)->first(); if (!$counter) { return \Response::json(['status' => 'Error', 'error' => 'Counter not found'], 422); } // Get last 6 months' data $date = \Carbon\Carbon::now()->subMonths(6); $datapoints = []; for ($i = 0; $i < 6; $i++) { if ($i == 5) { $label = 'Now'; } else { $label = '-' . (5 - $i) . ' Mo'; } $query = $counter->beans()->where('created_at', '<=', $date->addMonths(1)); if ($counter->type == \App\Models\Counter::CounterTypeCount) { $value = $query->sum('value'); } else { $value = $query->avg('value'); } $datapoints[] = ['title' => $label, 'value' => 1 * $value ?: 0]; } $jsonData = ['graph' => ['title' => $counter->name, 'type' => 'line', 'datasequences' => [['title' => $counter->name, 'datapoints' => $datapoints]]]]; ob_clean(); return \Response::json($jsonData, 200); }
public function delete($id) { /* @var ApiKey $apikey */ $apikey = ApiKey::find($id); $apikey->delete(); return $apikey; }
public function validateKey($route, $request) { $matches = ApiKey::where('api_key', Request::input('k'))->where('enabled', true)->where('revoked', false)->count(); if ($matches < 1) { return $this->redirectToWiki(); } }
/** * Show the application dashboard. * * @return Response */ public function getCount(\Illuminate\Http\Request $request) { // Validate API Key $apiKey = \App\Models\ApiKey::where('api_key', $request->get('apiKey'))->where('active', 1)->first(); if (!$apiKey) { return \Response::json(['status' => 'Error', 'error' => 'Invalid API Key.'], 401); } // Validate Data if (!$request->has('name')) { return \Response::json(['status' => 'Error', 'error' => 'The name field is required.'], 400); } if (!$request->has('count') && !$request->has('value')) { return \Response::json(['status' => 'Error', 'error' => 'Either the count or value field is required.'], 400); } $user = $apiKey->user; $counter = $user->counters()->where('name', $request->get('name'))->first(); if (!$counter) { $counter = \App\Models\Counter::create(['user_id' => $user->id, 'name' => $request->get('name'), 'type' => $request->has('count') ? \App\Models\Counter::CounterTypeCount : \App\Models\Counter::CounterTypeValue]); } else { if ($counter->type == \App\Models\Counter::CounterTypeCount && !$request->has('count')) { return \Response::json(['status' => 'Error', 'error' => 'Stat "' . $request->get('name') . '" already exists and is a counter stat, but the "count" field was not included.'], 422); } if ($counter->type == \App\Models\Counter::CounterTypeValue && !$request->has('value')) { return \Response::json(['status' => 'Error', 'error' => 'Stat "' . $request->get('name') . '" already exists and is a value stat, but the "value" field was not included.'], 422); } } $bean = \App\Models\Bean::create(['counter_id' => $counter->id, 'value' => $counter->type == \App\Models\Counter::CounterTypeCount ? $request->get('count') : $request->get('value')]); if ($request->has('timestamp')) { $bean->created_at = \Carbon\Carbon::createFromTimestampUTC($request->get('timestamp')); $bean->save(); } return \Response::json(['status' => 'Success'], 200); }
/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { if (env('APP_ENV') === 'testing') { return $next($request); } $authenticated = false; if (!isset($_SERVER['HTTP_X_SITE_API_KEY'])) { return response()->json(array('error' => 'This resource requires an API key.'), 403); } $key = $_SERVER['HTTP_X_SITE_API_KEY']; if ($key) { $key = ApiKey::where('key', '=', $key)->first(); if ($key) { session(['api_key' => $key->key]); $authenticated = true; } } if (!$authenticated) { return response()->json(array('error' => 'Invalid API Key'), 403); } return $next($request); }
public function run() { ApiKey::create(['key' => 'c1049812-6e62-11e5-9d70-feff819cdc9f', 'name' => 'Web App']); }