Skip to content

Make JSON library configurable #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/keycloak.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,16 @@ defmodule Keycloak do
|> OAuth2.Client.put_header("Accept", "application/json")
|> AuthCode.get_token(params, headers)
end


@doc """
Returns the configured JSON encoding library for Keycloak.
To customize the JSON library, including the following
in your `config/config.exs`:
config :keycloak, :json_library, Jason
"""
def json_library do
Application.get_env(:keycloak, :json_library, Poison)
end

end
2 changes: 1 addition & 1 deletion lib/keycloak/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defmodule Keycloak.Client do
site: "#{site}/auth",
authorize_url: "/realms/#{realm}/protocol/openid-connect/auth",
token_url: "/realms/#{realm}/protocol/openid-connect/token",
serializers: %{"application/json" => Poison}
serializers: %{"application/json" => Keycloak.json_library()}
]
|> Keyword.merge(config)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/keycloak/plug/verify_token.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ defmodule Keycloak.Plug.VerifyToken do
{:error, message} ->
conn
|> put_resp_content_type("application/vnd.api+json")
|> send_resp(401, Poison.encode!(%{error: message}))
|> send_resp(401, Keycloak.json_library().encode!(%{error: message}))
|> halt()
end
end
Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ defmodule Keycloak.Mixfile do
end

defp deps do
has_specific_json_library? = nil != Application.get_env(:keycloak, :json_library)
[
{:joken, "~> 2.0"},
{:oauth2, "~> 2.0"},
{:plug, "~> 1.4"},
{:poison, "~> 4.0"},
{:poison, "~> 4.0", optional: has_specific_json_library?},
{:credo, "~> 1.4", only: [:dev, :test], runtime: false},
{:ex_doc, "~> 0.26", only: :dev, runtime: false},
{:rexbug, "~> 1.0", only: :dev, runtime: false}
Expand Down