Auth0Client.Utils (auth0_client v1.1.0)

Copy Markdown View Source

Collection module of various utils needed for Auth0Client

Header building is split by whether it can fail. base_headers/0 is static and always succeeds. request_headers/2 may need a management token, so it returns an :ok/:error tuple — a tenant that rate-limits /oauth/token should surface as an error tuple from the calling API function, not as a raise.

Summary

Functions

The headers sent on every request, regardless of API.

The Authorization header list for a bearer token.

Removes a header by name, comparing case-insensitively.

Merges extra headers into base, letting extra win on duplicate names.

Returns the management API token.

Normalises a list of permissions into the shape Auth0's RBAC endpoints expect.

Builds the headers for a request, merging in any per-request extra headers.

Functions

base_headers()

The headers sent on every request, regardless of API.

base_url()

base_url(arg1)

bearer(token)

The Authorization header list for a bearer token.

Used both for the management token and for the tokens a caller supplies to the Authentication API — userinfo/1, the /mfa/* endpoints, global token revocation.

drop_header(headers, name)

Removes a header by name, comparing case-insensitively.

Needed for multipart uploads: Req sets its own content-type carrying the boundary, but only with put_new_header, so the default application/json would win and the boundary would never be sent.

http_opts()

merge_headers(base, extra)

Merges extra headers into base, letting extra win on duplicate names.

Names are compared case-insensitively, so a per-request Authorization replaces the default one rather than being sent alongside it.

mgmt_token()

@spec mgmt_token() :: {:ok, String.t()} | {:error, Auth0Client.Error.t()}

Returns the management API token.

A statically configured token is used as-is and never cached; otherwise Auth0Client.TokenState supplies one, fetching and renewing as needed.

normalize_permissions(permissions)

Normalises a list of permissions into the shape Auth0's RBAC endpoints expect.

Auth0 wants %{resource_server_identifier: _, permission_name: _} per permission, which is verbose to write by hand, so a {api_identifier, permission_name} tuple is accepted as shorthand:

normalize_permissions([{"https://api.example.com", "read:users"}])
#=> [%{resource_server_identifier: "https://api.example.com", permission_name: "read:users"}]

Maps pass through untouched, including the string-keyed maps Auth0 itself returns, so a permission read from one call can be fed straight into another.

oauth_url()

request_headers(tag, extra \\ [])

@spec request_headers(atom(), list()) ::
  {:ok, list()} | {:error, Auth0Client.Error.t()}

Builds the headers for a request, merging in any per-request extra headers.

Management requests carry a bearer token, so this returns {:error, reason} if no token can be obtained.

ua()