Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Feb 19, 2017
2 parents 9ca6027 + 21715be commit 7f40800
Showing 1 changed file with 57 additions and 21 deletions.
78 changes: 57 additions & 21 deletions src/Auth/HybridAuthAuthenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ class HybridAuthAuthenticate extends BaseAuthenticate

use EventDispatcherTrait;

/**
* The query string key used for remembering the referrered page when getting
* redirected to login.
*/
const QUERY_STRING_REDIRECT = 'redirect';

/**
* HybridAuth adapter.
*
Expand Down Expand Up @@ -104,17 +110,21 @@ protected function _init(Request $request)
$hybridConfig = Configure::read('HybridAuth');

if (empty($hybridConfig['base_url'])) {
$hybridConfig['base_url'] = Router::url(
[
'plugin' => 'ADmad/HybridAuth',
'controller' => 'HybridAuth',
'action' => 'endpoint',
'prefix' => false
],
true
);
$hybridConfig['base_url'] = [
'plugin' => 'ADmad/HybridAuth',
'controller' => 'HybridAuth',
'action' => 'endpoint',
'prefix' => false
];
}

$hybridConfig['base_url'] = $this->_appendRedirectQueryString(
$hybridConfig['base_url'],
$request->query(static::QUERY_STRING_REDIRECT)
);

$hybridConfig['base_url'] = Router::url($hybridConfig['base_url'], true);

try {
Hybrid_Auth::initialize($hybridConfig);
} catch (\Exception $e) {
Expand Down Expand Up @@ -195,19 +205,22 @@ public function authenticate(Request $request, Response $response)
return false;
}

$returnTo = Router::url(
[
'plugin' => 'ADmad/HybridAuth',
'controller' => 'HybridAuth',
'action' => 'authenticated',
'prefix' => false
],
true
);
if (!empty($this->_config['hauth_return_to'])) {
$returnTo = Router::url($this->_config['hauth_return_to'], true);
$returnTo = [
'plugin' => 'ADmad/HybridAuth',
'controller' => 'HybridAuth',
'action' => 'authenticated',
'prefix' => false
];
if ($this->config('hauth_return_to')) {
$returnTo = $this->config('hauth_return_to');
}
$params = ['hauth_return_to' => $returnTo];

$returnTo = $this->_appendRedirectQueryString(
$returnTo,
$request->query(static::QUERY_STRING_REDIRECT)
);

$params = ['hauth_return_to' => Router::url($returnTo, true)];
if ($provider === 'OpenID') {
$params['openid_identifier'] = $request->query($this->_config['fields']['openid_identifier']);
}
Expand Down Expand Up @@ -422,4 +435,27 @@ public function implementedEvents()
{
return ['Auth.logout' => 'logout'];
}

/**
* Append the "redirect" query string param to URL.
*
* @param string|array $url URL
* @param string $redirectQueryString Redirect query string
* @return string URL
*/
protected function _appendRedirectQueryString($url, $redirectQueryString)
{
if (!$redirectQueryString) {
return $url;
}

if (is_array($url)) {
$url['?'][static::QUERY_STRING_REDIRECT] = $redirectQueryString;
} else {
$char = strpos($url, '?') === false ? '?' : '&';
$url .= $char . static::QUERY_STRING_REDIRECT . '=' . urlencode($redirectQueryString);
}

return $url;
}
}

0 comments on commit 7f40800

Please sign in to comment.