Skip to content

Commit

Permalink
Refactorize some parts && bug fixe
Browse files Browse the repository at this point in the history
  • Loading branch information
kalypso63 committed Oct 2, 2017
1 parent 1bc6baa commit 23158c8
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 67 deletions.
44 changes: 21 additions & 23 deletions Classes/Service/SocialAuthenticationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,8 @@ class SocialAuthenticationService extends AbstractAuthenticationService
protected $extConfig = [];

/**
* Login data as passed to initAuth()
*/
protected $loginData = [];

/**
* A reference to the calling object
*
* @var AbstractUserAuthentication
* @var array
*/
protected $parentObject;

protected $arrayProvider = [
'facebook' => 1,
'google' => 2,
Expand Down Expand Up @@ -100,18 +91,21 @@ class SocialAuthenticationService extends AbstractAuthenticationService
protected $signalSlotDispatcher;

/**
* 100 / 101 Authenticated / Not authenticated -> in each case go on with additonal auth
* true - this service was able to authenticate the user
*/
const STATUS_AUTHENTICATION_SUCCESS_CONTINUE = 100;
const STATUS_AUTHENTICATION_FAILURE_CONTINUE = 101;
const STATUS_AUTHENTICATION_SUCCESS_CONTINUE = true;
/**
* 100
*/
const STATUS_AUTHENTICATION_FAILURE_CONTINUE = 100;
/**
* 200 - authenticated and no more checking needed - useful for IP checking without password
*/
const STATUS_AUTHENTICATION_SUCCESS_BREAK = 200;
/**
* FALSE - this service was the right one to authenticate the user but it failed
*/
const STATUS_AUTHENTICATION_FAILURE_BREAK = 0;
const STATUS_AUTHENTICATION_FAILURE_BREAK = false;

/**
* @return bool
Expand Down Expand Up @@ -141,10 +135,6 @@ public function init()
public function initAuth($subType, $loginData, $authenticationInformation, $parentObject)
{
$this->authUtility = $this->objectManager->get(\MV\SocialAuth\Utility\AuthUtility::class);
// Store login and authetication data
$this->loginData = $loginData;
$this->authenticationInformation = $authenticationInformation;
$this->parentObject = $parentObject;
parent::initAuth($subType, $loginData, $authenticationInformation, $parentObject);
}

Expand Down Expand Up @@ -176,7 +166,6 @@ protected function initTSFE()
public function getUser()
{
$user = $fileObject = null;
session_start();
// then grab the user profile
if ($this->provider && $this->isServiceAvailable()) {
//get user
Expand All @@ -197,7 +186,6 @@ public function getUser()
'name' => $this->cleanData($hybridUser->displayName),
'first_name' => $this->cleanData($hybridUser->firstName),
'last_name' => $this->cleanData($hybridUser->lastName),
'username' => $this->cleanData($hybridUser->displayName),
'password' => $password,
'email' => $this->cleanData($hybridUser->email),
'telephone' => $this->cleanData($hybridUser->phone),
Expand All @@ -208,6 +196,12 @@ public function getUser()
'tx_socialauth_identifier' => $this->cleanData($hybridUser->identifier),
'tx_socialauth_source' => $this->arrayProvider[$this->provider]
];
//username
if (!empty($hybridUser->email)) {
$fields['username'] = $hybridUser->email;
} else {
$fields['username'] = $this->cleanData($hybridUser->displayName, true);
}
//grab image
if (!empty($hybridUser->photoURL)) {
$uniqueName = strtolower($this->provider . '_' . $hybridUser->identifier) . '.jpg';
Expand Down Expand Up @@ -277,7 +271,7 @@ public function authUser(&$user)
return self::STATUS_AUTHENTICATION_FAILURE_CONTINUE;
}
$result = self::STATUS_AUTHENTICATION_FAILURE_CONTINUE;
if ($user) {
if ($user && $this->authUtility->isConnectedWithProvider($this->provider)) {
$result = self::STATUS_AUTHENTICATION_SUCCESS_BREAK;
}
//signal slot authUser
Expand All @@ -286,7 +280,6 @@ public function authUser(&$user)
return $result;
}


/**
* Returns TRUE if single sign on for the given provider is enabled in ext_conf and is available
*
Expand Down Expand Up @@ -357,7 +350,7 @@ protected function createFileReferenceFromFalFileObject($file, $userUid)
* @param string $str
* @return string
*/
protected function cleanData($str)
protected function cleanData($str, $forUsername = false)
{
$str = strip_tags($str);
//Remove extra spaces
Expand All @@ -368,6 +361,11 @@ protected function cleanData($str)
$str = utf8_encode($str);
}

if (true === $forUsername) {
$str = str_replace(' ', '', $str);
$str = mb_strtolower($str, 'utf-8');
}

return $str;
}
}
12 changes: 11 additions & 1 deletion Classes/Utility/AuthUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ public function initializeObject()

/**
* @param string $provider
* @param string $returnTo
*
* @return \Hybrid_User_Profile|FALSE
*/
Expand Down Expand Up @@ -161,8 +160,19 @@ public function authenticate($provider)
}
}

/**
* @param string $provider
*
* @return boolean
*/
public function isConnectedWithProvider($provider)
{
return $this->hybridAuth->isConnectedWith($provider);
}

/**
* logout from all providers when typo3 logout takes place
* return void
*/
public function logout()
{
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"homepage": "http://typo3.org",
"license": ["GPL-2.0+"],
"version": "1.1.8",
"version": "1.1.9",
"require": {
"hybridauth/hybridauth": "~2.9.0"
},
Expand Down
2 changes: 1 addition & 1 deletion ext_emconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
'uploadfolder' => 0,
'createDirs' => '',
'clearCacheOnLoad' => 0,
'version' => '1.1.8',
'version' => '1.1.9',
'constraints' => array(
'depends' => array(
'typo3' => '6.2.0-8.7.99'
Expand Down
86 changes: 45 additions & 41 deletions ext_localconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,55 @@
if (!defined('TYPO3_MODE')) {
die('Access denied.');
}

$composerAutoloadFile = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($_EXTKEY) . 'Resources/Private/Librairies/autoload.php';
if (file_exists($composerAutoloadFile)) {
require_once($composerAutoloadFile);
}

if (TYPO3_MODE === 'FE') {
\TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin(
'MV.' . $_EXTKEY,
'Pi1',
array(
'Auth' => 'list, connect, endpoint',
),
// non-cacheable actions
array(
'Auth' => 'connect, endpoint'
)
);
$extConfig = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['social_auth']);
if ($extConfig['providers.']['facebook.']['enabled'] || $extConfig['providers.']['google.']['enabled'] || $extConfig['providers.']['twitter.']['enabled']) {
$GLOBALS['TYPO3_CONF_VARS']['SVCONF']['auth']['setup']['FE_fetchUserIfNoSession'] = true;
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_post_processing'][$_EXTKEY] = 'MV\SocialAuth\Hooks\LogOffHook->postProcessing';
$boot = function ($_EXTKEY) {
$composerAutoloadFile = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($_EXTKEY) . 'Resources/Private/Librairies/autoload.php';
if (file_exists($composerAutoloadFile)) {
require_once($composerAutoloadFile);
}
if (TYPO3_MODE === 'FE') {
\TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin(
'MV.' . $_EXTKEY,
'Pi1',
array(
'Auth' => 'list, connect, endpoint',
),
// non-cacheable actions
array(
'Auth' => 'connect, endpoint'
)
);
$extConfig = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['social_auth']);
if ($extConfig['providers.']['facebook.']['enabled'] || $extConfig['providers.']['google.']['enabled'] || $extConfig['providers.']['twitter.']['enabled']) {
$GLOBALS['TYPO3_CONF_VARS']['SVCONF']['auth']['setup']['FE_fetchUserIfNoSession'] = true;
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_post_processing'][$_EXTKEY] = 'MV\SocialAuth\Hooks\LogOffHook->postProcessing';
}
//add marker to felogin if is loaded
if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('felogin')) {
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['felogin']['postProcContent'][$_EXTKEY] = 'MV\SocialAuth\Hooks\FeLoginHook->postProcContent';
}
}
}

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addService($_EXTKEY,
'auth' /* sv type */,
MV\SocialAuth\Service\SocialAuthenticationService::class /* sv key */,
array(
'title' => 'Social Authentification Service',
'description' => 'authentication for users from social providers (facebook, twitter...)',
'subtype' => 'authUserFE,getUserFE',
'available' => true,
'priority' => 82, /* will be called before default typo3 authentication service */
'quality' => 82,
'os' => '',
'exec' => '',
'className' => MV\SocialAuth\Service\SocialAuthenticationService::class,
)
);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addService($_EXTKEY,
'auth' /* sv type */,
MV\SocialAuth\Service\SocialAuthenticationService::class /* sv key */,
array(
'title' => 'Social Authentification Service',
'description' => 'authentication for users from social providers (facebook, twitter...)',
'subtype' => 'authUserFE,getUserFE',
'available' => true,
'priority' => 82, /* will be called before default typo3 authentication service */
'quality' => 82,
'os' => '',
'exec' => '',
'className' => MV\SocialAuth\Service\SocialAuthenticationService::class,
)
);

#Exclude some params
$GLOBALS['TYPO3_CONF_VARS']['FE']['cacheHash']['excludedParameters'][] = 'tx_socialauth_pi1[provider]';
$GLOBALS['TYPO3_CONF_VARS']['FE']['cacheHash']['excludedParameters'][] = 'tx_socialauth_pi1[redirect]';
};

$boot($_EXTKEY);
unset($boot);

#Exclude some params
$GLOBALS['TYPO3_CONF_VARS']['FE']['cacheHash']['excludedParameters'][] = 'tx_socialauth_pi1[provider]';
$GLOBALS['TYPO3_CONF_VARS']['FE']['cacheHash']['excludedParameters'][] = 'tx_socialauth_pi1[redirect]';

0 comments on commit 23158c8

Please sign in to comment.