Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
168 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule OAuth2.Application do | ||
@moduledoc false | ||
|
||
use Application | ||
|
||
def start(_, _) do | ||
import Supervisor.Spec, warn: false | ||
|
||
:ets.new(OAuth2.Serializer, [:named_table, :public, read_concurrency: true]) | ||
|
||
:ok = OAuth2.register_serializer("application/json", Poison) | ||
|
||
Supervisor.start_link([], strategy: :one_for_one) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,77 @@ | ||
defmodule OAuth2.Serializer do | ||
@moduledoc false | ||
|
||
require Logger | ||
@callback encode!(map) :: binary | ||
@callback decode!(binary) :: map | ||
|
||
defmodule NullSerializer do | ||
@moduledoc false | ||
@spec get(binary) :: atom | ||
def get(mime_type) do | ||
case :ets.lookup(__MODULE__, mime_type) do | ||
[] -> | ||
maybe_warn_missing_serializer(mime_type) | ||
OAuth2.Serializer.Null | ||
[{_, module}] -> | ||
module | ||
end | ||
end | ||
|
||
@doc """ | ||
Register a serialization module for a given mime type. | ||
@doc false | ||
def decode!(content), do: content | ||
## Example | ||
@doc false | ||
def encode!(content), do: content | ||
iex> OAuth2.Serializer.register("application/json", Poison) | ||
:ok | ||
iex> OAuth2.Serializer.get("application/json") | ||
Poison | ||
""" | ||
@spec register(binary, atom) :: :ok | ||
def register(mime_type, module) do | ||
:ets.insert(__MODULE__, {mime_type, module}) | ||
:ok | ||
end | ||
|
||
def decode!(content, type), do: serializer(type).decode!(content) | ||
@doc """ | ||
Un-register a serialization module for a given mime type. | ||
## Example | ||
iex> OAuth2.Serializer.unregister("application/json") | ||
:ok | ||
iex> OAuth2.Serializer.get("application/json") | ||
OAuth2.Serializer.Null | ||
""" | ||
@spec unregister(binary) :: :ok | ||
def unregister(mime_type) do | ||
:ets.delete(__MODULE__, mime_type) | ||
:ok | ||
end | ||
|
||
def encode!(content, type), do: serializer(type).encode!(content) | ||
@spec decode!(binary, binary) :: map | ||
def decode!(data, type), | ||
do: get(type).decode!(data) | ||
|
||
defp serializer(type) do | ||
serializer = Map.get(configured_serializers(), type, NullSerializer) | ||
warn_missing_serializer = Application.get_env(:oauth2, :warn_missing_serializer, true) | ||
@spec decode!(map, binary) :: binary | ||
def encode!(data, type), | ||
do: get(type).encode!(data) | ||
|
||
defp maybe_warn_missing_serializer(type) do | ||
if Application.get_env(:oauth2, :warn_missing_serializer, true) do | ||
require Logger | ||
|
||
if serializer == NullSerializer && warn_missing_serializer do | ||
Logger.warn """ | ||
A serializer was not configured for content-type '#{type}'. | ||
To remove this warning for this content-type, add the following to your `config.exs` file: | ||
To remove this warning for this content-type, consider registering a serializer: | ||
config :oauth2, | ||
serializers: %{ | ||
"#{type}" => MySerializer | ||
} | ||
OAuth2.register_serializer("#{type}", MySerializer) | ||
To remove this warning entirely, add the following to you `config.exs` file: | ||
To remove this warning entirely, add the following to your `config.exs` file: | ||
config :oauth2, | ||
warn_missing_serializer: false | ||
""" | ||
end | ||
|
||
serializer | ||
end | ||
|
||
defp configured_serializers do | ||
Application.get_env(:oauth2, :serializers) || | ||
raise("Missing serializers configuration! Make sure oauth2 app is added to mix application list") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule OAuth2.Serializer.Null do | ||
@moduledoc false | ||
|
||
@behaviour OAuth2.Serializer | ||
|
||
@doc false | ||
def decode!(data), do: data | ||
|
||
@doc false | ||
def encode!(data), do: data | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
defmodule OAuth2.SerializerTest do | ||
use ExUnit.Case, async: false | ||
alias OAuth2.Serializer | ||
|
||
doctest OAuth2.Serializer | ||
|
||
@json_mime "application/json" | ||
|
||
defmodule TestSerializer do | ||
def decode!(_), do: "decode_ok" | ||
def encode!(_), do: "encode_ok" | ||
end | ||
|
||
test "has default json serializer" do | ||
OAuth2.register_serializer(@json_mime, Poison) | ||
assert %{"foo" => 1} == Serializer.decode!(~s|{"foo": 1}|, @json_mime) | ||
end | ||
|
||
test "accepts serializer override" do | ||
OAuth2.register_serializer(@json_mime, TestSerializer) | ||
|
||
assert "decode_ok" == Serializer.decode!(~s|{"foo": 1}|, @json_mime) | ||
assert "encode_ok" == Serializer.encode!(%{"foo" => 1}, @json_mime) | ||
|
||
OAuth2.register_serializer(@json_mime, Poison) | ||
end | ||
|
||
test "fallsback to Null serializer" do | ||
assert OAuth2.Serializer.Null == Serializer.get("unknown") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule OAuth2.Serializer.NullTest do | ||
use ExUnit.Case | ||
|
||
alias OAuth2.Serializer.Null | ||
|
||
test "encode!" do | ||
assert "hello" == Null.encode!("hello") | ||
end | ||
|
||
test "decode!" do | ||
assert "hello" == Null.decode!("hello") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
Application.ensure_all_started(:bypass) | ||
ExUnit.start | ||
Application.put_env(:oauth2, :warn_missing_serializer, false) | ||
ExUnit.start() |