Skip to content

Commit 32e89cf

Browse files
aleDszjonatanklosko
authored andcommitted
Fix ets deletion when token is invalid/expired (#3164)
1 parent 89c380e commit 32e89cf

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

lib/livebook/zta/livebook_teams.ex

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,19 +169,19 @@ defmodule Livebook.ZTA.LivebookTeams do
169169
defp validate_access_token(name, conn, team, access_token) do
170170
node = get_session(conn, :livebook_teams_metadata_node)
171171

172+
entry =
173+
try do
174+
:erpc.call(node, :ets, :lookup_element, [name, access_token, 2, nil])
175+
catch
176+
_, _ -> nil
177+
end
178+
172179
case Teams.Requests.get_user_info(team, access_token) do
173180
{:ok, payload} ->
174181
{conn, build_metadata(team.id, payload)}
175182

176183
:econnrefused ->
177-
data =
178-
try do
179-
:erpc.call(node, :ets, :lookup_element, [name, access_token, 2, nil])
180-
catch
181-
_, _ -> nil
182-
end
183-
184-
case {System.os_time(:second), data} do
184+
case {System.os_time(:second), entry} do
185185
{current_timestamp, {exp, metadata}} when current_timestamp <= exp ->
186186
{conn, metadata}
187187

@@ -196,7 +196,7 @@ defmodule Livebook.ZTA.LivebookTeams do
196196
end
197197

198198
_otherwise ->
199-
:ets.delete(__MODULE__, access_token)
199+
entry && :erpc.call(node, :ets, :delete, [name, access_token])
200200
request_user_authentication(conn)
201201
end
202202
end

test/livebook_teams/zta/livebook_teams_test.exs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,20 @@ defmodule Livebook.ZTA.LivebookTeamsTest do
9292
assert html_response(conn, 403) =~
9393
"Failed to authenticate with Livebook Teams: you do not belong to this org"
9494
end
95+
96+
test "deletes the cache if access token is invalid",
97+
%{test: test, node: node, team: team} do
98+
{conn, code} = authenticate_user_on_teams(test, node, team)
99+
access_token = get_session(conn, :livebook_teams_access_token)
100+
metadata_node = get_session(conn, :livebook_teams_metadata_node)
101+
102+
TeamsRPC.revoke_auth_request(node, code)
103+
assert :erpc.call(metadata_node, :ets, :lookup_element, [test, access_token, 2, nil])
104+
105+
assert {%{halted: true} = conn, nil} = LivebookTeams.authenticate(test, conn, [])
106+
assert html_response(conn, 200) =~ "window.location.href = "
107+
refute :erpc.call(metadata_node, :ets, :lookup_element, [test, access_token, 2, nil])
108+
end
95109
end
96110

97111
describe "logout/2" do
@@ -126,10 +140,10 @@ defmodule Livebook.ZTA.LivebookTeamsTest do
126140
@moduletag subscribe_to_hubs_topics: [:connection]
127141
@moduletag subscribe_to_teams_topics: [:clients, :agents]
128142

129-
test "uses cached version of the identity payload", %{test: test, team: team, node: node} do
130-
start_supervised!({LivebookTeams, name: test, identity_key: team.id})
131-
{conn, code} = authenticate_user_on_teams(test, node, team)
143+
setup :livebook_teams_auth
132144

145+
test "uses cached version of the identity payload",
146+
%{conn: conn, test: test, node: node, code: code} do
133147
id = conn.assigns.current_user.id
134148
access_token = get_session(conn, :livebook_teams_access_token)
135149
groups = [%{"provider_id" => "1", "group_name" => "Foo"}]

test/support/integration/teams_rpc.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ defmodule Livebook.TeamsRPC do
257257
:erpc.call(node, TeamsRPC, :allow_auth_request, [token])
258258
end
259259

260+
def revoke_auth_request(node, code) do
261+
:erpc.call(node, TeamsRPC, :revoke_auth_request, [code])
262+
end
263+
260264
def toggle_teams_authentication(node, deployment_group) do
261265
:erpc.call(node, TeamsRPC, :toggle_teams_authentication, [deployment_group])
262266
end

0 commit comments

Comments
 (0)