Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use Facebook OAuth to secure my RESTful web service?

I'm writing a mobile phone app that allows users to register via Facebook. Once registered, users can then access personalised information via a RESTful web service I will host.

I've seen various mobile apps that appear to use a similar set-up but only present Facebook (or Twitter) OAuth authentication to their users. I'm wondering how this is done?

I thought that, to secure this web service, I could use HTTP Basic authentication over HTTPS with the user's Facebook OAuth access token as their password.

Is this secure? How do other apps handle security when they only register users via Facebook?

like image 736
Ricardo Gladwell Avatar asked Jul 09 '12 18:07

Ricardo Gladwell


People also ask

Does Facebook login use OAuth?

Web OAuth Login settings enables any OAuth client token flows that use the Facebook web login dialog to return tokens to your own website. This setting is in the Products > Facebook Login > Settings section of the App Dashboard.

Does OAuth2 support Facebook?

It's the only authentication protocol supported by the major vendors. Google recommends OAuth2 for all of its APIs, and Facebook's Graph API only supports OAuth2. The best way to understand OAuth2 is to look at what came before it and why we needed something different. It all started with Basic Auth.

How do I add OAuth to Facebook?

In the App Dashboard, choose your app and scroll to Add a Product Click Set Up in the Facebook Login card. Select Settings in the left side navigation panel and under Client OAuth Settings, enter your redirect URL in the Valid OAuth Redirect URIs field for successful authorization.


1 Answers

Apps that use this kind of a format generally do the following:

  1. The app itself is a registered application with FB - meaning it has an App Key
  2. When the user registers using FB - what's really happening is that they are giving permissions to the app, allowing it to see their data, post to their wall, etc (whatever permissions the app requests)
  3. Once the user is logged in - the App can then request their information from FB, as long as it authenticates with the service using its App Key.

So - within your application, you'll generally store the user's FB ID, and when you're making requests for data (or requests to post to the wall, etc) - you submit your App Key + the user's FB ID, along with whatever action information you need to supply. The FB service then replies with the data you have permissions to see - or performs the action, so long as you have permission to perform it.

In a RESTful environment, the trick is that you are supposed to be fully stateless - meaning no sessions are tracked. This is fine, however - because your App already has its App Key - so all you need is the user's FB ID per request. Easy enough if you just insert the ID into a cookie - or manage it client-side. How's this work?

When you register your App with Facebook, you have to supply a URL on which you'll be hosting that App. This is primarily in order to support cross-site cookies and CORS requests. In other words: as long as your request is coming from a URL recognized by FB to be associated with your App Key, FB knows which user it is that's on your site - because it has full access to its own cookies.

So what does this mean to you in trying to use FB to OAuth enable your site?

It essentially means that FB becomes your log-in system. You are asserting the following:

"As long as FB says the user is who they say they are - I trust it, too."

So - when a user arrives at your site and clicks the "Log In using Facebook" button - your site will get back either a success or a failure. You can get more info about how to implement this, specifically, by looking at the Facebook Developers site, and specifically, the following references:

  1. Facebook Login
  2. API Reference > Login
  3. The Login Dialog
  4. Access Tokens and Types
  5. Login Architecture

Once FB gives you back a token indicating success - you can assert that the person you get back info from via FB's API is the person using your site. So - if you store their FB ID in your database as your primary key, for example - you can now filter the results from your own API based on that value.

A round-trip might look something like:

  1. Unauthenticated user arrives at your site
  2. Redirect / provide a log-in button to authenticate with Facebook
  3. Determine the user's now authenticated identity by hitting the FB Graph API
  4. Your UI script now submits the FB ID received from Graph along with its requests to your API layer
  5. Your API layer filters data based on the FB ID (associated with your User record) - and returns proper data

Hope this is helpful. If you have questions - please ask in comments, and I'll try to add more detail as I can.

like image 141
Troy Alford Avatar answered Oct 19 '22 13:10

Troy Alford