From 9722a441beb4a82dd63d2052639b1d969069d17b Mon Sep 17 00:00:00 2001 From: Agustin Silva Date: Fri, 25 Nov 2022 17:12:36 -0300 Subject: [PATCH] Ensure module from :config_from is loaded --- lib/ueberauth/strategy/auth0.ex | 5 +- lib/ueberauth/strategy/auth0/oauth.ex | 6 +++ test/strategy/auth0_test.exs | 70 +++++++++++++++------------ 3 files changed, 47 insertions(+), 34 deletions(-) diff --git a/lib/ueberauth/strategy/auth0.ex b/lib/ueberauth/strategy/auth0.ex index 453bd6a..dd8ded8 100644 --- a/lib/ueberauth/strategy/auth0.ex +++ b/lib/ueberauth/strategy/auth0.ex @@ -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 @@ -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} -> diff --git a/lib/ueberauth/strategy/auth0/oauth.ex b/lib/ueberauth/strategy/auth0/oauth.ex index 3c54374..c946530 100644 --- a/lib/ueberauth/strategy/auth0/oauth.ex +++ b/lib/ueberauth/strategy/auth0/oauth.ex @@ -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 @@ -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: diff --git a/test/strategy/auth0_test.exs b/test/strategy/auth0_test.exs index a11f6c8..1fab97a 100644 --- a/test/strategy/auth0_test.exs +++ b/test/strategy/auth0_test.exs @@ -57,7 +57,10 @@ defmodule Ueberauth.Strategy.Auth0Test do assert conn.resp_body =~ ~s|>redirected.| 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]}| @@ -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]}| @@ -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 @@ -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 @@ -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 @@ -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