Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Refactor Google OAuth callbacks (#6396)
Adrian Gruntkowski35fa0c4ed005
diff --git a/lib/plausible/google/api.ex b/lib/plausible/google/api.exindex c1ab562d3d..60945d2957 100644--- a/lib/plausible/google/api.ex+++ b/lib/plausible/google/api.ex@@ -16,14 +16,37 @@ defmodule Plausible.Google.API do@verified_permission_levels ["siteOwner", "siteFullUser", "siteRestrictedUser"]- def search_console_authorize_url(site_id) do+ @valid_oauth_contexts ["search-console", "import"]++ @oauth_salt "google-oauth-state"++ def search_console_authorize_url(site) do"https://accounts.google.com/o/oauth2/v2/auth?client_id=#{client_id()}&redirect_uri=#{redirect_uri()}&prompt=consent&response_type=code&access_type=offline&scope=#{@search_console_scope}&state=" <>- Jason.encode!([site_id, "search-console"])+ sign_oauth_state(site, "search-console")end- def import_authorize_url(site_id) do+ def import_authorize_url(site) do"https://accounts.google.com/o/oauth2/v2/auth?client_id=#{client_id()}&redirect_uri=#{redirect_uri()}&prompt=consent&response_type=code&access_type=offline&scope=#{@import_scope}&state=" <>- Jason.encode!([site_id, "import"])+ sign_oauth_state(site, "import")+ end++ @doc false+ def sign_oauth_state(site, context) do+ Phoenix.Token.sign(PlausibleWeb.Endpoint, @oauth_salt, [site.id, context])+ end++ def verify_oauth_state(state) do+ case Phoenix.Token.verify(PlausibleWeb.Endpoint, @oauth_salt, state, max_age: 3600) do+ {:ok, [site_id, context]} when context in @valid_oauth_contexts ->+ if site = Plausible.Repo.get(Plausible.Site, site_id) do+ {:ok, %{site: site, context: context}}+ else+ {:error, {:invalid_oauth_state, :missing_site}}+ end++ {:error, token_error} ->+ {:error, {:invalid_oauth_state, token_error}}+ endenddef fetch_access_token!(code) dodiff --git a/lib/plausible/google/http.ex b/lib/plausible/google/http.exindex 4666cd0a44..843f1dc1e8 100644--- a/lib/plausible/google/http.ex+++ b/lib/plausible/google/http.ex@@ -1,4 +1,8 @@defmodule Plausible.Google.HTTP do+ @moduledoc """+ HTTP client implementation for Google APIs.+ """+require Loggeralias Plausible.HTTPClient@@ -34,7 +38,7 @@ defmodule Plausible.Google.HTTP doredirect_uri: redirect_uri()}- {:ok, response} = HTTPClient.post(url, headers, params)+ {:ok, response} = HTTPClient.impl().post(url, headers, params)response.bodyenddiff --git a/lib/plausible/site/google_auth.ex b/lib/plausible/site/google_auth.exindex d19b9d5a95..29ea929124 100644--- a/lib/plausible/site/google_auth.ex+++ b/lib/plausible/site/google_auth.ex@@ -1,4 +1,8 @@defmodule Plausible.Site.GoogleAuth do+ @moduledoc """+ Struct for storing google auth token info used by search console.+ """+use Ecto.Schemaimport Ecto.Changeset@@ -17,8 +21,8 @@ defmodule Plausible.Site.GoogleAuth dodef changeset(auth, attrs \\ %{}) doauth- |> cast(attrs, [:refresh_token, :access_token, :expires, :email, :user_id, :site_id])- |> validate_required([:refresh_token, :access_token, :expires, :email, :user_id, :site_id])+ |> cast(attrs, [:refresh_token, :access_token, :expires, :email])+ |> validate_required([:refresh_token, :access_token, :expires, :email])|> unique_constraint(:site)enddiff --git a/lib/plausible_web/controllers/auth_controller.ex b/lib/plausible_web/controllers/auth_controller.exindex 5dc6c25134..5b96db1015 100644--- a/lib/plausible_web/controllers/auth_controller.ex+++ b/lib/plausible_web/controllers/auth_controller.ex@@ -615,86 +615,139 @@ defmodule PlausibleWeb.AuthController doenddef google_auth_callback(conn, %{"error" => error, "state" => state} = params) do- [site_id, redirected_to | _] = Jason.decode!(state)+ case Plausible.Google.API.verify_oauth_state(state) do+ {:ok, %{site: site, context: context}} ->+ redirect_url =+ if context == "import" do+ Routes.site_path(conn, :settings_imports_exports, site.domain)+ else+ Routes.site_path(conn, :settings_integrations, site.domain)+ end- site = Repo.get(Plausible.Site, site_id)+ cond do+ error == "access_denied" ->+ conn+ |> put_flash(+ :error,+ "We were unable to authenticate your Google Analytics account. Please check that you have granted us permission to 'See and download your Google Analytics data' and try again."+ )+ |> redirect(to: redirect_url)- redirect_route =- if redirected_to == "import" do- Routes.site_path(conn, :settings_imports_exports, site.domain)- else- Routes.site_path(conn, :settings_integrations, site.domain)- end+ error in ["server_error", "temporarily_unavailable"] ->+ conn+ |> put_flash(+ :error,+ "We are unable to authenticate your Google Analytics account because Google's authentication service is temporarily unavailable. Please try again in a few moments."+ )+ |> redirect(to: redirect_url)- case error do- "access_denied" ->- conn- |> put_flash(- :error,- "We were unable to authenticate your Google Analytics account. Please check that you have granted us permission to 'See and download your Google Analytics data' and try again."- )- |> redirect(to: redirect_route)+ true ->+ Sentry.capture_message("Google OAuth callback failed. Params: #{inspect(params)}")+ generic_oauth_error_response(conn, redirect_url)+ end- message when message in ["server_error", "temporarily_unavailable"] ->- conn- |> put_flash(- :error,- "We are unable to authenticate your Google Analytics account because Google's authentication service is temporarily unavailable. Please try again in a few moments."+ {:error, reason} ->+ Sentry.capture_message(+ "Google OAuth callback failed. Reason: #{inspect(reason)}. Params: #{inspect(params)}")- |> redirect(to: redirect_route)- _any ->- Sentry.capture_message("Google OAuth callback failed. Reason: #{inspect(params)}")-- conn- |> put_flash(- :error,- "We were unable to authenticate your Google Analytics account. If the problem persists, please contact support for assistance."- )- |> redirect(to: redirect_route)+ generic_oauth_error_response(conn)endend- def google_auth_callback(conn, %{"code" => code, "state" => state}) do- res = Plausible.Google.API.fetch_access_token!(code)+ def google_auth_callback(conn, %{"code" => code, "state" => state} = params) do+ current_user = conn.assigns[:current_user]- [site_id, redirect_to | _] = Jason.decode!(state)+ with {:ok, %{site: site, context: context}} <- Plausible.Google.API.verify_oauth_state(state),+ :ok <- check_callback_site_permission(site, current_user) do+ token_data = Plausible.Google.API.fetch_access_token!(code)+ expires_in = Map.fetch!(token_data, "expires_in")+ expires_at = NaiveDateTime.add(NaiveDateTime.utc_now(), expires_in)- site = Repo.get(Plausible.Site, site_id)- expires_at = NaiveDateTime.add(NaiveDateTime.utc_now(), res["expires_in"])+ if context == "import" do+ google_import_callback(conn, site, token_data, expires_at)+ else+ google_search_console_callback(conn, site, token_data, expires_at)+ end+ else+ {:error, reason} ->+ Sentry.capture_message(+ "Google OAuth callback failed. Reason: #{inspect(reason)}. Params: #{inspect(params)}"+ )- case redirect_to do- "import" ->- redirect(conn,- to:- Routes.google_analytics_path(conn, :property_form, site.domain,- access_token: res["access_token"],- refresh_token: res["refresh_token"],- expires_at: NaiveDateTime.to_iso8601(expires_at)- )+ generic_oauth_error_response(conn)+ end+ end++ defp google_import_callback(conn, site, token_data, expires_at) do+ redirect(conn,+ to:+ Routes.google_analytics_path(conn, :property_form, site.domain,+ access_token: Map.fetch!(token_data, "access_token"),+ refresh_token: Map.fetch!(token_data, "refresh_token"),+ expires_at: NaiveDateTime.to_iso8601(expires_at))+ )+ end- _ ->- id_token = res["id_token"]- [_, body, _] = String.split(id_token, ".")- id = body |> Base.decode64!(padding: false) |> Jason.decode!()+ defp google_search_console_callback(conn, site, token_data, expires_at) do+ current_user = conn.assigns.current_user- Plausible.Site.GoogleAuth.changeset(%Plausible.Site.GoogleAuth{}, %{- email: id["email"],- refresh_token: res["refresh_token"],- access_token: res["access_token"],+ id_token = Map.fetch!(token_data, "id_token")+ [_, body, _] = String.split(id_token, ".")+ id = body |> Base.decode64!(padding: false) |> Jason.decode!()+ email = Map.fetch!(id, "email")+ refresh_token = Map.fetch!(token_data, "refresh_token")+ access_token = Map.fetch!(token_data, "access_token")++ Plausible.Site.GoogleAuth.changeset(%Plausible.Site.GoogleAuth{}, %{+ email: email,+ refresh_token: refresh_token,+ access_token: access_token,+ expires: expires_at,+ user_id: current_user.id+ })+ |> Ecto.Changeset.put_assoc(:site, site)+ |> Ecto.Changeset.put_assoc(:user, current_user)+ |> Repo.insert!(+ on_conflict: [+ set: [+ email: email,+ refresh_token: refresh_token,+ access_token: access_token,expires: expires_at,- user_id: conn.assigns[:current_user].id,- site_id: site_id- })- |> Repo.insert!()+ user_id: current_user.id,+ updated_at: NaiveDateTime.utc_now(:second),+ property: nil+ ]+ ],+ conflict_target: :site_id+ )- site = Repo.get(Plausible.Site, site_id)+ redirect(conn, to: Routes.site_path(conn, :settings_integrations, site.domain))+ end- redirect(conn, to: Routes.site_path(conn, :settings_integrations, site.domain))+ defp check_callback_site_permission(site, current_user) do+ if Plausible.Sites.get_for_user(current_user, site.domain,+ roles: [:owner, :admin, :super_admin]+ ) do+ :ok+ else+ {:error, :permission_denied}endend+ defp generic_oauth_error_response(conn, redirect_url \\ nil) do+ redirect_url = redirect_url || Routes.auth_path(conn, :login_form)++ conn+ |> put_flash(+ :error,+ "We were unable to authenticate your Google Analytics account. If the problem persists, please contact support for assistance."+ )+ |> redirect(to: redirect_url)+ end+defp redirect_to_login(conn) doredirect(conn, to: Routes.auth_path(conn, :login_form))enddiff --git a/lib/plausible_web/live/imports_exports_settings.ex b/lib/plausible_web/live/imports_exports_settings.exindex 945e5b1bf7..637d36cce2 100644--- a/lib/plausible_web/live/imports_exports_settings.ex+++ b/lib/plausible_web/live/imports_exports_settings.ex@@ -92,7 +92,7 @@ defmodule PlausibleWeb.Live.ImportsExportsSettings do<div class="flex gap-x-4 mt-4"><.button_linktheme="secondary"- href={Plausible.Google.API.import_authorize_url(@site.id)}+ href={Plausible.Google.API.import_authorize_url(@site)}disabled={@import_in_progress? or @at_maximum?}mt?={false}>@@ -116,7 +116,7 @@ defmodule PlausibleWeb.Live.ImportsExportsSettings do<div class="flex gap-x-4"><.button_linktheme="secondary"- href={Plausible.Google.API.import_authorize_url(@site.id)}+ href={Plausible.Google.API.import_authorize_url(@site)}disabled={@import_in_progress? or @at_maximum?}mt?={false}>diff --git a/lib/plausible_web/templates/site/settings_integrations.html.heex b/lib/plausible_web/templates/site/settings_integrations.html.heexindex 49e5594a13..55d891d08d 100644--- a/lib/plausible_web/templates/site/settings_integrations.html.heex+++ b/lib/plausible_web/templates/site/settings_integrations.html.heex@@ -89,7 +89,7 @@<% else %><PlausibleWeb.Components.Google.buttonid="search-console-connect"- to={Plausible.Google.API.search_console_authorize_url(@site.id)}+ to={Plausible.Google.API.search_console_authorize_url(@site)}/><div class="mt-8 text-sm">NB: You also need to set up your site ondiff --git a/test/plausible_web/controllers/auth_controller_test.exs b/test/plausible_web/controllers/auth_controller_test.exsindex 501183e41c..2f4de856aa 100644--- a/test/plausible_web/controllers/auth_controller_test.exs+++ b/test/plausible_web/controllers/auth_controller_test.exs@@ -1137,9 +1137,18 @@ defmodule PlausibleWeb.AuthControllerTest doenddescribe "GET /auth/google/callback" do- test "shows error and redirects back to settings when authentication fails", %{conn: conn} do- site = insert(:site)- callback_params = %{"error" => "access_denied", "state" => "[#{site.id},\"import\"]"}+ setup [:create_user, :log_in]++ test "shows access denied error and redirects back to settings when authentication fails for import",+ %{+ conn: conn+ } do+ site = new_site()++ state =+ Phoenix.Token.sign(PlausibleWeb.Endpoint, "google-oauth-state", [site.id, "import"])++ callback_params = %{"error" => "access_denied", "state" => state}conn = get(conn, Routes.auth_path(conn, :google_auth_callback), callback_params)assert redirected_to(conn, 302) ==@@ -1148,6 +1157,282 @@ defmodule PlausibleWeb.AuthControllerTest doassert Phoenix.Flash.get(conn.assigns.flash, :error) =~"unable to authenticate your Google Analytics"end++ test "shows access denied error and redirects back to settings when authentication fails for search console",+ %{conn: conn} do+ site = new_site()++ state =+ Phoenix.Token.sign(PlausibleWeb.Endpoint, "google-oauth-state", [+ site.id,+ "search-console"+ ])++ callback_params = %{"error" => "access_denied", "state" => state}+ conn = get(conn, Routes.auth_path(conn, :google_auth_callback), callback_params)++ assert redirected_to(conn, 302) ==+ Routes.site_path(conn, :settings_integrations, site.domain)++ assert Phoenix.Flash.get(conn.assigns.flash, :error) =~+ "unable to authenticate your Google Analytics"+ end++ test "shows server error and redirects back to settings when request fails on Google's end for import",+ %{+ conn: conn+ } do+ site = new_site()++ state =+ Phoenix.Token.sign(PlausibleWeb.Endpoint, "google-oauth-state", [site.id, "import"])++ callback_params = %{"error" => "server_error", "state" => state}+ conn = get(conn, Routes.auth_path(conn, :google_auth_callback), callback_params)++ assert redirected_to(conn, 302) ==+ Routes.site_path(conn, :settings_imports_exports, site.domain)++ assert Phoenix.Flash.get(conn.assigns.flash, :error) =~+ "Google's authentication service is temporarily unavailable"+ end++ test "shows server error and redirects back to settings when request fails on Google's end for search console",+ %{+ conn: conn+ } do+ site = new_site()++ state =+ Phoenix.Token.sign(PlausibleWeb.Endpoint, "google-oauth-state", [+ site.id,+ "search-console"+ ])++ callback_params = %{"error" => "temporarily_unavailable", "state" => state}+ conn = get(conn, Routes.auth_path(conn, :google_auth_callback), callback_params)++ assert redirected_to(conn, 302) ==+ Routes.site_path(conn, :settings_integrations, site.domain)++ assert Phoenix.Flash.get(conn.assigns.flash, :error) =~+ "Google's authentication service is temporarily unavailable"+ end++ test "shows generic error and redirects back to settings on unknown Google failure for import",+ %{+ conn: conn+ } do+ site = new_site()++ state =+ Phoenix.Token.sign(PlausibleWeb.Endpoint, "google-oauth-state", [site.id, "import"])++ callback_params = %{"error" => "unknown", "state" => state}+ conn = get(conn, Routes.auth_path(conn, :google_auth_callback), callback_params)++ assert redirected_to(conn, 302) ==+ Routes.site_path(conn, :settings_imports_exports, site.domain)++ assert Phoenix.Flash.get(conn.assigns.flash, :error) =~+ "unable to authenticate your Google Analytics"+ end++ test "shows generic error and redirects back to settings when request fails on Google's end for search console",+ %{+ conn: conn+ } do+ site = new_site()++ state =+ Phoenix.Token.sign(PlausibleWeb.Endpoint, "google-oauth-state", [+ site.id,+ "search-console"+ ])++ callback_params = %{"error" => "unknown", "state" => state}+ conn = get(conn, Routes.auth_path(conn, :google_auth_callback), callback_params)++ assert redirected_to(conn, 302) ==+ Routes.site_path(conn, :settings_integrations, site.domain)++ assert Phoenix.Flash.get(conn.assigns.flash, :error) =~+ "unable to authenticate your Google Analytics"+ end++ test "rejects error callback with tampered state", %{conn: conn} do+ site = new_site()++ state =+ Phoenix.Token.sign(PlausibleWeb.Endpoint, "google-oauth-state", [site.id, "import"])++ callback_params = %{"error" => "access_denied", "state" => state <> "gibberish"}+ conn = get(conn, Routes.auth_path(conn, :google_auth_callback), callback_params)++ assert redirected_to(conn, 302) == Routes.auth_path(conn, :login_form)+ end++ test "rejects error callback with signed state containing non-existent site", %{conn: conn} do+ site = new_site()+ Repo.delete!(site)++ state =+ Phoenix.Token.sign(PlausibleWeb.Endpoint, "google-oauth-state", [site.id, "import"])++ callback_params = %{"error" => "access_denied", "state" => state}+ conn = get(conn, Routes.auth_path(conn, :google_auth_callback), callback_params)++ assert redirected_to(conn, 302) == Routes.auth_path(conn, :login_form)+ end++ test "rejects success callback with tampered state", %{conn: conn} do+ site = new_site()++ state =+ Phoenix.Token.sign(PlausibleWeb.Endpoint, "google-oauth-state", [site.id, "import"])++ callback_params = %{"code" => "CodeForToken", "state" => state <> "gibberish"}+ conn = get(conn, Routes.auth_path(conn, :google_auth_callback), callback_params)++ assert redirected_to(conn, 302) == Routes.auth_path(conn, :login_form)+ end++ test "rejects success callback with signed state containing non-existent site", %{conn: conn} do+ site = new_site()+ Repo.delete!(site)++ state =+ Phoenix.Token.sign(PlausibleWeb.Endpoint, "google-oauth-state", [site.id, "import"])++ callback_params = %{"code" => "CodeForToken", "state" => state}+ conn = get(conn, Routes.auth_path(conn, :google_auth_callback), callback_params)++ assert redirected_to(conn, 302) == Routes.auth_path(conn, :login_form)+ end++ test "success callback rejects state pointing to a site the current user does not own",+ %{conn: conn} do+ victim_site = new_site()++ attacker = new_user()+ {:ok, conn: attacker_conn} = log_in(%{user: attacker, conn: conn})++ # craft a validly signed state pointing at the victim's site_id+ state =+ Phoenix.Token.sign(PlausibleWeb.Endpoint, "google-oauth-state", [+ victim_site.id,+ "search-console"+ ])++ callback_params = %{"code" => "CodeForToken", "state" => state}++ attacker_conn =+ get(attacker_conn, Routes.auth_path(conn, :google_auth_callback), callback_params)++ assert redirected_to(attacker_conn, 302) == Routes.auth_path(conn, :login_form)++ # confirm no GoogleAuth record was created for the victim's site+ refute Repo.exists?(+ from ga in Plausible.Site.GoogleAuth, where: ga.site_id == ^victim_site.id+ )+ end++ test "success callback for import redirects to property selection", %{conn: conn, user: user} do+ mock_google_access_token(user.email)++ site = new_site(owner: user)++ state =+ Phoenix.Token.sign(PlausibleWeb.Endpoint, "google-oauth-state", [site.id, "import"])++ callback_params = %{"code" => "CodeForToken", "state" => state}+ conn = get(conn, Routes.auth_path(conn, :google_auth_callback), callback_params)++ assert redirect_url = redirected_to(conn, 302)++ assert redirect_url =~ "/import/google-analytics/property"+ assert redirect_url =~ "access_token=SomeAccessToken"+ assert redirect_url =~ "refresh_token=SomeRefreshToken"+ assert redirect_url =~ "expires_at="+ end++ test "success callback for search console creates a google auth entry and redirects to integrations form",+ %{conn: conn, user: user} do+ site = new_site(owner: user)++ mock_google_access_token(user.email)++ state =+ Phoenix.Token.sign(PlausibleWeb.Endpoint, "google-oauth-state", [+ site.id,+ "search-console"+ ])++ callback_params = %{"code" => "CodeForToken", "state" => state}+ conn = get(conn, Routes.auth_path(conn, :google_auth_callback), callback_params)++ assert redirected_to(conn, 302) ==+ Routes.site_path(conn, :settings_integrations, site.domain)++ assert_matches %{+ access_token: "SomeAccessToken",+ refresh_token: "SomeRefreshToken",+ user_id: ^user.id,+ email: ^user.email,+ expires: %NaiveDateTime{}+ } =+ Repo.one(+ from ga in Plausible.Site.GoogleAuth, where: ga.site_id == ^site.id+ )+ end++ test "success callback for search console handles repeat callback for the same site gracefully",+ %{conn: conn, user: user} do+ site = new_site(owner: user)+ another_email = new_user().email++ mock_google_access_token(user.email)+ mock_google_access_token(another_email)++ state =+ Phoenix.Token.sign(PlausibleWeb.Endpoint, "google-oauth-state", [+ site.id,+ "search-console"+ ])++ callback_params = %{"code" => "CodeForToken", "state" => state}+ conn_first = get(conn, Routes.auth_path(conn, :google_auth_callback), callback_params)++ assert redirected_to(conn_first, 302) ==+ Routes.site_path(conn, :settings_integrations, site.domain)++ assert_matches %{+ access_token: "SomeAccessToken",+ refresh_token: "SomeRefreshToken",+ user_id: ^user.id,+ email: ^user.email,+ expires: %NaiveDateTime{}+ } =+ Repo.one(+ from ga in Plausible.Site.GoogleAuth, where: ga.site_id == ^site.id+ )++ conn_second = get(conn, Routes.auth_path(conn, :google_auth_callback), callback_params)++ assert redirected_to(conn_second, 302) ==+ Routes.site_path(conn, :settings_integrations, site.domain)++ assert_matches %{+ access_token: "SomeAccessToken",+ refresh_token: "SomeRefreshToken",+ user_id: ^user.id,+ email: ^another_email,+ expires: %NaiveDateTime{}+ } =+ Repo.one(+ from ga in Plausible.Site.GoogleAuth, where: ga.site_id == ^site.id+ )+ endenddescribe "POST /2fa/setup/initiate" do@@ -1860,4 +2145,29 @@ defmodule PlausibleWeb.AuthControllerTest doend)end++ defp mock_google_access_token(email) do+ body =+ %{"email" => email}+ |> Jason.encode!()+ |> Base.encode64(padding: false)++ expect(+ Plausible.HTTPClient.Mock,+ :post,+ fn _, _, _ ->+ {:ok,+ %Finch.Response{+ status: 200,+ headers: [{"content-type", "application/json"}],+ body: %{+ "expires_in" => 3600,+ "access_token" => "SomeAccessToken",+ "refresh_token" => "SomeRefreshToken",+ "id_token" => ".#{body}."+ }+ }}+ end+ )+ endend