paritytech / jsonrpsee

Rust JSON-RPC library on top of async/await
MIT License
629 stars 169 forks source link

Pass something from http middleware to rpc middleware #1370

Open dpc opened 4 months ago

dpc commented 4 months ago

I might have missed something, but I don't see a way to pass anything from http middlware to rpc middleware/handler.

I'd like to do authorization over http request, and then depending on the result possibly each rpc would enable/disable certain things etc.

Conceptually it's easy, but there seem to be nothing the two layers together, so it can't be done.

niklasad1 commented 4 months ago

Hey hey,

Have you looked at this example which does passes the HTTP headers to the RPC middleware stuff.

But you are right that it won't work with server builder API instead you have to utilize the "low-level API" and use jsonrpsee as as tower service and start the hyper server yourself.

Another example how we are using it in substrate to disable/enable rpc rate limiting middleware...

I hope that helps...

dpc commented 4 months ago

Thanks!

This is a bit mindbending. Do I understand correctly that the approach here is to create an rpc service for every http connection, kinda?

niklasad1 commented 4 months ago

This is a bit mindbending. Do I understand correctly that the approach here is to create an rpc service for every http connection, kinda?

Yes, if you want some state from HTTP request itself such as headers then it's only possible to utilize it by creating a rpc service per connection.

So I get that it's quite tricky to use/understand but that's the trade-off right because we didn't want to introduce any additional custom middleware stuff in jsonrpsee and clone stuff that may not used by users....

dinhani commented 1 month ago

You can pass data from HTTP to RPC with request extensions.

In this example we parse client identification in the HTTP layer and use it in the RPC layer.