Handle failed API requests, reauth when token expired

This commit is contained in:
Râu Cao 2023-11-22 21:08:01 +01:00
parent b51bbd446c
commit 35844b577c
Signed by: raucao
GPG Key ID: 15E65F399D084BA9
2 changed files with 75 additions and 48 deletions

View File

@ -54,11 +54,11 @@ function waitForInvoicePaid (room, nick, invoice, retries=0) {
} }
lndhub.getInvoice(invoice.payment_hash).then(async data => { lndhub.getInvoice(invoice.payment_hash).then(async data => {
if (data.is_paid) { if (data && data.is_paid) {
console.log(`Invoice paid: ${invoice.payment_hash}`); console.log(`Invoice paid: ${invoice.payment_hash}`);
await grantVoice(room, nick); await grantVoice(room, nick);
} else { } else {
setTimeout(waitForInvoicePaid, 3000, room, nick, invoice, retries++); if (!data) console.warn('Fetching invoice status failed');
setTimeout(waitForInvoicePaid, timeout, room, nick, invoice, retries++); setTimeout(waitForInvoicePaid, timeout, room, nick, invoice, retries++);
} }
}); });
@ -93,10 +93,14 @@ async function handleVoiceRequest (stanza) {
console.log(`${nick} requested voice in ${room}`); console.log(`${nick} requested voice in ${room}`);
const invoice = await lndhub.createInvoice(config.amounts.voice, `Donation for ${room}`); const invoice = await lndhub.createInvoice(config.amounts.voice, `Donation for ${room}`);
console.log(`Created lightning invoice for ${nick}: ${invoice.payment_hash}`) if (invoice) {
console.log(`Created lightning invoice for ${nick}: ${invoice.payment_hash}`)
await respondToVoiceRequest(room, nick, invoice); await respondToVoiceRequest(room, nick, invoice);
waitForInvoicePaid(room, nick, invoice); waitForInvoicePaid(room, nick, invoice);
} else {
console.warn(`Invoice creation failed!`);
// TODO notify ops contact
}
} catch(err) { } catch(err) {
console.error(err); console.error(err);
} }
@ -113,10 +117,14 @@ async function connectXmpp () {
} }
function connectLndhub () { function connectLndhub () {
return lndhub.auth(config.lndhub.username, config.lndhub.password).then(() => { return lndhub.auth(config.lndhub.username, config.lndhub.password).then(connected => {
console.log("Connected to Lndhub"); if (connected) {
console.log("Connected to Lndhub");
} else {
process.exit(1);
}
}).catch(err => { }).catch(err => {
console.warn(`Lndhub auth failed: ${err}`); console.error(`Lndhub connection failed: ${err}`);
}); });
} }

View File

@ -5,61 +5,80 @@ export default class Lndhub {
this.baseUrl = baseUrl; this.baseUrl = baseUrl;
} }
async callEndpoint (method, path, payload) {
const options = { method, headers: { 'Content-Type': 'application/json' } };
if (path !== '/auth') {
options.headers['Authorization'] = `Bearer ${this.accessToken}`;
}
if (typeof payload !== 'undefined') {
options.body = JSON.stringify(payload);
}
const res = await fetch(`${this.baseUrl}${path}`, options);
return res.json();
}
async handleErroredRequest (result, retryFunction, args=[]) {
console.warn('API request failed:', result.message);
if (result.code === 1) {
return this.reauth().then(connected => {
if (connected) {
console.warn('Lndhub reconnected, trying again...');
return this[retryFunction](...args);
}
});
} else {
return false;
}
}
async auth (username, password) { async auth (username, password) {
const payload = { "login": username, "password": password }; const payload = { "login": username, "password": password };
const data = await this.callEndpoint('post', '/auth', payload);
console.log('data', data);
const res = await fetch(`${this.baseUrl}/auth`, { if (data.error) {
method: 'post', console.warn('Lndhub connection failed:', data.message);
body: JSON.stringify(payload), return false;
headers: { 'Content-Type': 'application/json' } } else {
}); this.refreshToken = data.refresh_token;
const data = await res.json(); this.accessToken = data.access_token;
return true;
this.refreshToken = data.refresh_token; }
this.accessToken = data.access_token;
} }
async reauth (refreshToken) { async reauth (refreshToken) {
const payload = { "refresh_token": this.refreshToken }; const payload = { "refresh_token": this.refreshToken };
const data = await this.callEndpoint('post', '/auth', payload);
const res = await fetch(`${this.baseUrl}/auth`, { if (data.error) {
method: 'post', console.warn('Lndhub re-auth failed:', data.message);
body: JSON.stringify(payload), return false;
headers: { 'Content-Type': 'application/json' } } else {
}); this.accessToken = data.access_token;
const data = await res.json(); return true;
}
this.accessToken = data.access_token;
} }
async createInvoice (amount, description) { async createInvoice (amount, description) {
const payload = { amount, description }; const payload = { amount, description };
let data = await this.callEndpoint('post', '/v2/invoices', payload);
const res = await fetch(`${this.baseUrl}/v2/invoices`, { if (data.error) {
method: 'post', return this.handleErroredRequest(data, 'createInvoice', Array.from(arguments));
body: JSON.stringify(payload), } else {
headers: { return data;
'Content-Type': 'application/json', }
'Authorization': `Bearer ${this.accessToken}`
}
});
const data = await res.json();
// TODO re-auth if token has expired
return data;
} }
async getInvoice (paymentHash) { async getInvoice (paymentHash) {
const res = await fetch(`${this.baseUrl}/v2/invoices/${paymentHash}`, { const data = await this.callEndpoint('get', `/v2/invoices/${paymentHash}`);
method: 'get',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.accessToken}`
}
});
const data = await res.json();
// TODO re-auth if token has expired
return data; if (data.error) {
return this.handleErroredRequest(data, 'getInvoice', Array.from(arguments));
} else {
return data;
}
} }
} }