guzzleClient = new \Guzzle\Http\Client; } /** * @param $data * @return array|\Guzzle\Http\Message\Response * @throws Exception */ public function attemptLogin($data) { $data = array_merge($this->getClientData() + ["grant_type" => "password", "scope" => "mobile"], $data); $response = $this->guzzleClient->post('http://'.env('API_DOMAIN').'/v1.1/oauth/access_token', [], $data)->send(); if ($this->isValidResponse($response)) { $parsedResponse = $this->parseResponse($response); // Login was successful, check for the "application" param and create a new if it doesn't exist yet $accessToken = AccessToken::whereId($parsedResponse['accessToken'])->first(); $session = Session::whereId($accessToken->session_id)->first(); $ownerId = $session->owner_id; $user = User::where("userId", $ownerId)->first(); if(isset($user)) { if(isset($user) && is_null($user) && $user) { $application = isset($data["application"]) ? $data["application"] : "sipsup"; if (!$user->hasApplication($application)) $user->addApplication($application); } if (Request::has('test')) { if (!$this->validateDeviceProfile($user)) { throw new \Exception('invalid_login_exception', 400); } // Enable push notifications $this->enablePushNotifications($user); } } return $parsedResponse; } return $response; } /** * @return array|\Guzzle\Http\Message\Response */ public function attemptTokenRefresh() { $cookie = Request::has('refresh_token_cookie') ? Request::input('refresh_token_cookie') : $request->cookie('refreshToken'); $scope = Request::has('scope') ? Request::input('scope') : "mobile"; $data = array_merge($this->getClientData() + ["grant_type" => "refresh_token", "scope" => $scope], ["refresh_token" => $cookie]); $response = $this->guzzleClient->post('http://'.env('API_DOMAIN').'/v1.1/oauth/access_token', [], $data)->send(); if ($this->isValidResponse($response)) { $parsedResponse = $this->parseResponse($response); return $parsedResponse; } return $response; } /** * @param $email * @param $password * @param $loginType * @return bool */ public function verifyCredentials($email, $password, $loginType) { if ($loginType == "facebook" and Str::contains($email, "fb:") and Str::contains($password, "fb:")) { $facebookId = explode(":", $email)[1]; $user = User::where('facebookId', $facebookId)->first(); if ($user) return $user->userId; return false; } else if ($loginType == "regular" and is_user_anonymous(['primaryEmail' => $email])) { Auth::extend('anonymousAuth', function($app){ $myProvider = new EloquentUserProvider($app['hash'], AnonymousUser::class); return new Guard($myProvider, $app['session.store']); }); $validate = Auth::driver('anonymousAuth')->attempt([ 'primaryEmail' => $email, 'password' => $password ], false, true); if ($validate) return Auth::driver('anonymousAuth')->id(); return false; } else if ($loginType == "regular") { $validate = Auth::attempt([ 'primaryEmail' => $email, 'password' => $password ], false, true); if ($validate) return Auth::id(); return false; } return false; } /** * @param $response * @return array */ private function parseResponse($response) { return [ 'accessToken' => $response->json()['access_token'], 'accessTokenExpiration' => $response->json()['expires_in'], 'refreshToken' => $response->json()['refresh_token'], 'refreshTokenExpiration' => $response->json()['refresh_token_expires_in'] ]; } /** * @param $response * @return bool */ private function isValidResponse($response) { return isset($response->json()['access_token']); } /** * @return array */ private function getClientData() { $client = Client::whereName('MobileApp')->first(); return ['client_id' => $client->id, 'client_secret' => $client->secret]; } /** * @param $user * @return bool */ private function validateDeviceProfile($user) { if ($user->isAnonymous()) return true; $deviceIdHeader = Request::get('deviceId'); if (isset($deviceIdHeader)) { $deviceProfile = Profile::where('deviceId', $deviceIdHeader)->where('userId', $user->userId)->first(); if ($deviceProfile) { // If device profile exists and active = 0, return false; if ($deviceProfile->active == 0) return false; return true; } // Create device profile and return true $deviceProfile = Profile::create([ 'deviceId' => $deviceIdHeader, 'userId' => $user->userId, 'active' => 1 ]); if (!$deviceProfile) return false; return true; } return false; } /** * @param $user */ private function enablePushNotifications($user) { if ($user->isAnonymous()) return; $pushIdHeader = Request::get('pushId'); $deviceIdHeader = Request::get('deviceId'); if ($pushIdHeader and $deviceIdHeader) { $pushNotificationDevice = PushNotificationDevice::where('pushId', $pushIdHeader)->first(); if ($pushNotificationDevice) { $pushNotificationDevice->active = 1; $pushNotificationDevice->save(); return; } $pushNotificationDevice = PushNotificationDevice::create([ 'pushId' => $pushIdHeader, 'deviceId' => $deviceIdHeader, 'userId' => $user->userId, 'created' => date('Y-m-d H:i:s'), 'active' => 1 ]); return; } return; } }