Miglioramento supporto OAuth2

This commit is contained in:
Dasc3er 2021-07-27 14:49:18 +02:00
parent 5135a2c04c
commit 43203e5385
4 changed files with 45 additions and 35 deletions

View File

@ -33,6 +33,7 @@ class OAuth2
'help' => 'https://docs.openstamanager.com/faq/configurazione-oauth2#google', 'help' => 'https://docs.openstamanager.com/faq/configurazione-oauth2#google',
], ],
]; ];
protected $provider; protected $provider;
protected $account; protected $account;
@ -44,7 +45,7 @@ class OAuth2
} }
/** /**
* Inizializza il ->inprovider per l'autenticazione OAuth2. * Inizializza il provider per l'autenticazione OAuth2.
*/ */
public function init() public function init()
{ {
@ -57,6 +58,7 @@ class OAuth2
'clientId' => $this->account->client_id, 'clientId' => $this->account->client_id,
'clientSecret' => $this->account->client_secret, 'clientSecret' => $this->account->client_secret,
'redirectUri' => $redirect_uri, 'redirectUri' => $redirect_uri,
'accessType' => 'offline',
]); ]);
// Configurazioni specifiche per il provider di Microsoft Azure // Configurazioni specifiche per il provider di Microsoft Azure
@ -112,14 +114,14 @@ class OAuth2
// Fetch the authorization URL from the provider; this returns the // Fetch the authorization URL from the provider; this returns the
// urlAuthorize option and generates and applies any necessary parameters // urlAuthorize option and generates and applies any necessary parameters
// (e.g. state). // (e.g. state).
$authorizationUrl = $provider->getAuthorizationUrl($options); $authorization_url = $provider->getAuthorizationUrl($options);
// Get the state generated for you and store it to the session. // Get the state generated for you and store it to the session.
$this->account->oauth2_state = $provider->getState(); $this->account->oauth2_state = $provider->getState();
$this->account->save(); $this->account->save();
// Redirect the user to the authorization URL. // Redirect the user to the authorization URL.
return $authorizationUrl; return $authorization_url;
} elseif (!empty($this->account->oauth2_state) && $this->account->oauth2_state !== $state) { } elseif (!empty($this->account->oauth2_state) && $this->account->oauth2_state !== $state) {
$this->account->oauth2_state = null; $this->account->oauth2_state = null;
$this->account->save(); $this->account->save();
@ -129,27 +131,23 @@ class OAuth2
$this->account->oauth2_state = null; $this->account->oauth2_state = null;
$this->account->save(); $this->account->save();
// Try to get an access token using the authorization code grant. // Try to get an access token using the authorization code grant
$accessToken = $provider->getAccessToken('authorization_code', [ $access_token = $provider->getAccessToken('authorization_code', [
'code' => $code, 'code' => $code,
]); ]);
//dd($accessToken); $refresh_token = $access_token->getRefreshToken();
$this->setAccessToken($accessToken); $this->updateTokens($access_token, $refresh_token);
} }
return null; return null;
} }
/** public function getRefreshToken()
* Imposta l'access token per l'autenticazione OAuth2.
*
* @param AccessToken|null
*/
public function setAccessToken($value)
{ {
$this->account->access_token = serialize($value); $this->checkTokens();
$this->account->save();
return $this->account->refresh_token;
} }
/** /**
@ -159,32 +157,41 @@ class OAuth2
*/ */
public function getAccessToken() public function getAccessToken()
{ {
$this->checkTokens();
return unserialize($this->account->access_token);
}
protected function checkTokens() {
$access_token = unserialize($this->account->access_token); $access_token = unserialize($this->account->access_token);
if (!empty($access_token) && $access_token->hasExpired()) { if (!empty($access_token) && $access_token->hasExpired()) {
// Tentativo di refresh del token di accessp // Tentativo di refresh del token di accesso
if (!empty($access_token->getRefreshToken())) { $refresh_token = $this->account->refresh_token;
if (!empty($refresh_token)) {
$access_token = $this->getProvider()->getAccessToken('refresh_token', [ $access_token = $this->getProvider()->getAccessToken('refresh_token', [
'refresh_token' => $access_token->getRefreshToken(), 'refresh_token' => $this->account->refresh_token,
]); ]);
$refresh_token = $access_token->getRefreshToken();
} else { } else {
$access_token = null; $access_token = null;
$refresh_token = null;
} }
$this->setAccessToken($access_token); $this->updateTokens($access_token, $refresh_token);
} }
return $access_token;
} }
public function getRefreshToken() /**
* Imposta l'access token per l'autenticazione OAuth2.
*
* @param AccessToken|null
*/
public function updateTokens($access_token, $refresh_token)
{ {
$access_token = unserialize($this->account->access_token); $this->account->access_token = serialize($access_token);
$this->account->refresh_token = $refresh_token;
if (!empty($access_token)) { $this->account->save();
return $access_token->getRefreshToken();
}
return null;
} }
} }

View File

@ -21,6 +21,7 @@ if (!empty($state)) {
// Impostazione access token a null per reimpostare la configurazione // Impostazione access token a null per reimpostare la configurazione
$account->access_token = null; $account->access_token = null;
$account->refresh_token = null;
$account->save(); $account->save();
} }
@ -31,10 +32,10 @@ if (empty($account)) {
} }
// Inizializzazione // Inizializzazione
$oauth = new OAuth2($account); $oauth2 = new OAuth2($account);
// Redirect all'URL di autorizzazione del servizio esterno // Redirect all'URL di autorizzazione del servizio esterno
$redirect = $oauth->configure($code, $state); $redirect = $oauth2->configure($code, $state);
// Redirect automatico al record // Redirect automatico al record
if (empty($redirect)) { if (empty($redirect)) {

View File

@ -37,7 +37,7 @@ class EmailNotification extends PHPMailer implements NotificationInterface
public function __construct($account = null, $exceptions = null) public function __construct($account = null, $exceptions = null)
{ {
parent::__construct($exceptions); parent::__construct(true);
$this->CharSet = 'UTF-8'; $this->CharSet = 'UTF-8';
@ -54,7 +54,7 @@ class EmailNotification extends PHPMailer implements NotificationInterface
$this->IsSMTP(); $this->IsSMTP();
// Impostazioni di debug // Impostazioni di debug
$this->SMTPDebug = \App::debug() ? 2 : 0; $this->SMTPDebug = 2;
$this->Debugoutput = function ($str, $level) { $this->Debugoutput = function ($str, $level) {
$this->infos[] = $str; $this->infos[] = $str;
}; };
@ -212,6 +212,7 @@ class EmailNotification extends PHPMailer implements NotificationInterface
// Segnalazione degli errori // Segnalazione degli errori
if (!$result) { if (!$result) {
$logger = logger(); $logger = logger();
dd($this->infos);
foreach ($this->infos as $info) { foreach ($this->infos as $info) {
$logger->addRecord(\Monolog\Logger::ERROR, $info); $logger->addRecord(\Monolog\Logger::ERROR, $info);
} }

View File

@ -148,4 +148,5 @@ ALTER TABLE `em_accounts` ADD `provider` varchar(255),
ADD `client_id` TEXT, ADD `client_id` TEXT,
ADD `client_secret` TEXT, ADD `client_secret` TEXT,
ADD `oauth2_state` TEXT, ADD `oauth2_state` TEXT,
ADD `access_token` TEXT; ADD `access_token` TEXT,
ADD `refresh_token` TEXT;