首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有效请求签名生成

有效请求签名生成
EN

Stack Overflow用户
提问于 2018-11-23 13:17:22
回答 1查看 97关注 0票数 0

我试图连接到一个外部API,更具体地说,在这里复制这个Ruby代码:工作代码。我的env变量是正确的,也就是说,如果我更改它们,就会得到一个未找到响应的帐户。如果我使用上面链接中提供的Ruby代码,它就能工作。

代码语言:javascript
复制
    $date = new \DateTime(now());
    $date->setTimezone(new \DateTimeZone('Europe/Athens'));

    # Generates a date in this format: Wed, 21 Nov 2018 22:37:14 GMT
    $date = $date->format(\DateTime::RFC7231);
    $body = [
        'data' => [
            'type' => 'profile'
        ]
    ];
    $request_target = 'post /profiles';

    # Generates a digest using the request body
    $digest = 'SHA-256=' . base64_encode(hash('sha256', json_encode($body), true));

    $content_type = 'application/vnd.api+json';
    $accept_type = 'application/vnd.api+json';
    $version = '2016-09-01';

    # Generates the signing string. Note that the parts of the string are
    # concatenated with a newline character
    $signing_string = implode('\n', [
        "(request-target): {$request_target}",
        "date: {$date}",
        "digest: {$digest}"
    ]);

    # Creates the HMAC-SHA256 digest using the API secret and then base64
    # encodes that value
    $signature = trim(base64_encode(hash_hmac('sha256', $signing_string, env('COGNITO_SECRET'), true)));

    # Creates the authorization header and concatenates it together using
    # a comma
    $authorization = implode(',', [
        'Signature keyId="' . env('COGNITO_API_KEY') .'"',
        'algorithm="hmac-sha256"',
        'headers="(request-target) date digest"',
        'signature="' . $signature . '"'
    ]);

    $headers = [
        'Content-Type' => $content_type,
        'Cognito-Version' => $version,
        'Accept' => $accept_type,
        'Date' => $date,
        'Digest' => $digest,
        'Authorization' => $authorization,
    ];

    try {

        # Put everything together and execute the request. Note that the headers
        # are defined in the same order as they are defined in the Authorization
        # header above. They can be in any order, but they must be consistent.
        $client = new Client();
        $response = $client->post(env('COGNITO_ENDPOINT') . '/profiles', [
            RequestOptions::HEADERS => $headers,
            RequestOptions::JSON => $body,
            //'debug' => true
        ]);

    } catch (RequestException $e) {
        return $this->respondWithGeneralError(json_decode($e->getResponse()->getBody()));
    }catch (\Exception $e){
        return $this->respondWithGeneralError($e->getMessage());
    }

    return $this->respondWithSuccess('auth', $response);

但是我无法创建正确的签名,因为我得到的响应无法验证来自端点的请求签名

有人能在我的代码中发现任何错误或错位吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-23 14:32:06

我觉得这是个问题

代码语言:javascript
复制
$signing_string = implode('\n'

你对引号有错误,你需要使用双引号。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53447454

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档