Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authentication using Firebase for Flask Application running in appengine

Hi I have our website running on appengine with flask as backend framework and we have built our authentication and session management using libraries Flask-OAuth, Flask-Login.

But now I have a requirement to use firebase for authentication. I am able create sample applications following firebase tutorials but I do no how to integrate with existing application.

In Firenotes samples provided by firebase team they are using two separate services frontend and backend.

I thought of using firebase code in login.html page and once client authenticated pass the info to /profile url -> log the user_id in database and login-user using Flask-Login.

I am not sure whether the above flow is correct and I am not to ensure that it is correct one without any problems in future.

Please help with any ideas as I need to implement it very soon!!

like image 865
Rinsen S Avatar asked Jul 19 '17 04:07

Rinsen S


People also ask

How you can implement Firebase authentication on your mobile application?

To use an authentication provider, you need to enable it in the Firebase console. Go to the Sign-in Method page in the Firebase Authentication section to enable Email/Password sign-in and any other identity providers you want for your app.


Video Answer


1 Answers

Flask-Login uses session based authentication. Clients login using an authentication scheme. Since you are using Flask-OAuth, it's the oauth flow. If the user successfully authenticates, Flask-Login sends a response during the token exchange step setting an HTTP only cookie (meaning javascript can't access it) with a token unique to the user session. The client then authenticates future requests for the duration of the session with that token. The server can invalidate the session at any time, forcing the client to log in again.

Meanwhile, firebase authentication is JSON Web Token (JWT) authentication scheme. After completing the login flow, the firebase API retrieves a JWT from google's application servers.

To authenticate requests, you need to transmit that JWT on EVERY request. Your server MUST also validate the JWT to ensure that it is both valid and unexpired.

You'll note that the manner by which the JWT arrives at the server is unspecified by the firebase SDK and libraries. I recommend using a Authentication: JWT <google's jwt> header.

One way to resolve your question would be to use the JWT to complete the initial login flow, and then rely on session based auth from there. You'd set up a login endpoint that expects and validates a JWT, and responds with the set cookie header. From that point forward you continue using your flask-login provided session based auth.

Google actually has an example of this in their documentation: https://firebase.google.com/docs/auth/admin/manage-cookies

like image 72
Maus Avatar answered Oct 14 '22 03:10

Maus