Skip to content

Instantly share code, notes, and snippets.

@xtina-starr
Last active August 29, 2015 14:04
Show Gist options
  • Save xtina-starr/3218fbef84f62aa0b0a0 to your computer and use it in GitHub Desktop.
Save xtina-starr/3218fbef84f62aa0b0a0 to your computer and use it in GitHub Desktop.
Quick overview of OAuth 2.

OAuth 2


The OAuth 2.0 authorization process enables a third-party application to obtain limited access to an HTTP service and data through an authentication process. It is a protocol that lets external apps request authorization to private details without getting their password. This is preferred over Basic Authentication because tokens can be limited to specific types of data, and can be revoked by users at any time.

The OAuth roles:

  • Third-party Application: Client - the party seeking to gain access to resources (ex. Houston ISD's student management platform)
  • The API: Resource Server (ex. The API we will create)
  • Resource Owner: An entity capable of granting access to a protected resource (ex. Apex Learning)
  • The Authoratization Server: The server issuing access tokens to the client after successfully authenticating the resource owner and obtaining authorization.

The OAuth 2.0 Flow


  Protocol Flow

     +--------+                               +---------------+
     |        |--(A)- Authorization Request ->|   Resource    |
     |        |                               |     Owner     |
     |        |<-(B)-- Authorization Grant ---|               |
     |        |                               +---------------+
     |        |
     |        |                               +---------------+
     |        |--(C)-- Authorization Grant -->| Authorization |
     | Client |                               |     Server    |
     |        |<-(D)----- Access Token -------|               |
     |        |                               +---------------+
     |        |
     |        |                               +---------------+
     |        |--(E)----- Access Token ------>|    Resource   |
     |        |                               |     Server    |
     |        |<-(F)--- Protected Resource ---|               |
     +--------+                               +---------------+

OAuth 2.0 flow illustrated describes the interaction between the four roles and includes the following steps:

(A) The client requests authorization from the resource owner. The authorization request can be made directly to the resource owner (as shown), or preferably indirectly via the authorization server as an intermediary.

(B) The client receives an authorization grant, which is a credential representing the resource owner's authorization, expressed using one of four grant types defined in this specification or using an extension grant type. The authorization grant type depends on the method used by the client to request authorization and the types supported by the authorization server.

(C) The client requests an access token by authenticating with the authorization server and presenting the authorization grant.

(D) The authorization server authenticates the client and validates the authorization grant, and if valid, issues an access token.

(E) The client requests the protected resource from the resource server and authenticates by presenting the access token.

(F) The resource server validates the access token, and if valid, serves the request.


A practical example:

alt text

How this works:
A - a user opens their web-browser and goes to MyPhotos.com which stores all of their photos. MyPhotos.com doesn't handle authentication itself, so the user is redirected to the Authorization Server with a request for authorization. The user is presented with a login form and is asked if they want to approve the Resource Server (MyPhotos.com) to act on their behalf. The user logs in and they are redirected back to MyPhotos.com.

B - MyPhotos.com receives an authorization grant code as a part of the redirect and then passes this along to the client.

C - the Client then uses that authorization grant code to request an access token from the Authorization Server.

D - if the authorization grant code is valid, then the Authorization Server grants an access token. The access token is then used by the client to request resources from the Resource Server (MyPhotos.com).

E - MyPhotos.com receives the request for a resource and it receives the access token. In order to make sure it's a valid access token it sends the token directly to the Authorization Server to validate. If valid, the Authorization Server sends back information about the user.

F - having validated the user's request MyPhotos.com sends the requested resource back to the user.

Resource:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment