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: 3.0.0-rc1
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, Req0) ->
    {Headers, Req} = cowboy_req:headers(Req0),
    {
        swagger_utils:get_opt(
            {{packageName}}_utils:to_header(KeyParam),
            Headers
        ),
        Req
    };

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







© 2015 - 2024 Weber Informatics LLC | Privacy Policy