mailio_credentials behaviour (mailio v0.2.0)

Credential provider behaviour and facade.

Account documents never store secrets; they reference a provider as {Module, ProviderArgs}. The provider resolves credentials on demand, which keeps OAuth2 access-token refresh and external secret stores out of the persistence layer.

A credential is one of:

  • {password, User, Pass} - plain SASL PLAIN/LOGIN
  • {oauth2, User, AccessToken} - XOAUTH2 bearer token (OAUTHBEARER is not implemented)
  • {token_fun, Fun} - lazy/refreshable; Fun/0 yields one of the above
  • {ref, Name} - a credential kept under a name with register_ref/2; the name is what a supervised connection is given, so that no secret is in its start arguments
  • {provider, {Module, Args}, AccountId, Service} - the secret is fetched from Module each time it is needed. Args can hold secrets (the static provider's do), so a supervised connection is not given this form: it is given a {ref, Name} that stands for it

Summary

Functions

The credential a provider holds for an account and a service. It is resolved like any other: the provider, and whatever it returns, in one worker under one time limit and one depth limit.

Keep a credential under a name. A supervisor writes the arguments of its children in its reports, so a connection that is supervised is given the name ({ref, Name}) and not the credential, or a provider's arguments, which may hold a secret. The credential is kept in the node, not in any process's state.

Turn a credential into a concrete one. A {password, _, _} or {oauth2, _, _} is returned as it is. Anything else is followed in one worker process that is not the caller: a {token_fun, F} is called, a {provider, ...} is fetched, a {ref, Name} is looked up, and what comes back is followed in turn. The whole chain has one time limit and one depth limit, and when the time is up the worker is killed with whatever it started and linked. Errors: credentials_loop (more than max_depth links, default 8), credentials_timeout (more than timeout ms, default 10000 for the whole chain) and {credentials_failed, Reason}.

Types

cred/0

-type cred() ::
          {ref, term()} |
          {provider, provider(), binary(), service()} |
          {password, binary(), binary()} |
          {oauth2, binary(), binary()} |
          {token_fun, fun(() -> {ok, cred()} | {error, term()})}.

provider/0

-type provider() :: {module(), ProviderArgs :: term()}.

service/0

-type service() :: imap | smtp | jmap.

Callbacks

fetch/3

-callback fetch(AccountId :: binary(), Service :: service(), ProviderArgs :: term()) ->
                   {ok, cred()} | {error, term()}.

invalidate/3

-callback invalidate(AccountId :: binary(), Service :: service(), ProviderArgs :: term()) -> ok.

Functions

fetch(Provider, AccountId, Service)

-spec fetch(provider(), binary(), service()) -> {ok, cred()} | {error, term()}.

The credential a provider holds for an account and a service. It is resolved like any other: the provider, and whatever it returns, in one worker under one time limit and one depth limit.

forget_ref(Name)

-spec forget_ref(term()) -> ok.

invalidate(_, AccountId, Service)

-spec invalidate(provider(), binary(), service()) -> ok.

register_ref(Name, Cred)

-spec register_ref(term(), cred()) -> ok.

Keep a credential under a name. A supervisor writes the arguments of its children in its reports, so a connection that is supervised is given the name ({ref, Name}) and not the credential, or a provider's arguments, which may hold a secret. The credential is kept in the node, not in any process's state.

resolve(Cred)

-spec resolve(cred()) -> {ok, cred()} | {error, term()}.

Turn a credential into a concrete one. A {password, _, _} or {oauth2, _, _} is returned as it is. Anything else is followed in one worker process that is not the caller: a {token_fun, F} is called, a {provider, ...} is fetched, a {ref, Name} is looked up, and what comes back is followed in turn. The whole chain has one time limit and one depth limit, and when the time is up the worker is killed with whatever it started and linked. Errors: credentials_loop (more than max_depth links, default 8), credentials_timeout (more than timeout ms, default 10000 for the whole chain) and {credentials_failed, Reason}.

resolve(Error, Opts)

-spec resolve(cred(), #{timeout => timeout(), max_depth => pos_integer()}) ->
                 {ok, cred()} | {error, term()}.