validateRequest($request); if ($isvalid !== true) { return $isvalid; } try { $token = $this->proxy([ 'username' => $request->get('email'), 'password' => $request->get('password'), 'grantType' => 'password', ]); return $this->respond($token); } catch (\Exception $e) { return $this->respondUnauthorized(); } } /** * Validate the request. * * @param Request $request * @return \Illuminate\Http\JsonResponse|true */ private function validateRequest(Request $request) { $validator = Validator::make($request->all(), [ 'email' => 'email|required', 'password' => 'required', ]); if ($validator->fails()) { return $this->respondValidatorFailed($validator); } // Check if email exists. If not respond with an Unauthorized, this way a hacker // doesn't know if the login email exist or not, or if the password os wrong $count = User::where('email', $request->get('email')) ->count(); if ($count === 0) { return $this->respondUnauthorized(); } return true; } /** * Proxy a request to the OAuth server. * * @param array $data the data to send to the server * @return array */ private function proxy(array $data = []) { $http = new Client(); $response = $http->post(route('passport.token'), [ 'form_params' => [ 'grant_type' => $data['grantType'], 'client_id' => config('monica.mobile_client_id'), 'client_secret' => config('monica.mobile_client_secret'), 'username' => $data['username'], 'password' => $data['password'], 'scope' => '', ], ]); $data = json_decode($response->getBody()); return [ 'access_token' => $data->access_token, 'expires_in' => $data->expires_in, ]; } }