Packages
The shared sans-io core of the gleam-atproto client packages: XRPC error vocabulary and response handling, plus the OAuth effect kernel.
Current section
Files
Jump to
Current section
Files
src/atproto_core@oauth@resource.erl
-module(atproto_core@oauth@resource).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/atproto_core/oauth/resource.gleam").
-export([send/3]).
-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(
" A DPoP-authenticated resource request as an `Effect`: attach\n"
" `Authorization: DPoP <token>` and a proof bound to method, URL, and\n"
" access-token hash (`ath`), with the server-nonce retry.\n"
).
-file("src/atproto_core/oauth/resource.gleam", 53).
-spec send_once(
gleam@http@request:request(bitstring()),
binary(),
binary(),
binary(),
binary(),
gleam@option:option(binary())
) -> atproto_core@oauth@effect:effect({ok,
gleam@http@response:response(bitstring())} |
{error, binary()}).
send_once(Req, Access_token, Method, Target, Ath, Nonce) ->
atproto_core@oauth@effect:'try'(
atproto_core@oauth@effect:proof(Method, Target, Nonce, {some, Ath}),
fun(Proof) ->
_pipe@2 = atproto_core@oauth@effect:fetch_bits(
begin
_pipe = Req,
_pipe@1 = gleam@http@request:set_header(
_pipe,
<<"authorization"/utf8>>,
<<"DPoP "/utf8, Access_token/binary>>
),
gleam@http@request:set_header(
_pipe@1,
<<"dpop"/utf8>>,
Proof
)
end
),
atproto_core@oauth@effect:map_error(
_pipe@2,
fun atproto_core@xrpc:transport_error_to_string/1
)
end
).
-file("src/atproto_core/oauth/resource.gleam", 40).
-spec as_text(gleam@http@response:response(bitstring())) -> gleam@http@response:response(binary()).
as_text(Resp) ->
Json_body = case gleam@http@response:get_header(
Resp,
<<"content-type"/utf8>>
) of
{ok, Content_type} ->
gleam_stdlib:string_starts_with(
Content_type,
<<"application/json"/utf8>>
);
{error, nil} ->
false
end,
gleam@http@response:map(Resp, fun(Body) -> case Json_body of
true ->
_pipe = gleam@bit_array:to_string(Body),
gleam@result:unwrap(_pipe, <<""/utf8>>);
false ->
<<""/utf8>>
end end).
-file("src/atproto_core/oauth/resource.gleam", 76).
?DOC(" The DPoP `htu`: the request URI without query or fragment.\n").
-spec htu(gleam@http@request:request(any())) -> binary().
htu(Req) ->
U = gleam@http@request:to_uri(Req),
gleam@uri:to_string(
{uri,
erlang:element(2, U),
erlang:element(3, U),
erlang:element(4, U),
erlang:element(5, U),
erlang:element(6, U),
none,
none}
).
-file("src/atproto_core/oauth/resource.gleam", 17).
-spec send(gleam@http@request:request(bitstring()), binary(), binary()) -> atproto_core@oauth@effect:effect({ok,
gleam@http@response:response(bitstring())} |
{error, binary()}).
send(Req, Access_token, Ath) ->
Method = begin
_pipe = gleam@http:method_to_string(erlang:element(2, Req)),
string:uppercase(_pipe)
end,
Target = htu(Req),
atproto_core@oauth@effect:'try'(
send_once(Req, Access_token, Method, Target, Ath, none),
fun(First) ->
case atproto_core@oauth@transport:dpop_nonce_challenge(
as_text(First)
) of
{some, Nonce} ->
send_once(
Req,
Access_token,
Method,
Target,
Ath,
{some, Nonce}
);
none ->
atproto_core@oauth@effect:done({ok, First})
end
end
).