From 0bdc4654bfb0f42d838e15c3d36dab0b4472db00 Mon Sep 17 00:00:00 2001 From: Krateng Date: Wed, 5 Aug 2020 20:35:42 +0200 Subject: [PATCH] Added validate token endpoint to ListenBrainz API --- maloja/__pkginfo__.py | 2 +- maloja/compliant_api.py | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/maloja/__pkginfo__.py b/maloja/__pkginfo__.py index 3896945..f22f979 100644 --- a/maloja/__pkginfo__.py +++ b/maloja/__pkginfo__.py @@ -5,7 +5,7 @@ author = { "email":"maloja@krateng.dev", "github": "krateng" } -version = 2,6,6 +version = 2,6,7 versionstr = ".".join(str(n) for n in version) links = { "pypi":"malojaserver", diff --git a/maloja/compliant_api.py b/maloja/compliant_api.py index 01cdb92..8b912e5 100644 --- a/maloja/compliant_api.py +++ b/maloja/compliant_api.py @@ -167,7 +167,8 @@ class GNUFM2(APIHandler): class LBrnz1(APIHandler): def __init__(self): self.methods = { - "submit-listens":self.submit + "submit-listens":self.submit, + "validate-token":self.validate_token } self.errors = { BadAuthException:(401,{"code":401,"error":"You need to provide an Authorization header."}), @@ -190,6 +191,7 @@ class LBrnz1(APIHandler): raise InvalidAuthException() try: + log("scrobbling to listenbrainz, keys "+str(keys),module="debug") if keys["listen_type"] in ["single","import"]: payload = keys["payload"] for listen in payload: @@ -202,8 +204,18 @@ class LBrnz1(APIHandler): timestamp = int(datetime.datetime.now(tz=datetime.timezone.utc).timestamp()) #database.createScrobble(artists,title,timestamp) scrobbletrack(artiststr,titlestr,timestamp) - return 200,{"code":200,"status":"ok"} + return 200,{"code":200,"status":"ok"} else: return 200,{"code":200,"status":"ok"} except: raise MalformedJSONException() + + def validate_token(self,pathnodes,keys): + try: + token = keys.get("Authorization").replace("token ","").replace("Token ","").strip() + except: + raise BadAuthException() + if token not in database.allAPIkeys(): + raise InvalidAuthException() + else: + return 200,{"code":200,"message":"Token valid.",valid:True}