# `ExFattureincloud.OAuth`
[🔗](https://github.com/elchemista/ex_fattureincloud/blob/v0.1.0/lib/ex_fattureincloud/oauth.ex#L26)

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`.

# `authorization_url`

```elixir
@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`

```elixir
@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`

```elixir
@spec device_token(ExFattureincloud.Client.t(), keyword()) ::
  {:ok, ExFattureincloud.OAuth.Token.t()} | {:error, ExFattureincloud.Error.t()}
```

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

# `exchange_code`

```elixir
@spec exchange_code(ExFattureincloud.Client.t(), keyword()) ::
  {:ok, ExFattureincloud.OAuth.Token.t()} | {:error, ExFattureincloud.Error.t()}
```

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

# `generate_state`

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

Generates a cryptographically random URL-safe state value.

# `refresh_token`

```elixir
@spec refresh_token(ExFattureincloud.Client.t(), keyword()) ::
  {:ok, ExFattureincloud.OAuth.Token.t()} | {:error, ExFattureincloud.Error.t()}
```

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`

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

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

---

*Consult [api-reference.md](api-reference.md) for complete listing*
