Published on

OAuth 2.0 - Authorization Flow

Authors
  • avatar
    Name
    Skim
    Twitter

OAuth 2.0 is a widely used authorization framework that allows third-party applications to access a user's resources on a web service (like social media platforms) without exposing the user's credentials. OAuth 2.0 defines several different authorization flows or grant types, each suited for different use cases. Below is an explanation of the most common OAuth 2.0 flow, known as the "Authorization Code Flow."

  1. Client Registration: before a third-party application (client) can use OAuth 2.0, it needs to be registered with the authorization server (the server handling user authentication and authorization). During registration, the client receives a client identifier (ID) and a client secret. These credentials are used to authenticate the client with the authorization server.
  2. User Initiates Login: the user clicks on the "Log in with [Social Media Platform]" button on the third-party application.
  3. Authorization Request: the third-party application constructs an authorization request and sends the user to the authorization endpoint of the chosen social media platform. The request typically includes parameters such as:
    • response_type: Set to "code" to indicate the authorization code flow.
    • client_id: The client identifier obtained during registration.
    • redirect_uri: The URI where the user should be redirected after granting or denying permission.
    • scope: The permissions (scopes) the application is requesting.
  4. User Grants Permission: The user is prompted to log in to their social media account (if not already logged in) and is presented with the requested permissions that the third-party application is asking for.
  5. Authorization Code: If the user grants permission, the authorization server generates an authorization code and redirects the user back to the redirect URI provided by the third-party application, along with the authorization code.
  6. Token Exchange: Upon receiving the authorization code, the third-party application sends a POST request to the token endpoint of the authorization server. The request includes parameters such as:
    • grant_type: Set to "authorization_code" to indicate the authorization code flow.
    • code: The authorization code received in the previous step.
    • client_id and client_secret: The client credentials for authentication.
    • redirect_uri: The same redirect URI used in the authorization request.
  7. Access Token and Refresh Token: if the authorization server validates the request, it responds with an access token and optionally a refresh token. The access token is a short-lived credential that the client can use to access the user's resources on the social media platform's APIs. The refresh token is used to obtain a new access token without requiring user interaction.
  8. Accessing User Data: the third-party application can now use the access token to make API requests to the social media platform's servers on behalf of the user, as long as the token is valid and the requested scopes are allowed.

This flow ensures that sensitive user credentials are not exposed to the third-party application, and access is granted based on the user's consent. It provides a secure and standardized way for applications to access user data on various platforms while maintaining user privacy and security.