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') { Log::alert('API Authentication Success', [ 'discriminator' => $discriminator ]); $keyRecord->last_used = Carbon::now(); $keyRecord->save(); 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(); } }