/** * @param array $params * @throws Exception */ public function authenticate(array $params, ViewModel $model) { if (!isset($params['code']) || empty($params['code'])) { throw new Exception('Authentication failed, invalid or empty code.'); } $oAuthConf = Config::$a['oauth']['providers'][$this->authProvider]; $client = new \OAuth2\Client($oAuthConf['clientId'], $oAuthConf['clientSecret']); $client->setAccessTokenType(\OAuth2\Client::ACCESS_TOKEN_OAUTH); $response = $client->getAccessToken('https://api.twitch.tv/kraken/oauth2/token', 'authorization_code', array('redirect_uri' => sprintf(Config::$a['oauth']['callback'], $this->authProvider), 'code' => $params['code'])); if (empty($response) || isset($response['error'])) { throw new Exception('Invalid access_token response'); } if (!isset($response['result']) || empty($response['result']) || !isset($response['result']['access_token'])) { throw new Exception('Failed request for access token'); } $client->setAccessToken($response['result']['access_token']); $response = $client->fetch('https://api.twitch.tv/kraken/user'); if (empty($response['result']) || isset($response['error'])) { throw new Exception('Invalid user details response'); } if (is_string($response['result'])) { throw new Exception(sprintf('Invalid auth result %s', $response['result'])); } $authCreds = $this->getAuthCredentials($params['code'], $response['result']); $authCredHandler = new AuthenticationRedirectionFilter(); return $authCredHandler->execute($authCreds); }
/** * @param array $params * @throws Exception */ public function authenticate(array $params) { if (!isset($params['code']) || empty($params['code'])) { throw new Exception('Authentication failed, invalid or empty code.'); } $oAuthConf = Config::$a['oauth']['providers'][$this->authProvider]; $client = new \OAuth2\Client($oAuthConf['clientId'], $oAuthConf['clientSecret'], \OAuth2\Client::AUTH_TYPE_AUTHORIZATION_BASIC); $client->setAccessTokenType(\OAuth2\Client::ACCESS_TOKEN_BEARER); $response = $client->getAccessToken('https://ssl.reddit.com/api/v1/access_token', 'authorization_code', array('redirect_uri' => sprintf(Config::$a['oauth']['callback'], $this->authProvider), 'code' => $params['code'])); if (empty($response) || isset($response['error'])) { throw new Exception('Invalid access_token response'); } if (!isset($response['result']) || empty($response['result']) || !isset($response['result']['access_token'])) { throw new Exception('Failed request for access token'); } $client->setAccessToken($response['result']['access_token']); // Reddit requires a User-Agent $info = $client->fetch("https://oauth.reddit.com/api/v1/me.json", array(), 'GET', array('User-Agent' => 'destiny.gg/' . Config::version())); if (empty($info['result']) || !is_array($info['result']) || isset($info['error'])) { throw new Exception('Invalid user details response'); } $authCreds = $this->getAuthCredentials($params['code'], $info['result']); $authCredHandler = new AuthenticationRedirectionFilter(); return $authCredHandler->execute($authCreds); }
/** * @param array $params * @return string * @throws Exception */ public function authenticate(array $params) { if (!isset($params['oauth_token']) || empty($params['oauth_token']) || !isset($params['oauth_verifier']) || empty($params['oauth_verifier'])) { throw new Exception('Authentication failed'); } $oauth = Session::set('oauth'); if ($params['oauth_token'] !== $oauth['oauth_token']) { throw new Exception('Invalid login session'); } $twitterOAuthConf = Config::$a['oauth']['providers']['twitter']; $tmhOAuth = new \tmhOAuth(array('consumer_key' => $twitterOAuthConf['clientId'], 'consumer_secret' => $twitterOAuthConf['clientSecret'], 'token' => $oauth['oauth_token'], 'secret' => $oauth['oauth_token_secret'], 'curl_connecttimeout' => Config::$a['curl']['connecttimeout'], 'curl_timeout' => Config::$a['curl']['timeout'], 'curl_ssl_verifypeer' => Config::$a['curl']['verifypeer'])); /** @noinspection PhpVoidFunctionResultUsedInspection */ $code = $tmhOAuth->user_request(array('method' => 'POST', 'url' => $tmhOAuth->url('oauth/access_token', ''), 'params' => array('oauth_verifier' => trim($params['oauth_verifier'])))); if ($code != 200) { throw new Exception('Failed to retrieve user data'); } $data = $tmhOAuth->extract_params($tmhOAuth->response['response']); $authCreds = $this->getAuthCredentials($oauth['oauth_token'], $data); $authCredHandler = new AuthenticationRedirectionFilter(); return $authCredHandler->execute($authCreds); }
/** * @param array $params * @throws Exception */ public function authenticate(array $params) { if (!isset($params['code']) || empty($params['code'])) { throw new Exception('Authentication failed, invalid or empty code.'); } $authConf = Config::$a['oauth']['providers'][$this->authProvider]; $callback = sprintf(Config::$a['oauth']['callback'], $this->authProvider); $client = new \OAuth2\Client($authConf['clientId'], $authConf['clientSecret']); $response = $client->getAccessToken('https://accounts.google.com/o/oauth2/token', 'authorization_code', array('redirect_uri' => $callback, 'code' => $params['code'])); if (empty($response) || isset($response['error'])) { throw new Exception('Invalid access_token response'); } if (!isset($response['result']) || empty($response['result']) || !isset($response['result']['access_token'])) { throw new Exception('Failed request for access token'); } $client->setAccessToken($response['result']['access_token']); $response = $client->fetch('https://www.googleapis.com/oauth2/v2/userinfo'); if (empty($response['result']) || isset($response['error'])) { throw new Exception('Invalid user details response'); } $authCreds = $this->getAuthCredentials($params['code'], $response['result']); $authCredHandler = new AuthenticationRedirectionFilter(); return $authCredHandler->execute($authCreds); }
/** * @Route ("/register") * @HttpMethod ({"POST"}) * @Transactional * * Handle the confirmation request * @param array $params * @throws Exception */ public function registerProcess(array $params, ViewModel $model, Request $request) { $userService = UserService::instance(); $authService = AuthenticationService::instance(); $authCreds = $this->getSessionAuthenticationCredentials($params); $username = isset($params['username']) && !empty($params['username']) ? $params['username'] : ''; $email = isset($params['email']) && !empty($params['email']) ? $params['email'] : ''; $country = isset($params['country']) && !empty($params['country']) ? $params['country'] : ''; $rememberme = isset($params['rememberme']) && !empty($params['rememberme']) ? true : false; $authCreds->setUsername($username); $authCreds->setEmail($email); try { if (!isset($params['g-recaptcha-response']) || empty($params['g-recaptcha-response'])) { throw new Exception('You must solve the recaptcha.'); } $googleRecaptchaHandler = new GoogleRecaptchaHandler(); $googleRecaptchaHandler->resolve(Config::$a['g-recaptcha']['secret'], $params['g-recaptcha-response'], $request->ipAddress()); $authService->validateUsername($username); $authService->validateEmail($email); if (!empty($country)) { $countryArr = Country::getCountryByCode($country); if (empty($countryArr)) { throw new Exception('Invalid country'); } $country = $countryArr['alpha-2']; } $user = array(); $user['username'] = $username; $user['email'] = $email; $user['userStatus'] = 'Active'; $user['country'] = $country; $user['userId'] = $userService->addUser($user); $userService->addUserAuthProfile(array('userId' => $user['userId'], 'authProvider' => $authCreds->getAuthProvider(), 'authId' => $authCreds->getAuthId(), 'authCode' => $authCreds->getAuthCode(), 'authDetail' => $authCreds->getAuthDetail())); Session::set('authSession'); $authCredHandler = new AuthenticationRedirectionFilter(); return $authCredHandler->execute($authCreds); } catch (Exception $e) { $model->title = 'Register Error'; $model->username = $username; $model->email = $email; $model->follow = isset($params['follow']) ? $params['follow'] : ''; $model->authProvider = $authCreds->getAuthProvider(); $model->code = $authCreds->getAuthCode(); $model->error = $e; return 'register'; } }