Juank
Juank

Reputation: 6196

Should the access token in oAuth be generated every time the user logs in?

I've implemented the oAuth in php (currently for twitter) and as I've read in several tutorials you should store the access token in db for future use. However I don't see how you know if you have the access token stored for a particular user to decide if you should pull it out of the db or regenerate it. Here's a flow describing my question:

First time user signs in:

User returns 10 minutes later:

User returns 1 month later:

The main workflow for oAuth is clear, however it is not clear how to handle returning users and which data should be stored or not.

A million thanks!

Upvotes: 13

Views: 7472

Answers (2)

YaNuSH
YaNuSH

Reputation: 1097

The only thing I believe is missing here, is generate a random (long and unguessable) user id first time the user joins the system, and store it forever. this way you can tell who's taking the actions

Upvotes: 0

ariefbayu
ariefbayu

Reputation: 21979

You should not regenerate token for each access. Generate it only when it's expired. I've build twitter application using OAuth. Here my flow:

  1. when user login, I will check if they have token in DB

    1.1. If it's not exists, authenticate them and then store and use the resulting token

    1.2. If it's exists, use it.

    1.2.1. If twitter doesn't complain, then the token still valid, use it.

    1.2.2. If twitter complained, then the token is expired. Return to 1.1.

    1.2.3. If after x retry twitter still complained. Something wrong, notify admin!

Here's the graphical explanation:

enter image description here

Upvotes: 23

Related Questions