Skip to content

Commit

Permalink
Merge branch 'bc2' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
macifell committed Dec 8, 2023
2 parents b964c97 + 4668765 commit 5951cf1
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 63 deletions.
18 changes: 9 additions & 9 deletions lib/recognizer/bigcommerce.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ defmodule Recognizer.BigCommerce do
end
end

def generate_login_jwt(user) do
def login_redirect_uri(user) do
config(:login_uri) <> generate_login_jwt(user)
end

def logout_redirect_uri() do
config(:logout_uri)
end

defp generate_login_jwt(user) do
{:ok, token, _claims} =
user
|> Recognizer.Repo.preload(:bigcommerce_user)
Expand All @@ -36,14 +44,6 @@ defmodule Recognizer.BigCommerce do
token
end

def login_redirect_uri(jwt) do
config(:login_uri) <> jwt
end

def logout_redirect_uri() do
config(:logout_uri)
end

defp jwt_claims(user) do
%{
"aud" => "BigCommerce",
Expand Down
31 changes: 10 additions & 21 deletions lib/recognizer_web/authentication.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,13 @@ defmodule RecognizerWeb.Authentication do
|> redirect(to: Routes.prompt_two_factor_path(conn, :new))

{:ok, _user} ->
if BigCommerce.enabled?() && get_session(conn, :bc) do
log_in_bc_user(conn, user, params)
else
redirect_opts = login_redirect(conn)

conn
|> clear_session()
|> Guardian.Plug.sign_in(user, params)
|> redirect(redirect_opts)
end
end
end

defp log_in_bc_user(conn, user, params) do
jwt = BigCommerce.generate_login_jwt(user)
redirect_opts = login_redirect(conn, user)

conn
|> clear_session()
|> Guardian.Plug.sign_in(user, params)
|> put_session(:bc, true)
|> redirect(external: BigCommerce.login_redirect_uri(jwt))
conn
|> clear_session()
|> Guardian.Plug.sign_in(user, params)
|> redirect(redirect_opts)
end
end

@doc """
Expand Down Expand Up @@ -96,8 +82,11 @@ defmodule RecognizerWeb.Authentication do
@doc """
The URL to redirect the user to after authentication is done.
"""
def login_redirect(conn) do
def login_redirect(conn, user) do
cond do
get_session(conn, :bc) ->
[external: BigCommerce.login_redirect_uri(user)]

get_session(conn, :user_return_to) ->
[to: get_session(conn, :user_return_to)]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ defmodule RecognizerWeb.Accounts.UserRegistrationController do
]
when action in [:create]

def new(conn, %{"bc" => "true"} = params) do
conn
|> put_session(:bc, true)
|> new(Map.drop(params, ["bc"]))
end

def new(conn, params) do
user_params = Map.get(params, "user", %{})
changeset = Accounts.change_user_registration(%User{}, user_params)
Expand Down
20 changes: 5 additions & 15 deletions lib/recognizer_web/controllers/accounts/user_session_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ defmodule RecognizerWeb.Accounts.UserSessionController do
alias Recognizer.Accounts
alias RecognizerWeb.Authentication

def new(conn, %{"bc" => "true"}) do
conn
|> put_session(:bc, true)
|> render("new.html", error_message: nil)
end

def new(conn, _params) do
render(conn, "new.html", error_message: nil)
end
Expand Down Expand Up @@ -39,15 +33,11 @@ defmodule RecognizerWeb.Accounts.UserSessionController do
end
end

def delete(conn, %{"bc" => "true"}) do
conn
|> put_session(:bc, true)
|> Authentication.log_out_user()
end

def delete(conn, _params) do
conn
|> Authentication.conditional_flash(:info, "Logged out successfully.")
|> Authentication.log_out_user()
if !get_session(conn, :bc) do
Authentication.conditional_flash(conn, :info, "Logged out successfully.")
end

Authentication.log_out_user(conn)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ defmodule RecognizerWeb.Accounts.UserSettingsController do

plug :assign_email_and_password_changesets

def edit(conn, %{"bc" => "true"}) do
conn
|> put_session(:bc, true)
|> edit(%{})
end

def edit(conn, _params) do
cond do
get_session(conn, :bc) ->
Expand Down
2 changes: 1 addition & 1 deletion lib/recognizer_web/controllers/fallback_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule RecognizerWeb.FallbackController do
@impl Guardian.Plug.ErrorHandler
def auth_error(conn, {:already_authenticated, _reason}, _) do
conn
|> redirect(Authentication.login_redirect(conn))
|> redirect(Authentication.login_redirect(conn, Authentication.fetch_current_user(conn)))
|> halt()
end

Expand Down
24 changes: 19 additions & 5 deletions lib/recognizer_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,22 @@ defmodule RecognizerWeb.Router do
plug Guardian.Plug.EnsureNotAuthenticated
end

pipeline :bc do
plug :add_bc_to_session
end

defp add_bc_to_session(%{query_params: %{"bc" => "true"}} = conn, _opts) do
if Recognizer.BigCommerce.enabled?() do
put_session(conn, :bc, true)
end
end

defp add_bc_to_session(conn, _opts) do
conn
end

scope "/", RecognizerWeb do
pipe_through :browser
pipe_through [:bc, :browser]

get "/", HomepageController, :index

Expand All @@ -56,7 +70,7 @@ defmodule RecognizerWeb.Router do
end

scope "/", RecognizerWeb.OauthProvider, as: :oauth do
pipe_through [:browser, :auth, :user]
pipe_through [:browser, :bc, :auth, :user]

get "/oauth/authorize", AuthorizeController, :new
get "/oauth/authorize/:code", AuthorizeController, :show
Expand All @@ -65,7 +79,7 @@ defmodule RecognizerWeb.Router do
end

scope "/", RecognizerWeb.Accounts do
pipe_through [:browser, :auth, :guest]
pipe_through [:browser, :bc, :auth, :guest]

get "/create-account", UserRegistrationController, :new
post "/create-account", UserRegistrationController, :create
Expand All @@ -90,7 +104,7 @@ defmodule RecognizerWeb.Router do
end

scope "/", RecognizerWeb.Accounts.Prompt, as: :prompt do
pipe_through [:browser, :auth, :guest]
pipe_through [:browser, :bc, :auth, :guest]

get "/prompt/update-password", PasswordChangeController, :edit
put "/prompt/update-password", PasswordChangeController, :update
Expand All @@ -105,7 +119,7 @@ defmodule RecognizerWeb.Router do
end

scope "/", RecognizerWeb.Accounts do
pipe_through [:browser, :auth, :user]
pipe_through [:browser, :bc, :auth, :user]

get "/settings", UserSettingsController, :edit
put "/settings", UserSettingsController, :update
Expand Down

0 comments on commit 5951cf1

Please sign in to comment.