Extract LastFm.API.Signature
This commit is contained in:
+8
-21
@@ -1,20 +1,20 @@
|
|||||||
defmodule LastFm.API do
|
defmodule LastFm.API do
|
||||||
require Logger
|
require Logger
|
||||||
|
|
||||||
alias LastFm.{API.ErrorResponse, Artist, Session, Track}
|
alias LastFm.{Artist, Session, Track}
|
||||||
|
alias LastFm.API.{ErrorResponse, Signature}
|
||||||
|
|
||||||
def get_session(token, config) do
|
def get_session(token, config) do
|
||||||
# Note that params needs to be ordered alphabetically
|
|
||||||
params =
|
params =
|
||||||
[
|
%{
|
||||||
api_key: config.api_key,
|
api_key: config.api_key,
|
||||||
method: "auth.getSession",
|
method: "auth.getSession",
|
||||||
token: token
|
token: token
|
||||||
]
|
}
|
||||||
|
|
||||||
signature = signature(params, config.shared_secret)
|
signature = Signature.generate(params, config.shared_secret)
|
||||||
|
|
||||||
params = Keyword.put(params, :api_sig, signature)
|
params = Map.put(params, "api_sig", signature)
|
||||||
|
|
||||||
config
|
config
|
||||||
|> new_request()
|
|> new_request()
|
||||||
@@ -39,13 +39,9 @@ defmodule LastFm.API do
|
|||||||
end)
|
end)
|
||||||
|> Enum.into(%{})
|
|> Enum.into(%{})
|
||||||
|
|
||||||
params =
|
params = Map.merge(params, track_params)
|
||||||
params
|
|
||||||
|> Map.merge(track_params)
|
|
||||||
|
|
||||||
sorted_params = Enum.sort(params)
|
signature = Signature.generate(params, config.shared_secret)
|
||||||
|
|
||||||
signature = signature(sorted_params, config.shared_secret)
|
|
||||||
|
|
||||||
body = Map.merge(params, %{"api_sig" => signature, "format" => "json"})
|
body = Map.merge(params, %{"api_sig" => signature, "format" => "json"})
|
||||||
|
|
||||||
@@ -55,15 +51,6 @@ defmodule LastFm.API do
|
|||||||
|> post_request()
|
|> post_request()
|
||||||
end
|
end
|
||||||
|
|
||||||
defp signature(params, shared_secret) do
|
|
||||||
encoded_params =
|
|
||||||
Enum.map_join(params, fn {key, value} ->
|
|
||||||
"#{key}#{value}"
|
|
||||||
end)
|
|
||||||
|
|
||||||
:crypto.hash(:md5, encoded_params <> shared_secret) |> Base.encode16(case: :lower)
|
|
||||||
end
|
|
||||||
|
|
||||||
def get_recent_tracks(config) do
|
def get_recent_tracks(config) do
|
||||||
params =
|
params =
|
||||||
config
|
config
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
defmodule LastFm.API.Signature do
|
||||||
|
def generate(params, shared_secret) do
|
||||||
|
encoded_params =
|
||||||
|
params
|
||||||
|
# Params needs to be ASCII ordered alphabetically
|
||||||
|
|> Enum.sort()
|
||||||
|
|> Enum.map_join(fn {key, value} ->
|
||||||
|
"#{key}#{value}"
|
||||||
|
end)
|
||||||
|
|
||||||
|
:crypto.hash(:md5, encoded_params <> shared_secret) |> Base.encode16(case: :lower)
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user