简体   繁体   中英

Pusher Invalid signature: Expected HMAC SHA256 hex digest of

I have JavaScript code in one HTML file from which I am calling server for authentication:

<html>
<script>
<head>
    var options = { authEndpoint: "api/pusher.json?socket_id=9900&channel_name=presence-channel" }
    var pusher = new Pusher('98384343434343434', options);
    pusher.connection.bind('connected', function() {
        console.log("connected");
        socketId = pusher.connection.socket_id;
        console.log("socketId" + socketId);
    });

    var channel = pusher.subscribe('presence-channel');
</script>
</head>
<body></body>
</html>

And on the server the code is as below:

import com.pusher.rest.Pusher;
import com.pusher.rest.data.PresenceUser;
import com.webapp.actions.BusinessApiAction;


@Path("/api/pusher")
public class PusherAction extends BusinessApiAction {
    @POST

    @Produces({ "application/Json", "application/xml" })
    public Response pusher(@Context ServletContext context, @Context HttpServletRequest req, @Context HttpServletResponse res, @FormParam("socket_id") String socketId, @FormParam("channel_name") String channelName) throws Exception {
        System.out.println("\n\n===channel==> " + channelName + "\t socket id-->" + socketId);

        Pusher pusher = new Pusher("92063", "3055e2b132174078348c", "52cfe6c7ecb8420ad981");
        String userId = "5433d5da97d88628ec000300";
        Map<String, String> userInfo = new HashMap<>();
        userInfo.put("name", "Phil Leggetter");

        String authBody = pusher.authenticate(socketId, channelName, new PresenceUser(userId, userInfo));
        JSONObject j = new JSONObject(authBody);
        System.out.println("\n\n===authBody==> " + j.getString("auth"));
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> channelData = new HashMap<>();
        map.put("auth", j.getString("auth"));
        JSONObject ch = new JSONObject(j.getString("channel_data"));
        channelData.put("user_id", ch.getString("user_id"));
        channelData.put("user_info", userInfo);
        map.put("channel_data", ch.toString());

        return sendDataResponse(map);
        }

}

The returned response is 200 but Pusher is giving this error: Error in the pusher logger--

Pusher : Event sent : {"event":"pusher:subscribe","data":{"auth":"3055e2b132174078348c:980bf9a6d3a61d280d181785ccacd0e5e7999776085403f2d9bfe688842b8fe7","channel_data":"{\"user_info\":{\"name\":\"Phil Leggetter\"},\"user_id\":\"5433d5da97d88628ec000300\"}","channel":"presence-user2"}}

Pusher : Event recd : {"event":"pusher:error","data":{"code":null,"message":"Invalid signature: Expected HMAC SHA256 hex digest of 41797.10543542:presence-user2:{\"user_info\":{\"name\":\"Phil Leggetter\"},\"user_id\":\"5433d5da97d88628ec000300\"}, but got 980bf9a6d3a61d280d181785ccacd0e5e7999776085403f2d9bfe688842b8fe7"}}

Pusher : Error : {"type":"WebSocketError","error":{"type":"PusherError","data":{"code":null,"message":"Invalid signature: Expected HMAC SHA256 hex digest of 41797.10543542:presence-user2:{\"user_info\":{\"name\":\"Phil Leggetter\"},\"user_id\":\"5433d5da97d88628ec000300\"}, but got 980bf9a6d3a61d280d181785ccacd0e5e7999776085403f2d9bfe688842b8fe7"}}}

The authBody that is returned from pusher.authenticate should provide you with everything you need to respond to the client request. You just need to make sure that sendDataResponse send the authBody back as the body of the response as JSON.

I've updated the example you've provided to remove the lines that aren't required:

@Path("/api/pusher")
public class PusherAction extends BusinessApiAction {
    @POST

    @Produces({ "application/Json", "application/xml" })
    public Response pusher(@Context ServletContext context, @Context HttpServletRequest req, @Context HttpServletResponse res, @FormParam("socket_id") String socketId, @FormParam("channel_name") String channelName) throws Exception {
        System.out.println("\n\n===channel==> " + channelName + "\t socket id-->" + socketId);

        Pusher pusher = new Pusher(APP_ID, APP_KEY, APP_SECRET);
        String userId = "5433d5da97d88628ec000300";
        Map<String, String> userInfo = new HashMap<>();
        userInfo.put("name", "Phil Leggetter");

        String authBody = pusher.authenticate(socketId, channelName, new PresenceUser(userId, userInfo));

        return sendDataResponse(authBody);
    }

}

The pusher-rest-java library README show the additional functionality that was present isn't required: https://github.com/pusher/pusher-rest-java#authenticating-presence-channels

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM