src/rally_runtime@session.erl

-module(rally_runtime@session).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/rally_runtime/session.gleam").
-export([generate_id/0, extract_session_id/1, set_cookie_header/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.

-file("src/rally_runtime/session.gleam", 7).
?DOC(" Generate a cryptographically random session ID (128-bit hex).\n").
-spec generate_id() -> binary().
generate_id() ->
    _pipe = crypto:strong_rand_bytes(16),
    _pipe@1 = gleam_stdlib:base16_encode(_pipe),
    string:lowercase(_pipe@1).

-file("src/rally_runtime/session.gleam", 14).
?DOC(" Extract the rally_session cookie value from a cookie header string.\n").
-spec extract_session_id(binary()) -> {ok, binary()} | {error, nil}.
extract_session_id(Cookie_header) ->
    _pipe = Cookie_header,
    _pipe@1 = gleam@string:split(_pipe, <<";"/utf8>>),
    _pipe@2 = gleam@list:map(_pipe@1, fun gleam@string:trim/1),
    gleam@list:find_map(
        _pipe@2,
        fun(Pair) -> case gleam@string:split_once(Pair, <<"="/utf8>>) of
                {ok, {<<"rally_session"/utf8>>, Value}} ->
                    Session_id = gleam@string:trim(Value),
                    case Session_id of
                        <<""/utf8>> ->
                            {error, nil};

                        _ ->
                            {ok, Session_id}
                    end;

                _ ->
                    {error, nil}
            end end
    ).

-file("src/rally_runtime/session.gleam", 35).
-spec set_cookie_header(binary(), boolean()) -> binary().
set_cookie_header(Session_id, Secure) ->
    <<<<<<"rally_session="/utf8, Session_id/binary>>/binary,
            "; Path=/; HttpOnly; SameSite=Lax"/utf8>>/binary,
        (case Secure of
            true ->
                <<"; Secure"/utf8>>;

            false ->
                <<""/utf8>>
        end)/binary>>.