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_string_to_header/2, attach_bearer_token_header/2, attach_access_token_string_to_body/2, attach_access_token_to_body/2, attach_access_token_string_to_query_parameters/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", 18).
?DOC(" See [attach_bearer_token_header](#attach_bearer_token_header)\n").
-spec attach_bearer_token_string_to_header(
gleam@http@request:request(FZF),
binary()
) -> gleam@http@request:request(FZF).
attach_bearer_token_string_to_header(Req, Access_token) ->
flwr_oauth2@http_headers:set_bearer(Req, Access_token).
-file("src/flwr_oauth2/bearer_token.gleam", 10).
?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(FZC),
flwr_oauth2:access_token_response()
) -> gleam@http@request:request(FZC).
attach_bearer_token_header(Req, Token) ->
attach_bearer_token_string_to_header(Req, erlang:element(2, Token)).
-file("src/flwr_oauth2/bearer_token.gleam", 68).
-spec access_token_tuple(binary()) -> {binary(), binary()}.
access_token_tuple(Token) ->
{<<"access_token"/utf8>>, Token}.
-file("src/flwr_oauth2/bearer_token.gleam", 37).
?DOC(" See [attach_access_token_to_body](#attach_access_token_to_body)\n").
-spec attach_access_token_string_to_body(
gleam@http@request:request(list({binary(), binary()})),
binary()
) -> gleam@http@request:request(list({binary(), binary()})).
attach_access_token_string_to_body(Req, Access_token) ->
_pipe = erlang:element(4, Req),
_pipe@1 = gleam@list:prepend(_pipe, access_token_tuple(Access_token)),
gleam@http@request:set_body(Req, _pipe@1).
-file("src/flwr_oauth2/bearer_token.gleam", 29).
?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) ->
attach_access_token_string_to_body(Req, erlang:element(2, Token)).
-file("src/flwr_oauth2/bearer_token.gleam", 56).
?DOC(" See [attach_access_token_to_query_parameters](#attach_access_token_to_query_parameters)\n").
-spec attach_access_token_string_to_query_parameters(
gleam@http@request:request(FZL),
binary()
) -> gleam@http@request:request(FZL).
attach_access_token_string_to_query_parameters(Req, Access_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(Access_token)),
gleam@http@request:set_query(Req, _pipe@1).
-file("src/flwr_oauth2/bearer_token.gleam", 48).
?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(FZI),
flwr_oauth2:access_token_response()
) -> gleam@http@request:request(FZI).
attach_access_token_to_query_parameters(Req, Token) ->
attach_access_token_string_to_query_parameters(
Req,
erlang:element(2, Token)
).