diff --git a/src/Exception/AuthConfigException.php b/src/Exception/AuthConfigException.php new file mode 100644 index 0000000..2ee3bed --- /dev/null +++ b/src/Exception/AuthConfigException.php @@ -0,0 +1,12 @@ +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; } @@ -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'); diff --git a/src/Model/Config/Backend/AuthFile.php b/src/Model/Config/Backend/AuthFile.php new file mode 100644 index 0000000..b49b419 --- /dev/null +++ b/src/Model/Config/Backend/AuthFile.php @@ -0,0 +1,70 @@ + $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']; + } +} diff --git a/src/Model/Config/Source/AuthType.php b/src/Model/Config/Source/AuthType.php new file mode 100644 index 0000000..a7faa3c --- /dev/null +++ b/src/Model/Config/Source/AuthType.php @@ -0,0 +1,27 @@ + __('Client ID/Secret'), 'value' => self::AUTH_TYPE_CLIENT_ID_SECRET], + ['label' => __('File'), 'value' => self::AUTH_TYPE_FILE] + ]; + } +} diff --git a/src/etc/adminhtml/system.xml b/src/etc/adminhtml/system.xml index a0b3218..5da4be3 100644 --- a/src/etc/adminhtml/system.xml +++ b/src/etc/adminhtml/system.xml @@ -6,19 +6,39 @@ - + + tr33m4n\OauthGmail\Model\Config\Source\AuthType + + + + tr33m4n\OauthGmail\Model\Config\Backend\AuthFile + oauth_gmail + + file + + + + + client_id_secret + - Magento\Config\Model\Config\Backend\Encrypted + + client_id_secret + - + tr33m4n\OauthGmail\Block\Adminhtml\System\Config\Authenticate - + tr33m4n\OauthGmail\Block\Adminhtml\System\Config\SendTest diff --git a/src/etc/config.xml b/src/etc/config.xml index de40688..06c7d52 100644 --- a/src/etc/config.xml +++ b/src/etc/config.xml @@ -5,6 +5,7 @@ + client_id_secret