barrel_mcp_auth_custom (barrel_mcp v2.0.2)

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, NewState} | {error, Reason, NewState}

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.

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, State)

-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.