This commit is contained in:
counterweight 2025-02-13 00:02:40 +01:00
parent 768efaf3a2
commit fb9832fabb
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
8 changed files with 306 additions and 92 deletions

View file

@ -5,6 +5,9 @@ const crypto = require("crypto");
const invitesService = require('../services/invitesService');
const sessionService = require('../services/sessionService');
const nostrService = require('../services/nostrService');
const { error } = require('console');
const { TimeoutError } = require('sequelize');
const errors = require('../errors');
const router = express.Router();
@ -58,54 +61,40 @@ router.post('/sign-public-key-up', async (req, res) => {
});
router.get('/nostr-challenge', async (req, res) => {
const nostrChallenge = await nostrService.createNostrChallenge();
res.json({ 'challenge': nostrChallenge.challenge });
router.get('/signup/nostr-challenge', async (req, res) => {
const inviteUuid = req.cookies.inviteUuid;
const signUpChallenge = await invitesService.createSignUpChallenge(
inviteUuid
)
const relatedNostrChallenge = await nostrService.getNostrChallenge(
signUpChallenge.nostr_challenge_uuid
)
res.status(200).json({ 'challenge': relatedNostrChallenge.challenge });
});
router.post("/nostr-verify", async (req, res) => {
router.post("/signup/nostr-verify", async (req, res) => {
const signedEvent = req.body;
if (!signedEvent || !signedEvent.tags) {
return res.status(400).json({ success: false, error: "Invalid event format" });
try {
console.log(`Starting nostr-verify with event: ${signedEvent}`);
const completedSignUpChallenge = await invitesService.verifySignUpChallenge(signedEvent);
console.log(`Finished nostr-verify`);
} catch (error) {
if (error instanceof TimeoutError) {
console.error('The challenge is outdated.');
}
if (error instanceof errors.ChallengedUsedError) {
console.error('The challenge was already used, request a new one.');
}
if (error instanceof errors.InvalidSignatureError) {
console.error('Signature is not valid.')
}
}
const challengeTag = signedEvent.tags.find(tag => tag[0] === "challenge");
if (!challengeTag) {
return res.status(400).json({ success: false, error: "No challenge tag found" });
}
const challenge = challengeTag[1];
if (!(await nostrService.isNostrChallengeFresh(challenge))) {
return res.status(410).json({ success: false, error: "Challenge expired, request new one." })
}
if (await nostrService.hasNostrChallengeBeenCompleted(challenge)) {
return res.status(410).json({ success: false, error: "Challenge already used, request new one." })
}
const isSignatureValid = verifyEvent(signedEvent);
if (!isSignatureValid) {
return res.status(400).json({ success: false, error: "Invalid signature" });
}
if (!invitesService.isPublicKeyInvited(signedEvent.pubkey)) {
return res.status(400).json(
{
success: false,
error: "Valid signature, but npub is not invited to app."
}
)
}
await nostrService.completeNostrChallenge(
challenge,
signedEvent
)
return res.json({ success: true, signedEvent });
return res.status(200).json({ success: true });
});
module.exports = router;