Skip to content

Commit

Permalink
Add logic to handle uploading an auth JSON file
Browse files Browse the repository at this point in the history
  • Loading branch information
tr33m4n committed Jan 5, 2023
1 parent 02230aa commit 4594a7a
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 13 deletions.
12 changes: 12 additions & 0 deletions src/Exception/AuthConfigException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace tr33m4n\OauthGmail\Exception;

use Magento\Framework\Exception\LocalizedException;

class AuthConfigException extends LocalizedException
{
//
}
51 changes: 42 additions & 9 deletions src/Model/Client/ConfigureAuthConfig.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
<?php

declare(strict_types=1);

namespace tr33m4n\OauthGmail\Model\Client;

use Google\Client;
use Magento\Backend\Model\UrlInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Directory\ReadInterface;
use tr33m4n\OauthGmail\Exception\AuthConfigException;
use tr33m4n\OauthGmail\Model\Config\Source\AuthType;

class ConfigureAuthConfig
{
private const XML_CONFIG_AUTH_TYPE = 'system/oauth_gmail/auth_type';

private const XML_CONFIG_AUTH_FILE = 'system/oauth_gmail/auth_file';

private const XML_CONFIG_CLIENT_ID_PATH = 'system/oauth_gmail/client_id';

private const XML_CONFIG_CLIENT_SECRET_PATH = 'system/oauth_gmail/client_secret';
Expand All @@ -17,36 +27,59 @@ class ConfigureAuthConfig

private UrlInterface $url;

private ReadInterface $varDirectory;

/**
* ConfigureAuthConfig constructor.
*
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Framework\Filesystem $filesystem
* @param \Magento\Backend\Model\UrlInterface $url
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
Filesystem $filesystem,
UrlInterface $url
) {
$this->scopeConfig = $scopeConfig;
$this->url = $url;
$this->varDirectory = $filesystem->getDirectoryRead(DirectoryList::VAR_DIR);
}

/**
* Configure auth config
*
* @throws \Google\Exception
* @throws \tr33m4n\OauthGmail\Exception\AuthConfigException
* @param \Google\Client $client
* @return \Google\Client
*/
public function execute(Client $client) : Client
public function execute(Client $client): Client
{
$client->setAuthConfig([
'client_id' => $this->scopeConfig->getValue(self::XML_CONFIG_CLIENT_ID_PATH),
'client_secret' => $this->scopeConfig->getValue(self::XML_CONFIG_CLIENT_SECRET_PATH),
'redirect_uris' => [
$this->getRedirectUrl()
]
]);
switch ($this->scopeConfig->getValue(self::XML_CONFIG_AUTH_TYPE)) {
case AuthType::AUTH_TYPE_FILE:
$authFile = $this->varDirectory->getAbsolutePath(
$this->scopeConfig->getValue(self::XML_CONFIG_AUTH_FILE)
);

if (!$this->varDirectory->isExist($authFile)) {
throw new AuthConfigException(__('Auth file does not exist'));
}

$client->setAuthConfig($authFile);
break;
case AuthType::AUTH_TYPE_CLIENT_ID_SECRET:
$client->setAuthConfig([
'client_id' => $this->scopeConfig->getValue(self::XML_CONFIG_CLIENT_ID_PATH),
'client_secret' => $this->scopeConfig->getValue(self::XML_CONFIG_CLIENT_SECRET_PATH),
'redirect_uris' => [
$this->getRedirectUrl()
]
]);
break;
default:
throw new AuthConfigException(__('Invalid auth type'));
}

return $client;
}
Expand All @@ -56,7 +89,7 @@ public function execute(Client $client) : Client
*
* @return string
*/
private function getRedirectUrl() : string
private function getRedirectUrl(): string
{
$this->url->turnOffSecretKey();
$callbackUrl = $this->url->getUrl('oauthgmail/callback/authenticate');
Expand Down
70 changes: 70 additions & 0 deletions src/Model/Config/Backend/AuthFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace tr33m4n\OauthGmail\Model\Config\Backend;

use Magento\Config\Model\Config\Backend\File;
use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\Filesystem;
use Magento\Framework\Model\Context;
use Magento\Framework\Model\ResourceModel\AbstractResource;
use Magento\Framework\Registry;
use Magento\MediaStorage\Model\File\UploaderFactory;

class AuthFile extends File
{
/**
* AuthFile constructor.
*
* @throws \Magento\Framework\Exception\FileSystemException
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
* @param \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory
* @param \Magento\Config\Model\Config\Backend\File\RequestData\RequestDataInterface $requestData
* @param \Magento\Framework\Filesystem $filesystem
* @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
* @param array<string, mixed> $data
*/
public function __construct(
Context $context,
Registry $registry,
ScopeConfigInterface $config,
TypeListInterface $cacheTypeList,
UploaderFactory $uploaderFactory,
File\RequestData\RequestDataInterface $requestData,
Filesystem $filesystem, AbstractResource $resource = null,
AbstractDb $resourceCollection = null,
array $data = []
) {
// Ensure we do not save the auth JSON file in a public location
$this->_mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);

parent::__construct(
$context,
$registry,
$config,
$cacheTypeList,
$uploaderFactory,
$requestData,
$filesystem,
$resource,
$resourceCollection,
$data
);
}

/**
* @inheritDoc
*/
protected function _getAllowedExtensions(): array
{
return ['json'];
}
}
27 changes: 27 additions & 0 deletions src/Model/Config/Source/AuthType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace tr33m4n\OauthGmail\Model\Config\Source;

use Magento\Framework\Data\OptionSourceInterface;

class AuthType implements OptionSourceInterface
{
public const AUTH_TYPE_FILE = 'file';

public const AUTH_TYPE_CLIENT_ID_SECRET = 'client_id_secret';

/**
* {@inheritdoc}
*
* @return array[]
*/
public function toOptionArray(): array
{
return [
['label' => __('Client ID/Secret'), 'value' => self::AUTH_TYPE_CLIENT_ID_SECRET],
['label' => __('File'), 'value' => self::AUTH_TYPE_FILE]
];
}
}
28 changes: 24 additions & 4 deletions src/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,39 @@
<group id="oauth_gmail" translate="label" type="text" sortOrder="1" showInDefault="1"
showInWebsite="0" showInStore="0">
<label>Oauth Gmail</label>
<field id="client_id" translate="label" type="text" sortOrder="1" showInDefault="1"
<field id="auth_type" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0"
showInStore="0">
<label>Auth Type</label>
<source_model>tr33m4n\OauthGmail\Model\Config\Source\AuthType</source_model>
</field>
<field id="auth_file" translate="label" type="file" sortOrder="2" showInDefault="1" showInWebsite="0"
showInStore="0">
<label>Auth File</label>
<backend_model>tr33m4n\OauthGmail\Model\Config\Backend\AuthFile</backend_model>
<upload_dir config="system" scope_info="1">oauth_gmail</upload_dir>
<depends>
<field id="system/oauth_gmail/auth_type">file</field>
</depends>
</field>
<field id="client_id" translate="label" type="text" sortOrder="3" showInDefault="1"
showInWebsite="0" showInStore="0">
<label>Client ID</label>
<depends>
<field id="system/oauth_gmail/auth_type">client_id_secret</field>
</depends>
</field>
<field id="client_secret" translate="label" type="obscure" sortOrder="2" showInDefault="1"
<field id="client_secret" translate="label" type="obscure" sortOrder="4" showInDefault="1"
showInWebsite="0" showInStore="0">
<label>Client Secret</label>
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
<depends>
<field id="system/oauth_gmail/auth_type">client_id_secret</field>
</depends>
</field>
<field id="authenticate" sortOrder="3" showInDefault="1" showInWebsite="0" showInStore="0">
<field id="authenticate" sortOrder="5" showInDefault="1" showInWebsite="0" showInStore="0">
<frontend_model>tr33m4n\OauthGmail\Block\Adminhtml\System\Config\Authenticate</frontend_model>
</field>
<field id="send_test" sortOrder="3" showInDefault="1" showInWebsite="0" showInStore="0">
<field id="send_test" sortOrder="6" showInDefault="1" showInWebsite="0" showInStore="0">
<frontend_model>tr33m4n\OauthGmail\Block\Adminhtml\System\Config\SendTest</frontend_model>
</field>
</group>
Expand Down
1 change: 1 addition & 0 deletions src/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<system>
<oauth_gmail>
<client_secret backend_model="Magento\Config\Model\Config\Backend\Encrypted"/>
<auth_type>client_id_secret</auth_type>
</oauth_gmail>
</system>
</default>
Expand Down

0 comments on commit 4594a7a

Please sign in to comment.