From fd4dddda6ebf533631cb5d17f3406e3ca72ff01a Mon Sep 17 00:00:00 2001 From: Eneko Illarramendi Date: Thu, 16 Apr 2020 17:27:36 +0200 Subject: [PATCH] chore: rename api-key-macaroon --- lnbits/core/templates/core/_api_docs.html | 6 +++--- lnbits/core/views/api.py | 10 +++++----- lnbits/decorators.py | 6 +++--- lnbits/extensions/amilk/views_api.py | 8 ++++---- lnbits/extensions/paywall/views_api.py | 8 ++++---- lnbits/extensions/tpos/views_api.py | 14 +++++++------- lnbits/extensions/withdraw/views_api.py | 10 +++++----- lnbits/static/js/base.js | 4 ++-- 8 files changed, 33 insertions(+), 33 deletions(-) diff --git a/lnbits/core/templates/core/_api_docs.html b/lnbits/core/templates/core/_api_docs.html index 2899894..e500dcb 100644 --- a/lnbits/core/templates/core/_api_docs.html +++ b/lnbits/core/templates/core/_api_docs.html @@ -9,7 +9,7 @@ POST /api/v1/payments
Headers
- {"api_key": "{{ wallet.inkey }}"}
+ {"X-Api-Key": "{{ wallet.inkey }}"}
Body (application/json)
{"out": false, "amount": <int>, "memo": <string>}
Returns 201 CREATED (application/json)
@@ -24,7 +24,7 @@ POST /api/v1/payments
Headers
- {"api_key": "{{ wallet.adminkey }}"} + {"X-Api-Key": "{{ wallet.adminkey }}"}
Body (application/json)
{"out": true, "bolt11": <string>}
Returns 201 CREATED (application/json)
@@ -40,7 +40,7 @@ GET /api/v1/payments/<checking_id>
Headers
- {"api_key": "{{ wallet.inkey }}"} + {"X-Api-Key": "{{ wallet.inkey }}"}
Returns 200 OK (application/json)
{"paid": <bool>}
Curl example
diff --git a/lnbits/core/views/api.py b/lnbits/core/views/api.py index 63d7536..ed3b7f0 100644 --- a/lnbits/core/views/api.py +++ b/lnbits/core/views/api.py @@ -1,7 +1,7 @@ from flask import g, jsonify, request from lnbits.core import core_app -from lnbits.decorators import api_check_wallet_macaroon, api_validate_post_request +from lnbits.decorators import api_check_wallet_key, api_validate_post_request from lnbits.helpers import Status from lnbits.settings import WALLET @@ -9,7 +9,7 @@ from ..services import create_invoice, pay_invoice @core_app.route("/api/v1/payments", methods=["GET"]) -@api_check_wallet_macaroon(key_type="invoice") +@api_check_wallet_key("invoice") def api_payments(): if "check_pending" in request.args: for payment in g.wallet.get_payments(include_all_pending=True): @@ -21,7 +21,7 @@ def api_payments(): return jsonify(g.wallet.get_payments()), Status.OK -@api_check_wallet_macaroon(key_type="invoice") +@api_check_wallet_key("invoice") @api_validate_post_request( schema={ "amount": {"type": "integer", "min": 1, "required": True}, @@ -39,7 +39,7 @@ def api_payments_create_invoice(): return jsonify({"checking_id": checking_id, "payment_request": payment_request}), Status.CREATED -@api_check_wallet_macaroon(key_type="admin") +@api_check_wallet_key("admin") @api_validate_post_request(schema={"bolt11": {"type": "string", "empty": False, "required": True}}) def api_payments_pay_invoice(): try: @@ -63,7 +63,7 @@ def api_payments_create(): @core_app.route("/api/v1/payments/", methods=["GET"]) -@api_check_wallet_macaroon(key_type="invoice") +@api_check_wallet_key("invoice") def api_payment(checking_id): payment = g.wallet.get_payment(checking_id) diff --git a/lnbits/decorators.py b/lnbits/decorators.py index 827216d..ee5fbcc 100644 --- a/lnbits/decorators.py +++ b/lnbits/decorators.py @@ -8,14 +8,14 @@ from lnbits.core.crud import get_user, get_wallet_for_key from .helpers import Status -def api_check_wallet_macaroon(*, key_type: str = "invoice"): +def api_check_wallet_key(key_type: str = "invoice"): def wrap(view): @wraps(view) def wrapped_view(**kwargs): try: - g.wallet = get_wallet_for_key(request.headers["api_key"], key_type) + g.wallet = get_wallet_for_key(request.headers["X-Api-Key"], key_type) except KeyError: - return jsonify({"message": "`api_key` header missing."}), Status.BAD_REQUEST + return jsonify({"message": "`X-Api-Key` header missing."}), Status.BAD_REQUEST if not g.wallet: return jsonify({"message": "Wrong keys."}), Status.UNAUTHORIZED diff --git a/lnbits/extensions/amilk/views_api.py b/lnbits/extensions/amilk/views_api.py index 15a722c..e3fe610 100644 --- a/lnbits/extensions/amilk/views_api.py +++ b/lnbits/extensions/amilk/views_api.py @@ -1,7 +1,7 @@ from flask import g, jsonify, request from lnbits.core.crud import get_user -from lnbits.decorators import api_check_wallet_macaroon, api_validate_post_request +from lnbits.decorators import api_check_wallet_key, api_validate_post_request from lnbits.helpers import Status from lnbits.extensions.amilk import amilk_ext @@ -9,7 +9,7 @@ from .crud import create_amilk, get_amilk, get_amilks, delete_amilk @amilk_ext.route("/api/v1/amilk", methods=["GET"]) -@api_check_wallet_macaroon(key_type="invoice") +@api_check_wallet_key("invoice") def api_amilks(): wallet_ids = [g.wallet.id] @@ -20,7 +20,7 @@ def api_amilks(): @amilk_ext.route("/api/v1/amilk", methods=["POST"]) -@api_check_wallet_macaroon(key_type="invoice") +@api_check_wallet_key("invoice") @api_validate_post_request(schema={ "url": {"type": "string", "empty": False, "required": True}, "memo": {"type": "string", "empty": False, "required": True}, @@ -33,7 +33,7 @@ def api_amilk_create(): @amilk_ext.route("/api/v1/amilk/", methods=["DELETE"]) -@api_check_wallet_macaroon(key_type="invoice") +@api_check_wallet_key("invoice") def api_amilk_delete(amilk_id): amilk = get_amilk(amilk_id) diff --git a/lnbits/extensions/paywall/views_api.py b/lnbits/extensions/paywall/views_api.py index cea836d..6bd0f26 100644 --- a/lnbits/extensions/paywall/views_api.py +++ b/lnbits/extensions/paywall/views_api.py @@ -1,7 +1,7 @@ from flask import g, jsonify, request from lnbits.core.crud import get_user -from lnbits.decorators import api_check_wallet_macaroon, api_validate_post_request +from lnbits.decorators import api_check_wallet_key, api_validate_post_request from lnbits.helpers import Status from lnbits.extensions.paywall import paywall_ext @@ -9,7 +9,7 @@ from .crud import create_paywall, get_paywall, get_paywalls, delete_paywall @paywall_ext.route("/api/v1/paywalls", methods=["GET"]) -@api_check_wallet_macaroon(key_type="invoice") +@api_check_wallet_key("invoice") def api_paywalls(): wallet_ids = [g.wallet.id] @@ -20,7 +20,7 @@ def api_paywalls(): @paywall_ext.route("/api/v1/paywalls", methods=["POST"]) -@api_check_wallet_macaroon(key_type="invoice") +@api_check_wallet_key("invoice") @api_validate_post_request(schema={ "url": {"type": "string", "empty": False, "required": True}, "memo": {"type": "string", "empty": False, "required": True}, @@ -33,7 +33,7 @@ def api_paywall_create(): @paywall_ext.route("/api/v1/paywalls/", methods=["DELETE"]) -@api_check_wallet_macaroon(key_type="invoice") +@api_check_wallet_key("invoice") def api_paywall_delete(paywall_id): paywall = get_paywall(paywall_id) diff --git a/lnbits/extensions/tpos/views_api.py b/lnbits/extensions/tpos/views_api.py index 90535eb..3f7cb0c 100644 --- a/lnbits/extensions/tpos/views_api.py +++ b/lnbits/extensions/tpos/views_api.py @@ -2,7 +2,7 @@ from flask import g, jsonify, request from lnbits.core.crud import get_user from lnbits.core.services import create_invoice -from lnbits.decorators import api_check_wallet_macaroon, api_validate_post_request +from lnbits.decorators import api_check_wallet_key, api_validate_post_request from lnbits.helpers import Status from lnbits.settings import WALLET @@ -11,7 +11,7 @@ from .crud import create_tpos, get_tpos, get_tposs, delete_tpos @tpos_ext.route("/api/v1/tposs", methods=["GET"]) -@api_check_wallet_macaroon(key_type="invoice") +@api_check_wallet_key("invoice") def api_tposs(): wallet_ids = [g.wallet.id] @@ -22,7 +22,7 @@ def api_tposs(): @tpos_ext.route("/api/v1/tposs", methods=["POST"]) -@api_check_wallet_macaroon(key_type="invoice") +@api_check_wallet_key("invoice") @api_validate_post_request( schema={ "name": {"type": "string", "empty": False, "required": True}, @@ -38,7 +38,7 @@ def api_tpos_create(): @tpos_ext.route("/api/v1/tposs/", methods=["DELETE"]) -@api_check_wallet_macaroon(key_type="invoice") +@api_check_wallet_key("invoice") def api_tpos_delete(tpos_id): tpos = get_tpos(tpos_id) @@ -58,13 +58,13 @@ def api_tpos_delete(tpos_id): def api_tpos_create_invoice(tpos_id): tpos = get_tpos(tpos_id) - + if not tpos: return jsonify({"message": "TPoS does not exist."}), Status.NOT_FOUND try: memo = f"TPoS {tpos_id}" checking_id, payment_request = create_invoice(wallet_id=tpos.wallet, amount=g.data["amount"], memo=memo) - + except Exception as e: return jsonify({"message": str(e)}), Status.INTERNAL_SERVER_ERROR @@ -74,7 +74,7 @@ def api_tpos_create_invoice(tpos_id): def api_tpos_check_invoice(checking_id): print(checking_id) PAID = WALLET.get_invoice_status(checking_id).paid - + if PAID == True: return jsonify({"PAID": True}), Status.OK return jsonify({"PAID": False}), Status.OK diff --git a/lnbits/extensions/withdraw/views_api.py b/lnbits/extensions/withdraw/views_api.py index cd30abd..b8b0edf 100644 --- a/lnbits/extensions/withdraw/views_api.py +++ b/lnbits/extensions/withdraw/views_api.py @@ -3,7 +3,7 @@ from flask import g, jsonify, request from lnbits.core.crud import get_user, get_wallet from lnbits.core.services import pay_invoice -from lnbits.decorators import api_check_wallet_macaroon, api_validate_post_request +from lnbits.decorators import api_check_wallet_key, api_validate_post_request from lnbits.helpers import urlsafe_short_hash, Status from lnbits.extensions.withdraw import withdraw_ext @@ -18,7 +18,7 @@ from .crud import ( @withdraw_ext.route("/api/v1/links", methods=["GET"]) -@api_check_wallet_macaroon(key_type="invoice") +@api_check_wallet_key("invoice") def api_links(): wallet_ids = [g.wallet.id] @@ -29,7 +29,7 @@ def api_links(): @withdraw_ext.route("/api/v1/links/", methods=["GET"]) -@api_check_wallet_macaroon(key_type="invoice") +@api_check_wallet_key("invoice") def api_link_retrieve(link_id): link = get_withdraw_link(link_id) @@ -44,7 +44,7 @@ def api_link_retrieve(link_id): @withdraw_ext.route("/api/v1/links", methods=["POST"]) @withdraw_ext.route("/api/v1/links/", methods=["PUT"]) -@api_check_wallet_macaroon(key_type="invoice") +@api_check_wallet_key("invoice") @api_validate_post_request( schema={ "title": {"type": "string", "empty": False, "required": True}, @@ -79,7 +79,7 @@ def api_link_create(link_id=None): @withdraw_ext.route("/api/v1/links/", methods=["DELETE"]) -@api_check_wallet_macaroon(key_type="invoice") +@api_check_wallet_key("invoice") def api_link_delete(link_id): link = get_withdraw_link(link_id) diff --git a/lnbits/static/js/base.js b/lnbits/static/js/base.js index 0562309..60b25be 100644 --- a/lnbits/static/js/base.js +++ b/lnbits/static/js/base.js @@ -4,12 +4,12 @@ var EventHub = new Vue(); var LNbits = { api: { - request: function (method, url, macaroon, data) { + request: function (method, url, apiKey, data) { return axios({ method: method, url: url, headers: { - 'api_key': macaroon + 'X-Api-Key': apiKey }, data: data });