From 9c2ccd2bcfc94093fa5b092765f832bf7dc67ce4 Mon Sep 17 00:00:00 2001 From: Dan Schultzer <1254724+danschultzer@users.noreply.github.com> Date: Mon, 23 Dec 2024 14:05:11 -0800 Subject: [PATCH] Update to Elixir 1.18 --- .github/workflows/ci.yml | 8 +- .github/workflows/publish.yml | 4 +- CHANGELOG.md | 4 + lib/assent/config.ex | 7 +- lib/assent/jwt_adapter/assent_jwt.ex | 8 +- mix.exs | 3 +- mix.lock | 48 +++++------ test/assent/config_test.exs | 2 +- test/assent/http_adapter/finch_test.exs | 4 +- test/assent/http_adapter/httpc_test.exs | 2 +- test/assent/http_adapter/mint_test.exs | 2 +- test/assent/http_adapter/req_test.exs | 46 +++++++--- test/assent/jwt_adapter/assent_jwt_test.exs | 94 +++++++++++++-------- test/assent/jwt_adapter/jose_test.exs | 19 +++-- test/assent/strategies/apple_test.exs | 8 +- test/assent/strategies/oauth2_test.exs | 18 +++- test/assent/strategy_test.exs | 24 ++++-- test/assent_test.exs | 2 +- test/support/strategies/oauth2_test_case.ex | 6 +- test/support/strategies/oauth_test_case.ex | 6 +- test/support/strategies/oidc_test_case.ex | 8 +- test/support/test_case.ex | 12 +++ 22 files changed, 218 insertions(+), 117 deletions(-) create mode 100644 test/support/test_case.ex diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 80b4e0f..3ec9705 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,8 +16,8 @@ jobs: - uses: actions/checkout@v3 - uses: erlef/setup-beam@v1 with: - otp-version: 26.0 - elixir-version: 1.16 + otp-version: 27.0 + elixir-version: 1.18 - run: mix deps.get - run: mix compile --warnings-as-errors - run: mix credo --strict --ignore design.tagtodo @@ -27,8 +27,8 @@ jobs: strategy: matrix: version: - - otp: 26.0 - elixir: 1.16 + - otp: 27.0 + elixir: 1.18 os: ubuntu-latest - otp: 22.0 elixir: 1.13 diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index bdc75cb..79803ef 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -19,7 +19,7 @@ jobs: - uses: actions/checkout@v3 - uses: erlef/setup-beam@v1 with: - otp-version: 26.0 - elixir-version: 1.16 + otp-version: 27.0 + elixir-version: 1.18 - run: mix deps.get - run: mix hex.publish --yes diff --git a/CHANGELOG.md b/CHANGELOG.md index 7920a6b..f98df83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## v0.2.11 (TBA) + +* Default to using `JSON` instead of `Jason` for JSON parsing on Elixir 1.18 + ## v0.2.10 (2024-04-11) Requires Elixir 1.13+ diff --git a/lib/assent/config.ex b/lib/assent/config.ex index a01e746..8d85417 100644 --- a/lib/assent/config.ex +++ b/lib/assent/config.ex @@ -32,17 +32,20 @@ defmodule Assent.Config do defdelegate merge(config_a, config_b), to: Keyword + @default_json_library (Code.ensure_loaded?(JSON) && JSON) || Jason + @doc """ Fetches the JSON library in config. If not found in provided config, this will attempt to load the JSON library - from global application environment for `:assent`. Defaults to `Jason`. + from global application environment for `:assent`. Defaults to + `#{@default_json_library}`. """ @spec json_library(t()) :: module() def json_library(config) do case get(config, :json_library, nil) do nil -> - Application.get_env(:assent, :json_library, Jason) + Application.get_env(:assent, :json_library, @default_json_library) json_library -> json_library diff --git a/lib/assent/jwt_adapter/assent_jwt.ex b/lib/assent/jwt_adapter/assent_jwt.ex index 05fb2bd..0e5459b 100644 --- a/lib/assent/jwt_adapter/assent_jwt.ex +++ b/lib/assent/jwt_adapter/assent_jwt.ex @@ -42,11 +42,17 @@ defmodule Assent.JWTAdapter.AssentJWT do defp encode_json_base64(map, opts) do with {:ok, json_library} <- Config.fetch(opts, :json_library), - {:ok, json} <- json_library.encode(map) do + {:ok, json} <- json_encode(json_library, map) do {:ok, Base.url_encode64(json, padding: false)} end end + defp json_encode(json_library, map) do + {:ok, json_library.encode!(map)} + rescue + error -> {:error, error} + end + defp encode_claims(claims, opts) do case encode_json_base64(claims, opts) do {:ok, encoded_claims} -> diff --git a/mix.exs b/mix.exs index 459307a..cf1ca00 100644 --- a/mix.exs +++ b/mix.exs @@ -36,13 +36,12 @@ defmodule Assent.MixProject do [ # JWT libraries {:jose, "~> 1.8", optional: true}, - # HTTP clients (Jason is required for Req) + # HTTP clients {:certifi, ">= 0.0.0", optional: true}, {:ssl_verify_fun, ">= 0.0.0", optional: true}, {:finch, "~> 0.15", optional: true}, {:mint, "~> 1.0", optional: true}, {:req, "~> 0.4", optional: true}, - {:jason, "~> 1.0", optional: true}, # Docs and tests {:ex_doc, ">= 0.0.0", only: :dev, runtime: false}, {:credo, "~> 1.1", only: [:dev, :test]}, diff --git a/mix.lock b/mix.lock index b40271b..3084064 100644 --- a/mix.lock +++ b/mix.lock @@ -1,32 +1,30 @@ %{ - "bandit": {:hex, :bandit, "1.2.3", "a98d664a96fec23b68e776062296d76a94b4459795b38209f4ae89cb4225709c", [:mix], [{:hpax, "~> 0.1.1", [hex: :hpax, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:thousand_island, "~> 1.0", [hex: :thousand_island, repo: "hexpm", optional: false]}, {:websock, "~> 0.5", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "3e29150245a9b5f56944434e5240966e75c917dad248f689ab589b32187a81af"}, + "bandit": {:hex, :bandit, "1.6.1", "9e01b93d72ddc21d8c576a704949e86ee6cde7d11270a1d3073787876527a48f", [:mix], [{:hpax, "~> 1.0", [hex: :hpax, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:thousand_island, "~> 1.0", [hex: :thousand_island, repo: "hexpm", optional: false]}, {:websock, "~> 0.5", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "5a904bf010ea24b67979835e0507688e31ac873d4ffc8ed0e5413e8d77455031"}, "bunt": {:hex, :bunt, "1.0.0", "081c2c665f086849e6d57900292b3a161727ab40431219529f13c4ddcf3e7a44", [:mix], [], "hexpm", "dc5f86aa08a5f6fa6b8096f0735c4e76d54ae5c9fa2c143e5a1fc7c1cd9bb6b5"}, - "castore": {:hex, :castore, "1.0.5", "9eeebb394cc9a0f3ae56b813459f990abb0a3dedee1be6b27fdb50301930502f", [:mix], [], "hexpm", "8d7c597c3e4a64c395980882d4bca3cebb8d74197c590dc272cfd3b6a6310578"}, "certifi": {:hex, :certifi, "2.13.0", "e52be248590050b2dd33b0bb274b56678f9068e67805dca8aa8b1ccdb016bbf6", [:rebar3], [], "hexpm", "8f3d9533a0f06070afdfd5d596b32e21c6580667a492891851b0e2737bc507a1"}, - "credo": {:hex, :credo, "1.7.5", "643213503b1c766ec0496d828c90c424471ea54da77c8a168c725686377b9545", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "f799e9b5cd1891577d8c773d245668aa74a2fcd15eb277f51a0131690ebfb3fd"}, - "earmark_parser": {:hex, :earmark_parser, "1.4.39", "424642f8335b05bb9eb611aa1564c148a8ee35c9c8a8bba6e129d51a3e3c6769", [:mix], [], "hexpm", "06553a88d1f1846da9ef066b87b57c6f605552cfbe40d20bd8d59cc6bde41944"}, - "ex_doc": {:hex, :ex_doc, "0.31.1", "8a2355ac42b1cc7b2379da9e40243f2670143721dd50748bf6c3b1184dae2089", [:mix], [{:earmark_parser, "~> 1.4.39", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.1", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "3178c3a407c557d8343479e1ff117a96fd31bafe52a039079593fb0524ef61b0"}, - "file_system": {:hex, :file_system, "1.0.0", "b689cc7dcee665f774de94b5a832e578bd7963c8e637ef940cd44327db7de2cd", [:mix], [], "hexpm", "6752092d66aec5a10e662aefeed8ddb9531d79db0bc145bb8c40325ca1d8536d"}, - "finch": {:hex, :finch, "0.18.0", "944ac7d34d0bd2ac8998f79f7a811b21d87d911e77a786bc5810adb75632ada4", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: false]}, {:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2.6 or ~> 1.0", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "69f5045b042e531e53edc2574f15e25e735b522c37e2ddb766e15b979e03aa65"}, - "hpax": {:hex, :hpax, "0.1.2", "09a75600d9d8bbd064cdd741f21fc06fc1f4cf3d0fcc335e5aa19be1a7235c84", [:mix], [], "hexpm", "2c87843d5a23f5f16748ebe77969880e29809580efdaccd615cd3bed628a8c13"}, - "jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"}, - "jose": {:hex, :jose, "1.11.6", "613fda82552128aa6fb804682e3a616f4bc15565a048dabd05b1ebd5827ed965", [:mix, :rebar3], [], "hexpm", "6275cb75504f9c1e60eeacb771adfeee4905a9e182103aa59b53fed651ff9738"}, - "makeup": {:hex, :makeup, "1.1.1", "fa0bc768698053b2b3869fa8a62616501ff9d11a562f3ce39580d60860c3a55e", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "5dc62fbdd0de44de194898b6710692490be74baa02d9d108bc29f007783b0b48"}, - "makeup_elixir": {:hex, :makeup_elixir, "0.16.1", "cc9e3ca312f1cfeccc572b37a09980287e243648108384b97ff2b76e505c3555", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "e127a341ad1b209bd80f7bd1620a15693a9908ed780c3b763bccf7d200c767c6"}, - "makeup_erlang": {:hex, :makeup_erlang, "0.1.4", "29563475afa9b8a2add1b7a9c8fb68d06ca7737648f28398e04461f008b69521", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "f4ed47ecda66de70dd817698a703f8816daa91272e7e45812469498614ae8b29"}, - "mime": {:hex, :mime, "2.0.5", "dc34c8efd439abe6ae0343edbb8556f4d63f178594894720607772a041b04b02", [:mix], [], "hexpm", "da0d64a365c45bc9935cc5c8a7fc5e49a0e0f9932a761c55d6c52b142780a05c"}, - "mint": {:hex, :mint, "1.5.2", "4805e059f96028948870d23d7783613b7e6b0e2fb4e98d720383852a760067fd", [:mix], [{:castore, "~> 0.1.0 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "d77d9e9ce4eb35941907f1d3df38d8f750c357865353e21d335bdcdf6d892a02"}, - "nimble_options": {:hex, :nimble_options, "1.1.0", "3b31a57ede9cb1502071fade751ab0c7b8dbe75a9a4c2b5bbb0943a690b63172", [:mix], [], "hexpm", "8bbbb3941af3ca9acc7835f5655ea062111c9c27bcac53e004460dfd19008a99"}, - "nimble_ownership": {:hex, :nimble_ownership, "0.2.1", "3e44c72ebe8dd213db4e13aff4090aaa331d158e72ce1891d02e0ffb05a1eb2d", [:mix], [], "hexpm", "bf38d2ef4fb990521a4ecf112843063c1f58a5c602484af4c7977324042badee"}, + "credo": {:hex, :credo, "1.7.10", "6e64fe59be8da5e30a1b96273b247b5cf1cc9e336b5fd66302a64b25749ad44d", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "71fbc9a6b8be21d993deca85bf151df023a3097b01e09a2809d460348561d8cd"}, + "earmark_parser": {:hex, :earmark_parser, "1.4.42", "f23d856f41919f17cd06a493923a722d87a2d684f143a1e663c04a2b93100682", [:mix], [], "hexpm", "6915b6ca369b5f7346636a2f41c6a6d78b5af419d61a611079189233358b8b8b"}, + "ex_doc": {:hex, :ex_doc, "0.35.1", "de804c590d3df2d9d5b8aec77d758b00c814b356119b3d4455e4b8a8687aecaf", [:mix], [{:earmark_parser, "~> 1.4.39", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "2121c6402c8d44b05622677b761371a759143b958c6c19f6558ff64d0aed40df"}, + "file_system": {:hex, :file_system, "1.0.1", "79e8ceaddb0416f8b8cd02a0127bdbababe7bf4a23d2a395b983c1f8b3f73edd", [:mix], [], "hexpm", "4414d1f38863ddf9120720cd976fce5bdde8e91d8283353f0e31850fa89feb9e"}, + "finch": {:hex, :finch, "0.19.0", "c644641491ea854fc5c1bbaef36bfc764e3f08e7185e1f084e35e0672241b76d", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.6.2 or ~> 1.7", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 1.1", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "fc5324ce209125d1e2fa0fcd2634601c52a787aff1cd33ee833664a5af4ea2b6"}, + "hpax": {:hex, :hpax, "1.0.2", "762df951b0c399ff67cc57c3995ec3cf46d696e41f0bba17da0518d94acd4aac", [:mix], [], "hexpm", "2f09b4c1074e0abd846747329eaa26d535be0eb3d189fa69d812bfb8bfefd32f"}, + "jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"}, + "jose": {:hex, :jose, "1.11.10", "a903f5227417bd2a08c8a00a0cbcc458118be84480955e8d251297a425723f83", [:mix, :rebar3], [], "hexpm", "0d6cd36ff8ba174db29148fc112b5842186b68a90ce9fc2b3ec3afe76593e614"}, + "makeup": {:hex, :makeup, "1.2.1", "e90ac1c65589ef354378def3ba19d401e739ee7ee06fb47f94c687016e3713d1", [:mix], [{:nimble_parsec, "~> 1.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "d36484867b0bae0fea568d10131197a4c2e47056a6fbe84922bf6ba71c8d17ce"}, + "makeup_elixir": {:hex, :makeup_elixir, "1.0.1", "e928a4f984e795e41e3abd27bfc09f51db16ab8ba1aebdba2b3a575437efafc2", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "7284900d412a3e5cfd97fdaed4f5ed389b8f2b4cb49efc0eb3bd10e2febf9507"}, + "makeup_erlang": {:hex, :makeup_erlang, "1.0.1", "c7f58c120b2b5aa5fd80d540a89fdf866ed42f1f3994e4fe189abebeab610839", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "8a89a1eeccc2d798d6ea15496a6e4870b75e014d1af514b1b71fa33134f57814"}, + "mime": {:hex, :mime, "2.0.6", "8f18486773d9b15f95f4f4f1e39b710045fa1de891fada4516559967276e4dc2", [:mix], [], "hexpm", "c9945363a6b26d747389aac3643f8e0e09d30499a138ad64fe8fd1d13d9b153e"}, + "mint": {:hex, :mint, "1.6.2", "af6d97a4051eee4f05b5500671d47c3a67dac7386045d87a904126fd4bbcea2e", [:mix], [{:castore, "~> 0.1.0 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1 or ~> 0.2.0 or ~> 1.0", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "5ee441dffc1892f1ae59127f74afe8fd82fda6587794278d924e4d90ea3d63f9"}, + "nimble_options": {:hex, :nimble_options, "1.1.1", "e3a492d54d85fc3fd7c5baf411d9d2852922f66e69476317787a7b2bb000a61b", [:mix], [], "hexpm", "821b2470ca9442c4b6984882fe9bb0389371b8ddec4d45a9504f00a66f650b44"}, "nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"}, - "nimble_pool": {:hex, :nimble_pool, "1.0.0", "5eb82705d138f4dd4423f69ceb19ac667b3b492ae570c9f5c900bb3d2f50a847", [:mix], [], "hexpm", "80be3b882d2d351882256087078e1b1952a28bf98d0a287be87e4a24a710b67a"}, - "plug": {:hex, :plug, "1.15.3", "712976f504418f6dff0a3e554c40d705a9bcf89a7ccef92fc6a5ef8f16a30a97", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "cc4365a3c010a56af402e0809208873d113e9c38c401cabd88027ef4f5c01fd2"}, - "plug_crypto": {:hex, :plug_crypto, "2.0.0", "77515cc10af06645abbfb5e6ad7a3e9714f805ae118fa1a70205f80d2d70fe73", [:mix], [], "hexpm", "53695bae57cc4e54566d993eb01074e4d894b65a3766f1c43e2c61a1b0f45ea9"}, - "req": {:hex, :req, "0.4.11", "cb19f87d5251e7de30cfc67d1899696b290711092207c6b2e8fc2294f237fcdc", [:mix], [{:aws_signature, "~> 0.3.2", [hex: :aws_signature, repo: "hexpm", optional: true]}, {:brotli, "~> 0.3.1", [hex: :brotli, repo: "hexpm", optional: true]}, {:ezstd, "~> 1.0", [hex: :ezstd, repo: "hexpm", optional: true]}, {:finch, "~> 0.17", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mime, "~> 1.6 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:nimble_csv, "~> 1.0", [hex: :nimble_csv, repo: "hexpm", optional: true]}, {:nimble_ownership, "~> 0.2.0", [hex: :nimble_ownership, repo: "hexpm", optional: false]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "bbf4f2393c649fa4146a3b8470e2a7e8c9b23e4100a16c75f5e7d1d3d33144f3"}, + "nimble_pool": {:hex, :nimble_pool, "1.1.0", "bf9c29fbdcba3564a8b800d1eeb5a3c58f36e1e11d7b7fb2e084a643f645f06b", [:mix], [], "hexpm", "af2e4e6b34197db81f7aad230c1118eac993acc0dae6bc83bac0126d4ae0813a"}, + "plug": {:hex, :plug, "1.16.1", "40c74619c12f82736d2214557dedec2e9762029b2438d6d175c5074c933edc9d", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "a13ff6b9006b03d7e33874945b2755253841b238c34071ed85b0e86057f8cddc"}, + "plug_crypto": {:hex, :plug_crypto, "2.1.0", "f44309c2b06d249c27c8d3f65cfe08158ade08418cf540fd4f72d4d6863abb7b", [:mix], [], "hexpm", "131216a4b030b8f8ce0f26038bc4421ae60e4bb95c5cf5395e1421437824c4fa"}, + "req": {:hex, :req, "0.5.8", "50d8d65279d6e343a5e46980ac2a70e97136182950833a1968b371e753f6a662", [:mix], [{:brotli, "~> 0.3.1", [hex: :brotli, repo: "hexpm", optional: true]}, {:ezstd, "~> 1.0", [hex: :ezstd, repo: "hexpm", optional: true]}, {:finch, "~> 0.17", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mime, "~> 2.0.6 or ~> 2.1", [hex: :mime, repo: "hexpm", optional: false]}, {:nimble_csv, "~> 1.0", [hex: :nimble_csv, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "d7fc5898a566477e174f26887821a3c5082b243885520ee4b45555f5d53f40ef"}, "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"}, - "telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"}, - "test_server": {:hex, :test_server, "0.1.14", "c3cdf0b6c1be691ae50a14ee3ea4bd026250c321c2012f5dfaed336d8702a562", [:mix], [{:bandit, ">= 0.7.6", [hex: :bandit, repo: "hexpm", optional: true]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, ">= 2.0.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:x509, "~> 0.6", [hex: :x509, repo: "hexpm", optional: false]}], "hexpm", "5af2f98a22765ff3cc66f09d20c88b754586cf0c45cf0d2e0068e2a47f5041a7"}, - "thousand_island": {:hex, :thousand_island, "1.3.5", "6022b6338f1635b3d32406ff98d68b843ba73b3aa95cfc27154223244f3a6ca5", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "2be6954916fdfe4756af3239fb6b6d75d0b8063b5df03ba76fd8a4c87849e180"}, + "telemetry": {:hex, :telemetry, "1.3.0", "fedebbae410d715cf8e7062c96a1ef32ec22e764197f70cda73d82778d61e7a2", [:rebar3], [], "hexpm", "7015fc8919dbe63764f4b4b87a95b7c0996bd539e0d499be6ec9d7f3875b79e6"}, + "test_server": {:hex, :test_server, "0.1.17", "6b7fc2b449e9d2e65a4a25762c46e14e1927a6baa7ea2f2966ac105cab64b3c8", [:mix], [{:bandit, ">= 1.4.0", [hex: :bandit, repo: "hexpm", optional: true]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, ">= 2.0.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:x509, "~> 0.6", [hex: :x509, repo: "hexpm", optional: false]}], "hexpm", "18f6769a61f33193c210735e4966245a326aaf4181434dfb75d6b4baad9b8bcf"}, + "thousand_island": {:hex, :thousand_island, "1.3.7", "1da7598c0f4f5f50562c097a3f8af308ded48cd35139f0e6f17d9443e4d0c9c5", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "0139335079953de41d381a6134d8b618d53d084f558c734f2662d1a72818dd12"}, "websock": {:hex, :websock, "0.5.3", "2f69a6ebe810328555b6fe5c831a851f485e303a7c8ce6c5f675abeb20ebdadc", [:mix], [], "hexpm", "6105453d7fac22c712ad66fab1d45abdf049868f253cf719b625151460b8b453"}, - "x509": {:hex, :x509, "0.8.8", "aaf5e58b19a36a8e2c5c5cff0ad30f64eef5d9225f0fd98fb07912ee23f7aba3", [:mix], [], "hexpm", "ccc3bff61406e5bb6a63f06d549f3dba3a1bbb456d84517efaaa210d8a33750f"}, + "x509": {:hex, :x509, "0.8.10", "5d1ec6d5f4db31982f9dc34e6a1eebd631d04599e0b6c1c259f1dadd4495e11f", [:mix], [], "hexpm", "a191221665af28b9bdfff0c986ef55f80e126d8ce751bbdf6cefa846410140c0"}, } diff --git a/test/assent/config_test.exs b/test/assent/config_test.exs index c9c37fc..9d18e61 100644 --- a/test/assent/config_test.exs +++ b/test/assent/config_test.exs @@ -1,4 +1,4 @@ defmodule Assent.ConfigTest do - use ExUnit.Case + use Assent.TestCase doctest Assent.Config end diff --git a/test/assent/http_adapter/finch_test.exs b/test/assent/http_adapter/finch_test.exs index 2ab7554..274e24a 100644 --- a/test/assent/http_adapter/finch_test.exs +++ b/test/assent/http_adapter/finch_test.exs @@ -1,5 +1,5 @@ defmodule Assent.HTTPAdapter.FinchTest do - use ExUnit.Case + use Assent.TestCase doctest Assent.HTTPAdapter.Finch alias Assent.HTTPAdapter.Finch, as: FinchAdapter @@ -54,7 +54,7 @@ defmodule Assent.HTTPAdapter.FinchTest do assert capture_log(fn -> assert {:error, %Error{reason: :disconnected}} = FinchAdapter.request(:get, bad_host_url, nil, [], supervisor: supervisor) - end) =~ "{bad_cert,hostname_check_failed}" + end) =~ "hostname_check_failed" end test "handles SSL with bad certificate and no verification" do diff --git a/test/assent/http_adapter/httpc_test.exs b/test/assent/http_adapter/httpc_test.exs index b4482ee..ae8b19f 100644 --- a/test/assent/http_adapter/httpc_test.exs +++ b/test/assent/http_adapter/httpc_test.exs @@ -1,5 +1,5 @@ defmodule Assent.HTTPAdapter.HttpcTest do - use ExUnit.Case + use Assent.TestCase doctest Assent.HTTPAdapter.Httpc alias Assent.HTTPAdapter.{Httpc, HTTPResponse} diff --git a/test/assent/http_adapter/mint_test.exs b/test/assent/http_adapter/mint_test.exs index 88d876a..faa4463 100644 --- a/test/assent/http_adapter/mint_test.exs +++ b/test/assent/http_adapter/mint_test.exs @@ -1,5 +1,5 @@ defmodule Assent.HTTPAdapter.MintTest do - use ExUnit.Case + use Assent.TestCase doctest Assent.HTTPAdapter.Mint alias ExUnit.CaptureIO diff --git a/test/assent/http_adapter/req_test.exs b/test/assent/http_adapter/req_test.exs index d63676b..7b0c315 100644 --- a/test/assent/http_adapter/req_test.exs +++ b/test/assent/http_adapter/req_test.exs @@ -1,16 +1,25 @@ defmodule Assent.HTTPAdapter.ReqTest do - use ExUnit.Case + use Assent.TestCase doctest Assent.HTTPAdapter.Req - alias Mint.TransportError + alias Req.TransportError alias Assent.HTTPAdapter.{HTTPResponse, Req} + # Test retries quickly + @req_opts [retry_delay: 0] + describe "request/4" do test "handles SSL" do TestServer.start(scheme: :https) TestServer.add("/", via: :get) - req_opts = [connect_options: [transport_opts: [cacerts: TestServer.x509_suite().cacerts]]] + req_opts = + Keyword.put( + @req_opts, + :connect_options, + transport_opts: [cacerts: TestServer.x509_suite().cacerts], + protocols: [:http2] + ) assert {:ok, %HTTPResponse{status: 200, body: "HTTP/2"}} = Req.request(:get, TestServer.url(), nil, [], req_opts) @@ -20,7 +29,13 @@ defmodule Assent.HTTPAdapter.ReqTest do TestServer.start(scheme: :https) bad_host_url = TestServer.url(host: "bad-host.localhost") - req_opts = [connect_options: [transport_opts: [cacerts: TestServer.x509_suite().cacerts]]] + + req_opts = + Keyword.put( + @req_opts, + :connect_options, + transport_opts: [cacerts: TestServer.x509_suite().cacerts] + ) assert {:error, %TransportError{reason: {:tls_alert, {:handshake_failure, _error}}}} = Req.request(:get, bad_host_url, nil, [], req_opts) @@ -32,11 +47,12 @@ defmodule Assent.HTTPAdapter.ReqTest do bad_host_url = TestServer.url(host: "bad-host.localhost") - req_opts = [ - connect_options: [ + req_opts = + Keyword.put( + @req_opts, + :connect_options, transport_opts: [cacerts: TestServer.x509_suite().cacerts, verify: :verify_none] - ] - ] + ) assert {:ok, %HTTPResponse{status: 200}} = Req.request(:get, bad_host_url, nil, [], req_opts) @@ -62,7 +78,7 @@ defmodule Assent.HTTPAdapter.ReqTest do ) assert {:ok, %HTTPResponse{status: 200}} = - Req.request(:get, TestServer.url("/get?a=1"), nil, []) + Req.request(:get, TestServer.url("/get?a=1"), nil, [], @req_opts) end test "handles POST" do @@ -84,9 +100,15 @@ defmodule Assent.HTTPAdapter.ReqTest do ) assert {:ok, %HTTPResponse{status: 200}} = - Req.request(:post, TestServer.url("/post"), "a=1&b=2", [ - {"content-type", "application/x-www-form-urlencoded"} - ]) + Req.request( + :post, + TestServer.url("/post"), + "a=1&b=2", + [ + {"content-type", "application/x-www-form-urlencoded"} + ], + @req_opts + ) end end end diff --git a/test/assent/jwt_adapter/assent_jwt_test.exs b/test/assent/jwt_adapter/assent_jwt_test.exs index 0053b45..9cf1805 100644 --- a/test/assent/jwt_adapter/assent_jwt_test.exs +++ b/test/assent/jwt_adapter/assent_jwt_test.exs @@ -1,5 +1,5 @@ defmodule Assent.JWTAdapter.AssentJWTTest do - use ExUnit.Case + use Assent.TestCase doctest Assent.JWTAdapter.AssentJWT alias Assent.JWTAdapter.AssentJWT @@ -10,7 +10,7 @@ defmodule Assent.JWTAdapter.AssentJWTTest do @secret "your-256-bit-secret" test "sign/2" do - assert AssentJWT.sign(@claims, "HS256", @secret, json_library: Jason) == {:ok, @token} + assert AssentJWT.sign(@claims, "HS256", @secret, json_library: @json_library) == {:ok, @token} end test "sign/2 with invalid header" do @@ -18,7 +18,7 @@ defmodule Assent.JWTAdapter.AssentJWTTest do assert {:error, error} = AssentJWT.sign(@claims, "HS256", @secret, - json_library: Jason, + json_library: @json_library, private_key_id: unencodable ) @@ -30,30 +30,34 @@ defmodule Assent.JWTAdapter.AssentJWTTest do test "sign/2 with invalid claims" do unencodable = & &1 - assert {:error, error} = AssentJWT.sign(unencodable, "HS256", @secret, json_library: Jason) + assert {:error, error} = + AssentJWT.sign(unencodable, "HS256", @secret, json_library: @json_library) + assert error.message == "Failed to encode claims" assert %Protocol.UndefinedError{} = error.reason assert error.data == unencodable end test "sign/2 with invalid algorithm" do - assert {:error, error} = AssentJWT.sign(@claims, "none", @secret, json_library: Jason) + assert {:error, error} = AssentJWT.sign(@claims, "none", @secret, json_library: @json_library) assert error.message == "Failed to sign JWT" assert error.reason == "Unsupported JWT alg none or invalid JWK" assert {_, "none"} = error.data - assert {:error, error} = AssentJWT.sign(@claims, "HS000", @secret, json_library: Jason) + assert {:error, error} = + AssentJWT.sign(@claims, "HS000", @secret, json_library: @json_library) + assert error.message == "Failed to sign JWT" assert error.reason == "Invalid SHA-2 algorithm bit size: 000" assert {_, "HS000"} = error.data end test "verify/3" do - assert {:ok, jwt} = AssentJWT.verify(@token, "invalid", json_library: Jason) + assert {:ok, jwt} = AssentJWT.verify(@token, "invalid", json_library: @json_library) refute jwt.verified? assert jwt.claims == @claims - assert {:ok, jwt} = AssentJWT.verify(@token, @secret, json_library: Jason) + assert {:ok, jwt} = AssentJWT.verify(@token, @secret, json_library: @json_library) assert jwt.verified? assert jwt.claims == @claims end @@ -61,14 +65,14 @@ defmodule Assent.JWTAdapter.AssentJWTTest do test "verify/3 with invalid JWT format" do too_long = @token <> ".value" - assert {:error, error} = AssentJWT.verify(too_long, @secret, json_library: Jason) + assert {:error, error} = AssentJWT.verify(too_long, @secret, json_library: @json_library) assert error.message == "JWT must have exactly three parts" assert error.reason == :invalid_format assert [_, _, _, "value"] = error.data too_short = @token |> String.split(".") |> Enum.take(2) |> Enum.join(".") - assert {:error, error} = AssentJWT.verify(too_short, @secret, json_library: Jason) + assert {:error, error} = AssentJWT.verify(too_short, @secret, json_library: @json_library) assert error.message == "JWT must have exactly three parts" assert error.reason == :invalid_format assert [_, _] = error.data @@ -78,7 +82,7 @@ defmodule Assent.JWTAdapter.AssentJWTTest do test "verify/3 with header with invalid base64" do token = replace_jwt_at(@token, 0, @invalid_base64) - assert {:error, error} = AssentJWT.verify(token, @secret, json_library: Jason) + assert {:error, error} = AssentJWT.verify(token, @secret, json_library: @json_library) assert error.message == "Failed to decode header" assert error.reason == "Invalid Base64URL" assert error.data == @invalid_base64 @@ -95,17 +99,23 @@ defmodule Assent.JWTAdapter.AssentJWTTest do test "verify/3 with header with invalid json" do token = replace_jwt_at(@token, 0, @invalid_json) - assert {:error, error} = AssentJWT.verify(token, @secret, json_library: Jason) + assert {:error, error} = AssentJWT.verify(token, @secret, json_library: @json_library) assert error.message == "Failed to decode header" - assert %Jason.DecodeError{} = error.reason + + if unquote(@json_library == Jason) do + assert %Jason.DecodeError{} = error.reason + else + assert error.reason == {:invalid_byte, 0, 37} + end + assert error.data == @invalid_json end - @header Base.url_encode64(Jason.encode!(%{}), padding: false) + @header Base.url_encode64(@json_library.encode!(%{}), padding: false) test "verify/3 with header with missing \"alg\"" do token = replace_jwt_at(@token, 0, @header) - assert {:error, error} = AssentJWT.verify(token, @secret, json_library: Jason) + assert {:error, error} = AssentJWT.verify(token, @secret, json_library: @json_library) assert error.message == "Failed to decode header" assert error.reason == "No \"alg\" found in header" assert error.data == @header @@ -115,7 +125,7 @@ defmodule Assent.JWTAdapter.AssentJWTTest do test "verify/3 with claims with invalid base64" do token = replace_jwt_at(@token, 1, @invalid_base64) - assert {:error, error} = AssentJWT.verify(token, @secret, json_library: Jason) + assert {:error, error} = AssentJWT.verify(token, @secret, json_library: @json_library) assert error.message == "Failed to decode claims" assert error.reason == "Invalid Base64URL" assert error.data == @invalid_base64 @@ -125,9 +135,15 @@ defmodule Assent.JWTAdapter.AssentJWTTest do test "verify/3 with claims with invalid json" do token = replace_jwt_at(@token, 1, @invalid_json) - assert {:error, error} = AssentJWT.verify(token, @secret, json_library: Jason) + assert {:error, error} = AssentJWT.verify(token, @secret, json_library: @json_library) assert error.message == "Failed to decode claims" - assert %Jason.DecodeError{} = error.reason + + if unquote(@json_library == Jason) do + assert %Jason.DecodeError{} = error.reason + else + assert error.reason == {:invalid_byte, 0, 37} + end + assert error.data == @invalid_json end @@ -135,14 +151,14 @@ defmodule Assent.JWTAdapter.AssentJWTTest do test "verify/3 with signature with invalid base64" do token = replace_jwt_at(@token, 2, @invalid_base64) - assert {:error, error} = AssentJWT.verify(token, @secret, json_library: Jason) + assert {:error, error} = AssentJWT.verify(token, @secret, json_library: @json_library) assert error.message == "Failed to decode signature" assert error.reason == "Invalid Base64URL" assert error.data == @invalid_base64 end test "verify/3 with no secret" do - assert {:ok, jwt} = AssentJWT.verify(@token, nil, json_library: Jason) + assert {:ok, jwt} = AssentJWT.verify(@token, nil, json_library: @json_library) refute jwt.verified? end @@ -190,35 +206,42 @@ defmodule Assent.JWTAdapter.AssentJWTTest do """ test "sign/2" do - assert AssentJWT.sign(@claims, "RS256", @private_key, json_library: Jason) == {:ok, @token} + assert AssentJWT.sign(@claims, "RS256", @private_key, json_library: @json_library) == + {:ok, @token} refute AssentJWT.sign(@claims, "RS256", @private_key, - json_library: Jason, + json_library: @json_library, private_key_id: "key_id" ) == {:ok, @token} end test "sign/2 with invalid algorithm" do - assert {:error, error} = AssentJWT.sign(@claims, "RS000", @private_key, json_library: Jason) + assert {:error, error} = + AssentJWT.sign(@claims, "RS000", @private_key, json_library: @json_library) + assert error.message == "Failed to sign JWT" assert error.reason == "Invalid SHA-2 algorithm bit size: 000" assert {_, "RS000"} = error.data end test "sign/2 with invalid pem" do - assert {:error, error} = AssentJWT.sign(@claims, "RS256", "invalid", json_library: Jason) + assert {:error, error} = + AssentJWT.sign(@claims, "RS256", "invalid", json_library: @json_library) + assert error.message == "Failed to sign JWT" assert error.reason == "Invalid private key" assert {:error, error} = - AssentJWT.sign(@claims, "RS256", @private_key <> @private_key, json_library: Jason) + AssentJWT.sign(@claims, "RS256", @private_key <> @private_key, + json_library: @json_library + ) assert error.message == "Failed to sign JWT" assert error.reason == "Private key should only have one entry" end test "verify/3" do - assert {:ok, jwt} = AssentJWT.verify(@token, @public_key, json_library: Jason) + assert {:ok, jwt} = AssentJWT.verify(@token, @public_key, json_library: @json_library) assert jwt.verified? assert jwt.claims == @claims end @@ -231,13 +254,13 @@ defmodule Assent.JWTAdapter.AssentJWTTest do } test "verify/3 with JWK" do - assert {:ok, jwt} = AssentJWT.verify(@token, @jwk, json_library: Jason) + assert {:ok, jwt} = AssentJWT.verify(@token, @jwk, json_library: @json_library) assert jwt.verified? assert jwt.claims == @claims end test "verify/3 with nil secret" do - assert {:ok, jwt} = AssentJWT.verify(@token, nil, json_library: Jason) + assert {:ok, jwt} = AssentJWT.verify(@token, nil, json_library: @json_library) refute jwt.verified? assert jwt.claims == @claims end @@ -267,18 +290,19 @@ defmodule Assent.JWTAdapter.AssentJWTTest do } test "signs and verifies" do - assert {:ok, token} = AssentJWT.sign(@claims, "ES256", @private_key, json_library: Jason) + assert {:ok, token} = + AssentJWT.sign(@claims, "ES256", @private_key, json_library: @json_library) - assert {:ok, jwt} = AssentJWT.verify(token, @public_key, json_library: Jason) + assert {:ok, jwt} = AssentJWT.verify(token, @public_key, json_library: @json_library) assert jwt.verified? - assert {:ok, jwt} = AssentJWT.verify(@token, @public_key, json_library: Jason) + assert {:ok, jwt} = AssentJWT.verify(@token, @public_key, json_library: @json_library) assert jwt.verified? end test "sign/2 with invalid algorithm" do assert {:error, error} = - AssentJWT.sign(@claims, "ES000", @private_key, json_library: Jason) + AssentJWT.sign(@claims, "ES000", @private_key, json_library: @json_library) assert error.message == "Failed to sign JWT" assert error.reason == "Invalid SHA-2 algorithm bit size: 000" @@ -286,13 +310,15 @@ defmodule Assent.JWTAdapter.AssentJWTTest do end test "sign/2 with invalid pem" do - assert {:error, error} = AssentJWT.sign(@claims, "ES256", "invalid", json_library: Jason) + assert {:error, error} = + AssentJWT.sign(@claims, "ES256", "invalid", json_library: @json_library) + assert error.message == "Failed to sign JWT" assert error.reason == "Invalid private key" assert {:error, error} = AssentJWT.sign(@claims, "ES256", @private_key <> @private_key, - json_library: Jason + json_library: @json_library ) assert error.message == "Failed to sign JWT" diff --git a/test/assent/jwt_adapter/jose_test.exs b/test/assent/jwt_adapter/jose_test.exs index 356c1e5..14a48fc 100644 --- a/test/assent/jwt_adapter/jose_test.exs +++ b/test/assent/jwt_adapter/jose_test.exs @@ -1,5 +1,5 @@ defmodule Assent.JWTAdapter.JOSETest do - use ExUnit.Case + use Assent.TestCase doctest Assent.JWTAdapter.JOSE alias Assent.JWTAdapter.JOSE @@ -9,15 +9,15 @@ defmodule Assent.JWTAdapter.JOSETest do @secret "your-256-bit-secret" test "sign/2" do - assert JOSE.sign(@claims, "HS256", @secret, json_library: Jason) == {:ok, @token} + assert JOSE.sign(@claims, "HS256", @secret, json_library: @json_library) == {:ok, @token} end test "verify/3" do - assert {:ok, jwt} = JOSE.verify(@token, "invalid", json_library: Jason) + assert {:ok, jwt} = JOSE.verify(@token, "invalid", json_library: @json_library) refute jwt.verified? assert jwt.claims == @claims - assert {:ok, jwt} = JOSE.verify(@token, @secret, json_library: Jason) + assert {:ok, jwt} = JOSE.verify(@token, @secret, json_library: @json_library) assert jwt.verified? assert jwt.claims == @claims end @@ -66,16 +66,17 @@ defmodule Assent.JWTAdapter.JOSETest do """ test "sign/2" do - assert JOSE.sign(@claims, "RS256", @private_key, json_library: Jason) == {:ok, @token} + assert JOSE.sign(@claims, "RS256", @private_key, json_library: @json_library) == + {:ok, @token} refute JOSE.sign(@claims, "RS256", @private_key, - json_library: Jason, + json_library: @json_library, private_key_id: "key_id" ) == {:ok, @token} end test "verify/3" do - assert {:ok, jwt} = JOSE.verify(@token, @public_key, json_library: Jason) + assert {:ok, jwt} = JOSE.verify(@token, @public_key, json_library: @json_library) assert jwt.verified? assert jwt.claims == @claims end @@ -88,13 +89,13 @@ defmodule Assent.JWTAdapter.JOSETest do } test "verify/3 with JWK" do - assert {:ok, jwt} = JOSE.verify(@token, @jwk, json_library: Jason) + assert {:ok, jwt} = JOSE.verify(@token, @jwk, json_library: @json_library) assert jwt.verified? assert jwt.claims == @claims end test "verify/3 with nil secret" do - assert {:ok, jwt} = JOSE.verify(@token, nil, json_library: Jason) + assert {:ok, jwt} = JOSE.verify(@token, nil, json_library: @json_library) refute jwt.verified? assert jwt.claims == @claims end diff --git a/test/assent/strategies/apple_test.exs b/test/assent/strategies/apple_test.exs index facca55..cc2ac1d 100644 --- a/test/assent/strategies/apple_test.exs +++ b/test/assent/strategies/apple_test.exs @@ -73,7 +73,11 @@ defmodule Assent.Strategy.AppleTest do [id_token_opts: [claims: @id_token_claims], uri: "/auth/token"], fn _conn, params -> assert {:ok, jwt} = - AssentJWT.verify(params["client_secret"], @public_key, json_library: Jason) + AssentJWT.verify( + params["client_secret"], + @public_key, + json_library: @json_library + ) assert jwt.verified? assert jwt.header["alg"] == "ES256" @@ -96,7 +100,7 @@ defmodule Assent.Strategy.AppleTest do expected_user = Map.merge(@user, %{"given_name" => "John", "family_name" => "Doe"}) encoded_user = - Jason.encode!(%{ + @json_library.encode!(%{ name: %{ firstName: "John", lastName: "Doe" diff --git a/test/assent/strategies/oauth2_test.exs b/test/assent/strategies/oauth2_test.exs index 57d9aba..0198e99 100644 --- a/test/assent/strategies/oauth2_test.exs +++ b/test/assent/strategies/oauth2_test.exs @@ -297,7 +297,11 @@ defmodule Assent.Strategy.OAuth2Test do "urn:ietf:params:oauth:client-assertion-type:jwt-bearer" assert {:ok, jwt} = - AssentJWT.verify(params["client_assertion"], @client_secret, json_library: Jason) + AssentJWT.verify( + params["client_assertion"], + @client_secret, + json_library: @json_library + ) assert jwt.verified? assert jwt.header["alg"] == "HS256" @@ -332,7 +336,11 @@ defmodule Assent.Strategy.OAuth2Test do "urn:ietf:params:oauth:client-assertion-type:jwt-bearer" assert {:ok, jwt} = - AssentJWT.verify(params["client_assertion"], @public_key, json_library: Jason) + AssentJWT.verify( + params["client_assertion"], + @public_key, + json_library: @json_library + ) assert jwt.verified? assert jwt.header["alg"] == "RS256" @@ -365,7 +373,11 @@ defmodule Assent.Strategy.OAuth2Test do expect_oauth2_access_token_request([], fn _conn, params -> assert {:ok, jwt} = - AssentJWT.verify(params["client_assertion"], @public_key, json_library: Jason) + AssentJWT.verify( + params["client_assertion"], + @public_key, + json_library: @json_library + ) assert jwt.verified? assert jwt.header["kid"] == @private_key_id diff --git a/test/assent/strategy_test.exs b/test/assent/strategy_test.exs index 0f2a107..bce9279 100644 --- a/test/assent/strategy_test.exs +++ b/test/assent/strategy_test.exs @@ -1,5 +1,5 @@ defmodule Assent.StrategyTest do - use ExUnit.Case + use Assent.TestCase doctest Assent.Strategy alias Assent.{HTTPAdapter.HTTPResponse, Strategy} @@ -8,7 +8,7 @@ defmodule Assent.StrategyTest do expected = %{"a" => "1", "b" => "2"} headers = [{"content-type", "application/json"}] - body = Jason.encode!(expected) + body = @json_library.encode!(expected) assert Strategy.decode_response({:ok, %{body: body, headers: headers}}, []) == {:ok, %{body: expected, headers: headers}} @@ -50,6 +50,8 @@ defmodule Assent.StrategyTest do end defmodule HTTPMock do + @json_library (Code.ensure_loaded?(JSON) && JSON) || Jason + def request(:get, "http-adapter", nil, [], nil) do {:ok, %HTTPResponse{status: 200, headers: [], body: nil}} end @@ -63,7 +65,7 @@ defmodule Assent.StrategyTest do %HTTPResponse{ status: 200, headers: [{"content-type", "application/json"}], - body: Jason.encode!(%{"a" => 1}) + body: @json_library.encode!(%{"a" => 1}) }} end @@ -81,7 +83,7 @@ defmodule Assent.StrategyTest do %HTTPResponse{ status: 200, headers: [{"content-type", "text/javascript"}], - body: Jason.encode!(%{"a" => 1}) + body: @json_library.encode!(%{"a" => 1}) }} end @@ -91,7 +93,7 @@ defmodule Assent.StrategyTest do end def request(:get, "json-no-headers", nil, [], nil) do - {:ok, %HTTPResponse{status: 200, headers: [], body: Jason.encode!(%{"a" => 1})}} + {:ok, %HTTPResponse{status: 200, headers: [], body: @json_library.encode!(%{"a" => 1})}} end def request(:get, "form-data-body", nil, [], nil) do @@ -170,15 +172,21 @@ defmodule Assent.StrategyTest do request_url: "json-encoded-body-text/javascript-header" }} - assert {:error, %Jason.DecodeError{}} = + assert {:error, error} = Strategy.request(:get, "invalid-json-body", nil, [], http_adapter: HTTPMock) + if unquote(@json_library == Jason) do + assert %Jason.DecodeError{} = error + else + assert error == {:invalid_byte, 0, 37} + end + assert Strategy.request(:get, "json-no-headers", nil, [], http_adapter: HTTPMock) == {:ok, %HTTPResponse{ status: 200, headers: [], - body: Jason.encode!(%{"a" => 1}), + body: @json_library.encode!(%{"a" => 1}), http_adapter: HTTPMock, request_url: "json-no-headers" }} @@ -219,7 +227,7 @@ defmodule Assent.StrategyTest do def decode(_binary), do: {:ok, %{"alg" => "none", "custom_json" => true}} - def encode(_binary), do: {:ok, ""} + def encode!(_any), do: "" end @claims %{"iat" => 1_516_239_022, "name" => "John Doe", "sub" => "1234567890"} diff --git a/test/assent_test.exs b/test/assent_test.exs index 0c2d024..e143805 100644 --- a/test/assent_test.exs +++ b/test/assent_test.exs @@ -1,4 +1,4 @@ defmodule AssentTest do - use ExUnit.Case + use Assent.TestCase doctest Assent end diff --git a/test/support/strategies/oauth2_test_case.ex b/test/support/strategies/oauth2_test_case.ex index 8273501..e4568e2 100644 --- a/test/support/strategies/oauth2_test_case.ex +++ b/test/support/strategies/oauth2_test_case.ex @@ -2,6 +2,8 @@ defmodule Assent.Test.OAuth2TestCase do @moduledoc false use ExUnit.CaseTemplate + @json_library (Code.ensure_loaded?(JSON) && JSON) || Jason + setup _tags do TestServer.start() @@ -20,7 +22,7 @@ defmodule Assent.Test.OAuth2TestCase do using do quote do - use ExUnit.Case + use Assent.TestCase import unquote(__MODULE__) end @@ -90,6 +92,6 @@ defmodule Assent.Test.OAuth2TestCase do defp send_json_resp(conn, body, status_code) do conn |> Conn.put_resp_content_type("application/json") - |> Conn.send_resp(status_code, Jason.encode!(body)) + |> Conn.send_resp(status_code, @json_library.encode!(body)) end end diff --git a/test/support/strategies/oauth_test_case.ex b/test/support/strategies/oauth_test_case.ex index 9f2f458..d39142b 100644 --- a/test/support/strategies/oauth_test_case.ex +++ b/test/support/strategies/oauth_test_case.ex @@ -2,6 +2,8 @@ defmodule Assent.Test.OAuthTestCase do @moduledoc false use ExUnit.CaseTemplate + @json_library (Code.ensure_loaded?(JSON) && JSON) || Jason + setup _context do TestServer.start() @@ -20,7 +22,7 @@ defmodule Assent.Test.OAuthTestCase do using do quote do - use ExUnit.Case + use Assent.TestCase import unquote(__MODULE__) end @@ -48,7 +50,7 @@ defmodule Assent.Test.OAuthTestCase do response = case content_type do "application/x-www-form-urlencoded" -> URI.encode_query(response) - "application/json" -> Jason.encode!(response) + "application/json" -> @json_library.encode!(response) _any -> response end diff --git a/test/support/strategies/oidc_test_case.ex b/test/support/strategies/oidc_test_case.ex index c295313..bdafe09 100644 --- a/test/support/strategies/oidc_test_case.ex +++ b/test/support/strategies/oidc_test_case.ex @@ -5,6 +5,8 @@ defmodule Assent.Test.OIDCTestCase do alias Assent.Test.OAuth2TestCase alias Plug.Conn + @json_library (Code.ensure_loaded?(JSON) && JSON) || Jason + @private_key """ -----BEGIN RSA PRIVATE KEY----- MIIEogIBAAKCAQEAnzyis1ZjfNB0bBgKFMSvvkTtwlvBsaJq7S5wA+kzeVOVpVWw @@ -79,7 +81,7 @@ defmodule Assent.Test.OIDCTestCase do using do quote do - use ExUnit.Case + use Assent.TestCase import unquote(__MODULE__) import OAuth2TestCase, only: [expect_oauth2_user_request: 2, expect_oauth2_user_request: 3] @@ -130,7 +132,7 @@ defmodule Assent.Test.OIDCTestCase do to: fn conn -> conn |> Plug.Conn.put_resp_content_type("application/json") - |> Plug.Conn.send_resp(status_code, Jason.encode!(%{"keys" => keys})) + |> Plug.Conn.send_resp(status_code, @json_library.encode!(%{"keys" => keys})) end ) @@ -222,6 +224,6 @@ defmodule Assent.Test.OIDCTestCase do defp send_json_resp(conn, body, status_code) do conn |> Conn.put_resp_content_type("application/json") - |> Conn.send_resp(status_code, Jason.encode!(body)) + |> Conn.send_resp(status_code, @json_library.encode!(body)) end end diff --git a/test/support/test_case.ex b/test/support/test_case.ex new file mode 100644 index 0000000..3b6f503 --- /dev/null +++ b/test/support/test_case.ex @@ -0,0 +1,12 @@ +defmodule Assent.TestCase do + @moduledoc false + use ExUnit.CaseTemplate + + using do + quote do + # We add this module attribute for backwards compatibility. Once Elixir 1.18 is + # required we can default to JSON. + @json_library (Code.ensure_loaded?(JSON) && JSON) || Jason + end + end +end