ExFattureincloud.OAuth (ExFattureincloud v0.1.0)

Copy Markdown View Source

OAuth authorization-code, refresh-token and device-code helpers, following the official Go and TypeScript SDKs. JSON bodies are used, not HTTP Basic auth.

Call authorization_url/2 with a fresh generate_state/0; persist it in the user's server session and validate the callback with validate_state/2 before exchanging the code. Tokens are returned as OAuth.Token structs. Save the new refresh token after every successful refresh. These functions do not store credentials; ExFattureincloud.OAuth.TokenManager optionally coordinates renewal and application-provided persistence callbacks.

Device authorization requires provider enablement. device_token/2 performs exactly one poll: schedule subsequent polls using the returned interval and handle authorization_pending/slow_down through Error.code.

Summary

Functions

Builds the URL for the Authorization Code Flow browser redirect.

Requests a device code using :client_id and :scope, returning the provider's data as a map.

Makes one device-token poll using :client_id and :device_code.

Exchanges an authorization code using :client_id, :client_secret, :redirect_uri, and :code.

Generates a cryptographically random URL-safe state value.

Refreshes the access token using :client_id and :refresh_token.

Constant-time comparison; rejects missing or empty state values.

Functions

authorization_url(client, options)

@spec authorization_url(ExFattureincloud.Client.t(), keyword()) :: String.t()

Builds the URL for the Authorization Code Flow browser redirect.

Requires :client_id, the registered :redirect_uri, a :scope string or list, and the :state returned by generate_state/0.

device_code(client, options)

@spec device_code(ExFattureincloud.Client.t(), keyword()) ::
  {:ok, map()} | {:error, ExFattureincloud.Error.t()}

Requests a device code using :client_id and :scope, returning the provider's data as a map.

device_token(client, options)

Makes one device-token poll using :client_id and :device_code.

exchange_code(client, options)

Exchanges an authorization code using :client_id, :client_secret, :redirect_uri, and :code.

generate_state()

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

Generates a cryptographically random URL-safe state value.

refresh_token(client, options)

Refreshes the access token using :client_id and :refresh_token.

Pass :client_secret for Authorization Code Flow. If the response omits a refresh token, keep the previous refresh token when persisting the result.

validate_state(expected, actual)

@spec validate_state(term(), term()) :: :ok | {:error, :invalid_state}

Constant-time comparison; rejects missing or empty state values.