|
|
@@ -1,17 +1,20 @@
|
|
|
<?php
|
|
|
namespace Opentalent\OtConnect\Service;
|
|
|
|
|
|
+use DateTime;
|
|
|
use GuzzleHttp\Client;
|
|
|
use GuzzleHttp\Cookie\CookieJar;
|
|
|
use GuzzleHttp\Cookie\SetCookie;
|
|
|
+use GuzzleHttp\Exception\GuzzleException;
|
|
|
use GuzzleHttp\Exception\RequestException;
|
|
|
+use TYPO3\CMS\Core\Crypto\Random;
|
|
|
use TYPO3\CMS\Core\Database\ConnectionPool;
|
|
|
use TYPO3\CMS\Core\TimeTracker\TimeTracker;
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
|
|
use \TYPO3\CMS\Core\Authentication\AbstractAuthenticationService;
|
|
|
|
|
|
/**
|
|
|
- * Service "OpenID Authentication" for the "openid" extension.
|
|
|
+ * Authentication service based on the Opentalent API.
|
|
|
* See the README for more
|
|
|
*/
|
|
|
class OtAuthenticationService extends AbstractAuthenticationService
|
|
|
@@ -36,7 +39,7 @@ class OtAuthenticationService extends AbstractAuthenticationService
|
|
|
];
|
|
|
|
|
|
/**
|
|
|
- * The min delay to wait before the getUser method may regenerate the user's data in DB (seconds)
|
|
|
+ * The time in seconds during which the user's data in DB won't be re-updated after the last successful update
|
|
|
* Set it to 0 to disable the delay
|
|
|
*/
|
|
|
CONST USER_UPDATE_DELAY = 300;
|
|
|
@@ -152,9 +155,10 @@ class OtAuthenticationService extends AbstractAuthenticationService
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Return the name of the user currently authenticated on the API side, or null if no user is logged in
|
|
|
+ * Returns the name of the user currently authenticated on the API side, or null if no user is logged in
|
|
|
*
|
|
|
* @return string|null
|
|
|
+ * @throws GuzzleException
|
|
|
*/
|
|
|
protected function getAuthenticatedUsername() {
|
|
|
|
|
|
@@ -175,7 +179,7 @@ class OtAuthenticationService extends AbstractAuthenticationService
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Update the guzzle cookie jar with the current ones
|
|
|
+ * Update the guzzle cookie jar with the current session's ones
|
|
|
*/
|
|
|
private function fillCookieJar() {
|
|
|
|
|
|
@@ -198,6 +202,7 @@ class OtAuthenticationService extends AbstractAuthenticationService
|
|
|
* @param string $username
|
|
|
* @param string $password
|
|
|
* @return bool Returns true if the api accepted the login request
|
|
|
+ * @throws GuzzleException
|
|
|
*/
|
|
|
protected function logUser($username, $password) {
|
|
|
|
|
|
@@ -258,11 +263,11 @@ class OtAuthenticationService extends AbstractAuthenticationService
|
|
|
$q = $connection->select(['tx_otconnect_generationDate'], 'fe_users', ['username' => $username]);
|
|
|
$strGenDate = $q->fetch(3)[0];
|
|
|
|
|
|
- $genDate = \DateTime::createFromFormat("Y-m-d H:i:s", $strGenDate);
|
|
|
+ $genDate = DateTime::createFromFormat("Y-m-d H:i:s", $strGenDate);
|
|
|
if ($genDate == null) {
|
|
|
return true;
|
|
|
}
|
|
|
- $now = new \DateTime();
|
|
|
+ $now = new DateTime();
|
|
|
$diff = $now->getTimestamp() - $genDate->getTimestamp();
|
|
|
|
|
|
return ($diff > self::USER_UPDATE_DELAY);
|
|
|
@@ -289,7 +294,7 @@ class OtAuthenticationService extends AbstractAuthenticationService
|
|
|
$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('fe_users');
|
|
|
|
|
|
// Since we don't want to store the password in the TYPO3 DB, we store a random string instead
|
|
|
- $randomStr = (new \TYPO3\CMS\Core\Crypto\Random)->generateRandomHexString(10);
|
|
|
+ $randomStr = (new Random)->generateRandomHexString(10);
|
|
|
|
|
|
// Front-end user
|
|
|
$fe_row = [
|
|
|
@@ -360,6 +365,8 @@ class OtAuthenticationService extends AbstractAuthenticationService
|
|
|
$response = $this->client->request('GET', self::GET_USER_DATA_URI, ['cookies' => $this->jar]);
|
|
|
} catch (RequestException $e) {
|
|
|
return [];
|
|
|
+ } catch (GuzzleException $e) {
|
|
|
+ return [];
|
|
|
}
|
|
|
return json_decode($response->getBody(), true);
|
|
|
}
|
|
|
@@ -371,6 +378,7 @@ class OtAuthenticationService extends AbstractAuthenticationService
|
|
|
*
|
|
|
* @param array $user Data of user.
|
|
|
* @return int Code that shows if user is really authenticated.
|
|
|
+ * @throws GuzzleException
|
|
|
*/
|
|
|
public function authUser(array $user)
|
|
|
{
|
|
|
@@ -423,6 +431,8 @@ class OtAuthenticationService extends AbstractAuthenticationService
|
|
|
|
|
|
} catch (RequestException $e) {
|
|
|
return false;
|
|
|
+ } catch (GuzzleException $e) {
|
|
|
+ return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -430,6 +440,7 @@ class OtAuthenticationService extends AbstractAuthenticationService
|
|
|
* Unset a cookie by reducing its expiration date
|
|
|
*
|
|
|
* @param string $name
|
|
|
+ * @return bool
|
|
|
*/
|
|
|
protected function unset_cookie(string $name) {
|
|
|
$res = setcookie($name, '', time() - 1, '/', self::COOKIE_DOMAIN);
|