Understanding OAuth: An Overview
0The scenario: user Alice wants to allow SimpleService to access Twitter on her behalf.
Before OAuth this would be done by having Alice give SimpleService her login credentials for Twitter. She would type in her Twitter username and password into SimpleService, and SimpleService would use those credentials to access Twitter on her behalf. This was bad because SimpleService now knew Alice’s credentials, and could do malicious things or carelessly leak her credentials to malicious people.
With OAuth Alice doesn’t have to give SimpleService her Twitter credentials. Instead she goes through a process wherein she tells Twitter that SimpleService is allowed to act on her behalf.
The flow is:
First, before Alice is involved, Twitter and SimpleService exchange secret information: SimpleService requests Twitter to provide it with a consumer token and a secret. The consumer token will be used to identify SimpleService to Twitter, and the secret will be used to secure the communications to prevent others from pretending to be SimpleService.
This is setup once, allowing SimpleService and Twitter to communicate in general.
Once setup, for each user that wants to allow SimpleService to access Twitter on their behalf, the following happens:
- The user Alice goes to the SimpleService website and requests SimpleService to access her Twitter account.
- SimpleService calls Twitter with a
request_tokencall. This tells Twitter SimpleService is about to have a conversation with it regarding authorization. - Twitter responds with a
request_token. - SimpleService, having received the token from Twitter, redirects the user’s browser to a Twitter
authorizepage, passing the along therequest_tokenas part of the url. - Twitter grabs the token (along with various signatures and timestamps to verify the request is not forged).
- Twitter displays a page to the user asking her if she wants to give SimpleService access to her Twitter account.
- If the user says no the game is over, and the token is not authorized.
- If the user says yes, Twitter redirects the user back to SimpleService, passing along an authorization token, letting SimpleService know the user authorized access.
- SimpleService now exchanges the authorization token for an access token: it calls Twitter with the authorization token, and requests Twitter to give it an access token.
- Twitter examines the token SimpleService sent it, verifies that it’s not forged and that the user Alice had earlier authorized that token for access. It now believes that Alice wants to grant SimpleService access to her Twitter account. Twitter responds to SimpleService with an Access token.
- SimpleService grabs the access token and stores it, associating it with the user Alice.
Now the initial authorization dance is done: SimpleService has an access token that allows it to access Twitter on behalf of Alice.
To actually access Twitter on behalf of Alice, SimpleService includes the access token with each call it makes to Twitter on behalf of Alice. Twitter checks the token, verifies that is valid, and allows SimpleService to access Alice’s resources.
To be an OAuth service provider (that is, play the part of Twitter), you need to:
- Have a way to exchange consumer tokens and secret keys with third parties (eg. SimpleService) that want to access your APIs.
- Provide a request_token service that provides tokens that start the conversation for each authorization.
- Provide an authorization page that tells the user that a third party (eg. SimpleService) is requesting access to her resources, and allows her to accept or reject the request.
- Make a callback to the service (eg. SimpleService), informing it that the request has been authorized.
- Provide an access_token service that accepts a request token that’s been authorized and provides an access token to the third party (eg. SimpleService).
- Store the access token for the user that authorized it.
- Accept access tokens as an authentication mechanism for API calls, verifying the token’s validity and authenticating the user associated with that token.
Manage your expenses via Email, SMS, iPhone, Twitter, Voice (Call and say your expense), IM (Yahoo, AIM, MSN), or Web.

