-module(acumen@validate_challenge). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/acumen/validate_challenge.gleam"). -export([build/3, response/1]). -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( " Trigger validation of an ACME challenge.\n" "\n" " After setting up the challenge response (HTTP file, DNS record, etc.),\n" " use this to tell the ACME server to validate it.\n" "\n" " ## Example\n" "\n" " ```gleam\n" " import acumen\n" " import acumen/challenge\n" " import acumen/validate_challenge\n" "\n" " // After deploying the challenge response...\n" " let assert Ok(#(resp, ctx)) = acumen.execute(\n" " ctx,\n" " build: validate_challenge.build(challenge.url(http_challenge), _, registered_key),\n" " send: httpc.send,\n" " )\n" "\n" " let assert Ok(updated_challenge) = validate_challenge.response(resp)\n" " ```\n" ). -file("src/acumen/validate_challenge.gleam", 34). ?DOC(" Builds the POST request to trigger challenge validation.\n"). -spec build(acumen@url:url(), acumen:context(), acumen:registered_key()) -> {ok, gleam@http@request:request(binary())} | {error, acumen:acme_error()}. build(Url, Context, Key) -> _pipe = gleam@json:object([]), _pipe@1 = gleam@json:to_string(_pipe), _pipe@2 = acumen@internal@jws:sign_with_kid( erlang:element(2, Key), erlang:element(3, Key), _pipe@1, erlang:element(3, Context), Url ), _pipe@3 = gleam@result:map_error( _pipe@2, fun(Field@0) -> {jws_error, Field@0} end ), gleam@result:'try'( _pipe@3, fun(_capture) -> acumen:build_post_request(Url, _capture) end ). -file("src/acumen/validate_challenge.gleam", 65). -spec parse_challenge_response(gleam@http@response:response(binary())) -> {ok, acumen@challenge:challenge()} | {error, acumen:acme_error()}. parse_challenge_response(Resp) -> _pipe = gleam@json:parse( erlang:element(4, Resp), acumen@challenge:decoder() ), gleam@result:map_error( _pipe, fun(Error) -> {json_parse_error, acumen@internal@utils:json_parse_error_message( <<"challenge"/utf8>>, Error )} end ). -file("src/acumen/validate_challenge.gleam", 55). ?DOC( " Parses the challenge response after triggering validation.\n" "\n" " The returned challenge status may still be `Processing`.\n" ). -spec response(gleam@http@response:response(binary())) -> {ok, acumen@challenge:challenge()} | {error, acumen:acme_error()}. response(Resp) -> case erlang:element(2, Resp) of 200 -> parse_challenge_response(Resp); _ -> {error, {invalid_response, acumen@internal@utils:unexpected_status_message( erlang:element(2, Resp) )}} end.