no more models

This commit is contained in:
counterweight 2025-03-07 13:27:28 +01:00
parent 73a71d3ccb
commit 00670f051f
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C

View file

@ -3,13 +3,11 @@ const crypto = require('crypto');
const { Op } = require('sequelize'); const { Op } = require('sequelize');
const { verifyEvent } = require('nostr-tools'); const { verifyEvent } = require('nostr-tools');
const models = require('../models');
const errors = require('../errors');
class NostrServiceProvider { class NostrServiceProvider {
constructor({ constants }) { constructor({ models, constants, errors }) {
this.models = models;
this.constants = constants; this.constants = constants;
this.errors = errors;
} }
provide() { provide() {
@ -21,7 +19,7 @@ class NostrServiceProvider {
this.constants.DEFAULT_NOSTR_CHALLENGE_DURATION_SECONDS this.constants.DEFAULT_NOSTR_CHALLENGE_DURATION_SECONDS
); );
const nostrChallenge = await models.NostrChallengeCreated.create({ const nostrChallenge = await this.models.NostrChallengeCreated.create({
uuid: uuid.v7(), uuid: uuid.v7(),
challenge: crypto.randomBytes(32).toString('hex'), challenge: crypto.randomBytes(32).toString('hex'),
expires_at: expiryTimestamp.toISOString(), expires_at: expiryTimestamp.toISOString(),
@ -31,12 +29,12 @@ class NostrServiceProvider {
return nostrChallenge; return nostrChallenge;
}; };
async function getNostrChallenge( const getNostrChallenge = async (
nostrChallengeUuid = null, nostrChallengeUuid = null,
challenge = null challenge = null
) { ) => {
if (nostrChallengeUuid) { if (nostrChallengeUuid) {
return await models.NostrChallengeCreated.findOne({ return await this.models.NostrChallengeCreated.findOne({
where: { where: {
uuid: nostrChallengeUuid, uuid: nostrChallengeUuid,
}, },
@ -44,7 +42,7 @@ class NostrServiceProvider {
} }
if (challenge) { if (challenge) {
return await models.NostrChallengeCreated.findOne({ return await this.models.NostrChallengeCreated.findOne({
where: { where: {
challenge, challenge,
}, },
@ -52,40 +50,40 @@ class NostrServiceProvider {
} }
throw Error('You need to pass a uuid or a challenge.'); throw Error('You need to pass a uuid or a challenge.');
} };
async function verifyNostrChallenge(signedEvent) { const verifyNostrChallenge = async (signedEvent) => {
const challengeTag = signedEvent.tags.find( const challengeTag = signedEvent.tags.find(
(tag) => tag[0] === 'challenge' (tag) => tag[0] === 'challenge'
); );
const challenge = challengeTag[1]; const challenge = challengeTag[1];
if (!(await isNostrChallengeFresh(challenge))) { if (!(await isNostrChallengeFresh(challenge))) {
throw errors.ExpiredError('Challenge expired, request new one.'); throw this.errors.ExpiredError('Challenge expired, request new one.');
} }
if (await hasNostrChallengeBeenCompleted(challenge)) { if (await hasNostrChallengeBeenCompleted(challenge)) {
throw new errors.AlreadyUsedError( throw new this.errors.AlreadyUsedError(
'Challenge already used, request new one.' 'Challenge already used, request new one.'
); );
} }
const isSignatureValid = verifyEvent(signedEvent); const isSignatureValid = verifyEvent(signedEvent);
if (!isSignatureValid) { if (!isSignatureValid) {
throw new errors.InvalidSignatureError('Signature is not valid.'); throw new this.errors.InvalidSignatureError('Signature is not valid.');
} }
return await models.NostrChallengeCompleted.create({ return await this.models.NostrChallengeCompleted.create({
uuid: uuid.v7(), uuid: uuid.v7(),
challenge: challenge, challenge: challenge,
signed_event: signedEvent, signed_event: signedEvent,
public_key: signedEvent.pubkey, public_key: signedEvent.pubkey,
created_at: new Date().toISOString(), created_at: new Date().toISOString(),
}); });
} };
async function isNostrChallengeFresh(challengeString) { const isNostrChallengeFresh = async (challengeString) => {
const nostrChallenge = await models.NostrChallengeCreated.findOne({ const nostrChallenge = await this.models.NostrChallengeCreated.findOne({
where: { where: {
challenge: challengeString, challenge: challengeString,
expires_at: { expires_at: {
@ -98,11 +96,11 @@ class NostrServiceProvider {
return true; return true;
} }
return false; return false;
} };
async function hasNostrChallengeBeenCompleted(challengeString) { const hasNostrChallengeBeenCompleted = async (challengeString) => {
const completedNostrChallenge = const completedNostrChallenge =
await models.NostrChallengeCompleted.findOne({ await this.models.NostrChallengeCompleted.findOne({
where: { where: {
challenge: challengeString, challenge: challengeString,
}, },
@ -112,7 +110,7 @@ class NostrServiceProvider {
return true; return true;
} }
return false; return false;
} };
return { return {
createNostrChallenge, createNostrChallenge,