Add Webhooks and XMPP notifications for incoming sats #79

Merged
raucao merged 10 commits from feature/webhooks into master 2023-01-13 04:33:02 +00:00
6 changed files with 53 additions and 0 deletions
Showing only changes of commit 68e0d00f6e - Show all commits

View File

@@ -9,3 +9,5 @@ BTCPAY_API_URL='http://localhost:23001/api/v1'
LNDHUB_API_URL='http://localhost:3023'
LNDHUB_PUBLIC_URL='https://lndhub.kosmos.org'
WEBHOOKS_ALLOWED_IPS='10.1.1.163'

View File

@@ -5,3 +5,5 @@ BTCPAY_API_URL='http://10.1.1.163:23001/api/v1'
LNDHUB_LEGACY_API_URL='http://10.1.1.163:3026'
LNDHUB_API_URL='http://10.1.1.163:3026'
LNDHUB_PUBLIC_URL='https://lndhub.kosmos.org'
WEBHOOKS_ALLOWED_IPS='10.1.1.163'

View File

@@ -1,5 +1,9 @@
EJABBERD_API_URL='http://xmpp.example.com/api'
BTCPAY_API_URL='http://btcpay.example.com/api/v1'
LNDHUB_LEGACY_API_URL='http://localhost:3023'
LNDHUB_API_URL='http://localhost:3026'
LNDHUB_PUBLIC_URL='https://lndhub.kosmos.org'
WEBHOOKS_ALLOWED_IPS='10.1.1.23'

View File

@@ -0,0 +1,23 @@
class WebhooksController < ApplicationController
skip_forgery_protection
before_action :authorize_request
def lndhub
begin
payload = JSON.parse(request.body.read, symbolize_names: true)
raucao marked this conversation as resolved
Review

doesn't rails automatically parse the JSON because the proper content type is set?
so params[:type] and params[:user_login] should be enough?

doesn't rails automatically parse the JSON because the proper content type is set? so `params[:type]` and `params[:user_login]` should be enough?
Review

I didn't know, so I tried (also with explicitly setting the content type in the spec). Doesn't do it.

I didn't know, so I tried (also with explicitly setting the content type in the spec). Doesn't do it.
rescue
head :unprocessable_entity and return
end
head :ok
end
raucao marked this conversation as resolved Outdated
Outdated
Review

should not be the case, but this might be nil. maybe we should do a User.find_by!

should not be the case, but this might be nil. maybe we should do a `User.find_by!`

Good idea! Fails much cleaner and more expressively than when trying to access properties on nil later.

Good idea! Fails much cleaner and more expressively than when trying to access properties on `nil` later.
private
def authorize_request
if !ENV['WEBHOOKS_ALLOWED_IPS'].split(',').include?(request.remote_ip)
head :forbidden and return
end
end
end

View File

@@ -25,6 +25,8 @@ Rails.application.routes.draw do
get 'lnurlpay/:address', to: 'lnurlpay#index', constraints: { address: /[^\/]+/}
get 'lnurlpay/:address/invoice', to: 'lnurlpay#invoice', constraints: { address: /[^\/]+/}
post 'webhooks/lndhub', to: 'webhooks#lndhub'
namespace :api do
get 'kredits/onchain_btc_balance', to: 'kredits#onchain_btc_balance'
end

View File

@@ -0,0 +1,20 @@
require 'rails_helper'
RSpec.describe "Webhooks", type: :request do
describe "Allowed IP addresses" do
context "IP not allowed" do
it "returns a 403 status" do
post "/webhooks/lndhub"
expect(response).to have_http_status(:forbidden)
end
end
context "IP allowed" do
it "returns a 403 status" do
ENV['WEBHOOKS_ALLOWED_IPS'] = '127.0.0.1'
post "/webhooks/lndhub"
expect(response).to have_http_status(:unprocessable_entity)
end
end
end
end