Adding POC lastfm authorization button

This commit is contained in:
James Barnsley
2017-11-02 21:20:56 +13:00
parent 5abb9c775a
commit 35a61686b3
13 changed files with 847 additions and 270 deletions

View File

@ -13,7 +13,7 @@ from core import IrisCore
from raven import Client
logger = logging.getLogger(__name__)
__version__ = '3.6.1'
__version__ = '3.6.1'
##
# Core extension class
@ -35,7 +35,8 @@ class Extension( ext.Extension ):
schema['enabled'] = config.Boolean()
schema['country'] = config.String()
schema['locale'] = config.String()
schema['authorization_url'] = config.String()
schema['spotify_authorization_url'] = config.String()
schema['lastfm_authorization_url'] = config.String()
return schema
def setup(self, registry):

View File

@ -259,7 +259,8 @@ class IrisCore(object):
"spotify_username": spotify_username,
"country": self.config['iris']['country'],
"locale": self.config['iris']['locale'],
"authorization_url": self.config['iris']['authorization_url']
"spotify_authorization_url": self.config['iris']['spotify_authorization_url'],
"lastfm_authorization_url": self.config['iris']['lastfm_authorization_url']
}
}

View File

@ -2,4 +2,5 @@
enabled = true
country = NZ
locale = en_NZ
authorization_url = https://jamesbarnsley.co.nz/auth_v2.php
spotify_authorization_url = https://jamesbarnsley.co.nz/auth_spotify.php
lastfm_authorization_url = https://jamesbarnsley.co.nz/auth_lastfm.php

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,154 @@
<?php
/**
* LastFM Authentication proxy
*
* To use:
* 1. Create a LastFM App (https://www.last.fm/api/account/create), and paste in your credentials below
* 2. Save this script to a publicly-accessible server
* 3. Set config in mopidy.conf to point to this script
**/
// LastFM app credentials
define('API_URL','http://ws.audioscrobbler.com/2.0/?format=json');
define('API_KEY','269cc2b1dfb1059a89a2db21ceb81d2a');
define('API_SECRET','1551231f57eb3b73801e31bc0e89ab09');
define('REDIRECT_URI','https://jamesbarnsley.nz/auth_lastfm.php?action=start_session');
// Allow cross-domain requests
header("Access-Control-Allow-Origin: *");
// Set our cookies
if (isset($_GET['app'])){
setcookie('mopidy_iris', $_GET['app'], time()+3600);
}
/* ================================================================================= INIT ================ */
/* ======================================================================================================= */
if (isset($_GET['action'])){
switch ($_GET['action']){
case 'authorize':
header('Location: http://www.last.fm/api/auth/?api_key='.API_KEY.'&cb='.REDIRECT_URI);
exit;
case 'start_session':
$session = startSession($_GET['token']);
// Pass our error back to the popup opener
?>
<script type="text/javascript">
window.opener.postMessage( '<?php echo json_encode($session) ?>', "*");
window.close();
</script>
<?php
break;
case 'sign_request':
$data = $_GET;
$signed = signRequest($data);
echo json_encode($signed);
exit;
default:
echo 'Invalid action specified';
die();
}
} else {
echo "No action specified";
die();
}
/* ================================================================================= GETTERS ============= */
/* ======================================================================================================= */
/**
* Start a session
*
* @param $data = array
* @param $post = boolean (POST request)
**/
function startSession($token){
$ch = curl_init();
if (FALSE === $ch){
throw new Exception('Failed to initialize');
}
$data = signRequest(
array(
"method" => "auth.getSession",
"token" => $token
),
false
);
curl_setopt($ch, CURLOPT_URL,API_URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)){
echo 'CURL Error: '. curl_error($ch);
}
curl_close($ch);
$response = json_decode($response, true);
// Append our other important values to successful signings
if (!$response['error']){
$response['api_key'] = API_KEY;
}
return $response;
}
/**
* Perform a signed request
*
* @param $data = array
* @param $post = boolean (POST request)
**/
function signRequest($data = array(), $post = 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);
// Loop all the values in our request and add to our signature
$signature = "";
ksort($request);
foreach ($request as $key => $value){
$signature.= $key.$value;
}
// Finalize the signature
$signature.= API_SECRET;
$signature = md5($signature);
$request["api_sig"] = $signature;
return $request;
}

View File

@ -38,7 +38,7 @@
// Release details
// These are automatically injected by build.sh
var build = "1509392542";
var build = "1509609058";
var version = "3.6.1";
// Construct the script tag