/** * @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); }
/** * 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')); }
/** * @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.'); } $authConf = Config::$a['oauth']['providers'][$this->authProvider]; $callback = sprintf(Config::$a['oauth']['callback'], $this->authProvider); $client = new 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); }
/** * Gets/sends data from the nation builder site - use for GET and DELETE * * @param $request * @param $api_call * @param $method * @param $params * @return null */ public function communicate($request, $api_call, $method = "GET", $params = array()) { $session = $request->getSession(); if (!$session->has('oauth_token')) { $this->logger->info("No oauth token found"); return NULL; } $token = $session->get('oauth_token'); $send_url = $this->base_url . $api_call; // if paginating, pass along tokens to request $input_query = $request->query->all(); if (array_key_exists('__nonce', $input_query) && array_key_exists('__token', $input_query)) { $params['__nonce'] = $input_query['__nonce']; $params['__token'] = $input_query['__token']; } $client = new Client($this->client_id, $this->client_secret); $client->setAccessToken($token); $response = $client->fetch($send_url, $params, $method); if ($response['code'] !== 200) { $this->logger->info("Could not retrieve data. Response: " . json_encode($response)); return NULL; } return $response['result']; }
/** * 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; }
public function getData() { $clientId = $this->getClientId(); $clientSecret = $this->getClientSecret(); $merchantId = $this->getMerchantId(); $amount = $this->getAmount(); $description = $this->getDescription(); $redirectUrl = $this->getRedirectUrl(); $oauth2path = $this->getPath(); define("CLIENT_ID", $clientId); define("CLIENT_SECRET", $clientSecret); define("REDIRECT_URI", $redirectUrl); define("AUTHORIZATION_ENDPOINT", "https://paguei.online/app/api/authorize"); define("TOKEN_ENDPOINT", "https://paguei.online/app/api/token"); $client = new OAuth2\Client(CLIENT_ID, CLIENT_SECRET); if (!isset($_GET['code'])) { $auth_url = $client->getAuthenticationUrl(AUTHORIZATION_ENDPOINT, REDIRECT_URI); header('Location: ' . $auth_url); die('Redirect'); } else { $params = array('code' => $_GET['code'], 'redirect_uri' => REDIRECT_URI); $response = $client->getAccessToken(TOKEN_ENDPOINT, 'authorization_code', $params); $info = $response['result']; $client->setAccessToken($info['access_token']); $id = $merchantId; $description = urlencode($description); $urlfetch = 'https://paguei.online/app/api/transfer'; $urlfetch2 = $urlfetch . '/' . $id . '/' . $amount . '/' . $description . '.json'; $response = $client->fetch($urlfetch2); return $response; } }
/** * 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; }