maloja/maloja/apis/_apikeys.py

34 lines
975 B
Python
Raw Normal View History

### API KEYS
### symmetric keys are fine since we hopefully use HTTPS
from doreah.keystore import KeyStore
from doreah.logging import log
from ..globalconf import data_dir
apikeystore = KeyStore(file=data_dir['clients']("apikeys.yml"),save_endpoint="/apis/mlj_1/apikeys")
from .. import upgrade
upgrade.upgrade_apikeys()
log("Authenticated Machines: " + ", ".join([k for k in apikeystore]),module='apis')
# skip regular authentication if api key is present in request
# an api key now ONLY permits scrobbling tracks, no other admin tasks
def api_key_correct(request,args,kwargs):
if "key" in kwargs:
apikey = kwargs.pop("key")
elif "apikey" in kwargs:
apikey = kwargs.pop("apikey")
else: return False
2022-02-14 08:07:54 +03:00
if checkAPIkey(apikey):
client = [c for c in apikeystore if apikeystore[c]==apikey][0]
return {'client':client}
2022-02-14 08:07:54 +03:00
else:
return False
def checkAPIkey(key):
return apikeystore.check_key(key)
def allAPIkeys():
return [apikeystore[k] for k in apikeystore]