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

Added simple API key

This commit is contained in:
Krateng 2018-11-30 15:44:30 +01:00
parent 75a70a10b7
commit 19dc86d32a
5 changed files with 32 additions and 15 deletions

1
clients/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
authenticated_machines.tsv

2
clients/example_file.tsv Normal file
View File

@ -0,0 +1,2 @@
# Only the entries in authenticated_machines.tsv are used, this is an example file
YDzcmp8JpYHCcvJbDOVT7nEDoyCEND6K Example Machine
Can't render this file because it has a wrong number of fields in line 2.

View File

@ -5,6 +5,7 @@ import waitress
import os import os
import datetime import datetime
from cleanup import * from cleanup import *
from utilities import *
import sys import sys
@ -15,22 +16,18 @@ TRACKS = [] # Format: tuple(frozenset(artist_ref,...),title)
timestamps = set() timestamps = set()
c = CleanerAgent() c = CleanerAgent()
clients = []
lastsync = 0 lastsync = 0
# by id ### symmetric keys are fine for now since we hopefully use HTTPS
#def getScrobbleObject(o): def loadAPIkeys():
# #return {"artists":getTrackObject(SCROBBLES[o][0])["artists"],"title":getTrackObject(SCROBBLES[o][0])["title"],"time":SCROBBLES[o][1],"saved":SCROBBLES[o][2]} global clients
# return {"artists":getTrackObject(SCROBBLES[o][0])["artists"],"title":getTrackObject(SCROBBLES[o][0])["title"],"time":SCROBBLES[o][1]} clients = parseTSV("clients/authenticated_machines.tsv","string","string")
#
#def getArtistObject(o):
# return ARTISTS[o]
#
#def getTrackObject(o):
# return {"artists":[getArtistObject(a) for a in TRACKS[o][0]],"title":TRACKS[o][1]}
# by object def checkAPIkey(k):
return (k in [k for [k,d] in clients])
def getScrobbleObject(o): def getScrobbleObject(o):
track = getTrackObject(TRACKS[o[0]]) track = getTrackObject(TRACKS[o[0]])
@ -149,6 +146,11 @@ def post_scrobble():
keys = FormsDict.decode(request.forms) # The Dal★Shabet handler keys = FormsDict.decode(request.forms) # The Dal★Shabet handler
artists = keys.get("artist") artists = keys.get("artist")
title = keys.get("title") title = keys.get("title")
apikey = keys.get("key")
if not (checkAPIkey(apikey)):
response.status = 403
return ""
try: try:
time = int(keys.get("time")) time = int(keys.get("time"))
except: except:
@ -178,6 +180,8 @@ def runserver(DATABASE_PORT):
#buildh() #buildh()
build_db() build_db()
loadAPIkeys()
run(host='0.0.0.0', port=DATABASE_PORT, server='waitress') run(host='0.0.0.0', port=DATABASE_PORT, server='waitress')

View File

@ -210,9 +210,10 @@ function scrobble(artist,title,seconds) {
console.log("Scrobbling " + artist + " - " + title + "; " + seconds + " seconds playtime") console.log("Scrobbling " + artist + " - " + title + "; " + seconds + " seconds playtime")
artiststring = encodeURIComponent(artist) artiststring = encodeURIComponent(artist)
titlestring = encodeURIComponent(title) titlestring = encodeURIComponent(title)
APIKEY = "YDzcmp8JpYHCcvJbDOVT7nEDoyCEND6K" ///obviously this will not be hardcoded later
var xhttp = new XMLHttpRequest(); var xhttp = new XMLHttpRequest();
xhttp.open("POST","http://localhost:42010/db/newscrobble",true); xhttp.open("POST","http://localhost:42010/db/newscrobble",true);
xhttp.send("artist=" + artiststring + "&title=" + titlestring + "&duration=" + seconds) xhttp.send("artist=" + artiststring + "&title=" + titlestring + "&duration=" + seconds + "&key=" + APIKEY)
} }
function setUpdate() { function setUpdate() {

View File

@ -4,6 +4,7 @@ import _thread
import waitress import waitress
import urllib.request import urllib.request
import urllib.parse import urllib.parse
from urllib.error import *
import sys import sys
import signal import signal
@ -23,7 +24,7 @@ def mainpage():
# e.g. location /db { rewrite ^/db(.*)$ $1 break; proxy_pass http://yoururl:12349; } # e.g. location /db { rewrite ^/db(.*)$ $1 break; proxy_pass http://yoururl:12349; }
@get("/db/<pth:path>") @get("/db/<pth:path>")
def database(pth): def database_get(pth):
keys = FormsDict.decode(request.query) # The Dal★Shabet handler keys = FormsDict.decode(request.query) # The Dal★Shabet handler
keystring = "?" keystring = "?"
for k in keys: for k in keys:
@ -35,8 +36,16 @@ def database(pth):
return contents return contents
@post("/db/<pth:path>") @post("/db/<pth:path>")
def database(pth): def database_post(pth):
contents = urllib.request.urlopen("http://localhost:" + str(DATABASE_PORT) + "/" + pth,request.body).read() try:
proxyresponse = urllib.request.urlopen("http://localhost:" + str(DATABASE_PORT) + "/" + pth,request.body)
contents = proxyresponse.read()
response.status = proxyresponse.getcode()
except HTTPError as e:
contents = ""
response.status = e.code
response.content_type = "application/json" response.content_type = "application/json"
response.set_header("Access-Control-Allow-Origin","*") response.set_header("Access-Control-Allow-Origin","*")
return contents return contents