119 lines
No EOL
3.2 KiB
JavaScript
119 lines
No EOL
3.2 KiB
JavaScript
const uuid = require("uuid");
|
|
const crypto = require("crypto");
|
|
const { Op, TimeoutError } = require('sequelize');
|
|
const { verifyEvent } = require("nostr-tools");
|
|
|
|
const NostrChallengeCreated = require('../models/NostrChallengeCreated');
|
|
const NostrChallengeCompleted = require("../models/NostrChallengeCompleted");
|
|
|
|
const constants = require('../constants');
|
|
const errors = require('../errors');
|
|
|
|
async function createNostrChallenge() {
|
|
|
|
const currentTimestamp = new Date();
|
|
const expiryTimestamp = new Date(currentTimestamp.getTime());
|
|
expiryTimestamp.setSeconds(expiryTimestamp.getSeconds() + constants.DEFAULT_NOSTR_CHALLENGE_DURATION_SECONDS);
|
|
|
|
const nostrChallenge = await NostrChallengeCreated.create({
|
|
'uuid': uuid.v7(),
|
|
challenge: crypto.randomBytes(32).toString("hex"),
|
|
expires_at: expiryTimestamp.toISOString(),
|
|
created_at: currentTimestamp.toISOString()
|
|
});
|
|
|
|
return nostrChallenge;
|
|
}
|
|
|
|
async function getNostrChallenge(nostrChallengeUuid = null, challenge = null) {
|
|
|
|
if (nostrChallengeUuid) {
|
|
return await NostrChallengeCreated.findOne({
|
|
where: {
|
|
'uuid': nostrChallengeUuid
|
|
}
|
|
})
|
|
}
|
|
|
|
if (challenge) {
|
|
return await NostrChallengeCreated.findOne({
|
|
where: {
|
|
challenge
|
|
}
|
|
})
|
|
}
|
|
|
|
throw Error('You need to pass a uuid or a challenge.')
|
|
|
|
}
|
|
|
|
async function verifyNostrChallenge(signedEvent) {
|
|
const challengeTag = signedEvent.tags.find(tag => tag[0] === "challenge");
|
|
const challenge = challengeTag[1];
|
|
|
|
console.log("Checking if fresh")
|
|
if (!(await isNostrChallengeFresh(challenge))) {
|
|
throw TimeoutError("Challenge expired, request new one.");
|
|
}
|
|
|
|
console.log("Checking if completed")
|
|
if (await hasNostrChallengeBeenCompleted(challenge)) {
|
|
throw new errors.AlreadyUsedError("Challenge already used, request new one.");
|
|
}
|
|
|
|
console.log("Checking if valid")
|
|
const isSignatureValid = verifyEvent(signedEvent);
|
|
if (!isSignatureValid) {
|
|
throw new errors.InvalidSignatureError("Signature is not valid.");
|
|
}
|
|
|
|
console.log("Persisting")
|
|
return await NostrChallengeCompleted.create({
|
|
'uuid': uuid.v7(),
|
|
challenge: challenge,
|
|
signed_event: signedEvent,
|
|
public_key: signedEvent.pubkey,
|
|
created_at: new Date().toISOString()
|
|
}
|
|
);
|
|
}
|
|
|
|
async function isNostrChallengeFresh(challengeString) {
|
|
const nostrChallenge = await NostrChallengeCreated.findOne({
|
|
where: {
|
|
challenge: challengeString,
|
|
expires_at: {
|
|
[Op.gt]: new Date()
|
|
}
|
|
}
|
|
});
|
|
|
|
if (nostrChallenge) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
async function hasNostrChallengeBeenCompleted(challengeString) {
|
|
const completedNostrChallenge = await NostrChallengeCompleted.findOne(
|
|
{
|
|
where: {
|
|
challenge: challengeString
|
|
}
|
|
}
|
|
);
|
|
|
|
if (completedNostrChallenge) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
module.exports = {
|
|
createNostrChallenge,
|
|
getNostrChallenge,
|
|
verifyNostrChallenge,
|
|
isNostrChallengeFresh,
|
|
hasNostrChallengeBeenCompleted
|
|
}; |