Current section
Files
Jump to
Current section
Files
src/flwr_oauth2@bearer_token.erl
-module(flwr_oauth2@bearer_token).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/flwr_oauth2/bearer_token.gleam").
-export([attach_bearer_token_header/2, attach_access_token_to_body/2, attach_access_token_to_query_parameters/2]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(" This module aims to fulfill most of [RFC6750](https://datatracker.ietf.org/doc/html/rfc6750) for attaching an access token to an HTTP request to a protected resource.\n").
-file("src/flwr_oauth2/bearer_token.gleam", 9).
?DOC(
" Attach the access token to the `Authorization` header as Bearer token.\n"
" See [RFC6750](https://datatracker.ietf.org/doc/html/rfc6750#section-2.1).\n"
).
-spec attach_bearer_token_header(
gleam@http@request:request(HLQ),
flwr_oauth2:access_token_response()
) -> gleam@http@request:request(HLQ).
attach_bearer_token_header(Req, Token) ->
_pipe = Req,
gleam@http@request:set_header(
_pipe,
<<"authorization"/utf8>>,
<<"Bearer "/utf8, (erlang:element(2, Token))/binary>>
).
-file("src/flwr_oauth2/bearer_token.gleam", 47).
-spec access_token_tuple(flwr_oauth2:access_token_response()) -> {binary(),
binary()}.
access_token_tuple(Token) ->
{<<"access_token"/utf8>>, erlang:element(2, Token)}.
-file("src/flwr_oauth2/bearer_token.gleam", 24).
?DOC(
" Attach the access token to the body as access token.\n"
" This function adds the access token to the body of provided request.\n"
" It does not set the content type to `application/x-www-form-urlencoded`, but in order to send the body with the access token to a server it has to be set and the body needs to be url encoded.\n"
" See [RFC6750](https://datatracker.ietf.org/doc/html/rfc6750#section-2.2).\n"
).
-spec attach_access_token_to_body(
gleam@http@request:request(list({binary(), binary()})),
flwr_oauth2:access_token_response()
) -> gleam@http@request:request(list({binary(), binary()})).
attach_access_token_to_body(Req, Token) ->
_pipe = erlang:element(4, Req),
_pipe@1 = gleam@list:prepend(_pipe, access_token_tuple(Token)),
gleam@http@request:set_body(Req, _pipe@1).
-file("src/flwr_oauth2/bearer_token.gleam", 35).
?DOC(
" Attach the access token to the query parameters as access token.\n"
" See [RFC6750](https://datatracker.ietf.org/doc/html/rfc6750#section-2.3).\n"
).
-spec attach_access_token_to_query_parameters(
gleam@http@request:request(HLT),
flwr_oauth2:access_token_response()
) -> gleam@http@request:request(HLT).
attach_access_token_to_query_parameters(Req, Token) ->
_pipe = case gleam@http@request:get_query(Req) of
{error, _} ->
[];
{ok, Query_params} ->
Query_params
end,
_pipe@1 = gleam@list:prepend(_pipe, access_token_tuple(Token)),
gleam@http@request:set_query(Req, _pipe@1).