mirror of
https://github.com/the-djmaze/snappymail.git
synced 2024-09-20 07:35:55 +08:00
Test to add GMail as AdditionalAccount but it fails due to missing cookies
This commit is contained in:
parent
3e7eb46f33
commit
f73456c462
|
@ -1,10 +1,10 @@
|
||||||
(rl => {
|
(rl => {
|
||||||
const client_id = rl.pluginSettingsGet('login-gmail', 'client_id'),
|
const client_id = rl.pluginSettingsGet('login-gmail', 'client_id'),
|
||||||
login = () => {
|
login = mode => {
|
||||||
document.location = 'https://accounts.google.com/o/oauth2/auth?' + (new URLSearchParams({
|
document.location = 'https://accounts.google.com/o/oauth2/auth?' + (new URLSearchParams({
|
||||||
response_type: 'code',
|
response_type: 'code',
|
||||||
client_id: client_id,
|
client_id: client_id,
|
||||||
redirect_uri: document.location.href + '?LoginGMail',
|
redirect_uri: document.location.href + '?' + mode,
|
||||||
scope: [
|
scope: [
|
||||||
// Primary Google Account email address
|
// Primary Google Account email address
|
||||||
'https://www.googleapis.com/auth/userinfo.email',
|
'https://www.googleapis.com/auth/userinfo.email',
|
||||||
|
@ -36,10 +36,17 @@
|
||||||
container = e.detail.viewModelDom.querySelector('#plugin-Login-BottomControlGroup'),
|
container = e.detail.viewModelDom.querySelector('#plugin-Login-BottomControlGroup'),
|
||||||
btn = Element.fromHTML('<button type="button">GMail</button>'),
|
btn = Element.fromHTML('<button type="button">GMail</button>'),
|
||||||
div = Element.fromHTML('<div class="controls"></div>');
|
div = Element.fromHTML('<div class="controls"></div>');
|
||||||
btn.onclick = login;
|
btn.onclick = ()=>login('LoginGMail');
|
||||||
div.append(btn);
|
div.append(btn);
|
||||||
container && container.append(div);
|
container && container.append(div);
|
||||||
}
|
}
|
||||||
|
if ('PopupsAccount' === e.detail.viewModelTemplateID) {
|
||||||
|
const
|
||||||
|
container = e.detail.viewModelDom.querySelector('footer'),
|
||||||
|
btn = Element.fromHTML('<button class="btn" type="button">GMail</button>');
|
||||||
|
btn.onclick = ()=>login('AddGMail');
|
||||||
|
container && container.append(btn);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,7 @@ class LoginGMailPlugin extends \RainLoop\Plugins\AbstractPlugin
|
||||||
$this->addHook('sieve.before-login', 'clientLogin');
|
$this->addHook('sieve.before-login', 'clientLogin');
|
||||||
|
|
||||||
$this->addPartHook('LoginGMail', 'ServiceLoginGMail');
|
$this->addPartHook('LoginGMail', 'ServiceLoginGMail');
|
||||||
|
$this->addPartHook('AddGMail', 'ServiceAddGMail');
|
||||||
|
|
||||||
// Prevent Disallowed Sec-Fetch Dest: document Mode: navigate Site: cross-site User: true
|
// Prevent Disallowed Sec-Fetch Dest: document Mode: navigate Site: cross-site User: true
|
||||||
$this->addHook('filter.http-paths', 'httpPaths');
|
$this->addHook('filter.http-paths', 'httpPaths');
|
||||||
|
@ -47,16 +48,205 @@ class LoginGMailPlugin extends \RainLoop\Plugins\AbstractPlugin
|
||||||
\trim($oConfig->Get('security', 'secfetch_allow', '') . ';site=cross-site', ';')
|
\trim($oConfig->Get('security', 'secfetch_allow', '') . ';site=cross-site', ';')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
if (!empty($aPaths[0]) && 'AddGMail' === $aPaths[0]) {
|
||||||
|
$oConfig = \RainLoop\Api::Config();
|
||||||
|
$oConfig->Set('security', 'secfetch_allow',
|
||||||
|
\trim($oConfig->Get('security', 'secfetch_allow', '') . ';site=cross-site', ';')
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function ServiceLoginGMail() : string
|
public function ServiceLoginGMail() : string
|
||||||
|
{
|
||||||
|
$oActions = \RainLoop\Api::Actions();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
$aUserInfo = $this->gmailResponse('LoginGMail');
|
||||||
|
$oPassword = new \SnappyMail\SensitiveString($aUserInfo['id']);
|
||||||
|
$oAccount = $oActions->LoginProcess($aUserInfo['email'], $oPassword);
|
||||||
|
// $oAccount = MainAccount::NewInstanceFromCredentials($oActions, $aUserInfo['email'], $aUserInfo['email'], $oPassword, true);
|
||||||
|
if ($oAccount) {
|
||||||
|
// $oActions->SetMainAuthAccount($oAccount);
|
||||||
|
// $oActions->SetAuthToken($oAccount);
|
||||||
|
$oActions->StorageProvider()->Put($oAccount, StorageType::SESSION, \RainLoop\Utils::GetSessionToken(),
|
||||||
|
\SnappyMail\Crypt::EncryptToJSON(static::$auth, $oAccount->CryptKey())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (\Exception $oException)
|
||||||
|
{
|
||||||
|
$oActions->Logger()->WriteException($oException, \LOG_ERR);
|
||||||
|
}
|
||||||
|
$oActions->Location(\RainLoop\Utils::WebPath());
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add as additional account
|
||||||
|
*/
|
||||||
|
public function ServiceAddGMail() : string
|
||||||
|
{
|
||||||
|
$oActions = \RainLoop\Api::Actions();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (empty($_COOKIE)) {
|
||||||
|
// Failed in Firefox
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
$oMainAccount = $oActions->getMainAccountFromToken();
|
||||||
|
$aAccounts = $oActions->GetAccounts($oMainAccount);
|
||||||
|
|
||||||
|
$aUserInfo = $this->gmailResponse('AddGMail');
|
||||||
|
$sEmail = $aUserInfo['email'];
|
||||||
|
if ($oMainAccount->Email() === $sEmail || isset($aAccounts[$sEmail])) {
|
||||||
|
throw new ClientException(Notifications::AccountAlreadyExists);
|
||||||
|
}
|
||||||
|
|
||||||
|
$oPassword = new \SnappyMail\SensitiveString('oauth2');
|
||||||
|
$oAccount = $oActions->LoginProcess($aUserInfo['email'], $oPassword, false);
|
||||||
|
if ($oAccount) {
|
||||||
|
$aAccount = $oAccount->asTokenArray($oMainAccount);
|
||||||
|
$aAccount['name'] = '';
|
||||||
|
$oActions->LocalStorageProvider()->Put($oAccount, StorageType::CONFIG, 'oauth2',
|
||||||
|
\SnappyMail\Crypt::EncryptToJSON(static::$auth, $oAccount->CryptKey())
|
||||||
|
);
|
||||||
|
$aAccounts[$sEmail] = $aAccount;
|
||||||
|
$this->SetAccounts($oMainAccount, $aAccounts);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (\Exception $oException)
|
||||||
|
{
|
||||||
|
print_r($oException);
|
||||||
|
exit($oException->getMessage());
|
||||||
|
$oActions->Logger()->WriteException($oException, \LOG_ERR);
|
||||||
|
}
|
||||||
|
exit;
|
||||||
|
$oActions->Location(\RainLoop\Utils::WebPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function configMapping() : array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
\RainLoop\Plugins\Property::NewInstance('client_id')
|
||||||
|
->SetLabel('Client ID')
|
||||||
|
->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING)
|
||||||
|
->SetAllowedInJs()
|
||||||
|
->SetDescription('https://github.com/the-djmaze/snappymail/wiki/FAQ#gmail'),
|
||||||
|
\RainLoop\Plugins\Property::NewInstance('client_secret')
|
||||||
|
->SetLabel('Client Secret')
|
||||||
|
->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING)
|
||||||
|
->SetEncrypted()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function clientLogin(\RainLoop\Model\Account $oAccount, \MailSo\Net\NetClient $oClient, \MailSo\Net\ConnectSettings $oSettings) : void
|
||||||
|
{
|
||||||
|
if (\str_ends_with($oAccount->Email(), '@gmail.com')) {
|
||||||
|
$oActions = \RainLoop\Api::Actions();
|
||||||
|
if ($oAccount instanceof MainAccount) {
|
||||||
|
try {
|
||||||
|
$aData = static::$auth ?: \SnappyMail\Crypt::DecryptFromJSON(
|
||||||
|
$oActions->StorageProvider()->Get($oAccount, StorageType::SESSION, \RainLoop\Utils::GetSessionToken()),
|
||||||
|
$oAccount->CryptKey()
|
||||||
|
);
|
||||||
|
} catch (\Throwable $oException) {
|
||||||
|
// $oActions->Logger()->WriteException($oException, \LOG_ERR);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!empty($aData['expires']) && !empty($aData['access_token']) && !empty($aData['refresh_token'])) {
|
||||||
|
if (\time() >= $aData['expires']) {
|
||||||
|
$iExpires = \time();
|
||||||
|
$oGMail = $this->gmailConnector();
|
||||||
|
if ($oGMail) {
|
||||||
|
$aRefreshTokenResponse = $oGMail->getAccessToken(
|
||||||
|
static::TOKEN_URI,
|
||||||
|
'refresh_token',
|
||||||
|
array('refresh_token' => $aData['refresh_token'])
|
||||||
|
);
|
||||||
|
if (!empty($aRefreshTokenResponse['result']['access_token'])) {
|
||||||
|
$aData['access_token'] = $aRefreshTokenResponse['result']['access_token'];
|
||||||
|
$aResponse['expires'] = $iExpires + $aResponse['expires_in'];
|
||||||
|
$oActions->StorageProvider()->Put($oAccount, StorageType::SESSION, \RainLoop\Utils::GetSessionToken(),
|
||||||
|
\SnappyMail\Crypt::EncryptToJSON($aData, $oAccount->CryptKey())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$oSettings->passphrase = $aData['access_token'];
|
||||||
|
\array_unshift($oSettings->SASLMechanisms, 'OAUTHBEARER', 'XOAUTH2');
|
||||||
|
}
|
||||||
|
} else if ('oauth2' == $oSettings->passphrase) {
|
||||||
|
// \RainLoop\Model\AdditionalAccount
|
||||||
|
try {
|
||||||
|
$aData = static::$auth ?: \SnappyMail\Crypt::DecryptFromJSON(
|
||||||
|
$oActions->LocalStorageProvider()->Get($oAccount, StorageType::CONFIG, 'oauth2'),
|
||||||
|
$oAccount->CryptKey()
|
||||||
|
);
|
||||||
|
} catch (\Throwable $oException) {
|
||||||
|
// $oActions->Logger()->WriteException($oException, \LOG_ERR);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!empty($aData['expires']) && !empty($aData['access_token']) && !empty($aData['refresh_token'])) {
|
||||||
|
if (\time() >= $aData['expires']) {
|
||||||
|
$iExpires = \time();
|
||||||
|
$oGMail = $this->gmailConnector();
|
||||||
|
if ($oGMail) {
|
||||||
|
$aRefreshTokenResponse = $oGMail->getAccessToken(
|
||||||
|
static::TOKEN_URI,
|
||||||
|
'refresh_token',
|
||||||
|
array('refresh_token' => $aData['refresh_token'])
|
||||||
|
);
|
||||||
|
if (!empty($aRefreshTokenResponse['result']['access_token'])) {
|
||||||
|
$aData['access_token'] = $aRefreshTokenResponse['result']['access_token'];
|
||||||
|
$aResponse['expires'] = $iExpires + $aResponse['expires_in'];
|
||||||
|
$oActions->LocalStorageProvider()->Put($oAccount, StorageType::CONFIG, 'oauth2',
|
||||||
|
\SnappyMail\Crypt::EncryptToJSON($aData, $oAccount->CryptKey())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$oSettings->passphrase = $aData['access_token'];
|
||||||
|
\array_unshift($oSettings->SASLMechanisms, 'OAUTHBEARER', 'XOAUTH2');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function gmailConnector() : ?\OAuth2\Client
|
||||||
|
{
|
||||||
|
$client_id = \trim($this->Config()->Get('plugin', 'client_id', ''));
|
||||||
|
$client_secret = \trim($this->Config()->getDecrypted('plugin', 'client_secret', ''));
|
||||||
|
if ($client_id && $client_secret) {
|
||||||
|
try
|
||||||
|
{
|
||||||
|
$oGMail = new \OAuth2\Client($client_id, $client_secret);
|
||||||
|
$oActions = \RainLoop\Api::Actions();
|
||||||
|
$sProxy = $oActions->Config()->Get('labs', 'curl_proxy', '');
|
||||||
|
if (\strlen($sProxy)) {
|
||||||
|
$oGMail->setCurlOption(CURLOPT_PROXY, $sProxy);
|
||||||
|
$sProxyAuth = $oActions->Config()->Get('labs', 'curl_proxy_auth', '');
|
||||||
|
if (\strlen($sProxyAuth)) {
|
||||||
|
$oGMail->setCurlOption(CURLOPT_PROXYUSERPWD, $sProxyAuth);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $oGMail;
|
||||||
|
}
|
||||||
|
catch (\Exception $oException)
|
||||||
|
{
|
||||||
|
$oActions->Logger()->WriteException($oException, \LOG_ERR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected function gmailResponse(string $query) : array
|
||||||
{
|
{
|
||||||
$oActions = \RainLoop\Api::Actions();
|
$oActions = \RainLoop\Api::Actions();
|
||||||
$oHttp = $oActions->Http();
|
$oHttp = $oActions->Http();
|
||||||
$oHttp->ServerNoCache();
|
$oHttp->ServerNoCache();
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (isset($_GET['error'])) {
|
if (isset($_GET['error'])) {
|
||||||
throw new \RuntimeException($_GET['error']);
|
throw new \RuntimeException($_GET['error']);
|
||||||
}
|
}
|
||||||
|
@ -76,7 +266,7 @@ class LoginGMailPlugin extends \RainLoop\Plugins\AbstractPlugin
|
||||||
'authorization_code',
|
'authorization_code',
|
||||||
array(
|
array(
|
||||||
'code' => $_GET['code'],
|
'code' => $_GET['code'],
|
||||||
'redirect_uri' => $oHttp->GetFullUrl().'?LoginGMail'
|
'redirect_uri' => $oHttp->GetFullUrl().'?'.$query
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
if (200 != $aResponse['code']) {
|
if (200 != $aResponse['code']) {
|
||||||
|
@ -122,102 +312,7 @@ class LoginGMailPlugin extends \RainLoop\Plugins\AbstractPlugin
|
||||||
'expires' => $iExpires
|
'expires' => $iExpires
|
||||||
];
|
];
|
||||||
|
|
||||||
$oPassword = new \SnappyMail\SensitiveString($aUserInfo['id']);
|
return $aUserInfo;
|
||||||
$oAccount = $oActions->LoginProcess($aUserInfo['email'], $oPassword);
|
|
||||||
// $oAccount = MainAccount::NewInstanceFromCredentials($oActions, $aUserInfo['email'], $aUserInfo['email'], $oPassword, true);
|
|
||||||
if ($oAccount) {
|
|
||||||
// $oActions->SetMainAuthAccount($oAccount);
|
|
||||||
// $oActions->SetAuthToken($oAccount);
|
|
||||||
$oActions->StorageProvider()->Put($oAccount, StorageType::SESSION, \RainLoop\Utils::GetSessionToken(),
|
|
||||||
\SnappyMail\Crypt::EncryptToJSON(static::$auth, $oAccount->CryptKey())
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (\Exception $oException)
|
|
||||||
{
|
|
||||||
$oActions->Logger()->WriteException($oException, \LOG_ERR);
|
|
||||||
}
|
|
||||||
$oActions->Location(\RainLoop\Utils::WebPath());
|
|
||||||
exit;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function configMapping() : array
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
\RainLoop\Plugins\Property::NewInstance('client_id')
|
|
||||||
->SetLabel('Client ID')
|
|
||||||
->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING)
|
|
||||||
->SetAllowedInJs()
|
|
||||||
->SetDescription('https://github.com/the-djmaze/snappymail/wiki/FAQ#gmail'),
|
|
||||||
\RainLoop\Plugins\Property::NewInstance('client_secret')
|
|
||||||
->SetLabel('Client Secret')
|
|
||||||
->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING)
|
|
||||||
->SetEncrypted()
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function clientLogin(\RainLoop\Model\Account $oAccount, \MailSo\Net\NetClient $oClient, \MailSo\Net\ConnectSettings $oSettings) : void
|
|
||||||
{
|
|
||||||
if ($oAccount instanceof MainAccount && \str_ends_with($oAccount->Email(), '@gmail.com')) {
|
|
||||||
$oActions = \RainLoop\Api::Actions();
|
|
||||||
try {
|
|
||||||
$aData = static::$auth ?: \SnappyMail\Crypt::DecryptFromJSON(
|
|
||||||
$oActions->StorageProvider()->Get($oAccount, StorageType::SESSION, \RainLoop\Utils::GetSessionToken()),
|
|
||||||
$oAccount->CryptKey()
|
|
||||||
);
|
|
||||||
} catch (\Throwable $oException) {
|
|
||||||
// $oActions->Logger()->WriteException($oException, \LOG_ERR);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!empty($aData['expires']) && !empty($aData['access_token']) && !empty($aData['refresh_token'])) {
|
|
||||||
if (\time() >= $aData['expires']) {
|
|
||||||
$iExpires = \time();
|
|
||||||
$oGMail = $this->gmailConnector();
|
|
||||||
if ($oGMail) {
|
|
||||||
$aRefreshTokenResponse = $oGMail->getAccessToken(
|
|
||||||
static::TOKEN_URI,
|
|
||||||
'refresh_token',
|
|
||||||
array('refresh_token' => $aData['refresh_token'])
|
|
||||||
);
|
|
||||||
if (!empty($aRefreshTokenResponse['result']['access_token'])) {
|
|
||||||
$aData['access_token'] = $aRefreshTokenResponse['result']['access_token'];
|
|
||||||
$aResponse['expires'] = $iExpires + $aResponse['expires_in'];
|
|
||||||
$oActions->StorageProvider()->Put($oAccount, StorageType::SESSION, \RainLoop\Utils::GetSessionToken(),
|
|
||||||
\SnappyMail\Crypt::EncryptToJSON($aData, $oAccount->CryptKey())
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$oSettings->passphrase = $aData['access_token'];
|
|
||||||
\array_unshift($oSettings->SASLMechanisms, 'OAUTHBEARER', 'XOAUTH2');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function gmailConnector() : ?\OAuth2\Client
|
|
||||||
{
|
|
||||||
$client_id = \trim($this->Config()->Get('plugin', 'client_id', ''));
|
|
||||||
$client_secret = \trim($this->Config()->getDecrypted('plugin', 'client_secret', ''));
|
|
||||||
if ($client_id && $client_secret) {
|
|
||||||
try
|
|
||||||
{
|
|
||||||
$oGMail = new \OAuth2\Client($client_id, $client_secret);
|
|
||||||
$oActions = \RainLoop\Api::Actions();
|
|
||||||
$sProxy = $oActions->Config()->Get('labs', 'curl_proxy', '');
|
|
||||||
if (\strlen($sProxy)) {
|
|
||||||
$oGMail->setCurlOption(CURLOPT_PROXY, $sProxy);
|
|
||||||
$sProxyAuth = $oActions->Config()->Get('labs', 'curl_proxy_auth', '');
|
|
||||||
if (\strlen($sProxyAuth)) {
|
|
||||||
$oGMail->setCurlOption(CURLOPT_PROXYUSERPWD, $sProxyAuth);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $oGMail;
|
|
||||||
}
|
|
||||||
catch (\Exception $oException)
|
|
||||||
{
|
|
||||||
$oActions->Logger()->WriteException($oException, \LOG_ERR);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue