Skip to main content

OAuth 2.0 Flows

Mubarokah ID primarily uses the OAuth 2.0 Authorization Code Grant for third-party application authentication. This flow is designed for applications that can securely store a client secret. Additionally, a Refresh Token Flow is provided to obtain new access tokens without requiring user re-authentication. The Authorization Code Grant is the most common and secure OAuth 2.0 flow. It involves exchanging an authorization code for an access token.

Detailed Step-by-Step Implementation

Step 1: Authorization Request (User Redirection)

Your application initiates the flow by redirecting the user’s browser to Mubarokah ID’s authorization endpoint.
Parameters: Example Redirect URL Construction (JavaScript):
  • The user is prompted to log in to Mubarokah ID (if not already).
  • After successful login, Mubarokah ID displays a consent screen where the user can approve or deny the permissions (scopes) your application is requesting.

Step 3: Authorization Code Response (Redirection to Your App)

If the user grants consent, Mubarokah ID redirects the user back to your application’s redirect_uri with an authorization_code and the state parameter. Example Success Redirect:
Your application should first verify that the received state matches the one generated in Step 1 to prevent CSRF attacks.
Example Error Redirect: If the user denies access or an error occurs:

Step 4: Token Exchange (Server-to-Server)

This step must be performed on your application’s backend server, as it requires your client_secret.
Your application exchanges the authorization_code for an access_token and a refresh_token by making a POST request to Mubarokah ID’s token endpoint.
Request Body Parameters (application/x-www-form-urlencoded): Example Token Exchange (Node.js - Server-Side):
Successful Token Response (JSON):
Store the access_token and refresh_token securely, associating them with the user. The access_token is used to make API requests on behalf of the user.

Refresh Token Flow

Access tokens are short-lived for security reasons. When an access token expires, your application can use a refresh token to obtain a new access token without requiring the user to go through the authorization flow again.
Request Body Parameters (application/x-www-form-urlencoded): Example Refresh Token Request (Node.js - Server-Side):
Refresh tokens can also expire or be revoked. If the refresh token flow fails, your application should direct the user to re-initiate the Authorization Code Grant flow.

Client Credentials Grant (Machine-to-Machine)

For server-to-server communication where no user is directly involved, the Client Credentials Grant can be used. This flow allows your application to obtain an access token to access its own resources or act on its own behalf.
Request Body Parameters (application/x-www-form-urlencoded): Example Client Credentials Request (Node.js - Server-Side):
This flow typically does not return a refresh token. When the access token expires, your application must request a new one using the same Client Credentials Grant.