Reputation: 1307
I've been reading a lot about the topic but all I find are obsolete or partial answers, which don't really help me that much and actually just confused me more. I'm writing a Rest API (Node+Express+MongoDB) that is accessed by a web app (hosted on the same domain than the API) and an Android app.
I want the API to be accessed only by my applications and only by authorized users. I also want the users to be able to signup and login only using their Facebook account, and I need to be able to access some basic info like name, profile pic and email.
A possible scenario that I have in mind is:
Does this make sense? Does this approach have any macroscopic security hole that I'm missing? One problem I see using MongoDB to store these info is that the collection will quickly become bloated with old tokens. In this sense I think it would be best to use Redis with an expire policy of 1 hour so that old info will be automatically removed by Redis.
Upvotes: 24
Views: 6137
Reputation: 858
I know I'm late to the party, but I'd like to add a visual representation of this process as I'm dealing with this problem right now (specifically in dealing with the communication between the mobile app and the web api by securing it with a 3rd party provider like facebook).
For simplicity, I haven't included error checks, this is mostly just to outline a reasonable approach. Also for simplicity, I haven't included Tommy's suggestion to only pass your own custom api token once the authorization flow is over, although I agree that this is probably a good approach.
Please feel free to criticize this approach though, and I'll update as necessary.
Also, in this scenario, "My App" refers to a mobile application.
Upvotes: 0
Reputation: 2800
I think the better solution would be this:
fb_access_token
given, make sure its valid. Get user_id
,email
and cross-reference this with existing users to
see if its a new or old one.api_access_token
that you give back to the webapp and android app. If you need Facebook for
anything other than login, store that fb_access_token
and in your
DB associate it with the new api_access_token
and your user_id
.api_access_token
to authenticate it. If you need the fb_access_token
for getting more info, you can
do so by retrieving it from the DB.In summary: Whenever you can, avoid passing the fb_access_token
. If the api_access_token
is compromised, you have more control to see who the attacker is, what they're doing etc than if they were to get ahold of the fb_access_token
. You also have more control over settings an expiration date, extending fb_access_token
s, etc
Just make sure whenever you pass a access_token of any sort via HTTP, use SSL.
Upvotes: 36