@@ -74,6 +74,26 @@ defmodule Livebook.ZTA.LivebookTeams do
7474 |> halt ( ) , nil }
7575 end
7676
77+ defp handle_request ( conn , team , % { "teams_redirect" => _ , "redirect_to" => redirect_to } ) do
78+ case Teams.Requests . create_auth_request ( team ) do
79+ { :ok , % { "authorize_uri" => authorize_uri } } ->
80+ uri =
81+ authorize_uri
82+ |> URI . new! ( )
83+ |> URI . append_query ( "redirect_to=#{ redirect_to } " )
84+
85+ { conn
86+ |> redirect ( external: URI . to_string ( uri ) )
87+ |> halt ( ) , nil }
88+
89+ { _error_or_transport_error , _reason } ->
90+ { conn
91+ |> put_session ( :teams_error , true )
92+ |> redirect ( to: conn . request_path )
93+ |> halt ( ) , nil }
94+ end
95+ end
96+
7797 defp handle_request ( conn , team , _params ) do
7898 case get_session ( conn ) do
7999 % { "livebook_teams_access_token" => access_token } ->
@@ -100,14 +120,14 @@ defmodule Livebook.ZTA.LivebookTeams do
100120 |> halt ( ) , nil }
101121
102122 _ ->
103- request_user_authentication ( conn , team )
123+ request_user_authentication ( conn )
104124 end
105125 end
106126
107127 defp validate_access_token ( conn , team , access_token ) do
108128 case get_user_info ( team , access_token ) do
109129 { :ok , metadata } -> { conn , metadata }
110- _ -> request_user_authentication ( conn , team )
130+ _ -> request_user_authentication ( conn )
111131 end
112132 end
113133
@@ -118,41 +138,33 @@ defmodule Livebook.ZTA.LivebookTeams do
118138 end
119139 end
120140
121- defp request_user_authentication ( conn , team ) do
122- case Teams.Requests . create_auth_request ( team ) do
123- { :ok , % { "authorize_uri" => authorize_uri } } ->
124- # We have the browser do the redirect because the browser
125- # knows the current page location. Unfortunately, it is quite
126- # complex to know the actual host on the server, because the
127- # user may be running inside a proxy. So in order to make the
128- # feature more accessible, we do the redirecting on the client.
129- conn =
130- html ( conn , """
131- <!DOCTYPE html>
132- <html lang="en">
133- <head>
134- <meta charset="UTF-8">
135- <title>Redirecting...</title>
136- <script>
137- const redirectTo = new URL(window.location.href);
138- redirectTo.searchParams.append("teams_identity", "");
139-
140- const url = new URL("#{ authorize_uri } ");
141- url.searchParams.set("redirect_to", redirectTo.toString());
142- window.location.href = url.toString();
143- </script>
144- </head>
145- </html>
146- """ )
147-
148- { halt ( conn ) , nil }
149-
150- _ ->
151- { conn
152- |> put_session ( :teams_error , true )
153- |> redirect ( to: conn . request_path )
154- |> halt ( ) , nil }
155- end
141+ defp request_user_authentication ( conn ) do
142+ # We have the browser do the redirect because the browser
143+ # knows the current page location. Unfortunately, it is quite
144+ # complex to know the actual host on the server, because the
145+ # user may be running inside a proxy. So in order to make the
146+ # feature more accessible, we do the redirecting on the client.
147+ html_document = """
148+ <!DOCTYPE html>
149+ <html lang="en">
150+ <head>
151+ <meta charset="UTF-8">
152+ <title>Redirecting...</title>
153+ <script>
154+ const redirectTo = new URL(window.location.href);
155+ redirectTo.searchParams.append("teams_identity", "");
156+
157+ const url = new URL(window.location.href);
158+ url.searchParams.set("redirect_to", redirectTo.toString());
159+ url.searchParams.append("teams_redirect", "");
160+
161+ window.location.href = url.toString();
162+ </script>
163+ </head>
164+ </html>
165+ """
166+
167+ { conn |> html ( html_document ) |> halt ( ) , nil }
156168 end
157169
158170 defp get_user_info ( team , access_token ) do
0 commit comments