-
Notifications
You must be signed in to change notification settings - Fork 6
Update Surreal.Net Dependency Injection to enable both Multi Tenanted and Multi User scenarios #79
Description
Preamble
Currently Surreal.Net functions as a standard DB Driver where a single set of credentials is used for all connections to the database. However SurrealDB can do a lot more than your standard DB like generating and consuming access tokens. For this reason we must be able to isolate each users session.
Consideration
- The Drivers can be used with a single DB login for the liftime of the application. (This the the current state of the drivers) (This would generally (maybe always?) be done with Basic Auth)
- The Drivers can be used with a different namespace and database for each request.
- The Drivers can be used with a login per user for each request (Using JWT Auth)
The Changes
Dependency Injection
Update the projects Dependency Injection to allow for the service and options lifetime to be configured when being defined.
EF core uses the ServiceLifetime Enum to define both the contextLifetime and optionsLifetime.
By setting the ServiceLifetime to scoped each request (context of ASP.net) will have it's own instance of the SurrealDB driver.
Some Considerations
The Rest and RPC drivers are currently setup to be consumed as it they are singletons and are not lightweight enough to be created and destroyed repeatedly.
Fixing this should be as simple as setting the Drivers to use DI for HttpClient and WsClient (See typed clients).
Config will need to be injected also.
Driver Changes
Don't use default headers for the HttpClient, set them on a per request basis based on what is in the supplied config. (Is there an equivalent for WsClient?)
Middleware
Create a simple bit of middleware to extract a JWT from an incoming request and insert it into the Surreal.Net configuration for the driver to use during that request.