CodeCommitsIssuesPull requestsActionsInsightsSecurity
c6df5ba1427524b951136a5a760607f514f5e419

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

Controller/Adminhtml/Plugin/Proxy.php

129lines · modecode

1<?php
2namespace CloudFlare\Plugin\Controller\Adminhtml\Plugin;
3
4use \Magento\Backend\App\AbstractAction;
5use \Magento\Backend\App\Action\Context;
6use \Magento\Framework\Controller\Result\JsonFactory;
7use \Psr\Log\LoggerInterface;
8
9use \CloudFlare\Plugin\Backend;
10use \CloudFlare\Plugin\Model\KeyValueFactory;
11use \CF\Integration\DefaultConfig;
12use GuzzleHttp;
13
14
15class Proxy extends AbstractAction {
16
17 protected $clientAPIClient;
18 protected $config;
19 protected $dataStore;
20 protected $integrationContext;
21 protected $logger;
22 protected $keyValueFactory;
23 protected $magentoAPI;
24 protected $pluginAPIClient;
25
26 const FORM_KEY = "form_key";
27
28 /**
29 * @param \Magento\Backend\App\Action\Context $context
30 * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
31 * @param LoggerInterface $logger
32 */
33 public function __construct(
34 Context $context,
35 JsonFactory $resultJsonFactory,
36 LoggerInterface $logger,
37 KeyValueFactory $keyValueFactory
38
39 ) {
40 $this->resultJsonFactory = $resultJsonFactory;
41 $this->logger = $logger;
42 $this->keyValueFactory = $keyValueFactory;
43
44 $this->config = new DefaultConfig("[]"); //config only used for debug mode but we use monolog so not based on config anymore
45 $this->magentoAPI = new Backend\MagentoAPI($this->keyValueFactory, $this->logger);
46 $this->dataStore = new Backend\DataStore($this->magentoAPI);
47 $this->integrationContext = new \CF\Integration\DefaultIntegration($this->config, $this->magentoAPI, $this->dataStore, $this->logger);
48 $this->clientAPIClient = new \CF\API\Client($this->integrationContext);
49 $this->pluginAPIClient = new \CF\API\Plugin($this->integrationContext);
50
51 parent::__construct($context);
52 }
53
54 /**
55 * @return \Magento\Framework\Controller\Result\Json
56 */
57 public function execute() {
58 $result = $this->resultJsonFactory->create();
59
60 $magentoRequest = $this->getRequest();
61 $method = $magentoRequest->getMethod();
62 $parameters = $magentoRequest->getParams();
63 $body = $this->getJSONBody();
64 $path = (strtoupper($method === "GET") ? $parameters['proxyURL'] : $body['proxyURL']);
65
66 $request = new \CF\API\Request($method, $path, $parameters, $body);
67
68 $apiClient = null;
69 $routes = null;
70 if($this->isClientAPI($path)) {
71 $apiClient = $this->clientAPIClient;
72 $routes = Backend\ClientRoutes::$routes;
73 } else if($this->isPluginAPI($path)) {
74 $apiClient = $this->pluginAPIClient;
75 $routes = Backend\PluginRoutes::$routes;
76 } else {
77 $this->logger->error("Bad Request: ". $request->getUrl());
78 return $result->setData($this->clientAPIClient->createAPIError($request->getUrl()));
79 }
80
81 $router = new \CF\Router\DefaultRestAPIRouter($this->integrationContext, $apiClient, $routes);
82 $response = $router->route($request);
83
84 return $result->setData($response);
85 }
86
87 /**
88 * @param $path
89 * @return bool
90 */
91 public function isClientAPI($path) {
92 return (strpos($path, \CF\API\Client::ENDPOINT) !== false);
93 }
94
95 /**
96 * @param $path
97 * @return bool
98 */
99 public function isPluginAPI($path) {
100 return (strpos($path, \CF\API\Plugin::ENDPOINT) !== false);
101 }
102
103 /*
104 * Magento CSRF validation can't find the CSRF Token "form_key" if its in the JSON
105 * so we copy it from the JSON body to the Magento request parameters.
106 */
107 public function _processUrlKeys() {
108 $requestJsonBody = $this->getJSONBody();
109 if($requestJsonBody !== null && array_key_exists(self::FORM_KEY, $requestJsonBody)) {
110 $this->setJsonFormTokenOnMagentoRequest($requestJsonBody[self::FORM_KEY], $this->getRequest());
111 }
112 return parent::_processUrlKeys();
113 }
114
115 public function getJSONBody() {
116 return json_decode(file_get_contents('php://input'), true);
117 }
118
119 /**
120 * @param $token "form_key"
121 * @param $request
122 */
123 public function setJsonFormTokenOnMagentoRequest($token, $request) {
124 $parameters = $request->getParams();
125 $parameters[self::FORM_KEY] = $token;
126 $request->setParams($parameters);
127 return $request;
128 }
129}