def delete(self, id): data = secrets.get_all("api-keys") for x in data: if x["key"] == id: data.remove(x) secrets.save() break return Response(status=204)
def verify(token=None): """ Verify a provided JSON Web Token (JWT) for authentication. :param str token: JSON Web Token (JWT) :returns: True if valid, False if not """ if config.get("genesis", "anonymous"): return if request.headers.get("X-API-Key", None): api_key = request.headers.get("X-API-Key") data = secrets.get_all("api-keys") for x in data: if x["key"] == api_key: user = users.get(name=x["user"]) if not user or not user.admin: resp = jsonify(message="Authorization required") resp.status_code = 401 return resp else: return if not token: token = request.headers.get("Authorization", None) if not token: resp = jsonify(message="Authorization required") resp.status_code = 401 return resp token = token.split() if token[0] != "Bearer" or len(token) > 2: resp = jsonify(message="Malformed token") resp.status_code = 400 return resp token = token[1] try: tjwss = TimedJSONWebSignatureSerializer( secret_key=current_app.config["SECRET_KEY"], expires_in=3600, algorithm_name="HS256") payload = tjwss.loads(token) except SignatureExpired: resp = jsonify(message="Token expired") resp.status_code = 401 return resp except BadSignature: resp = jsonify(message="Malformed token signature") resp.status_code = 401 return resp user = users.get(name=payload["uid"]) if not user or not user.admin: resp = jsonify(message="Authorization required") resp.status_code = 401 return resp
def revoke(key): """Revoke an API key.""" try: data = secrets.get_all("api-keys") for x in data: if x["key"] == key: data.remove(x) secrets.save() break logger.info('ctl:keys:revoke', 'API key revoked') except Exception as e: raise CLIException(str(e))
def list_keys(): """List all API keys.""" try: keys = secrets.get_all("api-keys") if not keys: logger.info('ctl:keys:list', 'No keys found') return llen = len(sorted(keys, key=lambda x: len(x["user"]))[-1].name) for x in keys: click.echo( click.style( '{name: <45}'.format( name=x["key"]), fg="white", bold=True) + click.style('{name: <{fill}}'.format(name=x["user"], fill=llen + 3), fg="green") + " " + click.style(x["comment"], fg="yellow")) except Exception as e: raise CLIException(str(e))
def get(self): keys = secrets.get_all("api-keys") return jsonify(api_keys=keys)