bearerToken(); if (!is_null($key)) { // we have a valid discriminator $discriminator = Str::before($key, '.'); $loneKey = Str::after($key, '.'); $keyRecord = ApiKey::where('discriminator', $discriminator)->first(); if ($keyRecord && Hash::check($loneKey, $keyRecord->secret) && $keyRecord->status == 'active') { $keyRecord->last_used = Carbon::now(); $keyRecord->save(); Log::info('Recording API call, see context', [ 'uri' => $request->url(), 'name' => Route::currentRouteName(), 'discriminator' => $discriminator, 'ip' => $request->ip() ]); return $next($request); } return JSON::setResponseType('error') ->setStatus('authfail') ->setMessage('Invalid / Revoked API key.') ->setCode(401) ->build(); } return JSON::setResponseType('error') ->setStatus('malformed_key') ->setMessage('Missing or malformed API key.') ->setCode(400) ->build(); } }