Jump to content

How to do user authentication in Tonic gRPC Rust

wasab

I am currently learning Rust. 

I have just finished reading "the book" for Rust beginners and looking for a practice project. 

 

So i Googled for some project ideas and saw this

image.png.52c3b71dc81d743518cf9ea0e0e7787d.png

 

it comes from some kind of online coding bootcamp but i think the idea is pretty neat. i have done this before. I built a partially functional Twitter-like website in MERN stacks a few years back.

 

For rust, i have little idea of the stacks to use. I know i need at least three things, a database which i am leaning towards postgreSQL. For web server, there is very little info on any good backend framework because rust is so new, that there has yet to be an established and dominant backend framework. I decided to go with Tonic which is the rust implementation of the gRPC. It looks very neat but like i said, version 0.11 so not very mature.... but most concerning is very little documentation and tutorials.

 

If like say i want to build a grpc request endpoint with Tonic that is protected and only accessible to a logged in user, i would probably need to have some sort of middleware that searches for a token in the authorization header and checks for an authenticated session. what should i use and how should i go about doing this? i google online but i have yet to find any good and solid examples. 

 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...

I also bit struggled with it, needed async interception, implemented it with Tower, created small lib that simplifies this process (additionally it allows to create "Full" interception, ie add custom logic before and after service call. Might be helpful for somebody https://github.com/teimuraz/tonic-middleware

Link to comment
Share on other sites

Link to post
Share on other sites

11 hours ago, Teimuraz said:

I also bit struggled with it, needed async interception, implemented it with Tower, created small lib that simplifies this process (additionally it allows to create "Full" interception, ie add custom logic before and after service call. Might be helpful for somebody https://github.com/teimuraz/tonic-middleware

Awesome, this is neat. The async interceptor is limited and tower is a bit too complicated and boiler plate. This greatly helps. 

 

Edit: tested it and worked like a charm! 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×