UNPKG

5.27 kBMarkdownView Raw
1## Authorize on connect
2
3When client side creates the WebSocket instance and connects to the subscriptions server, it must provide the authorization token, but there are some issues with that:
4
51. It’s not possible to provide custom headers when creating WebSocket connection in browser.
6
7> related to https://github.com/apollostack/subscriptions-transport-ws/issues/50 ,
8https://github.com/apollostack/subscriptions-transport-ws/pull/45
9
10A possible workaround is to send the auth token in the url as query parameter and parse in on sever side while doing the connection handshake
11
12> (Note: graphql-subscription does not expose onConnection callback, only onSubscribe, which occur after the connection)
13
142. You have to create the actual connection and then reject it in the server side if there is an error with the user’s authorization token.
15At the moment, graphql-subscription does not expose `unsubscribe` or `disconnect` feature for a connection (should be part of `onConnection`)
16
17> related to https://github.com/apollostack/subscriptions-transport-ws/issues/51 , https://github.com/apollostack/subscriptions-transport-ws/pull/17
18
193. At the moment, the client side can provide the “context” object for the subscription (and not for the connection), which can contain his auth token.
20
21
22Another option is to send the auth token from the client side on a separate message (like, `ON_INIT`) before creating the subscriptions.
23
24
25## Authorization Lifecycle
26
27Another issue is the lifecycle of the authorization token, there are some cases that needs a solution:
28
291. How to handle invalidation of the auth token?
30If the client side logout from the application, it’s the client side’s responsibility to disconnect the WebSocket.
31
322. How to handle expiration of token?
33At the moment, If the client side’s token no longer valid, it should logout the user and disconnect the token.
34
35
36## Authorization Validation
37
38What is the server side’s responsibility when dealing with authorized WebSocket and subscriptions?
39
40If the client side does not disconnects the WebSocket when needed (for example, on logout or on expiration), and the WebSocket remains open, the client side will receive the publications of it’s existing subscriptions.
41It’s not an option to validate the auth token on the server before each publication, for each user that subscribed.
42
43
44## Existing WebSocket Authorization Solutions
45
46* Socket.io-auth (https://github.com/facundoolano/socketio-auth )
47 * Provides a similar flow to onConnection and onAuthorize with a token, the server can validate and authorize the connection only when the connection created.
48 * The auth token send via a custom WebSocket message.
49 * Features callbacks for `onAuthorize`, `onAuthorized`, `onReject`, `postAuthenticate`.
50* Sockjs-node (https://github.com/sockjs/sockjs-node#authorisation )
51 * Does not provide a built-in solution and suggest a self-implemented authorization.
52* https://gist.github.com/subudeepak/9897212
53* https://auth0.com/blog/auth-with-socket-io/
54* https://auth0.com/blog/auth-with-socket-io/
55 * Suggests using authorization on connection using url parameter with the auth token.
56* Meteor/DDP
57 * The DDP protocol itself has no concept of authentication. Authentication is done via normal method calls.
58 * The Meteor DDP server implementation allows method calls to store state on the connection object representing the current user ID, which is accessible from other methods and publications. If the user ID ever changes, all publications are basically re-evaluated from scratch (inside the server). Nothing special is done on the server side to allow methods to notice if the user ID changes while they are running; however, methods run in series unless they explicitly ask to unblock the connection (and the client tries to not send login methods in parallel with other methods).
59 * The Meteor Accounts package tracks DDP connections associated with resume tokens and disconnects them if the resume token associated with that connection is deleted from the database.
60 * Personal opinion from @glasser: having auth just be "another method" wasn't the best idea. It works better if it's an established-at-beginning-of-connection, disconnect-to-change thing. However, the general idea of having a way for changes to authn/authz to "rerun publishers" or "disconnect connections" is nice.
61
62
63
64## Implementation possibility:
65
66* Client creates a networkInterface with WebSocket client, and provides the auth token using one of the following:
67 * URL parameter with the auth token.
68 * Custom object that will be translated into INIT_MESSAGE and will be sent after initial connection handshake.
69* Server side “onConnection” fires and validate the token, if the token is invalid, it rejects the connection / disconnects the socket.
70
71#### Pros:
72
73* Simple to implement.
74 * Server side - need to add “onConnection” callback with ability to reject the connection.
75 * Client side - need to add ability to send custom object with the auth token (or take if from the requested URL).
76* Custom auth for each application.
77
78#### Cons:
79
80* Forces the client side to handle logout/expiration of the token
81* Server publications not validated.