save([ 'api_key_hash' => hash('sha256', $plain), 'api_key_prefix' => substr($plain, 0, 12), 'api_key_time' => time(), ]); return $plain; } /** * @param Request $request * @return string */ public static function fromRequest(Request $request) { $key = trim((string)$request->header('x-api-key', '')); if ($key === '') { $auth = trim((string)$request->header('authorization', '')); if (stripos($auth, 'bearer ') === 0) { $key = trim(substr($auth, 7)); } } if ($key === '') { $key = trim((string)$request->request('api_key', '')); } return $key; } /** * @param string $plain * @return Factory|null */ public static function findFactory($plain) { $plain = trim((string)$plain); if ($plain === '' || strpos($plain, 'psk_') !== 0) { return null; } return Factory::where('api_key_hash', hash('sha256', $plain)) ->where('status', 'normal') ->find(); } }