APIac

APIac: API access control for elixir

APIac is a standard interface for API access control and is composed of:

The following APIac plugs are available:

Usage

Just use one or more of these aforementioned APIac plugs, this library will be automatically imported.

Chaining plugs

APIac interfaces are designed so that you can chain APIac plugs. Example:

my_app/lib/my_app_web/router.ex

pipeline :api_public do
  plug APIacAuthBasic,
    realm: "Public API",
    callback: &MyApp.get_client_secret/2,
    set_error_response: APIacAuthBasic.set_WWWauthenticate_header/3,
    error_response_verbosity: :debug},
  plug APIacAuthBearer,
    bearer_validator:
      {APIacAuthBearer.Validator.Introspect, [
        issuer: "https://example.com/auth"
        tesla_middleware:[
          {Tesla.Middleware.BasicAuth, [username: "client_id_123", password: "WN2P3Ci+meSLtVipc1EZhbFm2oZyMgWIx/ygQhngFbo"]}
        ]]},
    bearer_extract_methods: [:header, :body],
    required_scopes: ["article:write", "comments:moderate"],
    forward_bearer: true,
    cache: {APIacAuthBearerCacheCachex, []}
  plug APIacFilterThrottler,
    key: &APIacFilterThrottler.Functions.throttle_by_ip_path/1,
    scale: 60_000,
    limit: 50,
    exec_cond: &APIac.machine_to_machine?/1,
    error_response_verbosity: :debug}
end

Terminology

APIac uses the OAuth2 terminology:

APIac plugs are designed for API accesses. Therefore, do not use it for end-user authentication as this may lead to security vulnerabilities. For example, the APIacAuthBasic authenticator does not handle weak user passwords and using it for browser-based authentication by end-user will result in security flaws.

Authenticators

The following table summarizes the information of the APIac authenticators:

Authenticator Machine-to-machine Accesses of subjects (real persons)
APIacAuthBasic
APIacAuthBearer OAuth2 client credentials flow OAuth2 authorization code, implicit and password flows<br/>OpenID Connect flows
APIacAuthMTLS
APIacAuthClientJWT
APIacAuthClientSecretPost

Machine-to-machine (also known as server-to-server or s2s) refers to access when only machines are involved, without subject.