Mercadopago.OAuth (mercadopago_sdk_elixir v0.2.1)

Copy Markdown View Source

OAuth 2.0 authorization code flow for marketplace/platform integrations.

The token endpoint needs no prior credentials, so bootstrap it with a tokenless client — Mercadopago.new(nil) — and keep the app secret in the request body:

verifier = Mercadopago.OAuth.generate_code_verifier()

url =
  Mercadopago.OAuth.get_authorization_url(app_id, redirect_uri, state,
    code_challenge: Mercadopago.OAuth.code_challenge(verifier)
  )

# ...redirect the seller to `url`, then on the callback:

{:ok, %{status: 200, response: %{"access_token" => token}}} =
  Mercadopago.OAuth.create(Mercadopago.new(nil), %{
    client_id: app_id,
    client_secret: app_secret,
    code: code,
    redirect_uri: redirect_uri,
    code_verifier: verifier
  })

Store verifier in the user's session alongside state; both must survive the redirect. PKCE is optional on MercadoPago today, but it removes the value of an intercepted authorization code and costs nothing to include.

Summary

Functions

Derives the S256 PKCE challenge from a verifier (RFC 7636 section 4.2).

Exchanges an authorization code for an access token.

Generates a PKCE code verifier: 32 random bytes, base64url-encoded without padding (RFC 7636 section 4.1). Keep it server-side; only its challenge is public.

Builds the authorization URL to redirect the seller to. Does not make an HTTP call.

Refreshes an expired access token. grant_type defaults to "refresh_token".

Functions

code_challenge(code_verifier)

@spec code_challenge(String.t()) :: String.t()

Derives the S256 PKCE challenge from a verifier (RFC 7636 section 4.2).

create(client, oauth_data, opts \\ [])

Exchanges an authorization code for an access token.

grant_type defaults to "authorization_code". Include :code_verifier when the authorization URL carried a PKCE challenge.

generate_code_verifier()

@spec generate_code_verifier() :: String.t()

Generates a PKCE code verifier: 32 random bytes, base64url-encoded without padding (RFC 7636 section 4.1). Keep it server-side; only its challenge is public.

get_authorization_url(app_id, redirect_uri, random_id, opts \\ [])

@spec get_authorization_url(String.t(), String.t(), String.t(), keyword()) ::
  String.t()

Builds the authorization URL to redirect the seller to. Does not make an HTTP call.

random_id is the CSRF state: generate it per authorization attempt, store it in the session, and compare it on the callback. The SDK neither generates nor verifies it.

Options

  • :code_challenge - PKCE challenge from code_challenge/1; omitted when nil
  • :code_challenge_method - defaults to "S256" when a challenge is given

refresh(client, oauth_data, opts \\ [])

Refreshes an expired access token. grant_type defaults to "refresh_token".