Skip to content

Commit

Permalink
Merge pull request #2 from silvagustin/2-improvements
Browse files Browse the repository at this point in the history
Ensure module from :config_from is loaded
  • Loading branch information
silvagustin authored Nov 25, 2022
2 parents c331862 + 9722a44 commit 9f8dc31
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 34 deletions.
5 changes: 3 additions & 2 deletions lib/ueberauth/strategy/auth0.ex
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ defmodule Ueberauth.Strategy.Auth0 do

module = option(conn, :oauth2_module)

callback_url = module.authorize_url!(opts, [otp_app: option(conn, :otp_app)])
callback_url = module.authorize_url!(opts, otp_app: option(conn, :otp_app))

redirect!(conn, callback_url)
end
Expand All @@ -132,7 +132,8 @@ defmodule Ueberauth.Strategy.Auth0 do
module = option(conn, :oauth2_module)
redirect_uri = callback_url(conn)

result = module.get_token!([code: code, redirect_uri: redirect_uri], [otp_app: option(conn, :otp_app)])
result =
module.get_token!([code: code, redirect_uri: redirect_uri], otp_app: option(conn, :otp_app))

case result do
{:ok, client} ->
Expand Down
6 changes: 6 additions & 0 deletions lib/ueberauth/strategy/auth0/oauth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ defmodule Ueberauth.Strategy.Auth0.OAuth do
%Plug.Conn{} = conn when not is_nil(configs) ->
with module when not is_nil(module) <-
Keyword.get(configs, :config_from),
{:loaded, {:module, _module}} <- {:loaded, Code.ensure_loaded(module)},
{:exported, true} <- {:exported, function_exported?(module, :get_domain, 1)},
{:exported, true} <- {:exported, function_exported?(module, :get_client_id, 1)},
{:exported, true} <- {:exported, function_exported?(module, :get_client_secret, 1)} do
Expand All @@ -152,6 +153,11 @@ defmodule Ueberauth.Strategy.Auth0.OAuth do
client_secret: apply(module, :get_client_secret, [conn])
)
else
{:loaded, {:error, :nofile}} ->
raise("""
Couldn't load module from `:config_from`
""")

{:exported, false} ->
raise("""
When using `:config_from`, the given module should export 3 functions:
Expand Down
70 changes: 38 additions & 32 deletions test/strategy/auth0_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ defmodule Ueberauth.Strategy.Auth0Test do
assert conn.resp_body =~ ~s|>redirected</a>.</body></html>|
assert conn.resp_body =~ ~s|href="https://example-app.auth0.com/authorize?|
assert conn.resp_body =~ ~s|client_id=clientidsomethingrandom|
assert conn.resp_body =~ ~s|redirect_uri=http%3A%2F%2Fwww.example.com%2Fauth%2Fauth0%2Fcallback|

assert conn.resp_body =~
~s|redirect_uri=http%3A%2F%2Fwww.example.com%2Fauth%2Fauth0%2Fcallback|

assert conn.resp_body =~ ~s|response_type=code|
assert conn.resp_body =~ ~s|scope=openid+profile+email|
assert conn.resp_body =~ ~s|state=#{conn.private[:ueberauth_state_param]}|
Expand All @@ -81,7 +84,10 @@ defmodule Ueberauth.Strategy.Auth0Test do
assert conn.resp_body =~ ~s|connection=facebook|
assert conn.resp_body =~ ~s|login_hint=user|
assert conn.resp_body =~ ~s|screen_hint=signup|
assert conn.resp_body =~ ~s|redirect_uri=http%3A%2F%2Fwww.example.com%2Fauth%2Fauth0%2Fcallback|

assert conn.resp_body =~
~s|redirect_uri=http%3A%2F%2Fwww.example.com%2Fauth%2Fauth0%2Fcallback|

assert conn.resp_body =~ ~s|response_type=code|
assert conn.resp_body =~ ~s|scope=profile+address+phone|
assert conn.resp_body =~ ~s|state=#{conn.private[:ueberauth_state_param]}|
Expand Down Expand Up @@ -148,14 +154,14 @@ defmodule Ueberauth.Strategy.Auth0Test do
|> Plug.Session.call(@session_options)
|> SpecRouter.call(@router)

assert conn.resp_body == "auth0 callback"
assert conn.resp_body == "auth0 callback"

auth = conn.assigns.ueberauth_failure
assert conn.private[:auth0_state] == nil

csrf_attack = %Ueberauth.Failure.Error{
message: "Cross-Site Request Forgery attack",
message_key: "csrf_attack",
message_key: "csrf_attack"
}

assert auth.provider == :auth0
Expand Down Expand Up @@ -290,19 +296,19 @@ defmodule Ueberauth.Strategy.Auth0Test do
|> Plug.Session.call(@session_options)
|> SpecRouter.call(@router)

assert conn.resp_body == "auth0 callback"
assert conn.resp_body == "auth0 callback"

auth = conn.assigns.ueberauth_auth
auth = conn.assigns.ueberauth_auth

# Same information as default token
assert auth.provider == :auth0
assert auth.strategy == Ueberauth.Strategy.Auth0
assert auth.uid == "auth0|lyy5v5utb6n9qfm4ihi3l7pv34po66"
assert conn.private.auth0_state == state
# Same information as default token
assert auth.provider == :auth0
assert auth.strategy == Ueberauth.Strategy.Auth0
assert auth.uid == "auth0|lyy5v5utb6n9qfm4ihi3l7pv34po66"
assert conn.private.auth0_state == state

## Difference here
assert auth.credentials.expires == false
assert auth.credentials.expires_at == nil
## Difference here
assert auth.credentials.expires == false
assert auth.credentials.expires_at == nil
end
end

Expand All @@ -329,18 +335,18 @@ defmodule Ueberauth.Strategy.Auth0Test do
|> Plug.Session.call(@session_options)
|> SpecRouter.call(@router)

assert conn.resp_body == "auth0 callback"
assert conn.resp_body == "auth0 callback"

auth = conn.assigns.ueberauth_failure
auth = conn.assigns.ueberauth_failure

token_unauthorized = %Ueberauth.Failure.Error{
message: "unauthorized_token",
message_key: "OAuth2"
}
token_unauthorized = %Ueberauth.Failure.Error{
message: "unauthorized_token",
message_key: "OAuth2"
}

assert auth.provider == :auth0
assert auth.strategy == Ueberauth.Strategy.Auth0
assert auth.errors == [token_unauthorized]
assert auth.provider == :auth0
assert auth.strategy == Ueberauth.Strategy.Auth0
assert auth.errors == [token_unauthorized]
end
end

Expand All @@ -367,18 +373,18 @@ defmodule Ueberauth.Strategy.Auth0Test do
|> Plug.Session.call(@session_options)
|> SpecRouter.call(@router)

assert conn.resp_body == "auth0 callback"
assert conn.resp_body == "auth0 callback"

auth = conn.assigns.ueberauth_failure
auth = conn.assigns.ueberauth_failure

some_error_in_body = %Ueberauth.Failure.Error{
message: %{"error" => "something_wrong", "error_description" => "Something went wrong"},
message_key: "OAuth2"
}
some_error_in_body = %Ueberauth.Failure.Error{
message: %{"error" => "something_wrong", "error_description" => "Something went wrong"},
message_key: "OAuth2"
}

assert auth.provider == :auth0
assert auth.strategy == Ueberauth.Strategy.Auth0
assert auth.errors == [some_error_in_body]
assert auth.provider == :auth0
assert auth.strategy == Ueberauth.Strategy.Auth0
assert auth.errors == [some_error_in_body]
end
end

Expand Down

0 comments on commit 9f8dc31

Please sign in to comment.