This commit is contained in:
counterweight 2025-03-06 01:59:23 +01:00
parent 69290f4c7a
commit 15217dc77a
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
3 changed files with 131 additions and 107 deletions

View file

@ -1,4 +1,8 @@
const invitesService = require('../services/invitesService'); const nostrService = require('../services/nostrService');
const InvitesServiceProvider = require('../services/invitesService');
const invitesService = new InvitesServiceProvider({
nostrService,
}).provide();
module.exports = async function createAppInvite(inviterNpub) { module.exports = async function createAppInvite(inviterNpub) {
const appInvite = await invitesService.createAppInvite(inviterNpub); const appInvite = await invitesService.createAppInvite(inviterNpub);

View file

@ -2,10 +2,14 @@ class ServicesProvider {
constructor() {} constructor() {}
provide() { provide() {
const invitesService = require('../services/invitesService');
const nostrService = require('../services/nostrService'); const nostrService = require('../services/nostrService');
const loginService = require('../services/loginService'); const loginService = require('../services/loginService');
const InvitesServiceProvider = require('../services/invitesService');
const invitesService = new InvitesServiceProvider({
nostrService,
}).provide();
const SessionServiceProvider = require('../services/sessionService'); const SessionServiceProvider = require('../services/sessionService');
const sessionService = new SessionServiceProvider({ const sessionService = new SessionServiceProvider({
invitesService, invitesService,

View file

@ -4,6 +4,12 @@ const nostrService = require('./nostrService');
const models = require('../models'); const models = require('../models');
const errors = require('../errors'); const errors = require('../errors');
class InvitesServiceProvider {
constructor({ nostrService }) {
this.nostrService = nostrService;
}
provide() {
async function appInviteExists(inviteUuid) { async function appInviteExists(inviteUuid) {
const invite = await models.AppInviteCreated.findOne({ const invite = await models.AppInviteCreated.findOne({
where: { uuid: inviteUuid }, where: { uuid: inviteUuid },
@ -63,10 +69,15 @@ async function createSignUpChallenge(appInviteUuid) {
} }
async function verifySignUpChallenge(signedEvent) { async function verifySignUpChallenge(signedEvent) {
const challengeTag = signedEvent.tags.find((tag) => tag[0] === 'challenge'); const challengeTag = signedEvent.tags.find(
(tag) => tag[0] === 'challenge'
);
const challenge = challengeTag[1]; const challenge = challengeTag[1];
const nostrChallenge = await nostrService.getNostrChallenge(null, challenge); const nostrChallenge = await nostrService.getNostrChallenge(
null,
challenge
);
const signUpChallenge = await models.SignUpChallengeCreated.findOne({ const signUpChallenge = await models.SignUpChallengeCreated.findOne({
where: { where: {
@ -75,21 +86,22 @@ async function verifySignUpChallenge(signedEvent) {
}); });
if (await nostrService.hasNostrChallengeBeenCompleted(challenge)) { if (await nostrService.hasNostrChallengeBeenCompleted(challenge)) {
throw new errors.AlreadyUsedError('This challenge has already been used.'); throw new errors.AlreadyUsedError(
'This challenge has already been used.'
);
} }
const completedNostrChallenge = const completedNostrChallenge =
await nostrService.verifyNostrChallenge(signedEvent); await nostrService.verifyNostrChallenge(signedEvent);
const completedSignUpChallenge = await models.SignUpChallengeCompleted.create( const completedSignUpChallenge =
{ await models.SignUpChallengeCompleted.create({
uuid: uuid.v7(), uuid: uuid.v7(),
nostr_challenge_completed_uuid: completedNostrChallenge.uuid, nostr_challenge_completed_uuid: completedNostrChallenge.uuid,
app_invite_uuid: signUpChallenge.app_invite_uuid, app_invite_uuid: signUpChallenge.app_invite_uuid,
public_key: completedNostrChallenge.public_key, public_key: completedNostrChallenge.public_key,
created_at: new Date().toISOString(), created_at: new Date().toISOString(),
} });
);
return completedSignUpChallenge; return completedSignUpChallenge;
} }
@ -109,7 +121,7 @@ async function isPublicKeySignedUp(publicKey) {
return false; return false;
} }
module.exports = { return {
appInviteExists, appInviteExists,
getAppInvite, getAppInvite,
isAppInviteSpent, isAppInviteSpent,
@ -118,3 +130,7 @@ module.exports = {
verifySignUpChallenge, verifySignUpChallenge,
isPublicKeySignedUp, isPublicKeySignedUp,
}; };
}
}
module.exports = InvitesServiceProvider;