/** * @param array $params * @return string * @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 Client($oAuthConf['clientId'], $oAuthConf['clientSecret']); $client->setAccessTokenType(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 * @return string * @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 Client($oAuthConf['clientId'], $oAuthConf['clientSecret'], Client::AUTH_TYPE_AUTHORIZATION_BASIC); $client->setAccessTokenType(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); }
/** * @return string */ public function getAuthenticationUrl() { $authConf = Config::$a['oauth']['providers'][$this->authProvider]; $callback = sprintf(Config::$a['oauth']['callback'], $this->authProvider); $client = new Client($authConf['clientId'], $authConf['clientSecret']); $client->setAccessTokenType(Client::ACCESS_TOKEN_BEARER); return $client->getAuthenticationUrl('https://accounts.google.com/o/oauth2/auth', $callback, array('scope' => 'openid email', 'state' => 'security_token=' . Session::getSessionId())); }
/** * Here you can render the homepage of the app */ public function index() { // Get OAuth2 parameters from config and session $clientId = Config::get('oauth.client'); $clientSecret = Config::get('oauth.secret'); $userAgent = Config::get('oauth.user_agent'); $accessTokenResult = $this->session->read('accessToken'); // Setup OAuth2 client to request resources from Reddit $client = new Client($clientId, $clientSecret, Client::AUTH_TYPE_AUTHORIZATION_BASIC); $client->setCurlOption(CURLOPT_USERAGENT, $userAgent); $client->setAccessToken($accessTokenResult["access_token"]); $client->setAccessTokenType(Client::ACCESS_TOKEN_BEARER); // Request user response $response = $client->fetch("https://oauth.reddit.com/api/v1/me.json"); $this->view->render("Home", array('me' => $response['result'], 'pageTitle' => 'Reddit profile example')); }
/** * Exchanges the code from the URI parameters for an access token, id token and user info * @return Boolean Wheter it exchanged the code or not correctly */ private function exchangeCode() { if (!isset($_REQUEST['code'])) { return false; } $code = $_REQUEST['code']; $this->debugInfo("Code: " . $code); // Generate the url to the API that will give us the access token and id token $auth_url = $this->generateUrl('token'); // Make the call $auth0_response = $this->oauth_client->getAccessToken($auth_url, "authorization_code", array("code" => $code, "redirect_uri" => $this->redirect_uri)); // Parse it $auth0_response = $auth0_response['result']; $this->debugInfo(json_encode($auth0_response)); $access_token = isset($auth0_response['access_token']) ? $auth0_response['access_token'] : false; $id_token = isset($auth0_response['id_token']) ? $auth0_response['id_token'] : false; if (!$access_token) { throw new ApiException('Invalid access_token - Retry login.'); } // Set the access token in the oauth client for future calls to the Auth0 API $this->oauth_client->setAccessToken($access_token); $this->oauth_client->setAccessTokenType(Client::ACCESS_TOKEN_BEARER); // Set it and persist it, if needed $this->setAccessToken($access_token); $this->setIdToken($id_token); $token = Auth0JWT::decode($id_token, $this->client_id, $this->client_secret); $user = ApiUsers::get($this->domain, $id_token, $token->user_id); $this->setUser($user); return true; }
/** * Exchanges the code from the URI parameters for an access token, id token and user info * @return Boolean Whether it exchanged the code or not correctly */ private function exchangeCode() { if (!isset($_REQUEST['code'])) { return false; } $code = $_REQUEST['code']; $this->debugInfo("Code: " . $code); // Generate the url to the API that will give us the access token and id token $auth_url = $this->generateUrl('token'); // Make the call $response = $this->oauth_client->getAccessToken($auth_url, "authorization_code", array("code" => $code, "redirect_uri" => $this->redirect_uri), array('Auth0-Client' => ApiClient::getInfoHeadersData()->build())); $auth0_response = $response['result']; if ($response['code'] !== 200) { throw new ApiException($auth0_response['error'] . ': ' . $auth0_response['error_description']); } $this->debugInfo(json_encode($auth0_response)); $access_token = isset($auth0_response['access_token']) ? $auth0_response['access_token'] : false; $id_token = isset($auth0_response['id_token']) ? $auth0_response['id_token'] : false; if (!$access_token) { throw new ApiException('Invalid access_token - Retry login.'); } if (!$id_token) { // id_token is not mandatory anymore. There is no need to force openid connect $this->debugInfo('Missing id_token after code exchange. Remember to ask for openid scope.'); } // Set the access token in the oauth client for future calls to the Auth0 API $this->oauth_client->setAccessToken($access_token); $this->oauth_client->setAccessTokenType(Client::ACCESS_TOKEN_BEARER); // Set it and persist it, if needed $this->setAccessToken($access_token); $this->setIdToken($id_token); $userinfo_url = $this->generateUrl('user_info'); $user = $this->oauth_client->fetch($userinfo_url); $this->setUser($user["result"]); return true; }
/** * Requests access token to Auth0 server, using authorization code. * * @param string $code Authorization code * * @return string */ protected final function getTokenFromCode($code) { $this->debugInfo("Code: " . $code); $auth_url = $this->generateUrl('token'); $auth0_response = $this->oauth_client->getAccessToken($auth_url, "authorization_code", array("code" => $code, "redirect_uri" => $this->redirect_uri)); $auth0_response = $auth0_response['result']; $this->debugInfo(json_encode($auth0_response)); $access_token = isset($auth0_response['access_token']) ? $auth0_response['access_token'] : false; if (!$access_token) { throw new ApiException('Invalid access_token - Retry login.'); } $this->oauth_client->setAccessToken($access_token); $this->oauth_client->setAccessTokenType(OAuth2\Client::ACCESS_TOKEN_BEARER); $this->setAccessToken($access_token); return $access_token; }