Skip to content
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

Make custom redirect_uri_match_fun work with authorization validations #58

Merged
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v0.5.3 (TBA)

* Fixed bug in `ExOauth2Provider.RedirectURI.valid_for_authorization?/3` where the `:redirect_uri_match_fun` configuration option was not used
* Deprecated `ExOauth2Provider.RedirectURI.matches?/2`

## v0.5.2 (2019-06-10)

* Added `:redirect_uri_match_fun` configuration option for custom matching of redirect uri
Expand Down
13 changes: 8 additions & 5 deletions lib/ex_oauth2_provider/redirect_uri.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ defmodule ExOauth2Provider.RedirectURI do
Config.force_ssl_in_redirect_uri?(config) and uri.scheme == "http"
end

@doc false
@deprecated "Use `matches?/3` instead"
def matches?(uri, client_uri), do: matches?(uri, client_uri, [])

@doc """
Check if uri matches client uri
"""
@spec matches?(binary(), binary(), keyword()) :: boolean()
def matches?(uri, client_uri, config \\ [])
def matches?(uri, client_uri, config) when is_binary(uri) and is_binary(client_uri) do
matches?(URI.parse(uri), URI.parse(client_uri), config)
end
Expand All @@ -63,14 +66,14 @@ defmodule ExOauth2Provider.RedirectURI do
def valid_for_authorization?(url, client_url, config) do
url
|> validate(config)
|> do_valid_for_authorization?(client_url)
|> do_valid_for_authorization?(client_url, config)
end

defp do_valid_for_authorization?({:error, _error}, _client_url), do: false
defp do_valid_for_authorization?({:ok, url}, client_url) do
defp do_valid_for_authorization?({:error, _error}, _client_url, _config), do: false
defp do_valid_for_authorization?({:ok, url}, client_url, config) do
client_url
|> String.split()
|> Enum.any?(&matches?(url, &1))
|> Enum.any?(&matches?(url, &1, config))
end

@doc """
Expand Down
8 changes: 4 additions & 4 deletions test/ex_oauth2_provider/redirect_uri_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ defmodule ExOauth2Provider.RedirectURITest do

test "matches?#true" do
uri = "https://app.co/aaa"
assert RedirectURI.matches?(uri, uri)
assert RedirectURI.matches?(uri, uri, [])
end

test "matches?#true with custom match method" do
Expand All @@ -62,15 +62,15 @@ defmodule ExOauth2Provider.RedirectURITest do
end

test "matches?#true ignores query parameter on comparison" do
assert RedirectURI.matches?("https://app.co/?query=hello", "https://app.co/")
assert RedirectURI.matches?("https://app.co/?query=hello", "https://app.co/", [])
end

test "matches?#false" do
refute RedirectURI.matches?("https://app.co/?query=hello", "https://app.co")
refute RedirectURI.matches?("https://app.co/?query=hello", "https://app.co", [])
end

test "matches?#false with domains that doesn't start at beginning" do
refute RedirectURI.matches?("https://app.co/?query=hello", "https://example.com?app.co=test")
refute RedirectURI.matches?("https://app.co/?query=hello", "https://example.com?app.co=test", [])
end

test "valid_for_authorization?#true" do
Expand Down