8 changed files with 336 additions and 13 deletions
			
			
		| @ -0,0 +1,51 @@ | ||||
| <?php | ||||
| /** | ||||
|  * @link http://www.yiiframework.com/ | ||||
|  * @copyright Copyright (c) 2008 Yii Software LLC | ||||
|  * @license http://www.yiiframework.com/license/ | ||||
|  */ | ||||
| 
 | ||||
| namespace yii\authclient\provider; | ||||
| 
 | ||||
| use Yii; | ||||
| 
 | ||||
| /** | ||||
|  * Class OAuth1 | ||||
|  * | ||||
|  * @author Paul Klimov <klimov.paul@gmail.com> | ||||
|  * @since 2.0 | ||||
|  */ | ||||
| class OAuth1 extends \yii\authclient\OAuth1 implements ProviderInterface | ||||
| { | ||||
| 	use ProviderTrait; | ||||
| 
 | ||||
| 	/** | ||||
| 	 * @inheritdoc | ||||
| 	 */ | ||||
| 	public function authenticate() | ||||
| 	{ | ||||
| 		// user denied error | ||||
| 		if (isset($_GET['denied'])) { | ||||
| 			return $this->redirectCancel(); | ||||
| 		} | ||||
| 
 | ||||
| 		if (isset($_REQUEST['oauth_token'])) { | ||||
| 			$oauthToken = $_REQUEST['oauth_token']; | ||||
| 		} | ||||
| 
 | ||||
| 		if (!isset($oauthToken)) { | ||||
| 			// Get request token. | ||||
| 			$requestToken = $this->fetchRequestToken(); | ||||
| 			// Get authorization URL. | ||||
| 			$url = $this->buildAuthUrl($requestToken); | ||||
| 			// Redirect to authorization URL. | ||||
| 			return Yii::$app->getResponse()->redirect($url); | ||||
| 		} else { | ||||
| 			// Upgrade to access token. | ||||
| 			$accessToken = $this->fetchAccessToken(); | ||||
| 			$this->isAuthenticated = true; | ||||
| 		} | ||||
| 
 | ||||
| 		return $this->isAuthenticated; | ||||
| 	} | ||||
| } | ||||
| @ -0,0 +1,58 @@ | ||||
| <?php | ||||
| /** | ||||
|  * @link http://www.yiiframework.com/ | ||||
|  * @copyright Copyright (c) 2008 Yii Software LLC | ||||
|  * @license http://www.yiiframework.com/license/ | ||||
|  */ | ||||
| 
 | ||||
| namespace yii\authclient\provider; | ||||
| use Yii; | ||||
| use yii\base\Exception; | ||||
| 
 | ||||
| /** | ||||
|  * Class OAuth2 | ||||
|  * | ||||
|  * @author Paul Klimov <klimov.paul@gmail.com> | ||||
|  * @since 2.0 | ||||
|  */ | ||||
| class OAuth2 extends \yii\authclient\OAuth2 implements ProviderInterface | ||||
| { | ||||
| 	use ProviderTrait; | ||||
| 
 | ||||
| 	/** | ||||
| 	 * @inheritdoc | ||||
| 	 */ | ||||
| 	public function authenticate() | ||||
| 	{ | ||||
| 		if (isset($_GET['error'])) { | ||||
| 			if ($_GET['error'] == 'access_denied') { | ||||
| 				// user denied error | ||||
| 				return $this->redirectCancel(); | ||||
| 			} else { | ||||
| 				// request error | ||||
| 				if (isset($_GET['error_description'])) { | ||||
| 					$errorMessage = $_GET['error_description']; | ||||
| 				} elseif (isset($_GET['error_message'])) { | ||||
| 					$errorMessage = $_GET['error_message']; | ||||
| 				} else { | ||||
| 					$errorMessage = http_build_query($_GET); | ||||
| 				} | ||||
| 				throw new Exception('Auth error: ' . $errorMessage); | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		// Get the access_token and save them to the session. | ||||
| 		if (isset($_GET['code'])) { | ||||
| 			$code = $_GET['code']; | ||||
| 			$token = $this->fetchAccessToken($code); | ||||
| 			if (!empty($token)) { | ||||
| 				$this->isAuthenticated = true; | ||||
| 			} | ||||
| 		} else { | ||||
| 			$url = $this->buildAuthUrl(); | ||||
| 			return Yii::$app->getResponse()->redirect($url); | ||||
| 		} | ||||
| 
 | ||||
| 		return $this->isAuthenticated; | ||||
| 	} | ||||
| } | ||||
| @ -0,0 +1,38 @@ | ||||
| <?php | ||||
| use yii\helpers\Html; | ||||
| use yii\helpers\Json; | ||||
| 
 | ||||
| /* @var $this \yii\base\View */ | ||||
| /* @var $url string */ | ||||
| /* @var $enforceRedirect boolean */ | ||||
| 
 | ||||
| $redirectJavaScript = <<<EOL | ||||
| function popupWindowRedirect(url, enforceRedirect = true) { | ||||
| 	if (window.opener) { | ||||
| 		window.close(); | ||||
| 		if (enforceRedirect) { | ||||
| 			window.opener.location = url; | ||||
| 		} | ||||
| 	} else { | ||||
| 		window.location = url; | ||||
| 	} | ||||
| } | ||||
| EOL; | ||||
| 
 | ||||
| $redirectJavaScript .= 'popupWindowRedirect(' . Json::encode($url) . ', ' . Json::encode($enforceRedirect) . ');'; | ||||
| 
 | ||||
| ?> | ||||
| <!DOCTYPE html> | ||||
| <html> | ||||
| <head> | ||||
| 	<?= Html::script($redirectJavaScript); ?> | ||||
| </head> | ||||
| <body> | ||||
| <h2 id="title" style="display:none;">Redirecting back to the "<?= Yii::$app->name; ?>"...</h2>
 | ||||
| <h3 id="link"><a href="<?= $url; ?>">Click here to return to the "<?= Yii::$app->name; ?>".</a></h3>
 | ||||
| <script type="text/javascript"> | ||||
| 	document.getElementById('title').style.display = ''; | ||||
| 	document.getElementById('link').style.display = 'none'; | ||||
| </script> | ||||
| </body> | ||||
| </html> | ||||
| @ -0,0 +1,90 @@ | ||||
| <?php | ||||
| 
 | ||||
| namespace yiiunit\extensions\authclient\provider; | ||||
| 
 | ||||
| 
 | ||||
| use yii\authclient\provider\ProviderInterface; | ||||
| use yii\authclient\provider\ProviderTrait; | ||||
| use yii\base\Object; | ||||
| use yiiunit\extensions\authclient\TestCase; | ||||
| 
 | ||||
| class ProviderTraitTest extends TestCase | ||||
| { | ||||
| 	protected function setUp() | ||||
| 	{ | ||||
| 		$config = [ | ||||
| 			'components' => [ | ||||
| 				'user' => [ | ||||
| 					'identityClass' => '\yii\web\IdentityInterface' | ||||
| 				], | ||||
| 				'request' => [ | ||||
| 					'hostInfo' => 'http://testdomain.com', | ||||
| 					'scriptUrl' => '/index.php', | ||||
| 				], | ||||
| 			] | ||||
| 		]; | ||||
| 		$this->mockApplication($config, '\yii\web\Application'); | ||||
| 	} | ||||
| 
 | ||||
| 	public function testSetGet() | ||||
| 	{ | ||||
| 		$provider = new Provider(); | ||||
| 
 | ||||
| 		$id = 'test_service_id'; | ||||
| 		$provider->setId($id); | ||||
| 		$this->assertEquals($id, $provider->getId(), 'Unable to setup id!'); | ||||
| 
 | ||||
| 		$successUrl = 'http://test.success.url'; | ||||
| 		$provider->setSuccessUrl($successUrl); | ||||
| 		$this->assertEquals($successUrl, $provider->getSuccessUrl(), 'Unable to setup success URL!'); | ||||
| 
 | ||||
| 		$cancelUrl = 'http://test.cancel.url'; | ||||
| 		$provider->setCancelUrl($cancelUrl); | ||||
| 		$this->assertEquals($cancelUrl, $provider->getCancelUrl(), 'Unable to setup cancel URL!'); | ||||
| 	} | ||||
| 
 | ||||
| 	public function testGetDescriptiveData() | ||||
| 	{ | ||||
| 		$provider = new Provider(); | ||||
| 
 | ||||
| 		$this->assertNotEmpty($provider->getName(), 'Unable to get name!'); | ||||
| 		$this->assertNotEmpty($provider->getTitle(), 'Unable to get title!'); | ||||
| 	} | ||||
| 
 | ||||
| 	/** | ||||
| 	 * @depends testSetGet | ||||
| 	 */ | ||||
| 	public function testGetDefaultSuccessUrl() | ||||
| 	{ | ||||
| 		$provider = new Provider(); | ||||
| 
 | ||||
| 		$this->assertNotEmpty($provider->getSuccessUrl(), 'Unable to get default success URL!'); | ||||
| 	} | ||||
| 
 | ||||
| 	/** | ||||
| 	 * @depends testSetGet | ||||
| 	 */ | ||||
| 	public function testGetDefaultCancelUrl() | ||||
| 	{ | ||||
| 		$provider = new Provider(); | ||||
| 
 | ||||
| 		$this->assertNotEmpty($provider->getSuccessUrl(), 'Unable to get default cancel URL!'); | ||||
| 	} | ||||
| 
 | ||||
| 	public function testRedirect() | ||||
| 	{ | ||||
| 		$provider = new Provider(); | ||||
| 
 | ||||
| 		$url = 'http://test.url'; | ||||
| 		$response = $provider->redirect($url, true); | ||||
| 
 | ||||
| 		$this->assertContains($url, $response->content); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| class Provider extends Object implements ProviderInterface | ||||
| { | ||||
| 	use ProviderTrait; | ||||
| 
 | ||||
| 	public function authenticate() {} | ||||
| } | ||||
					Loading…
					
					
				
		Reference in new issue