ruby-on-rails 如何从亚马逊SNS订阅中获取HTTP令牌?

6tdlim6h  于 2023-02-20  发布在  Ruby
关注(0)|答案(3)|浏览(127)

如何通过http确认订阅Amazon SNS?我目前正在使用Rails应用程序,希望收到电子邮件退回通知。因此,我尝试浏览Amazon论坛,找到的最接近的是:https://forums.aws.amazon.com/thread.jspa?threadID=87555,它告诉我使用request.raw_post获取请求并进行JSON解析。然而,当我尝试这样做时,它给我一个错误消息,说请求为空。我的方法正确吗?有人能指导我如何从Amazon SNS获取订阅令牌,以便我确认订阅吗?

qlzsbp2j

qlzsbp2j1#

这对我很有效。在你的控制器中,创建一个类似下面代码的动作。你想要令牌路由,所以你可以在那里看到它。但是我选择了SubscribeURL路由,并且只对URL做一个GET。

def sns
     request_json = JSON.parse(request.raw_post, {symbolize_names: true})
     subscribe_url = request_json[:SubscribeURL]
     token = request_json[:Token]
  end
2g32fytz

2g32fytz2#

这是一个适合我的php脚本。(有一些日志行将其写入文件i/tmp/log.txt,这有助于调试正在发生的事情。首先,我从https://sns.us-east-1.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem下载了amazon cert。请确保也可以在任何地方使用utf8。
对我来说,它只适用于http,当我订阅https url时,我看不到服务器上的任何访问。

<?php

mLog ("\n\n----------------Start Receive -------------\n");

require_once APP_DIR . 'util/AWSSDKforPHP/sdk.class.php';

$h= fopen("php://input","r");
$X = stream_get_contents($h);

$J = json_decode( $X , true);

mLog("Received body: \n" . json_encode($J). "\n");

function mLog($Msg) {
    file_put_contents(sys_get_temp_dir() .'/log.txt',$Msg,FILE_APPEND);
}

function CheckCall($J) {

    if ($J["Type"]=="Notification") {
        $SB='';
        $SB .= "Message\n";
        $SB .= $J["Message"] . "\n";
        $SB .= "MessageId\n";
        $SB .= $J["MessageId"] . "\n";

        if ($J["Subject"] != null) {
            $SB .= "Subject\n";
            $SB .= $J["Subject"]. "\n";
        }

        $SB .= "Timestamp\n";
        $SB .= $J["Timestamp"] . "\n";

        $SB .="TopicArn\n";
        $SB .= $J["TopicArn"]."\n";
        $SB .="Type\n";
        $SB .=$J["Type"]."\n";

    } else if ($J["Type"]=="SubscriptionConfirmation") {
        $SB='';
        $SB .= "Message\n";
        $SB .= $J["Message"] . "\n";
        $SB .= "MessageId\n";
        $SB .= $J["MessageId"] . "\n";
        $SB .= "SubscribeURL\n";
        $SB .= $J["SubscribeURL"] . "\n";
        $SB .= "Timestamp\n";
        $SB .= $J["Timestamp"] . "\n";

        $SB .= "Token\n";
        $SB .= $J["Token"] ."\n";
        $SB .="TopicArn\n";
        $SB .= $J["TopicArn"]."\n";
        $SB .="Type\n";
        $SB .=$J["Type"]."\n";
    } else {
        $SB='';
    }

    $cert = file_get_contents('SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem');
    $pubkey_id = openssl_get_publickey($cert);

    mLog("Signed string:\n" . $SB . "End Signed string\n");

    $Sig=base64_decode($J["Signature"]);
    if ( openssl_verify($SB, $Sig, $pubkey_id)) {
        return true;
    } else {

        return false;
    }
}

if (!CheckCall($J)) {
    mLog("Authorization failed. \n");
    header('HTTP/1.1 500 Internal Server Error');
    echo "<html><head></head><body>".sys_get_temp_dir()."</body></html>";

    exit(0);
}

$sns = new AmazonSNS();

if ($J["Type"]=="SubscriptionConfirmation") {
    $Res = $sns->confirm_subscription($J["TopicArn"], $J["Token"]);
    mLog("confitm_subscription sended: \n" . json_encode($Res));
} else if ($J["Type"]=="Notification") {
    mLog("-----------Begin Message-----------\n");
    mLog("Subject: ".$J["Subject"]. "\n");
    mLog($J["Message"]);
    mLog("\n-----------End Messagr------------\n\n");

} else {
    mLog("Invalid type \n");
}
echo "<html><head></head><body>Authentication Error</body></html>";
cedebl8k

cedebl8k3#

使用PHP:

你可以进入file_get_contents('php://input');
你会收到类似于下面的json结构:

{
  "Type" : "SubscriptionConfirmation",
  "MessageId" : "4..050670318",
  "Token" : "2336412f37fb687f5d51e6e2425f00....",
  "TopicArn" : "arn:aws:sns......",
  "Message" : "You have chosen to subscribe to the topic arn:aws:sns:us-east-1:430......\nTo confirm the subscription, visit the SubscribeURL included in this message.",
  "SubscribeURL" : "https://sns.us-east-1.amazonaws.com/?Action=ConfirmSubscription&TopicArn=arn:aws:sns:us-....&Token=2336412f37fb687f5d51e6e2425f004aef17b3ebbd98e122....",
  "Timestamp" : "2020-06-30T14:31:43.953Z",
  "SignatureVersion" : "1",
  "Signature" : "i3zpoQ3mAbcxTr18m/z5PL9Ls...",
  "SigningCertURL" : "https://sns.us-east-1.amazonaws.com/..."
}

相关问题