Android Pusher 无效签名 - 客户端
Android Pusher Invalid Signature - Client Side
使用 Pusher,出现以下错误签名无效问题:
Invalid signature: Expected HMAC SHA256 hex digest of socketID:channel, but got hash.
这是我的代码:
public static PusherOptions pusherChannelAuthorise(){
Log.v(TAG, "pusherChannelAuthorise");
mMobileToken = Handler_Login.fetchmobile();
PUSHER_MOBILETOKEN_MAP.put(PUSHER_MOBILETOKEN_LABEL, mMobileToken);
HttpAuthorizer authoriser = new HttpAuthorizer(url);
authoriser.setHeaders(PUSHER_MOBILETOKEN_MAP);
options = new PusherOptions().setAuthorizer(authoriser);
return options;
}
public static void connect(){
Log.v(TAG, "connect" + "::CONNECTED::");
PusherOptions presenceChannelOptions = pusherChannelAuthorise();
presenceChannelConnected = true;
pusher = new Pusher(pusherHash, presenceChannelOptions);
pusher.connect(new ConnectionEventListener() {
@Override
public void onConnectionStateChange(ConnectionStateChange change) {
socketId = pusher.getConnection().getSocketId();
Log.v(TAG, "The socketId is: " + socketId);
}
@Override
public void onError(String message, String code, Exception e) {
Log.v(TAG, "There was a problem connecting!");
}
}, ConnectionState.CONNECTED);
presencechannel = pusher.subscribePresence(PUSHER_PUBLIC_CHANNEL_NAME, listener);
String myUuid = Handler_Login.getMyUuid();
privatechannel = pusher.subscribePrivate(DOCSYNC_CHANNEL + myUuid, docSyncListener);
privatechannel.bind("client-init", docSyncListener = new PrivateChannelEventListener() {
@Override
public void onEvent(String channelName, String eventName, String data) {
Log.v(TAG, "Private Test onEvent: " + channelName + " " + eventName + " " + data);
}
@Override
public void onSubscriptionSucceeded(String channelName) {
Log.v(TAG, "Private Channel onSubscriptionSucceeded: " + channelName);
}
@Override
public void onAuthenticationFailure(String message, Exception e) {
Log.v(TAG, "Private Channel onAuthenticationFailure: " + message + ":::" + e);
}
});
}
我做错了什么?我是否必须在 headers 中为授权人设置其他参数? @leggetter
存在通道的身份验证工作正常,但私有通道失败。
编辑:
服务器代码(PHP、Laravel):
public function postMobilePusher(Request $request)
{
if (null !== $request->header('mobileToken')) {
$currentUser = User::where('mobileToken', '=', $request->header('mobileToken'))->first();
if (null !== $currentUser) {
define('APP_ID', ID);
define('APP_KEY', KEY);
define('APP_SECRET', SECRET);
$pusher = new Pusher(APP_KEY, APP_SECRET, APP_ID);
$socket_id = $request->get('socket_id');
$channel_name = $request->get('channel_name');
$user_id = $currentUser->id;
$user_info = [
'firstName' => $currentUser->firstName
];
return $auth = $pusher->presence_auth( $channel_name, $socket_id, $user_id, $user_info );
}
}
return abort(403, 'Unauthorized action.');
}
更新服务器端代码以将 socket_auth
用于私人频道,将 presence_auth
用于在线频道:
public function postMobilePusher(Request $request)
{
if (null !== $request->header('mobileToken')) {
$currentUser = User::where('mobileToken', '=', $request->header('mobileToken'))->first();
if (null !== $currentUser) {
define('APP_ID', ID);
define('APP_KEY', KEY);
define('APP_SECRET', SECRET);
$pusher = new Pusher(APP_KEY, APP_SECRET, APP_ID);
$socket_id = $request->get('socket_id');
$channel_name = $request->get('channel_name');
$auth = null;
if(starts_with($channel_name, 'private-')) {
// TODO: check user has permission to access channel
$auth = $pusher->socket_auth($channel_name, $socket_id);
}
else {
// presence
// TODO: check user has permission to access channel
$user_id = $currentUser->id;
$user_info = [
'firstName' => $currentUser->firstName
];
$auth = $pusher->presence_auth( $channel_name, $socket_id, $user_id, $user_info );
}
}
}
return abort(403, 'Unauthorized action.');
}
使用 Pusher,出现以下错误签名无效问题:
Invalid signature: Expected HMAC SHA256 hex digest of socketID:channel, but got hash.
这是我的代码:
public static PusherOptions pusherChannelAuthorise(){
Log.v(TAG, "pusherChannelAuthorise");
mMobileToken = Handler_Login.fetchmobile();
PUSHER_MOBILETOKEN_MAP.put(PUSHER_MOBILETOKEN_LABEL, mMobileToken);
HttpAuthorizer authoriser = new HttpAuthorizer(url);
authoriser.setHeaders(PUSHER_MOBILETOKEN_MAP);
options = new PusherOptions().setAuthorizer(authoriser);
return options;
}
public static void connect(){
Log.v(TAG, "connect" + "::CONNECTED::");
PusherOptions presenceChannelOptions = pusherChannelAuthorise();
presenceChannelConnected = true;
pusher = new Pusher(pusherHash, presenceChannelOptions);
pusher.connect(new ConnectionEventListener() {
@Override
public void onConnectionStateChange(ConnectionStateChange change) {
socketId = pusher.getConnection().getSocketId();
Log.v(TAG, "The socketId is: " + socketId);
}
@Override
public void onError(String message, String code, Exception e) {
Log.v(TAG, "There was a problem connecting!");
}
}, ConnectionState.CONNECTED);
presencechannel = pusher.subscribePresence(PUSHER_PUBLIC_CHANNEL_NAME, listener);
String myUuid = Handler_Login.getMyUuid();
privatechannel = pusher.subscribePrivate(DOCSYNC_CHANNEL + myUuid, docSyncListener);
privatechannel.bind("client-init", docSyncListener = new PrivateChannelEventListener() {
@Override
public void onEvent(String channelName, String eventName, String data) {
Log.v(TAG, "Private Test onEvent: " + channelName + " " + eventName + " " + data);
}
@Override
public void onSubscriptionSucceeded(String channelName) {
Log.v(TAG, "Private Channel onSubscriptionSucceeded: " + channelName);
}
@Override
public void onAuthenticationFailure(String message, Exception e) {
Log.v(TAG, "Private Channel onAuthenticationFailure: " + message + ":::" + e);
}
});
}
我做错了什么?我是否必须在 headers 中为授权人设置其他参数? @leggetter
存在通道的身份验证工作正常,但私有通道失败。
编辑: 服务器代码(PHP、Laravel):
public function postMobilePusher(Request $request)
{
if (null !== $request->header('mobileToken')) {
$currentUser = User::where('mobileToken', '=', $request->header('mobileToken'))->first();
if (null !== $currentUser) {
define('APP_ID', ID);
define('APP_KEY', KEY);
define('APP_SECRET', SECRET);
$pusher = new Pusher(APP_KEY, APP_SECRET, APP_ID);
$socket_id = $request->get('socket_id');
$channel_name = $request->get('channel_name');
$user_id = $currentUser->id;
$user_info = [
'firstName' => $currentUser->firstName
];
return $auth = $pusher->presence_auth( $channel_name, $socket_id, $user_id, $user_info );
}
}
return abort(403, 'Unauthorized action.');
}
更新服务器端代码以将 socket_auth
用于私人频道,将 presence_auth
用于在线频道:
public function postMobilePusher(Request $request)
{
if (null !== $request->header('mobileToken')) {
$currentUser = User::where('mobileToken', '=', $request->header('mobileToken'))->first();
if (null !== $currentUser) {
define('APP_ID', ID);
define('APP_KEY', KEY);
define('APP_SECRET', SECRET);
$pusher = new Pusher(APP_KEY, APP_SECRET, APP_ID);
$socket_id = $request->get('socket_id');
$channel_name = $request->get('channel_name');
$auth = null;
if(starts_with($channel_name, 'private-')) {
// TODO: check user has permission to access channel
$auth = $pusher->socket_auth($channel_name, $socket_id);
}
else {
// presence
// TODO: check user has permission to access channel
$user_id = $currentUser->id;
$user_info = [
'firstName' => $currentUser->firstName
];
$auth = $pusher->presence_auth( $channel_name, $socket_id, $user_id, $user_info );
}
}
}
return abort(403, 'Unauthorized action.');
}