125 lines
3.2 KiB
JavaScript
125 lines
3.2 KiB
JavaScript
const uuid = require('uuid');
|
|
const crypto = require('crypto');
|
|
const { Op, TimeoutError } = require('sequelize');
|
|
const { verifyEvent } = require('nostr-tools');
|
|
|
|
const models = require('../models');
|
|
|
|
const constants = require('../constants');
|
|
const errors = require('../errors');
|
|
|
|
class NostrServiceProvider {
|
|
constructor() {}
|
|
|
|
provide() {
|
|
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 models.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 models.NostrChallengeCreated.findOne({
|
|
where: {
|
|
uuid: nostrChallengeUuid,
|
|
},
|
|
});
|
|
}
|
|
|
|
if (challenge) {
|
|
return await models.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];
|
|
|
|
if (!(await isNostrChallengeFresh(challenge))) {
|
|
throw TimeoutError('Challenge expired, request new one.');
|
|
}
|
|
|
|
if (await hasNostrChallengeBeenCompleted(challenge)) {
|
|
throw new errors.AlreadyUsedError(
|
|
'Challenge already used, request new one.'
|
|
);
|
|
}
|
|
|
|
const isSignatureValid = verifyEvent(signedEvent);
|
|
if (!isSignatureValid) {
|
|
throw new errors.InvalidSignatureError('Signature is not valid.');
|
|
}
|
|
|
|
return await models.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 models.NostrChallengeCreated.findOne({
|
|
where: {
|
|
challenge: challengeString,
|
|
expires_at: {
|
|
[Op.gt]: new Date(),
|
|
},
|
|
},
|
|
});
|
|
|
|
if (nostrChallenge) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
async function hasNostrChallengeBeenCompleted(challengeString) {
|
|
const completedNostrChallenge =
|
|
await models.NostrChallengeCompleted.findOne({
|
|
where: {
|
|
challenge: challengeString,
|
|
},
|
|
});
|
|
|
|
if (completedNostrChallenge) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
return {
|
|
createNostrChallenge,
|
|
getNostrChallenge,
|
|
verifyNostrChallenge,
|
|
isNostrChallengeFresh,
|
|
hasNostrChallengeBeenCompleted,
|
|
};
|
|
}
|
|
}
|
|
module.exports = NostrServiceProvider;
|