Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass Authorization Bearer access token in websocket javascript client

My API Manager tool mandates that i should pass the Authorization Bearer access token with the websocket invocation call. They are providing samples of java code where they do that.

The bearer token is set like ("Authorization", "Bearer e2238f3a-e43c-3f54-a05a-dd2e4bd4631f") .How can i do that in javascript?

// HttpResponseDecoder to WebSocketHttpResponseDecoder in the pipeline.
final WebSocketClientHandler handler = new WebSocketClientHandler(
    WebSocketClientHandshakerFactory
        .newHandshaker(uri, WebSocketVersion.V13, null, 
            false, new DefaultHttpHeaders()
                .add("Authorization", "Bearer e2238f3a-e43c-3f54-a05a-dd2e4bd4631f"))); 

This is how they do it in java Is there a way like this to do it from my java script[Angular2] web application? How can i pass this from UI? Any Websocket bases testing tool can emulate this?

like image 439
Janier Avatar asked Dec 04 '17 01:12

Janier


1 Answers

I found this answer here: https://stackoverflow.com/a/26123316/1715121

"It is impossible for now to use the Authentication header, because of the design of Javascript WebSocket API. More information can be found in this thread: HTTP headers in Websockets client API

However, Bearer authentication type allows a request parameter named "access_token": http://self-issued.info/docs/draft-ietf-oauth-v2-bearer.html#query-param This method is compatible with websocket connection."

like image 194
Ele Avatar answered Sep 19 '22 14:09

Ele