/** * Any migration code in here is wrapped inside of a transaction. * * @return bool */ public function safeUp() { $this->addColumnAfter('oauth_tokens', 'accessToken', array(ColumnType::Varchar, 'required' => false), 'pluginHandle'); $this->addColumnAfter('oauth_tokens', 'secret', array(ColumnType::Varchar, 'required' => false), 'accessToken'); $this->addColumnAfter('oauth_tokens', 'endOfLife', array(ColumnType::Varchar, 'required' => false), 'secret'); $this->addColumnAfter('oauth_tokens', 'refreshToken', array(ColumnType::Varchar, 'required' => false), 'endOfLife'); require_once CRAFT_PLUGINS_PATH . 'oauth/vendor/autoload.php'; $rows = craft()->db->createCommand()->select('*')->from('oauth_tokens')->queryAll(); foreach ($rows as $row) { $token = $row['encodedToken']; $token = @unserialize(base64_decode($token)); if ($token && is_object($token)) { $attributes = array(); $attributes['accessToken'] = $token->getAccessToken(); if (method_exists($token, 'getAccessTokenSecret')) { $attributes['secret'] = $token->getAccessTokenSecret(); } $attributes['endOfLife'] = $token->getEndOfLife(); $attributes['refreshToken'] = $token->getRefreshToken(); $this->update('oauth_tokens', $attributes, 'id = :id', array(':id' => $row['id'])); } } OauthPlugin::log('Dropping the encodedToken column from the structures table...', LogLevel::Info, true); $this->dropColumn('oauth_tokens', 'encodedToken'); OauthPlugin::log('Done dropping the encodedToken column from the structures table.', LogLevel::Info, true); return true; }
/** * Any migration code in here is wrapped inside of a transaction. * * @return bool */ public function safeUp() { // unique index for 'userMapping' and 'provider' $tableName = 'oauth_tokens'; $providersTable = $this->dbConnection->schema->getTable('{{' . $tableName . '}}'); if ($providersTable) { $columns = 'userMapping, provider'; $unique = true; $this->createIndex($tableName, $columns, $unique); } else { OauthPlugin::log('Could not find an `' . $tableName . '` table. Wut?', LogLevel::Error); } // unique index for 'userId' and 'provider' $tableName = 'oauth_tokens'; $providersTable = $this->dbConnection->schema->getTable('{{' . $tableName . '}}'); if ($providersTable) { $columns = 'userId, provider'; $unique = true; $this->createIndex($tableName, $columns, $unique); } else { OauthPlugin::log('Could not find an `' . $tableName . '` table. Wut?', LogLevel::Error); } // unique index for 'namespace' and 'provider' $tableName = 'oauth_tokens'; $providersTable = $this->dbConnection->schema->getTable('{{' . $tableName . '}}'); if ($providersTable) { $columns = 'namespace, provider'; $unique = true; $this->createIndex($tableName, $columns, $unique); } else { OauthPlugin::log('Could not find an `' . $tableName . '` table. Wut?', LogLevel::Error); } return true; }
/** * Any migration code in here is wrapped inside of a transaction. * * @return bool */ public function safeUp() { $providersTable = $this->dbConnection->schema->getTable('{{oauth_providers}}'); if ($providersTable) { if ($this->renameColumn('{{oauth_providers}}', 'providerClass', 'class')) { OauthPlugin::log('Renamed `{{oauth_providers}}`.`providerClass` to `{{oauth_providers}}`.`class`.', LogLevel::Info, true); } else { OauthPlugin::log('Couldn\'t rename `{{oauth_providers}}`.`providerClass` to `{{oauth_providers}}`.`class`.', LogLevel::Warning); } } else { OauthPlugin::log('Could not find an `{{oauth_providers}}` table. Wut?', LogLevel::Error); } return true; }
/** * Connect * * @return null */ public function actionConnect() { $token = false; $success = false; $error = false; $errorMsg = false; // handle $providerHandle = craft()->httpSession->get('oauth.handle'); if (!$providerHandle) { $providerHandle = craft()->request->getParam('provider'); if ($providerHandle) { craft()->httpSession->add('oauth.handle', $providerHandle); } else { throw new Exception("Couldn’t retrieve OAuth provider."); } } // session vars $scope = craft()->httpSession->get('oauth.scope'); $authorizationOptions = craft()->httpSession->get('oauth.authorizationOptions'); $referer = craft()->httpSession->get('oauth.referer'); OauthPlugin::log('OAuth Connect - Connect with `' . $providerHandle . '` OAuth provider' . "\r\n" . 'Session Data: ' . print_r(['oauth.referer' => $referer, 'oauth.scope' => $scope, 'oauth.authorizationOptions' => $authorizationOptions], true) . "\r\n", LogLevel::Info); try { // provider $provider = craft()->oauth->getProvider($providerHandle); // connect $tokenResponse = $provider->connect(['scope' => $scope, 'authorizationOptions' => $authorizationOptions]); // token if ($tokenResponse) { $token = OauthHelper::realTokenToArray($tokenResponse); } else { throw new Exception("Error with token"); } $success = true; } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) { $error = true; $errorMsg = $e->getMessage(); if ($errorMsg == 'invalid_client') { $errorMsg = Craft::t("Invalid OAuth client ID or secret."); } } catch (\Exception $e) { $error = true; $errorMsg = $e->getMessage(); } // build up response $response = array('error' => $error, 'errorMsg' => $errorMsg, 'success' => $success, 'token' => $token); OauthPlugin::log("OAuth Connect - Response\r\n" . 'Session Data: ' . print_r(['oauth.response' => $response], true) . "\r\n", LogLevel::Info); craft()->httpSession->add('oauth.response', $response); // redirect $this->redirect($referer); }
/** * Any migration code in here is wrapped inside of a transaction. * * @return bool */ public function safeUp() { $tokensTable = $this->dbConnection->schema->getTable('{{oauth_tokens}}'); if ($tokensTable) { if (($userMappingColumn = $tokensTable->getColumn('userMapping')) == null) { OauthPlugin::log('Adding `userMapping` column to the `oauth_tokens` table.', LogLevel::Info, true); $this->addColumnAfter('oauth_tokens', 'userMapping', array(AttributeType::String, 'required' => false), 'userId'); OauthPlugin::log('Added `userMapping` column to the `oauth_tokens` table.', LogLevel::Info, true); } else { OauthPlugin::log('Tried to add a `userMapping` column to the `oauth_tokens` table, but there is already one there.', LogLevel::Warning); } } else { OauthPlugin::log('Could not find an `oauth_tokens` table. Wut?', LogLevel::Error); } return true; }
/** * Connect * * @return null */ public function actionConnect() { $token = false; $success = false; $error = false; $errorMsg = false; // handle $providerHandle = craft()->httpSession->get('oauth.handle'); if (!$providerHandle) { $providerHandle = craft()->request->getParam('provider'); if ($providerHandle) { craft()->httpSession->add('oauth.handle', $providerHandle); } else { throw new Exception("Couldn’t retrieve OAuth provider."); } } // session vars $scope = craft()->httpSession->get('oauth.scope'); $authorizationOptions = craft()->httpSession->get('oauth.authorizationOptions'); $referer = craft()->httpSession->get('oauth.referer'); OauthPlugin::log('OAuth Connect - Step 2A' . "\r\n" . print_r(['handle' => $providerHandle, 'scope' => $scope, 'authorizationOptions' => $authorizationOptions, 'referer' => $referer], true), LogLevel::Info, true); try { // provider $provider = craft()->oauth->getProvider($providerHandle); // connect $tokenResponse = $provider->connect(['scope' => $scope, 'authorizationOptions' => $authorizationOptions]); // token if ($tokenResponse) { $token = OauthHelper::realTokenToArray($tokenResponse); } else { throw new Exception("Error with token"); } $success = true; } catch (\Exception $e) { $error = true; $errorMsg = $e->getMessage(); } // build up response $response = array('error' => $error, 'errorMsg' => $errorMsg, 'success' => $success, 'token' => $token); OauthPlugin::log('OAuth Connect - Step 2B' . "\r\n" . print_r(['response' => $response], true), LogLevel::Info, true); craft()->httpSession->add('oauth.response', $response); // redirect $this->redirect($referer); }