OAuth Token

Contents

We encourage the use of OAuth 2.0 over TLS as an alternative to Mutual TLS for all integrations, having the possibility to have a different token for each provider's resources.

The motivations for this recommendation are:

  • Security: complies with highest standard of security.
  • Standardization: it's a standard mechanism of authentication and also one of the most used.
  • MTLS support: can be used as extra security layer.
  • Plenty of external providers or libraries for custom implementations (Auth0, Google Cloud, AWS, etc.)

General considerations:
  • The providers needs to implement an endpoint that provides the authentication token for a given resource (audience).
  • The method for the mentioned endpoint should be POST.
  • The endpoint needs to be secured using a client_id and client_secret, both needs to be provided by the provider on a very secure way.
  • The client_id and client_secret will be send as basic Authentication header.
  • The client_id and client_secret never will be send as part of the body request.
  • The request body will have the necessary information to identify the integration flow such as fields audience, grant_type and client_credentials.

Example of communication:

Request

POST {url}/oauth/token
 --header 'Content-Type: application/json'
 --header 'Authorization: Basic <Base64 encoded $client_id:$client_secret>'
 --data-raw '{
  "audience": "<API_IDENTIFIER>",
  "grant_type": "client_credentials"
 }'

Headers

NameValueDescription
Content-Typeapplication/jsonExpected media type of the resource.
AuthorizationBasic <Base64 encoded $client_id:$client_secret>Should be Basic type, the value is the base64 encode of the concatenation of client_id and client_secret using a colon (:) as a separation character.

The request body will have a JSON object with following fields:

NameType of dataDescriptionType
audienceStringResource name that the token is being requested:
  • authorizations
  • tracking-pull
  • agencies
  • booking
  • logistic-feed
  • handling-unit
  • rtt
  • fiscal-info
  • revoke
  • coverage
Mandatory
grant_typeStringValid value:
  • client_credentials
Mandatory

Response

HTTP 200 OK
 --header 'Content-Type: application/json'
 {
  "access_token": "<ACCESS_TOKEN>",
  "token_type": "Bearer",
  "expires_in": 86400
 }

The response body needs to have following fields:

NameType of dataDescriptionType
access_tokenStringAccess token for the requested resource.Mandatory
token_typeStringToken type, should be alway of type “Bearer” given that client credentials grant type will be used.Mandatory
expires_inLongToken expiration time expressed in seconds.Mandatory

Status Codes

StatusCodeDescription
AUTHORIZED200Authentication with success.
BAD REQUEST400Whenever the request is invalid.
UNAUTHORIZED401In the case that credentials are invalid or it does not have access to requested resource.
ERROR500Any authentication server issue.

Revocation of OAuth Token

The revocation of OAuth tokens in MercadoLibre resembles the IETF standard, which states that, for the revocation of tokens generated through the OAuth 2.0 protocol, the client can notify the authorization server that a specific access token should no longer be considered valid. Additionally, this feature will allow us to act on demand in response to any eventuality that may arise, regardless of the token's expiration time; in other words, we do not have to wait for the token to expire. The token revocation request is made via an HTTP POST request to the revocation endpoint. This request must include the token that you wish to revoke. The format of the request body must be application/x-www-form-urlencoded and contain a 'token' parameter with the value of the token to be revoked. As a response, the server indicates whether the token was successfully revoked. If successful, the server returns an HTTP status code 200. If the token could not be revoked, an HTTP status code 4xx or 5xx is returned. As a security requirement, the standard establishes measures to ensure that only the legitimate holder of the token can revoke it. This includes the authentication of the client making the revocation request, using a valid access token generated by the audience "revoke," for which the logistics provider must enable token generation.

In addition to authentication, the client must have the appropriate permissions to perform the token revocation (audience). For example, a token can only be revoked by the same client and with the audience "revoke." It should never be possible to revoke a token if the token in the header is the same as the one being requested for revocation.

EndpointHeadersBodyResponse
POST https://service_url/oauth/revoke
  • Authorization: Bearer NEW_ACCESS_TOKEN_WITH_REVOKE_AUDIENCE
  • Content-Type: application/x-www-form-urlencoded
  • Content-Type: application/json

Request

curl -X POST https://service_url/oauth/revoke  
  -H 'Authorization: Bearer NEW_ACCESS_TOKEN_WITH_REVOKE_AUDIENCE' 
  -H 'Content-Type: application/x-www-form-urlencoded' 
  -d 'token=ACCESS_TOKEN_TO_REVOKE'

Response Formats for OAuth Token Revocation

2xx OK

{
    "status": "OK"
}

4XX FAILED

{
    "status": "FAILED",
    "message": "Descriptive message indicating the reason for the failure."
}

5XX ERROR

{
    "status": "ERROR",
    "message": "Descriptive message indicating the reason for the server error."
}

Testing Suite

There's a section on our test suite (docker) to simulte our API call to /oauth/token with a form to complete required details such as client_id, client_secret and a dropdown to select audience (api identifiers). This docker builds the Authorization Basic header using the credentials provided in the form (client_id and client_secret) encoded with Base64 to be included in the request. To test each integration including the oauth token (Auth, Cancel, Agency Auth, Agency, Booking, Tracking Pull) there's a field in each screen to complete with the token generated manually so that the docker adds the Bearer header with such token on the integration request.