barrel_mcp_auth_custom (barrel_mcp v3.0.0)

View Source

Custom authentication provider for barrel_mcp.

Allows using a custom module for authentication without implementing the full barrel_mcp_auth behaviour. The custom module only needs to export two functions:

  • init(Opts) -> {ok, State} - Initialize auth state
  • authenticate(Token, State) -> {ok, AuthInfo, State} | {error, Reason, State}

Usage

   barrel_mcp:start_http(#{
       port => 9090,
       auth => #{
           provider => barrel_mcp_auth_custom,
           provider_opts => #{
               module => my_auth_module
           }
       }
   }).

The custom module:

   -module(my_auth_module).
   -export([init/1, authenticate/2]).
  
   init(_Opts) ->
       {ok, #{}}.
  
   authenticate(Token, State) ->
       case validate_token(Token) of
           {ok, Info} -> {ok, Info, State};
           error -> {error, invalid_token, State}
       end.

The state returned by authenticate/2 is discarded: the provider is called with the state init/1 produced on every request, so anything that has to persist across requests belongs in a process or table of the module's own.

Summary

Functions

Authenticate request by extracting token and calling custom module.

Generate challenge response for failed authentication.

Initialize the custom auth provider. Expects module key in Opts pointing to the custom auth module.

Functions

authenticate(Request, _)

-spec authenticate(map(), map()) -> {ok, map()} | {error, term()}.

Authenticate request by extracting token and calling custom module.

challenge(Reason, State)

-spec challenge(term(), map()) -> {integer(), map(), binary()}.

Generate challenge response for failed authentication.

init(Opts)

-spec init(map()) -> {ok, map()}.

Initialize the custom auth provider. Expects module key in Opts pointing to the custom auth module.