From 7c547f2d805c72ee36d46059a8b900850d2138c3 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Mon, 5 May 2025 19:19:12 +0100 Subject: [PATCH] Redirect to Last.fm and receive a token (unused) --- config/runtime.exs | 4 ++++ lib/last_fm.ex | 5 +++++ lib/last_fm/config.ex | 6 ++++++ lib/music_library_web/controllers/last_fm_controller.ex | 7 +++++++ .../controllers/session_html/new.html.heex | 3 +++ lib/music_library_web/router.ex | 2 ++ 6 files changed, 27 insertions(+) create mode 100644 lib/music_library_web/controllers/last_fm_controller.ex diff --git a/config/runtime.exs b/config/runtime.exs index 71c3d955..c3a193e3 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -24,6 +24,10 @@ if api_key = System.get_env("LAST_FM_API_KEY") do config :music_library, LastFm, api_key: api_key end +if shared_secret = System.get_env("LAST_FM_SHARED_SECRET") do + config :music_library, LastFm, shared_secret: shared_secret +end + if user = System.get_env("LAST_FM_USER") do config :music_library, LastFm, user: user end diff --git a/lib/last_fm.ex b/lib/last_fm.ex index a1b64e4a..f9f4f4af 100644 --- a/lib/last_fm.ex +++ b/lib/last_fm.ex @@ -41,5 +41,10 @@ defmodule LastFm do end end + def auth_url do + last_fm_config = last_fm_config() + "https://www.last.fm/api/auth/?api_key=" <> last_fm_config.api_key + end + defp last_fm_config, do: LastFm.Config.resolve(:music_library) end diff --git a/lib/last_fm/config.ex b/lib/last_fm/config.ex index 570bf5f1..a7822477 100644 --- a/lib/last_fm/config.ex +++ b/lib/last_fm/config.ex @@ -1,6 +1,7 @@ defmodule LastFm.Config do @type t :: %__MODULE__{ api_key: String.t(), + shared_secret: String.t(), user: String.t(), auto_refresh: boolean(), refresh_interval: pos_integer(), @@ -10,6 +11,7 @@ defmodule LastFm.Config do @enforce_keys [:api_key, :user] defstruct api_key: "", + shared_secret: "", user: "", auto_refresh: true, refresh_interval: 60_000, @@ -21,6 +23,10 @@ defmodule LastFm.Config do type: :string, required: true ], + shared_secret: [ + type: :string, + required: true + ], user: [ type: :string, required: true diff --git a/lib/music_library_web/controllers/last_fm_controller.ex b/lib/music_library_web/controllers/last_fm_controller.ex new file mode 100644 index 00000000..3c59aa5f --- /dev/null +++ b/lib/music_library_web/controllers/last_fm_controller.ex @@ -0,0 +1,7 @@ +defmodule MusicLibraryWeb.LastFmController do + use MusicLibraryWeb, :controller + + def callback(conn, %{"token" => _token}) do + conn |> redirect(to: ~p"/") + end +end diff --git a/lib/music_library_web/controllers/session_html/new.html.heex b/lib/music_library_web/controllers/session_html/new.html.heex index d539ae68..6e8ba6e8 100644 --- a/lib/music_library_web/controllers/session_html/new.html.heex +++ b/lib/music_library_web/controllers/session_html/new.html.heex @@ -35,5 +35,8 @@ + + Connect to Last.fm + diff --git a/lib/music_library_web/router.ex b/lib/music_library_web/router.ex index 4cfa3e82..3784d993 100644 --- a/lib/music_library_web/router.ex +++ b/lib/music_library_web/router.ex @@ -29,6 +29,8 @@ defmodule MusicLibraryWeb.Router do get "/login", SessionController, :new post "/sessions/create", SessionController, :create + get "/auth/last_fm/callback", LastFmController, :callback + scope "/" do pipe_through :logged_in