HTTP endpoint for less-awkward server-side token management

This commit is contained in:
James Barnsley
2016-10-30 18:06:43 +13:00
parent 49b511b038
commit 474ebc6738
9 changed files with 63 additions and 58 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@ public_html
/.sass-cache/ /.sass-cache/
*.orig *.orig
Thumbs.db Thumbs.db
*.pyc

View File

@ -4,7 +4,8 @@ import logging, os, json
import tornado.web import tornado.web
import tornado.websocket import tornado.websocket
from mopidy import config, ext from mopidy import config, ext
from frontend import IrisFrontend, RequestHandler from frontend import IrisFrontend
from http import RequestHandler
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
__version__ = '2.10.1' __version__ = '2.10.1'
@ -49,10 +50,9 @@ def factory(config, core):
(r"/images/(.*)", tornado.web.StaticFileHandler, { (r"/images/(.*)", tornado.web.StaticFileHandler, {
"path": config['local-images']['image_dir'] "path": config['local-images']['image_dir']
}), }),
(r'/http', RequestHandler, { (r'/http/([^/]*)', RequestHandler, {
'core': core, 'core': core,
'config': config, 'config': config
'version': __version__
}), }),
(r'/(.*)', tornado.web.StaticFileHandler, { (r'/(.*)', tornado.web.StaticFileHandler, {
"path": path, "path": path,

Binary file not shown.

View File

@ -213,53 +213,4 @@ class IrisFrontend(pykka.ThreadingActor, CoreListener):
## ##
def restart( self ): def restart( self ):
os.execl(sys.executable, *([sys.executable]+sys.argv)) os.execl(sys.executable, *([sys.executable]+sys.argv))
class RequestHandler(tornado.web.RequestHandler):
def set_default_headers(self):
self.set_header("Access-Control-Allow-Origin", "*")
def initialize(self, core, config, version):
self.core = core
self.config = config
self.version = version
##
# Get a new spotify authentication token
#
# Uses the Client Credentials Flow, so is invisible to the user. We need this token for
# any backend spotify requests (we don't tap in to Mopidy-Spotify, yet). Also used for
# passing token to frontend for javascript requests without use of the Authorization Code Flow.
##
def refresh_spotify_token( self ):
url = 'https://accounts.spotify.com/api/token'
authorization = 'YTg3ZmI0ZGJlZDMwNDc1YjhjZWMzODUyM2RmZjUzZTI6ZDdjODlkMDc1M2VmNDA2OGJiYTE2NzhjNmNmMjZlZDY='
headers = {'Authorization' : 'Basic ' + authorization}
data = {'grant_type': 'client_credentials'}
data_encoded = urllib.urlencode( data )
req = urllib2.Request(url, data_encoded, headers)
try:
response = urllib2.urlopen(req, timeout=30).read()
response_dict = json.loads(response)
self.spotify_token = response_dict
return response_dict
except urllib2.HTTPError as e:
return e
##
# Get a new spotify authentication token
#
# Uses the Client Credentials Flow, so is invisible to the user. We need this token for
# any backend spotify requests (we don't tap in to Mopidy-Spotify, yet). Also used for
# passing token to frontend for javascript requests without use of the Authorization Code Flow.
##
def get(self):
token = self.refresh_spotify_token()
self.write( token )

Binary file not shown.

53
mopidy_iris/http.py Executable file
View File

@ -0,0 +1,53 @@
from __future__ import unicode_literals
import logging, json, urllib, urllib2
import tornado.web
from spotipy import Spotify
# import logger
logger = logging.getLogger(__name__)
class RequestHandler(tornado.web.RequestHandler):
def set_default_headers(self):
self.set_header("Access-Control-Allow-Origin", "*")
def initialize(self, core, config):
self.core = core
self.config = config
def get(self, slug=None):
if( slug == 'refresh_spotify_token' ):
self.write( self.refresh_spotify_token() )
return
else:
self.write('Invalid request')
return
##
# Get a new spotify authentication token
#
# Uses the Client Credentials Flow, so is invisible to the user. We need this token for
# any backend spotify requests (we don't tap in to Mopidy-Spotify, yet). Also used for
# passing token to frontend for javascript requests without use of the Authorization Code Flow.
##
def refresh_spotify_token( self ):
url = 'https://accounts.spotify.com/api/token'
authorization = 'YTg3ZmI0ZGJlZDMwNDc1YjhjZWMzODUyM2RmZjUzZTI6ZDdjODlkMDc1M2VmNDA2OGJiYTE2NzhjNmNmMjZlZDY='
headers = {'Authorization' : 'Basic ' + authorization}
data = {'grant_type': 'client_credentials'}
data_encoded = urllib.urlencode( data )
req = urllib2.Request(url, data_encoded, headers)
try:
response = urllib2.urlopen(req, timeout=30).read()
response = json.loads(response)
return response
except urllib2.HTTPError as e:
return e

View File

@ -1068,7 +1068,7 @@
$.ajax({ $.ajax({
method: 'GET', method: 'GET',
url: '//' + getState().mopidy.host + ':' + getState().mopidy.port + '/iris/http?action=refresh_spotify_token', url: '//' + getState().mopidy.host + ':' + getState().mopidy.port + '/iris/http/refresh_spotify_token',
dataType: "json", dataType: "json",
timeout: 10000 timeout: 10000
}).then(function (response) { }).then(function (response) {
@ -7629,7 +7629,7 @@
/* 66 */ /* 66 */
/***/ function(module, exports, __webpack_require__) { /***/ function(module, exports, __webpack_require__) {
var __WEBPACK_AMD_DEFINE_RESULT__;var require;/* WEBPACK VAR INJECTION */(function(process) {/** @license MIT License (c) copyright 2010-2014 original author or authors */ var require;var __WEBPACK_AMD_DEFINE_RESULT__;/* WEBPACK VAR INJECTION */(function(process) {/** @license MIT License (c) copyright 2010-2014 original author or authors */
/** @author Brian Cavalier */ /** @author Brian Cavalier */
/** @author John Hann */ /** @author John Hann */

File diff suppressed because one or more lines are too long

View File

@ -130,7 +130,7 @@ function refreshToken( dispatch, getState ){
$.ajax({ $.ajax({
method: 'GET', method: 'GET',
url: '//'+getState().mopidy.host+':'+getState().mopidy.port+'/iris/http?action=refresh_spotify_token', url: '//'+getState().mopidy.host+':'+getState().mopidy.port+'/iris/http/refresh_spotify_token',
dataType: "json", dataType: "json",
timeout: 10000 timeout: 10000
}) })