1
0
mirror of https://github.com/krateng/maloja.git synced 2023-08-10 21:12:55 +03:00

More elegant client checking for scrobbles

This commit is contained in:
krateng 2022-04-06 21:08:14 +02:00
parent 36f7ab1670
commit de18ecff26
3 changed files with 12 additions and 22 deletions

View File

@ -14,21 +14,15 @@ log("Authenticated Machines: " + ", ".join([k for k in apikeystore]),module='api
# skip regular authentication if api key is present in request # skip regular authentication if api key is present in request
# an api key now ONLY permits scrobbling tracks, no other admin tasks # an api key now ONLY permits scrobbling tracks, no other admin tasks
def api_key_correct(request): def api_key_correct(request,args,kwargs):
request.malojaclient = None if "key" in kwargs:
args = request.params apikey = kwargs.pop("key")
try: elif "apikey" in kwargs:
args.update(request.json) apikey = kwargs.pop("apikey")
except:
pass
if "key" in args:
apikey = args.pop("key")
elif "apikey" in args:
apikey = args.pop("apikey")
else: return False else: return False
if checkAPIkey(apikey): if checkAPIkey(apikey):
request.malojaclient = [c for c in apikeystore if apikeystore[c]==apikey][0] client = [c for c in apikeystore if apikeystore[c]==apikey][0]
return True return {'client':client}
else: else:
return False return False

View File

@ -3,7 +3,7 @@ import os
from bottle import response, static_file, request, FormsDict from bottle import response, static_file, request, FormsDict
from doreah.logging import log from doreah.logging import log
from doreah.auth import authenticated_api, authenticated_api_with_alternate from doreah.auth import authenticated_api, authenticated_api_with_alternate, authenticated_function
# nimrodel API # nimrodel API
from nimrodel import EAPI as API from nimrodel import EAPI as API
@ -223,8 +223,8 @@ def compare_external(**keys):
@api.post("newscrobble") @api.post("newscrobble")
@authenticated_api_with_alternate(api_key_correct) @authenticated_function(alternate=api_key_correct,api=True,pass_auth_result_as='auth_result')
def post_scrobble(artist:Multi=None,**keys): def post_scrobble(artist:Multi=None,auth_result=None,**keys):
"""Submit a new scrobble. """Submit a new scrobble.
:param string artist: Artist. Can be submitted multiple times as query argument for multiple artists. :param string artist: Artist. Can be submitted multiple times as query argument for multiple artists.
@ -250,7 +250,7 @@ def post_scrobble(artist:Multi=None,**keys):
return database.incoming_scrobble( return database.incoming_scrobble(
rawscrobble, rawscrobble,
client=request.malojaclient, client='browser' if auth_result.get('doreah_native_auth_check') else auth_result.get('client'),
fix=(keys.get("nofix") is None) fix=(keys.get("nofix") is None)
) )
# TODO: malojaclient needs to be converted to proper argument in doreah # TODO: malojaclient needs to be converted to proper argument in doreah

View File

@ -86,11 +86,7 @@ coa = CollectorAgent()
## ##
## ##
def incoming_scrobble(rawscrobble,fix=True,client=None,dbconn=None,**kwargs): def incoming_scrobble(rawscrobble,fix=True,client=None,dbconn=None):
# TODO: just collecting all extra kwargs now. at some point, rework the authenticated api with alt function
# to actually look at the converted args instead of the request object and remove the key
# so that this function right here doesnt get the key passed to it
if (not "track_artists" in rawscrobble) or (len(rawscrobble['track_artists']) == 0) or (not "track_title" in rawscrobble): if (not "track_artists" in rawscrobble) or (len(rawscrobble['track_artists']) == 0) or (not "track_title" in rawscrobble):
log(f"Incoming scrobble {rawscrobble} [Source: {client}] is not valid") log(f"Incoming scrobble {rawscrobble} [Source: {client}] is not valid")