Magento OAuth 身份验证无法处理自定义 URL 方案

问题描述 投票:0回答:2

要取回 OAuth 令牌,我想使用自定义 URL 方案作为我的 iOS 应用程序的回调 URL,例如

myapp://oauth-callback

然而,Magento 似乎无法处理这样的 URL 方案,因为它返回以下错误消息

HTTP Status 400: Bad Request, Response: oauth_problem=parameter_rejected&message=oauth_callback

如果我设置以 http:// 开头的回调 URL,则请求确实有效,并且我收到了 OAuth 令牌,问题是操作系统使用此 URL 打开浏览器,这是我们的应用程序中不需要的行为。

ios swift magento oauth
2个回答
3
投票

由于“myapp”,您的 url 在 Zend_Uri::check($url) 中无效。 这是 check() 函数:

public static function check($uri)
{
    try {
        $uri = self::factory($uri);
    } catch (Exception $e) {
        return false;
    }

    return $uri->valid();
}

让我们看看工厂是如何运作的:

public static function factory($uri = 'http', $className = null)
    {
        // Separate the scheme from the scheme-specific parts
        $uri            = explode(':', $uri, 2);
        $scheme         = strtolower($uri[0]);
        $schemeSpecific = isset($uri[1]) === true ? $uri[1] : '';

        if (strlen($scheme) === 0) {
            #require_once 'Zend/Uri/Exception.php';
            throw new Zend_Uri_Exception('An empty string was supplied for the scheme');
        }

        // Security check: $scheme is used to load a class file, so only alphanumerics are allowed.
        if (ctype_alnum($scheme) === false) {
            #require_once 'Zend/Uri/Exception.php';
            throw new Zend_Uri_Exception('Illegal scheme supplied, only alphanumeric characters are permitted');
        }

        if ($className === null) {
            /**
             * Create a new Zend_Uri object for the $uri. If a subclass of Zend_Uri exists for the
             * scheme, return an instance of that class. Otherwise, a Zend_Uri_Exception is thrown.
             */
            switch ($scheme) {
                case 'http':
                    // Break intentionally omitted
                case 'https':
                    $className = 'Zend_Uri_Http';
                    break;

                case 'mailto':
                    // TODO
                default:
                    #require_once 'Zend/Uri/Exception.php';
                    throw new Zend_Uri_Exception("Scheme \"$scheme\" is not supported");
                    break;
            }
        }

        #require_once 'Zend/Loader.php';
        try {
            Zend_Loader::loadClass($className);
        } catch (Exception $e) {
            #require_once 'Zend/Uri/Exception.php';
            throw new Zend_Uri_Exception("\"$className\" not found");
        }

        $schemeHandler = new $className($scheme, $schemeSpecific);

        if (! $schemeHandler instanceof Zend_Uri) {
            #require_once 'Zend/Uri/Exception.php';
            throw new Zend_Uri_Exception("\"$className\" is not an instance of Zend_Uri");
        }

        return $schemeHandler;
    }

它使用 Zend_Uri_Http 类来表示 http:// 和 https:// 方案。您只需在列表中添加自己的方案即可。

如何解决?

  1. 将 lib/Zend/Uri.php 文件复制到 app/code/local/Zend/Uri.php
  2. 在工厂函数中找到“switch”代码块(~第119行)并将其替换为下一个:
    
    switch ($scheme) {
    case 'http':
        // Break intentionally omitted
    case 'https':
        $className = 'Zend_Uri_Http';
        break;
    case 'myapp':
        $className = 'Zend_Uri_Http';
        break;
    case 'mailto':
        // TODO
    default:
        #require_once 'Zend/Uri/Exception.php';
        throw new Zend_Uri_Exception("Scheme \"$scheme\" is not supported");
        break;
    }
    
  3. 保存文件并清除 Magento 缓存。现在你的 myapp:// 方案将作为 http:// 和 https://

0
投票

您可以尝试此拉取请求中提供的修复。虽然它适用于 OpenMage,但它应该适用于 Magento 1.9。

© www.soinside.com 2019 - 2024. All rights reserved.