/** * Returns the redirect URI * * @return array|string */ public function getRedirectUri() { // Force `addTrailingSlashesToUrls` to `false` while we generate the redirectUri $addTrailingSlashesToUrls = \Craft\craft()->config->get('addTrailingSlashesToUrls'); \Craft\craft()->config->set('addTrailingSlashesToUrls', false); $redirectUri = \Craft\UrlHelper::getActionUrl('oauth/connect'); // Set `addTrailingSlashesToUrls` back to its original value \Craft\craft()->config->set('addTrailingSlashesToUrls', $addTrailingSlashesToUrls); // We don't want the CP trigger showing in the action URL. $redirectUri = str_replace(\Craft\craft()->config->get('cpTrigger') . '/', '', $redirectUri); OauthPlugin::log('Redirect URI: ' . $redirectUri, LogLevel::Info); return $redirectUri; }
/** * Get token by ID */ public function getTokenById($id) { if ($id) { $record = Oauth_TokenRecord::model()->findById($id); if ($record) { $token = Oauth_TokenModel::populateModel($record); // will refresh token if needed try { if ($this->_refreshToken($token)) { // save refreshed token $this->saveToken($token); } } catch (\Exception $e) { OauthPlugin::log("OAuth.Debug - Couldn't refresh token\r\n" . $e->getMessage() . '\\r\\n' . $e->getTraceAsString(), LogLevel::Error, true); } return $token; } } }
/** * Connect OAuth 1.0 */ public function connectOauth1($options) { $token = false; // source oauth provider $oauthProvider = $this->getProvider(); // twitter cancel if (\Craft\craft()->request->getParam('denied')) { throw new \Exception("An error occured: " . \Craft\craft()->request->getParam('denied')); } $user = \Craft\craft()->request->getParam('user'); $oauth_token = \Craft\craft()->request->getParam('oauth_token'); $oauth_verifier = \Craft\craft()->request->getParam('oauth_verifier'); $denied = \Craft\craft()->request->getParam('denied'); if ($oauth_token && $oauth_verifier) { OauthPlugin::log('OAuth 1 Connect - Step 2', LogLevel::Info, true); $temporaryCredentials = unserialize(\Craft\craft()->httpSession->get('temporary_credentials')); $token = $oauthProvider->getTokenCredentials($temporaryCredentials, $oauth_token, $oauth_verifier); \Craft\craft()->httpSession->add('token_credentials', serialize($token)); OauthPlugin::log('OAuth 1 Connect - Step 2 - Data' . "\r\n" . print_r(['temporaryCredentials' => $temporaryCredentials, 'oauth_token' => $oauth_token, 'oauth_verifier' => $oauth_verifier, 'token' => $token], true), LogLevel::Info, true); } elseif ($denied) { OauthPlugin::log('OAuth 1 Connect - Step 1.5' . "\r\n" . print_r(["Client access denied by the user"], true), LogLevel::Info, true); throw new \Exception("Client access denied by the user"); } else { OauthPlugin::log('OAuth 1 Connect - Step 1', LogLevel::Info, true); $temporaryCredentials = $oauthProvider->getTemporaryCredentials(); \Craft\craft()->httpSession->add('temporary_credentials', serialize($temporaryCredentials)); $authorizationUrl = $oauthProvider->getAuthorizationUrl($temporaryCredentials); \Craft\craft()->request->redirect($authorizationUrl); OauthPlugin::log('OAuth 1 Connect - Step 1 - Data' . "\r\n" . print_r(['temporaryCredentials' => $temporaryCredentials, 'authorizationUrl' => $authorizationUrl], true), LogLevel::Info, true); } return $token; }