Skip to content
This repository was archived by the owner on Jun 21, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/file_finder_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ defmodule FileFinderWeb do
import Plug.Conn
import Phoenix.Controller
import Phoenix.LiveView.Router
import FileFinderWeb.Api.Auth, only: [authenticate_shop: 2]
import FileFinderWeb.Events.Auth, only: [authenticate_event: 2]
import FileFinderWeb.Plugs.Api, only: [authenticate_shop: 2]
import FileFinderWeb.Plugs.Events, only: [authenticate_event: 2]
end
end

Expand Down
37 changes: 37 additions & 0 deletions lib/file_finder_web/controllers/mixpanel_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
defmodule FileFinderWeb.MixpanelController do
use FileFinderWeb, :controller

def lib_js(conn, _params) do
js = HTTPoison.get("https://cdn.mxpnl.com/libs/mixpanel-2-latest.js")
render("mixpanel.js", js: js)
end

def lib_min_js(conn, _params) do
js = HTTPoison.get("https://cdn.mxpnl.com/libs/mixpanel-2-latest.min.js")
render("mixpanel.js", js: js)
end

def request(conn, _params) do
url =
if conn.path_info |> List.first() == "decide" do
"https://decide.mixpanel.com"
else
"https://api.mixpanel.com"
end

ip =
cond do
get_req_header(conn, "x-forwarded-for") |> length() > 0 ->
get_resp_header(conn, "x-forwarded-for") |> List.first()

get_req_header(conn, "x-real-ip") |> length() > 0 ->
get_req_header(conn, "x-real-ip") |> List.first()

true ->
# TODO: format
conn.remote_ip
end

render("mixpanel.json", json: %{debug: "TODO"})

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pickup here

end
end
15 changes: 15 additions & 0 deletions lib/file_finder_web/mixpanel_router.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule FileFinderWeb.MixpanelRouter do
use FileFinderWeb, :router

pipeline :proxy do
plug :accepts, ["js", "json"]
end

scope "/", FileFinderWeb do
pipe_through :proxy

get "/lib.js", MixpanelController, :lib_js
get "/lib.min.js", MixpanelController, :lib_min_js
get "/*path", MixpanelController, :api_request
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule FileFinderWeb.Api.Auth do
defmodule FileFinderWeb.Plugs.Api do
@moduledoc """
A module plug that verifies the bearer token in the request headers.
The authorization header value may look like `Bearer xxxxxxx`.
Expand Down Expand Up @@ -48,16 +48,16 @@ defmodule FileFinderWeb.Api.Auth do

## Examples

iex> FileFinderWeb.Api.Auth.verify_token("good-token", conn)
iex> FileFinderWeb.Plugs.Api.verify_token("good-token", conn)
{:ok, 1}

iex> FileFinderWeb.Api.Auth.verify_token("bad-token", conn)
iex> FileFinderWeb.Plugs.Api.verify_token("bad-token", conn)
{:error, :invalid}

iex> FileFinderWeb.Api.Auth.verify_token("old-token", conn)
iex> FileFinderWeb.Plugs.Api.verify_token("old-token", conn)
{:error, :expired}

iex> FileFinderWeb.Api.Auth.verify_token(nil, conn)
iex> FileFinderWeb.Plugs.Api.verify_token(nil, conn)
{:error, :missing}

"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule FileFinderWeb.Events.Auth do
defmodule FileFinderWeb.Plugs.Events do
@moduledoc """
A module plug that verifies webhook event signatures.
"""
Expand Down Expand Up @@ -47,13 +47,13 @@ defmodule FileFinderWeb.Events.Auth do

## Examples

iex> FileFinderWeb.Events.Auth.verify_signature("good-signature", conn)
iex> FileFinderWeb.Plugs.Events.verify_signature("good-signature", conn)
{:ok}

iex> FileFinderWeb.Events.Auth.verify_signature("bad-signature", conn)
iex> FileFinderWeb.Plugs.Events.verify_signature("bad-signature", conn)
{:error, :invalid}

iex> FileFinderWeb.Events.Auth.verify_signature(nil, conn)
iex> FileFinderWeb.Plugs.Events.verify_signature(nil, conn)
{:error, :missing_signature}

"""
Expand Down
24 changes: 24 additions & 0 deletions lib/file_finder_web/plugs/mixpanel.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule FileFinderWeb.Plugs.Mixpanel do
import Plug.Conn

@doc false
def init(default), do: default

@doc false
def call(conn, router) do
case get_subdomain(conn.host) do
subdomain when subdomain == "mixpanel" ->
conn
|> router.call(router.init({}))
|> halt()

_ ->
conn
end
end

defp get_subdomain(host) do
root_host = Subdomainer.Endpoint.config(:url)[:host]
String.replace(host, ~r/.?#{root_host}/, "")
end
end
4 changes: 2 additions & 2 deletions lib/file_finder_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule FileFinderWeb.Router do

pipeline :api do
plug :accepts, ["json"]
plug FileFinderWeb.Api.Auth
plug FileFinderWeb.Plugs.Api
end

pipeline :browser do
Expand All @@ -23,7 +23,7 @@ defmodule FileFinderWeb.Router do
pipeline :events do
plug :accepts, ["json"]
plug :put_secure_browser_headers
plug FileFinderWeb.Events.Auth
plug FileFinderWeb.Plugs.Events
end

scope "/", FileFinderWeb do
Expand Down
11 changes: 11 additions & 0 deletions lib/file_finder_web/views/mixpanel_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule FileFinderWeb.MixpanelView do
use FileFinderWeb, :view

def render("mixpanel.js", %{js: js}) do
js
end

def render("mixpanel.json", %{json: json}) do
json
end
end