All Downloads are FREE. Search and download functionalities are using the official Maven repository.

erlang-server.auth.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
-module({{packageName}}_auth).

-export([authorize_api_key/5]).

-spec authorize_api_key(
    LogicHandler :: atom(),
    OperationID :: {{packageName}}_api:operation_id(),
    From :: header | qs_val,
    KeyParam :: iodata() | atom(),
    Req ::cowboy_req:req()
)-> {true, Context :: #{binary() => any()}, Req ::cowboy_req:req()} |
    {false, AuthHeader :: binary(), Req ::cowboy_req:req()}.

authorize_api_key(LogicHandler, OperationID, From, KeyParam, Req0) ->
    {ApiKey, Req} = get_api_key(From, KeyParam, Req0),
    case ApiKey of
        undefined ->
            AuthHeader = <<"">>,
            {false, AuthHeader, Req};
        _ ->
            Result = {{packageName}}_logic_handler:authorize_api_key(
                LogicHandler,
                OperationID,
                ApiKey
            ),
            case Result of
              {{#authMethods}}
                {{#isApiKey}}
                {true, Context}  ->
                    {true, Context, Req};
                {{/isApiKey}}
              {{/authMethods}}
                false ->
                    AuthHeader = <<"">>,
                    {false, AuthHeader, Req}
            end
    end.

get_api_key(header, KeyParam, Req) ->
    Headers = cowboy_req:headers(Req),
    {
        maps:get(
            {{packageName}}_utils:to_header(KeyParam),
            Headers,
            undefined
        ),
        Req
    };

get_api_key(qs_val, KeyParam, Req) ->
    QS = cowboy_req:parse_qs(Req),
    { {{packageName}}_utils:get_opt(KeyParam, QS), Req}.




© 2015 - 2024 Weber Informatics LLC | Privacy Policy