diff --git a/build_tools/auth_lastfm.php b/build_tools/auth_lastfm.php index ec5b814b..d612e203 100755 --- a/build_tools/auth_lastfm.php +++ b/build_tools/auth_lastfm.php @@ -11,8 +11,8 @@ // LastFM app credentials define('API_URL','http://ws.audioscrobbler.com/2.0/?format=json'); -define('API_KEY','YOUR_KEY_HERE'); -define('API_SECRET','YOUR_SECRET_HERE'); +define('API_KEY','YOUR_API_KEY_HERE'); +define('API_SECRET','YOUR_API_SECRET_HERE'); define('REDIRECT_URI','YOUR_REDIRECT_URI_HERE'); // Allow cross-domain requests @@ -35,7 +35,8 @@ if (isset($_GET['action'])){ exit; case 'start_session': - $session = startSession($_GET['token']); + $session = startSession($_GET['token'], false); + $session['origin'] = "auth_lastfm"; // Pass our error back to the popup opener ?> @@ -47,14 +48,16 @@ if (isset($_GET['action'])){ break; - case 'sign_request': + case 'sign_request': + header('Content-Type: text/json'); $data = $_GET; $signed = signRequest($data); echo json_encode($signed); exit; default: - echo 'Invalid action specified'; + header('Content-Type: text/json'); + echo '{"error": "Invalid action specified"}'; die(); } @@ -83,16 +86,15 @@ function startSession($token){ if (FALSE === $ch){ throw new Exception('Failed to initialize'); } - - $data = signRequest( - array( - "method" => "auth.getSession", - "token" => $token - ), - false + $signature = "api_key".API_KEY."methodauth.getSessiontoken".$token.API_SECRET; + $signature = md5($signature); + $data = array( + "api_key" => API_KEY, + "api_sig" => $signature, + "token" => $token ); - curl_setopt($ch, CURLOPT_URL,API_URL); + curl_setopt($ch, CURLOPT_URL,API_URL."&method=auth.getSession"); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); @@ -124,31 +126,38 @@ function startSession($token){ * Perform a signed request * * @param $data = array - * @param $post = boolean (POST request) + * @param $include_signatore = boolean **/ -function signRequest($data = array(), $post = true){ +function signRequest($data = array(), $include_signature = true){ unset($data['action']); - $ch = curl_init(); - - if (FALSE === $ch){ - throw new Exception('Failed to initialize'); - } - // Make sure we've got our API key included $request = array_merge(array("api_key" => API_KEY), $data); + // Drop jQuery callback argument + unset($request["_"]); + // Loop all the values in our request and add to our signature $signature = ""; + $params = ""; ksort($request); foreach ($request as $key => $value){ $signature.= $key.$value; + if ($params != ""){ + $params.= "&"; + } + $params.= $key."=".$value; } - + // Finalize the signature $signature.= API_SECRET; $signature = md5($signature); $request["api_sig"] = $signature; + // Add finalised params as a complete string + $params.= "&api_sig=".$signature; + $request["params"] = $params; + return $request; } + diff --git a/build_tools/auth_spotify.php b/build_tools/auth_spotify.php index 205a1cdb..bafe34ee 100755 --- a/build_tools/auth_spotify.php +++ b/build_tools/auth_spotify.php @@ -1,16 +1,9 @@