Reputation: 25793
My React app uses Firebase Authentication and the Real-Time Database. I now need to access 3rd party services using OAuth 2.0. To do this securely, I am using the OAuth 2.0 Authorization Code Flow with Firebase Functions as my back-end:
But this is where I am stuck. How do I save the access token in the backend so that the React app can access the 3rd party resources? I don't want to store the access token in the React app because that's not secure.
Upvotes: 2
Views: 3275
Reputation: 29243
OVERALL FACTORS
Typically these are the areas that your SPA Security choices influence, and there are trade offs:
IN ALL CASES
Your app should:
OPTION 1: FRONT END MODEL / COOKIELESS
This involves using tokens in the browser and has these benefits:
This is widely used and should only be exploitable if your app has cross site scripting vulnerabilities. Some resources of mine:
OPTION 2: BACK END MODEL / AUTH COOKIES
This involves proxying via a back end and carrying tokens around in authentication cookies. I would use this model if developing an online banking app but for medium security apps it can add a lot of overhead. You may have to write more security code and it is possible to end up with a less secure solution than option 1:
Out of interest the respected mod_auth_openidc library can be helpful for this type of solution.
BEST OF BOTH WORLDS
If using tokens in a browser is deemed unacceptable I would aim to follow option 1 as much as possible. Then implement option 2 as an additive step:
Upvotes: 1
Reputation: 25793
After trying bunch of different approaches, I ended up storing the access token & the refresh token in the back-end, both tied to the firebase user. The tokens are only accessible through firebase functions. This approach has the following advantages:
Upvotes: 1