Skip to content

Commit

Permalink
Integrate Auth0
Browse files Browse the repository at this point in the history
Insert Time Entry with Auth0.uid
Filer Time Entry with Auth0.uid
  • Loading branch information
co0lsky committed Feb 20, 2020
1 parent 5413f3d commit 7677240
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 7 deletions.
12 changes: 12 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ config :phoenix, :json_library, Jason
# Use Tzdata for timezone
config :elixir, :time_zone_database, Tzdata.TimeZoneDatabase

# Configures Ueberauth
config :ueberauth, Ueberauth,
providers: [
auth0: { Ueberauth.Strategy.Auth0, [] },
]

# Configures Ueberauth's Auth0 auth provider
config :ueberauth, Ueberauth.Strategy.Auth0.OAuth,
domain: "dev-9ulkbitr.au.auth0.com",
client_id: "AXO8aZvsYsKhKKisUOwkWlR9orqUnD4c",
client_secret: "Lm9Jr1ENqGlN8FhOtYYea_T3mQEjSX2FN7U8DsREeh9X24lQqd7UMxqSis1Cz6b4"

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env()}.exs"
1 change: 1 addition & 0 deletions lib/deep_work_hours/time_entry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule DeepWorkHours.TimeEntry do
field :start_date_time, :utc_datetime
field :end_date_time, :utc_datetime
field :total_time, :time
field :uid, :string

timestamps()
end
Expand Down
36 changes: 36 additions & 0 deletions lib/deep_work_hours_web/controllers/auth_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
defmodule DeepWorkHoursWeb.AuthController do
use DeepWorkHoursWeb, :controller
alias DeepWorkHoursWeb.Router.Helpers

plug Ueberauth

alias Ueberauth.Strategy.Helpers

def logout(conn, _params) do
conn
|> put_flash(:info, "You have been logged out!")
|> configure_session(drop: true)
|> redirect(to: "/")
end

def callback(%{assigns: %{ueberauth_failure: _fails}} = conn, _params) do
conn
|> put_flash(:error, "Failed to authenticate.")
|> redirect(to: "/")
end

def callback(%{assigns: %{ueberauth_auth: auth}} = conn, _params) do
case UserFromAuth.find_or_create(auth) do
{:ok, user} ->
conn
|> put_flash(:info, "Successfully authenticated as " <> user.name <> ".")
|> put_session(:current_user, user)
|> redirect(to: "/")
{:error, reason} ->
conn
|> put_flash(:error, reason)
|> redirect(to: "/")
end
end

end
17 changes: 16 additions & 1 deletion lib/deep_work_hours_web/controllers/page_controller.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
defmodule DeepWorkHoursWeb.PageController do
use DeepWorkHoursWeb, :controller

plug :secure

defp secure(conn, _params) do
user = get_session(conn, :current_user)
case user do
nil ->
conn
|> redirect(to: "/auth/auth0")
|> halt
_ ->
conn
|> assign(:current_user, user)
end
end

def index(conn, _params) do
render(conn, "index.html")
render(conn, "index.html", current_user: get_session(conn, :current_user))
end
end
3 changes: 2 additions & 1 deletion lib/deep_work_hours_web/live/entries_list_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ defmodule DeepWorkHoursWeb.EntriesListLive do
Phoenix.View.render(DeepWorkHoursWeb.PageView, "entries_list.html", assigns)
end

def mount(_params, %{}, socket) do
def mount(_params, %{"current_user" => current_user}, socket) do
entries = DeepWorkHours.Repo.all(
from(
t in DeepWorkHours.TimeEntry,
where: t.uid == ^current_user.id,
group_by: t.date,
select: %{day: t.date, total: sum(t.total_time)}
)
Expand Down
14 changes: 11 additions & 3 deletions lib/deep_work_hours_web/live/timer_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ defmodule DeepWorkHoursWeb.TimerLive do
Phoenix.View.render(DeepWorkHoursWeb.PageView, "timer.html", assigns)
end

def mount(_params, %{}, socket) do
{:ok, reset_timer socket}
# def mount(_params, %{}, socket) do
# {:ok, reset_timer socket}
# end

def mount(_params, %{"current_user" => current_user}, socket) do
{:ok, socket
|> reset_timer
|> assign(:current_user, current_user)
}
end

def handle_info(:update, socket) do
Expand Down Expand Up @@ -58,7 +65,8 @@ defmodule DeepWorkHoursWeb.TimerLive do
date: Date.utc_today(),
start_date_time: DateTime.truncate(socket.assigns.start_time, :second),
end_date_time: DateTime.truncate(end_time, :second),
total_time: socket.assigns.current_time
total_time: socket.assigns.current_time,
uid: socket.assigns.current_user.id
}

DeepWorkHours.Repo.insert time_entry
Expand Down
99 changes: 99 additions & 0 deletions lib/deep_work_hours_web/models/user_from_auth.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
defmodule UserFromAuth do
@moduledoc """
Retrieve the user information from an auth request
"""
require Logger
require Poison

alias Ueberauth.Auth

def find_or_create(%Auth{provider: :identity} = auth) do
case validate_pass(auth.credentials) do
:ok ->
{:ok, basic_info(auth)}
{:error, reason} -> {:error, reason}
end
end

def find_or_create(%Auth{} = auth) do
{:ok, basic_info(auth)}
end

# github does it this way
defp avatar_from_auth(
%{
info: %{
urls: %{
avatar_url: image
}
}
}
), do: image

# facebook does it this way
defp avatar_from_auth(
%{
info: %{
image: image
}
}
), do: image

# default case if nothing matches
defp avatar_from_auth(auth) do
Logger.warn auth.provider <> " needs to find an avatar URL!"
Logger.debug(Poison.encode!(auth))
nil
end

defp basic_info(auth) do
%{id: auth.uid, name: name_from_auth(auth), avatar: avatar_from_auth(auth)}
end

defp name_from_auth(auth) do
if auth.info.name do
auth.info.name
else
name = [auth.info.first_name, auth.info.last_name]
|> Enum.filter(&(&1 != nil and &1 != ""))

cond do
length(name) == 0 -> auth.info.nickname
true -> Enum.join(name, " ")
end
end
end

defp validate_pass(
%{
other: %{
password: ""
}
}
) do
{:error, "Password required"}
end

defp validate_pass(
%{
other: %{
password: pw,
password_confirmation: pw
}
}
) do
:ok
end

defp validate_pass(
%{
other: %{
password: _
}
}
) do
{:error, "Passwords do not match"}
end

defp validate_pass(_), do: {:error, "Password Required"}
end
11 changes: 11 additions & 0 deletions lib/deep_work_hours_web/router.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule DeepWorkHoursWeb.Router do
use DeepWorkHoursWeb, :router

require Ueberauth

pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
Expand All @@ -13,10 +15,19 @@ defmodule DeepWorkHoursWeb.Router do
plug :accepts, ["json"]
end

scope "/auth", DeepWorkHoursWeb do
pipe_through :browser

get "/:provider", AuthController, :request
get "/:provider/callback", AuthController, :callback
post "/:provider/callback", AuthController, :callback
end

scope "/", DeepWorkHoursWeb do
pipe_through :browser

get "/", PageController, :index
get "/logout", AuthController, :logout
end

# Other scopes may use custom stacks.
Expand Down
8 changes: 6 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule DeepWorkHours.MixProject do
def application do
[
mod: {DeepWorkHours.Application, []},
extra_applications: [:logger, :runtime_tools]
extra_applications: [:ueberauth, :ueberauth_auth0, :logger, :runtime_tools, :timex],
]
end

Expand All @@ -44,8 +44,12 @@ defmodule DeepWorkHours.MixProject do
{:jason, "~> 1.0"},
{:plug_cowboy, "~> 2.0"},
{:phoenix_live_view, "~> 0.7.0"},
{:poison, "~> 3.1"},
{:floki, ">= 0.0.0", only: :test},
{:tzdata, "~> 1.0.1"}
{:timex, "~> 3.0"},
{:tzdata, "~> 1.0.1"},
{:ueberauth, "~> 0.4"},
{:ueberauth_auth0, "~> 0.3"}
]
end

Expand Down
6 changes: 6 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
%{
"certifi": {:hex, :certifi, "2.5.1", "867ce347f7c7d78563450a18a6a28a8090331e77fa02380b4a21962a65d36ee5", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm", "805abd97539caf89ec6d4732c91e62ba9da0cda51ac462380bbd28ee697a8c42"},
"combine": {:hex, :combine, "0.10.0", "eff8224eeb56498a2af13011d142c5e7997a80c8f5b97c499f84c841032e429f", [:mix], [], "hexpm", "1b1dbc1790073076580d0d1d64e42eae2366583e7aecd455d1215b0d16f2451b"},
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm", "4a0850c9be22a43af9920a71ab17c051f5f7d45c209e40269a1938832510e4d9"},
"cowboy": {:hex, :cowboy, "2.7.0", "91ed100138a764355f43316b1d23d7ff6bdb0de4ea618cb5d8677c93a7a2f115", [:rebar3], [{:cowlib, "~> 2.8.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "04fd8c6a39edc6aaa9c26123009200fc61f92a3a94f3178c527b70b767c6e605"},
"cowlib": {:hex, :cowlib, "2.8.0", "fd0ff1787db84ac415b8211573e9a30a3ebe71b5cbff7f720089972b2319c8a4", [:rebar3], [], "hexpm", "79f954a7021b302186a950a32869dbc185523d99d3e44ce430cd1f3289f41ed4"},
Expand All @@ -17,6 +18,7 @@
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
"mime": {:hex, :mime, "1.3.1", "30ce04ab3175b6ad0bdce0035cba77bba68b813d523d1aac73d9781b4d193cf8", [:mix], [], "hexpm", "6cbe761d6a0ca5a31a0931bf4c63204bceb64538e664a8ecf784a9a6f3b875f1"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
"oauth2": {:hex, :oauth2, "2.0.0", "338382079fe16c514420fa218b0903f8ad2d4bfc0ad0c9f988867dfa246731b0", [:mix], [{:hackney, "~> 1.13", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "881b8364ac7385f9fddc7949379cbe3f7081da37233a1aa7aab844670a91e7e7"},
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm", "17ef63abde837ad30680ea7f857dd9e7ced9476cdd7b0394432af4bfc241b960"},
"phoenix": {:hex, :phoenix, "1.4.13", "67271ad69b51f3719354604f4a3f968f83aa61c19199343656c9caee057ff3b8", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 1.1", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.8.1 or ~> 1.9", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 1.0 or ~> 2.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ab765a0feddb81fc62e2116c827b5f068df85159c162bee760745276ad7ddc1b"},
"phoenix_ecto": {:hex, :phoenix_ecto, "4.1.0", "a044d0756d0464c5a541b4a0bf4bcaf89bffcaf92468862408290682c73ae50d", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.9", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "c5e666a341ff104d0399d8f0e4ff094559b2fde13a5985d4cb5023b2c2ac558b"},
Expand All @@ -27,10 +29,14 @@
"plug": {:hex, :plug, "1.9.0", "8d7c4e26962283ff9f8f3347bd73838e2413fbc38b7bb5467d5924f68f3a5a4a", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "9902eda2c52ada2a096434682e99a2493f5d06a94d6ac6bcfff9805f952350f1"},
"plug_cowboy": {:hex, :plug_cowboy, "2.1.2", "8b0addb5908c5238fac38e442e81b6fcd32788eaa03246b4d55d147c47c5805e", [:mix], [{:cowboy, "~> 2.5", [hex: :cowboy, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "7d722581ce865a237e14da6d946f92704101740a256bd13ec91e63c0b122fc70"},
"plug_crypto": {:hex, :plug_crypto, "1.1.2", "bdd187572cc26dbd95b87136290425f2b580a116d3fb1f564216918c9730d227", [:mix], [], "hexpm", "6b8b608f895b6ffcfad49c37c7883e8df98ae19c6a28113b02aa1e9c5b22d6b5"},
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm", "fec8660eb7733ee4117b85f55799fd3833eb769a6df71ccf8903e8dc5447cfce"},
"postgrex": {:hex, :postgrex, "0.15.3", "5806baa8a19a68c4d07c7a624ccdb9b57e89cbc573f1b98099e3741214746ae4", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "4737ce62a31747b4c63c12b20c62307e51bb4fcd730ca0c32c280991e0606c90"},
"ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm", "451d8527787df716d99dc36162fca05934915db0b6141bbdac2ea8d3c7afc7d7"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.5", "6eaf7ad16cb568bb01753dbbd7a95ff8b91c7979482b95f38443fe2c8852a79b", [:make, :mix, :rebar3], [], "hexpm", "13104d7897e38ed7f044c4de953a6c28597d1c952075eb2e328bc6d6f2bfc496"},
"telemetry": {:hex, :telemetry, "0.4.1", "ae2718484892448a24470e6aa341bc847c3277bfb8d4e9289f7474d752c09c7f", [:rebar3], [], "hexpm", "4738382e36a0a9a2b6e25d67c960e40e1a2c95560b9f936d8e29de8cd858480f"},
"timex": {:hex, :timex, "3.6.1", "efdf56d0e67a6b956cc57774353b0329c8ab7726766a11547e529357ffdc1d56", [:mix], [{:combine, "~> 0.10", [hex: :combine, repo: "hexpm", optional: false]}, {:gettext, "~> 0.10", [hex: :gettext, repo: "hexpm", optional: false]}, {:tzdata, "~> 0.1.8 or ~> 0.5 or ~> 1.0.0", [hex: :tzdata, repo: "hexpm", optional: false]}], "hexpm", "f354efb2400dd7a80fd9eb6c8419068c4f632da4ac47f3d8822d6e33f08bc852"},
"tzdata": {:hex, :tzdata, "1.0.3", "73470ad29dde46e350c60a66e6b360d3b99d2d18b74c4c349dbebbc27a09a3eb", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "a6e1ee7003c4d04ecbd21dd3ec690d4c6662db5d3bbdd7262d53cdf5e7c746c1"},
"ueberauth": {:hex, :ueberauth, "0.6.2", "25a31111249d60bad8b65438b2306a4dc91f3208faa62f5a8c33e8713989b2e8", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "db9fbfb5ac707bc4f85a297758406340bf0358b4af737a88113c1a9eee120ac7"},
"ueberauth_auth0": {:hex, :ueberauth_auth0, "0.4.0", "4eac2c9dbc334804792eee47eeccf952dcb64f80ade8f68e9e9ff90ed2fba33d", [:mix], [{:oauth2, "~> 2.0", [hex: :oauth2, repo: "hexpm", optional: false]}, {:ueberauth, "~> 0.6", [hex: :ueberauth, repo: "hexpm", optional: false]}], "hexpm", "6ce7660bcdbc54b19c4e9c94ebfae81b17d4abef288383c020a86ff63fe300a3"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm", "1d1848c40487cdb0b30e8ed975e34e025860c02e419cb615d255849f3427439d"},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule DeepWorkHours.Repo.Migrations.AddUidToTimeEntries do
use Ecto.Migration

def change do
alter table("time_entries") do
add :uid, :string
end
end
end

0 comments on commit 7677240

Please sign in to comment.