Skip to content

Commit

Permalink
yii\authclient\clients\GooglePlus extracted
Browse files Browse the repository at this point in the history
  • Loading branch information
klimov-paul committed Mar 17, 2015
1 parent 09985e1 commit b199a71
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 11 deletions.
60 changes: 60 additions & 0 deletions extensions/authclient/clients/GooglePlus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/

namespace yii\authclient\clients;

/**
* GooglePlus is an enhanced version of the [[GoogleOAuth]], which uses Google+ sign-in flow,
* which relies on embedded JavaScript code to generate a sign-in button.
*
* Example application configuration:
*
* ~~~
* 'components' => [
* 'authClientCollection' => [
* 'class' => 'yii\authclient\Collection',
* 'clients' => [
* 'google' => [
* 'class' => 'yii\authclient\clients\GooglePlus',
* 'clientId' => 'google_client_id',
* 'clientSecret' => 'google_client_secret',
* ],
* ],
* ]
* ...
* ]
* ~~~
*
* @see GoogleOAuth
* @see yii\authclient\widgets\GooglePlusButton
* @see https://developers.google.com/+/web/signin
*
* @author Paul Klimov <[email protected]>
* @since 2.0
*/
class GooglePlus extends GoogleOAuth
{
/**
* @inheritdoc
*/
protected function defaultReturnUrl()
{
return 'postmessage';
}

/**
* @inheritdoc
*/
protected function defaultViewOptions()
{
return [
'widget' => [
'class' => 'yii\authclient\widgets\GooglePlusButton'
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@

namespace yii\authclient\widgets;

use yii\authclient\clients\GoogleOAuth;
use yii\authclient\clients\GooglePlus;
use yii\base\InvalidConfigException;
use yii\base\Widget;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\web\View;

/**
* GoogleSignInButton renders Google+ sign-in button.
* This widget is designed to interact with [[GoogleOAuth]].
* GooglePlusButton renders Google+ sign-in button.
* This widget is designed to interact with [[GooglePlus]].
*
* @see GoogleOAuth
* @see GooglePlus
* @see https://developers.google.com/+/web/signin/
*
* @property string|array $callback
*
* @author Paul Klimov <[email protected]>
* @since 2.0
*/
class GoogleSignInButton extends Widget
class GooglePlusButton extends Widget
{
/**
* @var GoogleOAuth google auth client instance.
* @var GooglePlus google auth client instance.
*/
public $client;
/**
Expand Down Expand Up @@ -72,8 +72,8 @@ public function getCallback()
*/
public function init()
{
if (!($this->client instanceof GoogleOAuth)) {
throw new InvalidConfigException('"' . $this->className() . '::client" must be instance of "' . GoogleOAuth::className() . '"');
if (!($this->client instanceof GooglePlus)) {
throw new InvalidConfigException('"' . $this->className() . '::client" must be instance of "' . GooglePlus::className() . '"');
}
}

Expand All @@ -86,7 +86,12 @@ public function run()
return $this->renderButton();
}

protected function generateCallback($url = null)
/**
* Generates JavaScript callback function, which will be used to handle auth response.
* @param array $url auth callback URL.
* @return string JavaScript function name.
*/
protected function generateCallback($url = [])
{
if (empty($url)) {
$url = ['auth', 'authclient' => $this->client->id];
Expand All @@ -102,9 +107,21 @@ protected function generateCallback($url = null)
$js = <<<JS
function $callbackName(authResult) {
var urlParams = [];
for (var propName in authResult) {
urlParams.push(encodeURIComponent(propName) + '=' + encodeURIComponent(authResult[propName]));
if (authResult['code']) {
urlParams.push('code=' + encodeURIComponent(authResult['code']));
} else if (authResult['error']) {
urlParams.push('error=' + encodeURIComponent(authResult['error']));
urlParams.push('error_description=' + encodeURIComponent(authResult['error_description']));
} else {
for (var propName in authResult) {
var propValue = authResult[propName];
if (typeof propValue != 'object') {
urlParams.push(encodeURIComponent(propName) + '=' + encodeURIComponent(propValue));
}
}
}
window.location = '$url' + urlParams.join('&');
}
JS;
Expand Down Expand Up @@ -142,6 +159,9 @@ protected function renderButton()
'data-cookiepolicy' => 'single_host_origin',
'data-requestvisibleactions' => null,
'data-scope' => $this->client->scope,
'data-accesstype' => 'offline',
'data-width' => 'iconOnly',
//'data-approvalprompt' => 'force',
],
$this->buttonHtmlOptions
);
Expand Down

0 comments on commit b199a71

Please sign in to comment.