apiac v1.0.0 APIac View Source

Convenience functions to work with APIac and API requests

Link to this section Summary

Functions

Returns true if the connection is authenticated by an APIac plug, false otherwise

Returns the APIac.Authenticator that has authenticated the connection, nil if none has

Returns the name of the client, or nil if the connection is unauthenticated

Returns the following error response verbosity level depending on the environment

Returns true if this is a machine-to-machine authentication (i.e. no subject involved), false otherwise

Returns metadata associated with the authenticated connection, or nil if there's none

Returns true if the input string is an rfc7230 quoted-string, false otherwise

Returns true if the input string is an rfc7230 token, false otherwise

Returns true if the input string is an rfc7235 token68, false otherwise

Sets the HTTP WWW-Authenticate header of a Plug.Conn and returns it.

Returns the name of the subject, or nil if it was not set (unauthenticated connection, machine-tomachine authentication...)

Returns true if the authentication is on behalf of a real user (the subject), false otherwise

Link to this section Types

Link to this type

http_authn_scheme()

View Source
http_authn_scheme() :: String.t()
Link to this type

metadata()

View Source
metadata() :: %{required(String.t()) => String.t()}

Link to this section Functions

Returns true if the connection is authenticated by an APIac plug, false otherwise

Link to this function

authenticator(arg1)

View Source
authenticator(Plug.Conn.t()) :: atom() | nil

Returns the APIac.Authenticator that has authenticated the connection, nil if none has

Returns the name of the client, or nil if the connection is unauthenticated

Link to this function

default_error_response_verbosity(conn)

View Source
default_error_response_verbosity(Plug.Conn.t()) :: :debug | :normal | :minimal

Returns the following error response verbosity level depending on the environment:

  • dev: :debug
  • test: :normal
  • prod: :normal

It uses the APIac configuration key :env that is by defaults executed to Mix.env()

Link to this function

machine_to_machine?(conn)

View Source
machine_to_machine?(Plug.Conn.t()) :: boolean()

Returns true if this is a machine-to-machine authentication (i.e. no subject involved), false otherwise

Link to this function

metadata(arg1)

View Source
metadata(Plug.Conn.t()) :: %{required(String.t()) => String.t()} | nil

Returns metadata associated with the authenticated connection, or nil if there's none

Link to this function

rfc7230_quotedstring?(val)

View Source
rfc7230_quotedstring?(String.t()) :: boolean()

Returns true if the input string is an rfc7230 quoted-string, false otherwise

Link to this function

rfc7230_token?(val)

View Source
rfc7230_token?(String.t()) :: boolean()

Returns true if the input string is an rfc7230 token, false otherwise

Link to this function

rfc7235_token68?(val)

View Source
rfc7235_token68?(String.t()) :: boolean()

Returns true if the input string is an rfc7235 token68, false otherwise

Link to this function

set_WWWauthenticate_challenge(conn, scheme, params)

View Source
set_WWWauthenticate_challenge(Plug.Conn.t(), http_authn_scheme(), %{
  required(String.t()) => String.t()
}) :: Plug.Conn.t()

Sets the HTTP WWW-Authenticate header of a Plug.Conn and returns it.

Note that the parameters are passed as a map whose:

  • keys are rfc7230 tokens
  • values are rfc7230 quoted-strings, but without the enclosing quotes: they are mandatory and therefore added automatically

Examples

iex> conn(:get, "/ressource") |>
...> Plug.Conn.put_status(:unauthorized) |>
...> APIac.set_WWWauthenticate_challenge("Basic", %{"realm" => "realm_1"}) |>
...> APIac.set_WWWauthenticate_challenge("Bearer", %{"realm" => "realm_1", "error" => "insufficient_scope", "scope" => "group:read group:write"})
%Plug.Conn{
  adapter: {Plug.Adapters.Test.Conn, :...},
  assigns: %{},
  before_send: [],
  body_params: %Plug.Conn.Unfetched{aspect: :body_params},
  cookies: %Plug.Conn.Unfetched{aspect: :cookies},
  halted: false,
  host: "www.example.com",
  method: "GET",
  owner: #PID<0.202.0>,
  params: %Plug.Conn.Unfetched{aspect: :params},
  path_info: ["ressource"],
  path_params: %{},
  peer: {{127, 0, 0, 1}, 111317},
  port: 80,
  private: %{},
  query_params: %Plug.Conn.Unfetched{aspect: :query_params},
  query_string: "",
  remote_ip: {127, 0, 0, 1},
  req_cookies: %Plug.Conn.Unfetched{aspect: :cookies},
  req_headers: [],
  request_path: "/ressource",
  resp_body: nil,
  resp_cookies: %{},
  resp_headers: [
    {"cache-control", "max-age=0, private, must-revalidate"},
    {"www-authenticate",
     "Basic realm="realm_1", Bearer error="insufficient_scope", realm="realm_1", scope="group:read group:write""}
  ],
  scheme: :http,
  script_name: [],
  secret_key_base: nil,
  state: :unset,
  status: 401
}
Link to this function

subject(arg1)

View Source
subject(Plug.Conn.t()) :: subject() | nil

Returns the name of the subject, or nil if it was not set (unauthenticated connection, machine-tomachine authentication...)

Link to this function

subject_authenticated?(arg1)

View Source
subject_authenticated?(Plug.Conn.t()) :: boolean()

Returns true if the authentication is on behalf of a real user (the subject), false otherwise