MobDev.GooglePlay.OAuth (mob_dev v0.7.33)

Copy Markdown View Source

OAuth2 browser-based authorization for Google APIs.

Opens the user's default browser to Google's consent screen, then listens on a random port on 127.0.0.1 for the authorization code callback. No external CLI tool (gcloud, etc.) is required — only a browser.

Each sign-in sends a random state and a PKCE (S256) code challenge. A callback whose state does not match is answered with 400 and ignored, and the code is exchanged together with the PKCE verifier, so only this process can redeem it.

OAuth client registration

This module ships with placeholder client credentials. Before authorize/1 will work you must register a Google OAuth "Desktop app" client:

  1. Go to https://console.cloud.google.com/apis/credentials
  2. Click Create credentials → OAuth client ID
  3. Application type: Desktop app — name it mob_dev CLI
  4. Click Create — note the Client ID and Client secret
  5. Fill in @default_client_id and @default_client_secret in this file

For installed CLI tools, the client_secret is not actually secret — this follows Google's documented guidance for desktop applications (the same model used by the gcloud CLI). The token is only obtainable by a user who explicitly grants consent via their browser.

You can also override with environment variables: GOOGLE_OAUTH_CLIENT_ID / GOOGLE_OAUTH_CLIENT_SECRET.

Summary

Types

The per-sign-in secrets: the state value and the PKCE verifier and challenge.

Functions

Runs the browser-based OAuth2 flow and returns a bearer access token.

Builds the Google OAuth2 authorization URL, carrying the request's state and PKCE code challenge.

The PKCE S256 code challenge for verifier (RFC 7636 §4.2).

Generates the per-sign-in secrets: a random state and a PKCE verifier with its S256 challenge.

Parses an OAuth callback HTTP request line against the expected state.

Returns the scopes requested during the setup wizard OAuth flow.

Types

request()

@type request() :: %{
  state: String.t(),
  code_verifier: String.t(),
  code_challenge: String.t()
}

The per-sign-in secrets: the state value and the PKCE verifier and challenge.

Functions

authorize(opts \\ [])

@spec authorize(keyword()) :: {:ok, String.t()} | {:error, String.t()}

Runs the browser-based OAuth2 flow and returns a bearer access token.

Opens the user's browser to the Google consent screen, then waits up to timeout_ms milliseconds (default 120 000) for the callback redirect.

Options:

  • :scopes — list of OAuth scope strings (required)
  • :timeout_ms — callback wait timeout in ms (default: 120_000)

Returns {:ok, access_token} or {:error, reason}.

build_auth_url(client_id, scopes, redirect_uri, map)

@spec build_auth_url(String.t(), [String.t()], String.t(), request()) :: String.t()

Builds the Google OAuth2 authorization URL, carrying the request's state and PKCE code challenge.

Pure function — useful for testing and for displaying the URL in case the automatic browser open fails.

code_challenge(verifier)

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

The PKCE S256 code challenge for verifier (RFC 7636 §4.2).

new_request()

@spec new_request() :: request()

Generates the per-sign-in secrets: a random state and a PKCE verifier with its S256 challenge.

parse_callback_request(request_line, expected_state)

@spec parse_callback_request(String.t(), String.t()) ::
  {:ok, String.t()} | {:error, String.t()} | {:ignore, String.t()}

Parses an OAuth callback HTTP request line against the expected state.

The request line has the form:

GET /callback?state=STATE&code=AUTH_CODE&scope=... HTTP/1.1

Returns {:ok, code}, {:error, reason} when Google reports an error for this sign-in, or {:ignore, reason} when the request is not this sign-in's callback (wrong or missing state, another path, unparseable). An ignored request never yields a code.

setup_scopes()

@spec setup_scopes() :: [String.t()]

Returns the scopes requested during the setup wizard OAuth flow.