如何从Bash生成push身份验证字符串



我在生成"正确的"身份验证字符串时遇到麻烦通过curl发送消息给push

这是我的脚本,秘密部分当然被剪掉了:

#!/bin/bash
key="my_key"
secret="my_secret"
appID="my_app_id"
timestamp=`date +%s`
data='{"name":"say_stuff","channel":"test","data":"{"message":"oh_yeah"}"}'
md5data=`echo "$data" | md5`
authSig=`echo 'POSTn/apps/"$appID"/eventsnauth_key="$key"&auth_timestamp="$timestamp"&auth_version=1.0&body_md5="$md5data"' | openssl dgst -sha256 -hex -hmac "$secret"`
curl -H "Content-Type:application/json" -d "$data" "http://api.pusherapp.com/apps/$appID/events?body_md5=$md5data&auth_version=1.0&auth_key=$key&auth_timestamp=$timestamp&auth_signature=$authSig"

authSig确实生成了,看起来像有效的HmacSHA256Hex

但是,当它运行curl命令时,我得到这样的响应:

Invalid signature: you should have sent HmacSHA256Hex("POSTn/apps/$appID/eventsnauth_key=$key&auth_timestamp=1432086733&auth_version=1.0&body_md5=e5997a811232ffae050be74242254ceb", your_secret_key), but you sent "55029a5e2d1058b352b5c22709e7fb9cb0c6f147846ed09dbc6bcaf6a7a804c7"

我的机器(Mac OS X 10.10)上的openssl实用程序是否可能与push的不同?

现在我注意到一些有趣的事情。如果你去这里:

https://pusher.com/docs/rest_api

然后向下滚动到"工作身份验证示例",您将能够跟随示例。

我试着用下面的例子生成签名:

echo 'POSTn/apps/3/eventsnauth_key=278d425bdf160c739803&auth_timestamp=1353088179&auth_version=1.0&body_md5=ec365a775a4cd0599faeb73354201b6f' | openssl dgst -sha256 -hex -hmac 7ad3773142a6692b25b8

得到

aa368756587116f3997427fe1b315ed0e2f2faa555066e565a25cfe6f47c9396

而不是他们的例子,结果是

da454824c97ba181a32ccc17a72625ba02771f50b50e1e7430e47a1f3f457e6c

尝试如下:

#!/bin/bash
key="my_key"
secret="my_secret"
appID="my_app_id"
timestamp=$(date +%s)
data='{"name":"say_stuff","channel":"test","data":"{"message":"oh_yeah"}"}'
# Be sure to use `printf %s` to prevent a trailing n from being added to the data.
md5data=$(printf '%s' "$data" | md5)
path="/apps/${appID}/events"
queryString="auth_key=${key}&auth_timestamp=${timestamp}&auth_version=1.0&body_md5=${md5data}"
# Be sure to use a multi-line, double quoted string that doesn't end in n as 
# input for the SHA-256 HMAC.
authSig=$(printf '%s' "POST
$path
$queryString" | openssl dgst -sha256 -hex -hmac "$secret")
curl -H "Content-Type:application/json" -d "$data" "http://api.pusherapp.com${path}?${queryString}&auth_signature=${authSig}"

你的代码有几个问题:

  • 通过使用echo,您在md5openssl的输入中添加尾随换行符,从而改变了数据。
  • 传递给openssl的字符串中的n序列意味着表示实际的换行符,而您使用它们作为字面值

另外,我已经删除了重复的代码,使用${name}变量引用(名称包含在花括号中)以获得更好的视觉清晰度,我还修复了双引号问题。


关于来自网站的样本哈希:再次,你的问题是使用echo,而不是将嵌入的n序列扩展到实际的换行符;下面的shell命令使给出正确的结果:

# Expand the 'n' sequences to newlines using an ANSI C-quoted string
# ($'...')
s=$'POSTn/apps/3/eventsnauth_key=278d425bdf160c739803&auth_timestamp=1353088179&auth_version=1.0&body_md5=ec365a775a4cd0599faeb73354201b6f'
# Pass to openssl using `printf %s`.
printf %s "$s" | openssl dgst -sha256 -hex -hmac 7ad3773142a6692b25b8

这已经有一段时间了,但我最近需要这个,下面的脚本为我工作:

#!/bin/bash
# Replace the following placeholders
PUSHER_AUTH_KEY='<your-auth-key>'
PUSHER_AUTH_SECRET='<your-secret-key>'
PUSHER_URL_PREFIX='https://api-<your-cluster>.pusher.com'
PUSHER_URL_SUFFIX='/apps/<your-app-id>/events'
PUSHER_DATA='{"data":"{"message":"hello world"}","name":"my-event","channel":"my-channel"}'
# From here on it should be mostly standard
PUSHER_AUTH_VERSION='1.0'
PUSHER_AUTH_TIMESTAMP=$(date +%s)
PUSHER_URL=$PUSHER_URL_PREFIX$PUSHER_URL_SUFFIX
PUSHER_BODY_MD5=$(echo -n $PUSHER_DATA | openssl dgst -md5 -hex)
PUSHER_SIGNATURE="POSTn$PUSHER_URL_SUFFIXnauth_key=$PUSHER_AUTH_KEY&auth_timestamp=$PUSHER_AUTH_TIMESTAMP&auth_version=$PUSHER_AUTH_VERSION&body_md5=$PUSHER_BODY_MD5"
PUSHER_AUTH_SIGNATURE=$(echo -en $PUSHER_SIGNATURE | openssl dgst -sha256 -hex -hmac $PUSHER_AUTH_SECRET)
curl -i -H 'Content-Type: application/json' -d "$PUSHER_DATA" 
"$PUSHER_URL?"
"body_md5=$PUSHER_BODY_MD5&"
"auth_version=$PUSHER_AUTH_VERSION&"
"auth_key=$PUSHER_AUTH_KEY&"
"auth_timestamp=$PUSHER_AUTH_TIMESTAMP&"
"auth_signature=$PUSHER_AUTH_SIGNATURE"

这是非常基本的,使用push网站上的样本数据,但可以根据您的用例改进以接收一些参数作为输入。这里的目标是您可以大致了解它是如何工作的。

请注意openssl必须安装才能工作。

相关内容

最新更新