Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"lmdb": "^2.9.1",
"lnsocket": "^0.3.3",
"nostr": "^0.2.8",
"nostr-tools": "^2.3.1",
"nostr-tools": "^2.12.0",
"uuid": "^9.0.1",
"ws": "^8.16.0"
},
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const express = require('express')
const debug = require('debug')('api')
const { PurpleInvoiceManager } = require('./invoicing')
const { WebAuthManager } = require('./web_auth')
const { SimplePool } = require('nostr-tools/pool')

const ENV_VARS = ["LN_NODE_ID", "LN_NODE_ADDRESS", "LN_RUNE", "LN_WS_PROXY", "DEEPL_KEY", "DB_PATH", "NOTEDECK_INSTALL_MD"]

Expand Down Expand Up @@ -53,7 +54,8 @@ function PurpleApi(opts = {}) {
this.invoice_manager = new PurpleInvoiceManager(this, process.env.LN_NODE_ID, process.env.LN_NODE_ADDRESS, process.env.LN_RUNE, process.env.LN_WS_PROXY)
debug("loaded invoice-manager node_id:%s node_addr:%s rune:%s proxy:%s", process.env.LN_NODE_ID, process.env.LN_NODE_ADDRESS, process.env.LN_RUNE, process.env.LN_WS_PROXY)
this.invoice_manager.connect_and_init()
this.web_auth_manager = new WebAuthManager(dbs)
this.pool = new SimplePool()
this.web_auth_manager = new WebAuthManager(dbs, this.pool)

return this
}
Expand Down
21 changes: 10 additions & 11 deletions src/web_auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ const { nip04 } = require('nostr-tools')
const { v4: uuidv4 } = require('uuid')
const { current_time } = require('./utils')
const { finalizeEvent } = require('nostr-tools/pure')
const { Relay, useWebSocketImplementation } = require('nostr-tools/relay')
const { unauthorized_response } = require('./server_helpers')
const { useWebSocketImplementation } = require('nostr-tools/pool')
useWebSocketImplementation(require('ws'))
const { SimplePool } = require('nostr-tools/pool')

const DEFAULT_SESSION_EXPIRY = 60 * 60 * 24 * 7 // 1 week
const DEFAULT_OTP_MAX_TRIES = 10 // 10 tries before an OTP is invalidated
Expand All @@ -19,9 +20,11 @@ const DEFAULT_OTP_EXPIRY = 60 * 5 // 5 minutes
class WebAuthManager {
/** Initializes the WebAuthManager
* @param {object} dbs - The PurpleApi dbs object
* @param {SimplePool} pool - The relay pool to use for sending events
*/
constructor(dbs) {
constructor(dbs, pool) {
this.dbs = dbs
this.pool = pool
this.otp_max_tries = process.env.OTP_MAX_TRIES || DEFAULT_OTP_MAX_TRIES
this.session_expiry = process.env.SESSION_EXPIRY || DEFAULT_SESSION_EXPIRY
this.otp_expiry = process.env.OTP_EXPIRY || DEFAULT_OTP_EXPIRY
Expand Down Expand Up @@ -63,14 +66,10 @@ class WebAuthManager {

const signed_event = finalizeEvent(event, secret_key);

for (let relay_url of relays) {
try {
const relay = await Relay.connect(relay_url);
await relay.publish(signed_event);
relay.close();
} catch (error) {
console.error(`Error sending OTP via relay ${relay_url}:`, error);
}
try {
await this.pool.publish(relays, signed_event)
} catch (error) {
console.error(`Error sending OTP:`, error);
}
}

Expand Down Expand Up @@ -106,7 +105,7 @@ class WebAuthManager {
// MARK: Utils

/**
* Generates a random 6-digit OTP code
* Generates a random 6-digit OTP code
* @returns {string} The generated OTP code
*/
random_otp() {
Expand Down
1 change: 1 addition & 0 deletions test/controllers/purple_test_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class PurpleTestController {
process.env.SESSION_EXPIRY = 60*60*24*7
process.env.OTP_EXPIRY = 60*5
process.env.TESTFLIGHT_URL = "https://testflight.apple.com/join/abc123"
process.env.NOTEDECK_INSTALL_MD = "./notedeck-install-instructions.md"
}

setup_stubs() {
Expand Down
Loading