I have an Express application setup and need some advice on storing tokens.
I am receiving an access token from an OAuth 2 server after authenticating a user account, which I then need to use for subsequent api requests.
I want to hide the token value from the client and I believe one way of doing this is to save the token on the server in an encoded cookie so that when further requests are made, these can be routed through middleware and the cookie can then be used for retrieval of the token stored sever side and then used as a header value in the ongoing request to the actual api endpoint.
Someone has actually already asked this question - How to store an auth token in an Angular app This is exactly the flow I am working with in my application but the answer talks about using an Angular service and I'm not so sure I would want to do this, surely this can all be handled by Express so the client side code doesnt need to know about the token, just any errors the API server returns back.
So summary of flow I think I need:
There is probably middleware already out there that handles this kinda thing, I have already seen PassportJS which seems to be the kinda thing I may want to use, but I'm not so sure it handles the OAuth2 token flow on the server I am working against (password grant) and instead seems to be more suited to the redirect login OAuth flow.
I surely need somewhere to save the token value in Express, so some form of storage (not in memory I dont think).
I am fairly new to Express so would appreciate any suggestions\advice on how to approach this.
Thanks
# Tokens stored in localStorage are automatically protected from CSRF attacks, because localStorage items are not automatically sent to servers with each HTTP request. But they are vulnerable to XSS attacks, where they can be easily accessed by JavaScript.
The solution. We will authenticate with Google on a Node. js server, send the access_token and refresh_token to the user on the front end after the authentication is done, and store the refresh_token on localStorage. You can also store this on your user database file.
When the user signs in at SSO, the refresh/access tokens are stored in localStorage. This will be available for subsequent visits to SSO.
The most secure way to do this is just as you described:
secure
and httpOnly
cookie flags when you do this: this ensures the cookie CANNOT BE READ by client-side Javascript code, or over any non-SSL connection.If your service also needs to make asynchronous requests to Google / Facebook / etc. when the user is NOT actively clicking around on your site, you should also store their token in your user database somewhere as well -- this way you can make requests on behalf of the user whenever you need to.
I'm the author of express-stormpath, a Node auth library (similar to Passport), and this is how we do things over there to ensure maximal security!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With