/** * Execute method. */ public function execute() { $code = $this->getRequest()->getParam('code', false); $userId = $this->getRequest()->getParam('state'); //load admin user $adminUser = $this->adminUser->create()->load($userId); //app code and admin user must be present if ($code && $adminUser->getId()) { $clientId = $this->scopeConfig->getValue(\Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_CLIENT_ID); $clientSecret = $this->scopeConfig->getValue(\Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_CLIENT_SECRET_ID); //callback uri if not set custom $redirectUri = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB, true); $redirectUri .= 'connector/email/callback'; $data = 'client_id=' . $clientId . '&client_secret=' . $clientSecret . '&redirect_uri=' . $redirectUri . '&grant_type=authorization_code' . '&code=' . $code; //callback url $url = $this->config->getTokenUrl(); //@codingStandardsIgnoreStart $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_POST, count($data)); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']); $response = json_decode(curl_exec($ch)); if ($response === false) { $this->helper->error('Error Number: ' . curl_errno($ch), []); } if (isset($response->error)) { $this->helper->error('OAUTH failed ' . $response->error, []); } elseif (isset($response->refresh_token)) { //save the refresh token to the admin user $adminUser->setRefreshToken($response->refresh_token)->save(); } //@codingStandardsIgnoreEnd } //redirect to automation index page $this->_redirect($this->adminHelper->getUrl('dotdigitalgroup_email/studio')); }
/** * Autorisation url for OAUTH. * * @return string */ public function getAuthoriseUrl() { $clientId = $this->_scopeConfig->getValue(\Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_CLIENT_ID); //callback uri if not set custom $redirectUri = $this->getRedirectUri(); $redirectUri .= 'connector/email/callback'; $adminUser = $this->auth->getUser(); //query params $params = ['redirect_uri' => $redirectUri, 'scope' => 'Account', 'state' => $adminUser->getId(), 'response_type' => 'code']; $authorizeBaseUrl = $this->configHelper->getAuthorizeLink(); $url = $authorizeBaseUrl . http_build_query($params) . '&client_id=' . $clientId; return $url; }
/** * Generate new token and connect from the admin. * * @return string */ public function generatetokenAction() { $adminUser = $this->auth->getUser(); $refreshToken = $adminUser->getRefreshToken(); if ($refreshToken) { $token = $this->client->getAccessToken($this->buildUrlParams($refreshToken), $this->configFactory->getTokenUrl()); //save the refresh token to the admin user if (is_string($token)) { $adminUser->setRefreshToken($token)->save(); } return $token; } else { $this->messageManager->addNoticeMessage('Please Connect To Access The Page.'); } }