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

Implemented handshake

This commit is contained in:
Krateng 2020-10-29 16:46:45 +01:00
parent 5c2928e13b
commit 8cf93c3100
2 changed files with 19 additions and 19 deletions

View File

@ -69,6 +69,7 @@ class APIHandler:
else: else:
log("Unhandled Exception with " + self.__apiname__ + ": " + str(exceptiontype)) log("Unhandled Exception with " + self.__apiname__ + ": " + str(exceptiontype))
response.status,result = 500,{"status":"Unknown error","code":500} response.status,result = 500,{"status":"Unknown error","code":500}
raise
return result return result
#else: #else:

View File

@ -6,7 +6,7 @@ class AudioscrobblerLegacy(APIHandler):
__apiname__ = "Legacy Audioscrobbler" __apiname__ = "Legacy Audioscrobbler"
__doclink__ = "https://web.archive.org/web/20190531021725/https://www.last.fm/api/submissions" __doclink__ = "https://web.archive.org/web/20190531021725/https://www.last.fm/api/submissions"
__aliases__ = [ __aliases__ = [
"audioscrobbler/1.2", "audioscrobbler_legacy",
] ]
def init(self): def init(self):
@ -27,28 +27,27 @@ class AudioscrobblerLegacy(APIHandler):
} }
def get_method(self,pathnodes,keys): def get_method(self,pathnodes,keys):
return keys.get("method") if keys.get("hs") == 'true': return 'handshake'
def handshake(self,pathnodes,keys): def handshake(self,pathnodes,keys):
print(keys)
user = keys.get("u") user = keys.get("u")
auth = keys.get("a") auth = keys.get("a")
timestamp = keys.get("t") timestamp = keys.get("t")
apikey = keys.get("api_key") apikey = keys.get("api_key")
host = keys.get("Host") host = keys.get("Host")
protocol = 'https'
# expect username and password # expect username and password
if user is not None and apikey is None: if auth is not None:
receivedToken = lastfmToken(password, timestamp)
authenticated = False
for key in database.allAPIkeys(): for key in database.allAPIkeys():
if checkPassword(receivedToken, key, timestamp): if check_token(auth, key, timestamp):
authenticated = True sessionkey = generate_key(self.mobile_sessions)
break return 200, (
if authenticated: "OK\n"
sessionkey = generate_key(self.mobile_sessions) f"{sessionkey}\n"
return 200, "OK\n" + f"{protocol}://{host}/apis/audioscrobbler_legacy/nowplaying\n"
sessionkey + "\n" + f"{protocol}://{host}/apis/audioscrobbler_legacy/scrobble\n"
protocol + "://"+domain+":"+port+"/apis/legacyaudioscrobbler/nowplaying" + "\n" + )
protocol + "://"+domain+":"+port+"/apis/legacyaudioscrobbler/scrobble" + "\n"
else: else:
raise InvalidAuthException() raise InvalidAuthException()
else: else:
@ -103,9 +102,9 @@ def generate_key(ls):
ls.append(key) ls.append(key)
return key return key
def lastfmToken(password, ts): def lastfm_token(password, ts):
return md5(md5(password), ts) return md5(md5(password) + ts)
def checkPassword(receivedToken, expectedKey, ts): def check_token(received_token, expected_key, ts):
expectedToken = lastfmToken(expectedKey, ts) expected_token = lastfm_token(expected_key, ts)
return receivedToken == expectedToken return received_token == expected_token