deepstream.io icon indicating copy to clipboard operation
deepstream.io copied to clipboard

Question: Request Metadata (e.g. IP)

Open TertiumQuid opened this issue 10 years ago • 5 comments

Are there any plans to expose sort of request metadata to the deepstream server? Namely, IP address would be useful, for example to block ranges for regions like China, operators like AWS, or else for specific cases like throttling an IP which makes too many requests in a timeframe.

While I could add something like a firewall or IDS further up in the network, my case requires application logic and so putting this in the deepstream permissions handler or in shared RPC provider logic makes the most sense.

TertiumQuid avatar Nov 17 '16 16:11 TertiumQuid

You have access to all the connection data via the isValidUser method within a authentication handler.

If you want to put in custom logic, you can create an authentication handler that extends one of the ones we provide ( http, file ) or implement your own.

    isValidUser( connectionData, authData, callback ) {
    }

yasserf avatar Nov 17 '16 16:11 yasserf

Thanks, that's great! I missed it in the docs somehow, but I now see that many of the github examples implement this.

Related question:

Is it possible to perform deepstream actions from within the isValidUser method, for example to publish an event or update a record? I suppose I could implement an HTTP authentication handler that also serves as provider and both authenticates users and publishes auth-related events, but it would be simpler and faster to do it all from within the deepstream server's authentication handler itself, e.g. something like:

isValidUser( connectionData, authData, callback ) {
  if (connectionData.ipAddress === someBlackListedIpAddress)
    this.event.emit('authentication/error', authData.username);
}

TertiumQuid avatar Nov 19 '16 11:11 TertiumQuid

That isn't possible no. The server does not implement custom logic due to the seperation of concerns and to ensure correct scalability and architecture. The server doesn't expose any of the client apis.

As you stated, using a http endpoint would be a great place to put that logic. You could in theory create a client within the same process as deepstream ( within the authentication plugin ) but it does raise a few questions about cyclic dependencies. You would create a server that requires a client which requires a server to connect to...

yasserf avatar Nov 19 '16 12:11 yasserf

Thanks! And since I'm already "overloading" this thread, I have yet another related question: is there an intended flow for first-time clients/users?

Because clients and providers authenticate with deepstream.io in the same way, it seems like an architecture-level responsibility and that individual user authentication would happen in the app logic. However, deepstream also stores userData for the connection, and makes it available in app-level logic like valve implying that the authentication performed by deepstream implementations is intended to be coupled to things like account tokens, user models, etc.

But to connect to deepstream in the first place, there still needs to be some sort of anonymous user concept with username/password credentials for the deepstream connection authentication. If I understand correctly, that means that after authentication the deepstream connection will need to be "upgraded" by reconnecting with new credentials. Is that correct, or is there a cleaner way? My flow currently looks like this:

  1. connect to deepstream and authenticate with some sort of anonymous placeholder credentials.
  2. register a new user (via a record, rpc, whatever) using anonymous client permissions, do not store userData (since anonymous client credentials are "shared")
  3. upon success respond with new credentials (e.g. an auth token), and then terminate anonymous connection
  4. reopen deepstream connection and authenticate with newly created credentials and store userData

There are both pros and cons to this approach, but I just want to confirm I'm using deepstream as intended. Thanks again!

TertiumQuid avatar Nov 22 '16 08:11 TertiumQuid

hey, I know this was asked a while ago but wanted to provide an answer for anyone seeing it going forward.

Your approach actually makes perfect sense, to be honest we usually don't use deepstream to store user credentials, mainly so we can use things like facebook/twitter strategies but there is no reason why you can't!

yasserf avatar Aug 10 '17 09:08 yasserf