SergioBrito
SergioBrito

Reputation: 83

OAuth2 Session Timeout vs Session ID Timeout and Refresh Tokens

In my app, I allow my users to authenticate with their existing google, microsoft, etc accounts via OAuth2.

Everything runs smoothly. Upon getting callback to local redirect url, I successfully request and receive bearer token from endpoint. With access token in hand, I then request UserInfo from user info endpoint. I then compare email address in UserInfo JSON object with the email address registered in user record and if they match, I consider user signed in.

Now, my question is regarding bearer token and session timeouts.

Google bearer token looks like it authorizes for 60 minutes; while my server session lasts 30 minutes (I haven't changed the default).

Since user already got successfully authenticated, the session id will remain active and alive while there's activity within every 30 minutes. However the bearer token expiration will have expired after an hour.

I would normally assume that I need to refresh the access token before it expires so long as there's activity within the established server session. However, google does not appear to have a refresh token endpoint.

But even if it did, would it be desirable to do this?

Or since the fact that I have an active session id from an authenticated user is enough to allow access to protected resources while only the session id is 'alive'?

I'm assuming it is, since some websites allow customers to maintain their sessions for days at a time, at which time, bearer token would have expired long before.

And lastly, how long would you recommend I keep my users (customers, really) with an open session? My website is on online store.

Thanks to all!

Upvotes: 2

Views: 5885

Answers (1)

Michal Trojanowski
Michal Trojanowski

Reputation: 12322

Your session can be completely detached from Google's session. You should only be concerned whether your session is still active. The validity of the access token from Google is not relevant here. Remember that the expiration time of an access token has, in fact, nothing to do with a user's session at all. E.g. you log in a user using Google. The user authenticates at Google and you get an access token, which is valid for 60 minutes. The user then logs out at Google. Your access token will still be valid until that 60 minutes pass, even though the user logged out from Google, and her session is no longer valid there.

As for the length of the session, this is really up to you. If you know your customers are likely to come back often, and you want to make it easier for them you can even keep a session indefinitely. In such a scenario you should think about security and privacy - if the user leaves their account logged in on a shared computer, how much could it hurt them if someone else manages to use their session after a week or so. If you know your customers are likely to come back every few weeks or months to your store, then it really doesn't matter if you keep the session open for a day or five. Most of them will have to log in again anyway.

So to answer the question of the length of the session you should study the behavior of your users and take into account security and privacy issues.

Upvotes: 1

Related Questions