diff --git a/lib/atex/crypto.ex b/lib/atex/crypto.ex index e497376..0fe0412 100644 --- a/lib/atex/crypto.ex +++ b/lib/atex/crypto.ex @@ -31,7 +31,7 @@ defmodule Atex.Crypto do @p256_a @p256_p - 3 @p256_b 0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B @p256_n 0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551 - @p256_oid {1, 2, 840, 10045, 3, 1, 7} + @p256_oid {1, 2, 840, 10_045, 3, 1, 7} # secp256k1 @k256_p 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F @@ -398,18 +398,20 @@ defmodule Atex.Crypto do # If s > n/2, replace s with n - s and re-encode the DER sequence. @spec normalize_low_s(binary(), pos_integer()) :: binary() defp normalize_low_s(der_sig, curve_order) do - with {:ok, r_bin, s_bin} <- parse_der_ecdsa(der_sig) do - s = :binary.decode_unsigned(s_bin) - - if s <= div(curve_order, 2) do + case parse_der_ecdsa(der_sig) do + {:ok, r_bin, s_bin} -> + s = :binary.decode_unsigned(s_bin) + + if s <= div(curve_order, 2) do + der_sig + else + new_s = curve_order - s + new_s_bin = :binary.encode_unsigned(new_s) + encode_der_ecdsa(r_bin, new_s_bin) + end + + _ -> der_sig - else - new_s = curve_order - s - new_s_bin = :binary.encode_unsigned(new_s) - encode_der_ecdsa(r_bin, new_s_bin) - end - else - _ -> der_sig end end diff --git a/lib/atex/identity_resolver/cache.ex b/lib/atex/identity_resolver/cache.ex index 126e769..217aaa4 100644 --- a/lib/atex/identity_resolver/cache.ex +++ b/lib/atex/identity_resolver/cache.ex @@ -1,10 +1,4 @@ defmodule Atex.IdentityResolver.Cache do - # TODO: need the following: - # did -> handle mapping - # handle -> did mapping - # did -> document mapping? - # User should be able to call a single function to fetch all info for either did and handle, including the link between them. - # Need some sort of TTL so that we can refresh as necessary alias Atex.IdentityResolver.Identity @cache Application.compile_env(:atex, :identity_cache, Atex.IdentityResolver.Cache.ETS) diff --git a/lib/atex/lexicon.ex b/lib/atex/lexicon.ex index d6de973..72d98d5 100644 --- a/lib/atex/lexicon.ex +++ b/lib/atex/lexicon.ex @@ -171,7 +171,6 @@ defmodule Atex.Lexicon do def_to_schema(nsid, def_name, record) end - # TODO: add struct to types defp def_to_schema( nsid, def_name, @@ -290,7 +289,6 @@ defmodule Atex.Lexicon do [{atomise(def_name), {:%{}, [], quoted_schemas}, {:%{}, [], quoted_types}, quoted_struct}] end - # TODO: validating errors? defp def_to_schema(nsid, _def_name, %{type: "query"} = def) do params = if def[:parameters] do diff --git a/lib/atex/lexicon/validators.ex b/lib/atex/lexicon/validators.ex index 1812b75..a4ab0ff 100644 --- a/lib/atex/lexicon/validators.ex +++ b/lib/atex/lexicon/validators.ex @@ -99,8 +99,7 @@ defmodule Atex.Lexicon.Validators do @spec strings_to_re(list(String.t())) :: Regex.t() defp strings_to_re(strings) do strings - |> Enum.map(&String.replace(&1, "*", ".+")) - |> Enum.join("|") + |> Enum.map_join("|", &String.replace(&1, "*", ".+")) |> then(&~r/^(#{&1})$/) end end diff --git a/lib/atex/oauth.ex b/lib/atex/oauth.ex index e2e0376..df65807 100644 --- a/lib/atex/oauth.ex +++ b/lib/atex/oauth.ex @@ -366,13 +366,13 @@ defmodule Atex.OAuth do } body = - if !Config.is_localhost(), - do: + if Config.is_localhost(), + do: body, + else: Map.merge(body, %{ client_assertion_type: "urn:ietf:params:oauth:client-assertion-type:jwt-bearer", client_assertion: client_assertion - }), - else: body + }) Req.new(method: :post, url: authz_metadata.token_endpoint, form: body) |> send_oauth_dpop_request(dpop_key) @@ -620,7 +620,6 @@ defmodule Atex.OAuth do {:ok, body, dpop_nonce} {:ok, %{body: %{"error" => error, "error_description" => error_description}}} -> - IO.inspect(request) {:error, {:oauth_error, error, error_description}, dpop_nonce} {:ok, _} -> @@ -631,8 +630,6 @@ defmodule Atex.OAuth do end true -> - IO.inspect(request) - {:error, {:oauth_error, resp.body["error"], resp.body["error_description"]}, dpop_nonce} end diff --git a/lib/atex/oauth/permission.ex b/lib/atex/oauth/permission.ex index 21fe4e2..2c674d2 100644 --- a/lib/atex/oauth/permission.ex +++ b/lib/atex/oauth/permission.ex @@ -268,8 +268,7 @@ defmodule Atex.OAuth.Permission do defp stringify_parameters(params) do params - |> Enum.map(fn {key, value} -> "#{key}=#{encode_param_value(value)}" end) - |> Enum.join("&") + |> Enum.map_join("&", fn {key, value} -> "#{key}=#{encode_param_value(value)}" end) |> then(&"?#{&1}") end @@ -352,7 +351,7 @@ defmodule Atex.OAuth.Permission do struct = %__MODULE__{ resource: "account", positional: Atom.to_string(attr), - parameters: if(!is_nil(action), do: [{"action", Atom.to_string(action)}], else: []) + parameters: if(action != nil, do: [{"action", Atom.to_string(action)}], else: []) } if as_string, do: to_string(struct), else: struct @@ -792,7 +791,7 @@ defmodule Atex.OAuth.Permission do aud = Keyword.get(opts, :aud) as_string = Keyword.get(opts, :as_string) - parameters = if !is_nil(aud), do: [{"aud", aud}], else: [] + parameters = if aud != nil, do: [{"aud", aud}], else: [] struct = %__MODULE__{ resource: "include", diff --git a/lib/atex/oauth/plug.ex b/lib/atex/oauth/plug.ex index 3b54147..cc09ec4 100644 --- a/lib/atex/oauth/plug.ex +++ b/lib/atex/oauth/plug.ex @@ -268,9 +268,7 @@ defmodule Atex.OAuth.Plug do message: "OAuth issuer does not match PDS' authorization server", reason: :issuer_mismatch - err -> - IO.inspect(err) - + _err -> raise Atex.OAuth.Error, message: "Failed to validate authorization code or token", reason: :token_validation_failed diff --git a/lib/atex/util.ex b/lib/atex/util.ex index efb43a4..60d767c 100644 --- a/lib/atex/util.ex +++ b/lib/atex/util.ex @@ -79,8 +79,7 @@ defmodule Atex.Util do defp format_ipv6({a, b, c, d, e, f, g, h}) when is_ipv6(a, b, c, d, e, f, g, h) do [a, b, c, d, e, f, g, h] - |> Enum.map(&Integer.to_string(&1, 16)) - |> Enum.join(":") + |> Enum.map_join(":", &Integer.to_string(&1, 16)) |> String.downcase() end end diff --git a/lib/atex/xrpc.ex b/lib/atex/xrpc.ex index ca589b2..26b5d66 100644 --- a/lib/atex/xrpc.ex +++ b/lib/atex/xrpc.ex @@ -174,7 +174,9 @@ defmodule Atex.XRPC do if Code.ensure_loaded?(module) and function_exported?(module, :content_type, 0) do headers = Keyword.get(opts, :headers, []) - has_content_type? = Enum.any?(headers, fn {k, _} -> String.downcase(k) == "content-type" end) + + has_content_type? = + Enum.any?(headers, fn {k, _} -> String.downcase(k) == "content-type" end) unless has_content_type? do raise """ diff --git a/lib/mix/tasks/atex.lexicons.ex b/lib/mix/tasks/atex.lexicons.ex index 9ac45fe..967b426 100644 --- a/lib/mix/tasks/atex.lexicons.ex +++ b/lib/mix/tasks/atex.lexicons.ex @@ -51,7 +51,7 @@ defmodule Mix.Tasks.Atex.Lexicons do output = Keyword.get(options, :output, "lib/atproto") paths = Enum.flat_map(globs, &Path.wildcard/1) - if length(paths) == 0 do + if paths == [] do Mix.shell().error("No valid search paths have been provided, aborting.") else Mix.shell().info("Generating modules for lexicons into #{output}") @@ -63,21 +63,18 @@ defmodule Mix.Tasks.Atex.Lexicons do end end - # TODO: validate schema? defp generate(input, output) do lexicon = input |> File.read!() |> JSON.decode!() - - if not is_binary(lexicon["id"]) do - raise ArgumentError, message: "Malformed lexicon: does not have an `id` field." - end + |> Recase.Enumerable.atomize_keys() + |> Atex.Lexicon.Schema.lexicon!() code = lexicon |> template() |> Code.format_string!() |> Enum.join("") file_path = - lexicon["id"] + lexicon.id |> String.split(".") |> Enum.join("/") |> then(&(&1 <> ".ex")) diff --git a/priv/templates/lexicon.eex b/priv/templates/lexicon.eex index 21ed8d9..3bff551 100644 --- a/priv/templates/lexicon.eex +++ b/priv/templates/lexicon.eex @@ -1,4 +1,4 @@ -defmodule <%= Atex.NSID.to_atom(lexicon["id"], false) %> do +defmodule <%= lexicon.id |> Atex.NSID.new!() |> Atex.NSID.to_atom(false) %> do @moduledoc false use Atex.Lexicon diff --git a/test/atex/repo/fixtures_test.exs b/test/atex/repo/fixtures_test.exs index d1f1b10..a05956e 100644 --- a/test/atex/repo/fixtures_test.exs +++ b/test/atex/repo/fixtures_test.exs @@ -103,7 +103,7 @@ defmodule Atex.Repo.FixturesTest do test "list_record_keys returns rkeys for a collection", %{repo: repo} do {:ok, keys} = Repo.list_record_keys(repo, "app.bsky.feed.post") - assert length(keys) > 0 + assert keys != [] assert Enum.all?(keys, &is_binary/1) assert keys == Enum.sort(keys) end