Skip to content

Commit cbb832f

Browse files
committed
changes from code review
1 parent 35dac5e commit cbb832f

File tree

8 files changed

+27
-17
lines changed

8 files changed

+27
-17
lines changed

assets/js/phoenix/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ export const TRANSPORTS = {
2727
export const XHR_STATES = {
2828
complete: 4
2929
}
30+
export const AUTH_TOKEN_PREFIX = "base64url.bearer.phx."

assets/js/phoenix/longpoll.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
SOCKET_STATES,
3-
TRANSPORTS
3+
TRANSPORTS,
4+
AUTH_TOKEN_PREFIX
45
} from "./constants"
56

67
import Ajax from "./ajax"
@@ -17,9 +18,9 @@ export default class LongPoll {
1718

1819
constructor(endPoint, protocols){
1920
// we only support subprotocols for authToken
20-
// ["phoenix", "base64url.bearer.authorization.phx.BASE64_ENCODED_TOKEN"]
21-
if (protocols.length === 2 && protocols[1].startsWith("base64url.bearer.authorization.phx.")) {
22-
this.authToken = atob(protocols[1].slice("base64url.bearer.authorization.phx.".length))
21+
// ["phoenix", "base64url.bearer.phx.BASE64_ENCODED_TOKEN"]
22+
if (protocols.length === 2 && protocols[1].startsWith(AUTH_TOKEN_PREFIX)) {
23+
this.authToken = atob(protocols[1].slice(AUTH_TOKEN_PREFIX.length))
2324
}
2425
this.endPoint = null
2526
this.token = null
@@ -64,8 +65,8 @@ export default class LongPoll {
6465

6566
poll(){
6667
const headers = {"Accept": "application/json"}
67-
if (this.authToken) {
68-
headers["Authorization"] = `Bearer ${this.authToken}`
68+
if(this.authToken){
69+
headers["X-Phoenix-AuthToken"] = this.authToken
6970
}
7071
this.ajax("GET", headers, null, () => this.ontimeout(), resp => {
7172
if(resp){

assets/js/phoenix/socket.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
DEFAULT_VSN,
77
SOCKET_STATES,
88
TRANSPORTS,
9-
WS_CLOSE_NORMAL
9+
WS_CLOSE_NORMAL,
10+
AUTH_TOKEN_PREFIX
1011
} from "./constants"
1112

1213
import {
@@ -352,7 +353,7 @@ export default class Socket {
352353
// Sec-WebSocket-Protocol based token
353354
// (longpoll uses Authorization header instead)
354355
if (this.authToken) {
355-
protocols.push(`base64url.bearer.authorization.phx.${btoa(this.authToken).replace(/=/g, "")}`)
356+
protocols.push(`${AUTH_TOKEN_PREFIX}${btoa(this.authToken).replace(/=/g, "")}`)
356357
}
357358
this.conn = new this.transport(this.endPointURL(), protocols)
358359
this.conn.binaryType = this.binaryType

assets/test/channel_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describe("with transport", function (){
6767
const socket = new Socket("/socket", {authToken})
6868

6969
socket.connect()
70-
expect(socket.conn.protocols).toEqual(["phoenix", "base64url.bearer.authorization.phx.MTIzNA"])
70+
expect(socket.conn.protocols).toEqual(["phoenix", "base64url.bearer.phx.MTIzNA"])
7171
})
7272
})
7373

lib/phoenix/endpoint.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -846,8 +846,7 @@ defmodule Phoenix.Endpoint do
846846
* the websocket transport, this enables a token to be passed through the `Sec-WebSocket-Protocol` header.
847847
* the longpoll transport, this allows the token to be passed through the `Authorization` header.
848848
849-
The token is available in the `connect_info` as `:auth_token`, which must be separately enabled in the
850-
corresponding `websocket` or `longpoll` configurations.
849+
The token is available in the `connect_info` as `:auth_token`.
851850
852851
Custom transports might implement their own mechanism.
853852

lib/phoenix/socket/transport.ex

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,14 @@ defmodule Phoenix.Socket.Transport do
259259
def load_config(config) do
260260
{connect_info, config} = Keyword.pop(config, :connect_info, [])
261261

262+
connect_info =
263+
if config[:auth_token] do
264+
# auth_token is included by default when enabled
265+
[:auth_token | connect_info]
266+
else
267+
connect_info
268+
end
269+
262270
connect_info =
263271
Enum.map(connect_info, fn
264272
key when key in [:peer_data, :trace_context_headers, :uri, :user_agent, :x_headers, :auth_token] ->
@@ -486,7 +494,7 @@ defmodule Phoenix.Socket.Transport do
486494
{:session, connect_session(conn, endpoint, session, opts)}
487495

488496
:auth_token ->
489-
{:auth_token, conn.private[:__phoenix_transport_auth_token]}
497+
{:auth_token, conn.private[:phoenix_transport_auth_token]}
490498

491499
{key, val} ->
492500
{key, val}

lib/phoenix/transports/long_poll.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,12 @@ defmodule Phoenix.Transports.LongPoll do
268268
end
269269

270270
defp maybe_auth_token_from_header(conn, true) do
271-
case Plug.Conn.get_req_header(conn, "authorization") do
271+
case Plug.Conn.get_req_header(conn, "x-phoenix-authtoken") do
272272
[] ->
273273
conn
274274

275-
["Bearer " <> token | _] ->
276-
Plug.Conn.put_private(conn, :__phoenix_transport_auth_token, token)
275+
[token | _] ->
276+
Plug.Conn.put_private(conn, :phoenix_transport_auth_token, token)
277277
end
278278
end
279279

lib/phoenix/transports/websocket.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ defmodule Phoenix.Transports.WebSocket do
1717

1818
@connect_info_opts [:check_csrf]
1919

20-
@auth_token_prefix "base64url.bearer.authorization.phx."
20+
@auth_token_prefix "base64url.bearer.phx."
2121

2222
import Plug.Conn
2323

@@ -112,7 +112,7 @@ defmodule Phoenix.Transports.WebSocket do
112112
token = Base.decode64!(encoded_token, padding: false)
113113

114114
conn
115-
|> put_private(:__phoenix_transport_auth_token, token)
115+
|> put_private(:phoenix_transport_auth_token, token)
116116
|> set_actual_subprotocols(actual_subprotocols)
117117

118118
_ ->

0 commit comments

Comments
 (0)