Skip to content

Commit 3359f65

Browse files
authored
Redirects the user to Teams logout page (#2959)
1 parent 0c58516 commit 3359f65

File tree

6 files changed

+24
-64
lines changed

6 files changed

+24
-64
lines changed

lib/livebook/teams/requests.ex

-9
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,6 @@ defmodule Livebook.Teams.Requests do
237237
get("/api/v1/org/identity", %{access_token: access_token}, team)
238238
end
239239

240-
@doc """
241-
Send a request to Livebook Team API to revoke session from given access token.
242-
"""
243-
@spec logout_identity_provider(Team.t(), String.t()) ::
244-
{:ok, String.t()} | {:error, map()} | {:transport_error, String.t()}
245-
def logout_identity_provider(team, access_token) do
246-
post("/api/v1/org/identity/revoke", %{access_token: access_token}, team)
247-
end
248-
249240
@doc """
250241
Normalizes errors map into errors for the given schema.
251242
"""

lib/livebook/zta/livebook_teams.ex

+11-6
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,19 @@ defmodule Livebook.ZTA.LivebookTeams do
3838
# Our extension to Livebook.ZTA to deal with logouts
3939
def logout(name, conn) do
4040
token = get_session(conn, :livebook_teams_access_token)
41-
4241
team = Livebook.ZTA.get(name)
4342

44-
case Teams.Requests.logout_identity_provider(team, token) do
45-
{:ok, _no_content} -> :ok
46-
{:error, %{}} -> {:error, "You are already logged out."}
47-
{:transport_error, reason} -> {:error, reason}
48-
end
43+
url =
44+
Livebook.Config.teams_url()
45+
|> URI.new!()
46+
|> URI.append_path("/identity/logout")
47+
|> URI.append_query("org_id=#{team.org_id}&access_token=#{token}")
48+
|> URI.to_string()
49+
50+
conn
51+
|> configure_session(renew: true)
52+
|> clear_session()
53+
|> redirect(external: url)
4954
end
5055

5156
defp handle_request(conn, team, %{"teams_identity" => _, "code" => code}) do

lib/livebook_web/controllers/user_controller.ex

+2-6
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,11 @@ defmodule LivebookWeb.UserController do
1717
conn
1818
|> configure_session(renew: true)
1919
|> clear_session()
20-
|> render("logout.html")
20+
|> redirect(to: ~p"/")
2121
end
2222

2323
defp do_zta_logout(conn) do
2424
{_type, module, _key} = Livebook.Config.identity_provider()
25-
26-
case module.logout(LivebookWeb.ZTA, conn) do
27-
:ok -> do_logout(conn)
28-
{:error, reason} -> conn |> redirect(to: ~p"/") |> put_flash(:error, reason)
29-
end
25+
module.logout(LivebookWeb.ZTA, conn)
3026
end
3127
end

lib/livebook_web/controllers/user_html/logout.html.heex

-18
This file was deleted.

test/livebook_teams/zta/livebook_teams_test.exs

+7-4
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,15 @@ defmodule Livebook.ZTA.LivebookTeamsTest do
128128
build_conn(:get, "/")
129129
|> init_test_session(Plug.Conn.get_session(conn))
130130

131-
assert LivebookTeams.logout(test, conn) == :ok
131+
assert %{status: 302} = conn = LivebookTeams.logout(test, conn)
132+
[url] = get_resp_header(conn, "location")
133+
assert %{status: 200} = Req.get!(url)
132134

133-
# Step 5: If we try to revoke again, it should fail
134-
assert {:error, _} = LivebookTeams.logout(test, conn)
135+
# Step 5: It we try to authenticate again, it should redirect to Teams
136+
conn =
137+
build_conn(:get, "/")
138+
|> init_test_session(Plug.Conn.get_session(conn))
135139

136-
# Step 6: It we try to authenticate again, it should redirect to Teams
137140
{conn, nil} = LivebookTeams.authenticate(test, conn, [])
138141
assert conn.halted
139142
assert html_response(conn, 200) =~ "window.location.href = "

test/livebook_web/controllers/user_controller_test.exs

+4-21
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,11 @@ defmodule LivebookWeb.UserControllerTest do
22
use LivebookWeb.ConnCase, async: true
33

44
describe "GET /logout" do
5-
test "renders logout template when logged in", %{conn: conn} do
6-
conn = login_user(conn)
7-
8-
conn = get(conn, ~p"/logout")
9-
10-
assert html_response(conn, 200) =~ "You have been logged out"
11-
end
12-
135
test "redirects when already logged out", %{conn: conn} do
14-
conn = logout_user(conn)
15-
16-
conn = get(conn, ~p"/logout")
17-
18-
assert redirected_to(conn) == ~p"/"
19-
end
20-
21-
defp login_user(conn) do
22-
Phoenix.ConnTest.init_test_session(conn, %{user_id: 1})
23-
end
24-
25-
defp logout_user(conn) do
26-
Phoenix.ConnTest.init_test_session(conn, %{})
6+
assert conn
7+
|> Phoenix.ConnTest.init_test_session(%{})
8+
|> get(~p"/logout")
9+
|> redirected_to() == ~p"/"
2710
end
2811
end
2912
end

0 commit comments

Comments
 (0)