Skip to content

Commit

Permalink
Add support for Magento 2 OAuth 1.0a workflow and PECL oauth extension.
Browse files Browse the repository at this point in the history
  • Loading branch information
colinmollenhour committed Jun 20, 2024
1 parent 6f2e5cc commit 008712f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
6 changes: 4 additions & 2 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ FROM php:8.2-fpm

# Install SOAP
RUN apt-get update -y \
&& apt-get install -y libxml2-dev \
&& apt-get install -y libxml2-dev libpcre3-dev \
&& apt-get clean -y \
&& docker-php-ext-install soap
&& docker-php-ext-install soap \
&& pecl install oauth \
&& docker-php-ext-enable oauth

COPY php.ini /usr/local/etc/php/conf.d/zz-shipstream.ini

Expand Down
56 changes: 56 additions & 0 deletions pub/magento2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
require __DIR__.'/../app/bootstrap.php';

if (isset($_SERVER['PATH_INFO']) && preg_match_all('/\w+\/\w+/', $_SERVER['PATH_INFO'], $matches)) {
foreach ($matches[0] as $data) {
list($key, $value) = explode('/', $data);
if ( ! isset($_GET[$key])) {
$_GET[$key] = $value;
}
}
}

$debug = ! empty($_GET['debug']);
$action = $_GET['action'] ?? NULL;

try {
$plugin = $_GET['plugin'] ?? NULL;
if ( ! $plugin) {
throw new Exception('Plugin not specified.', 400);
}
$consumerKey = $_REQUEST['oauth_consumer_key'] ?? NULL;
if (empty($consumerKey)) {
throw new Exception('oauth_consumer_key not specified.', 400);
}
$middleware = new Middleware($plugin, $debug);
switch ($action) {
case 'activate':
$middleware->saveCache('magento2_'.$consumerKey, $_POST, 3600);
$middleware->log('Magento 2 OAuth data saved.');
echo 'OK';
break;
case 'authenticate':
$oauthData = $middleware->loadCache('magento2_'.$consumerKey);
if (empty($oauthData)) {
throw new Exception('OAuth data not found.', 400);
}
$middleware->oauthHandleRedirect([
'oauth_consumer_key' => $oauthData['oauth_consumer_key'],
'oauth_consumer_secret' => $oauthData['oauth_consumer_secret'],
'oauth_verifier' => $oauthData['oauth_verifier'],
]);
header("Location: {$_REQUEST['success_call_back']}");
$middleware->log('Magento 2 OAuth completed.');
break;
}
} catch (Throwable $e) {
if (empty($middleware)) {
error_log($e->getMessage());
} else {
$middleware->logException($e);
}
http_response_code(400);
echo $e->getMessage();
}

0 comments on commit 008712f

Please sign in to comment.