invites
This commit is contained in:
parent
69290f4c7a
commit
15217dc77a
3 changed files with 131 additions and 107 deletions
|
|
@ -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);
|
||||||
|
|
|
||||||
|
|
@ -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,
|
||||||
|
|
|
||||||
|
|
@ -4,117 +4,133 @@ const nostrService = require('./nostrService');
|
||||||
const models = require('../models');
|
const models = require('../models');
|
||||||
const errors = require('../errors');
|
const errors = require('../errors');
|
||||||
|
|
||||||
async function appInviteExists(inviteUuid) {
|
class InvitesServiceProvider {
|
||||||
const invite = await models.AppInviteCreated.findOne({
|
constructor({ nostrService }) {
|
||||||
where: { uuid: inviteUuid },
|
this.nostrService = nostrService;
|
||||||
});
|
|
||||||
if (invite) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getAppInvite(inviteUuid) {
|
|
||||||
const invite = await models.AppInviteCreated.findOne({
|
|
||||||
where: { uuid: inviteUuid },
|
|
||||||
});
|
|
||||||
return invite;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function isAppInviteSpent(appInviteUuid) {
|
|
||||||
const signUpChallengeCompleted =
|
|
||||||
await models.SignUpChallengeCompleted.findOne({
|
|
||||||
where: {
|
|
||||||
app_invite_uuid: appInviteUuid,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (signUpChallengeCompleted) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function createAppInvite(inviterPubKey) {
|
|
||||||
return await models.AppInviteCreated.create({
|
|
||||||
uuid: uuid.v7(),
|
|
||||||
inviter_pub_key: inviterPubKey,
|
|
||||||
created_at: new Date().toISOString(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function createSignUpChallenge(appInviteUuid) {
|
|
||||||
if (!(await appInviteExists(appInviteUuid))) {
|
|
||||||
throw new errors.NotFoundError("Invite doesn't exist.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (await isAppInviteSpent(appInviteUuid)) {
|
provide() {
|
||||||
throw new errors.AlreadyUsedError('Invite has already been used.');
|
async function appInviteExists(inviteUuid) {
|
||||||
}
|
const invite = await models.AppInviteCreated.findOne({
|
||||||
|
where: { uuid: inviteUuid },
|
||||||
const nostrChallenge = await nostrService.createNostrChallenge();
|
});
|
||||||
|
if (invite) {
|
||||||
return await models.SignUpChallengeCreated.create({
|
return true;
|
||||||
uuid: uuid.v7(),
|
}
|
||||||
nostr_challenge_uuid: nostrChallenge.uuid,
|
return false;
|
||||||
app_invite_uuid: appInviteUuid,
|
|
||||||
created_at: new Date().toISOString(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function verifySignUpChallenge(signedEvent) {
|
|
||||||
const challengeTag = signedEvent.tags.find((tag) => tag[0] === 'challenge');
|
|
||||||
const challenge = challengeTag[1];
|
|
||||||
|
|
||||||
const nostrChallenge = await nostrService.getNostrChallenge(null, challenge);
|
|
||||||
|
|
||||||
const signUpChallenge = await models.SignUpChallengeCreated.findOne({
|
|
||||||
where: {
|
|
||||||
nostr_challenge_uuid: nostrChallenge.uuid,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (await nostrService.hasNostrChallengeBeenCompleted(challenge)) {
|
|
||||||
throw new errors.AlreadyUsedError('This challenge has already been used.');
|
|
||||||
}
|
|
||||||
|
|
||||||
const completedNostrChallenge =
|
|
||||||
await nostrService.verifyNostrChallenge(signedEvent);
|
|
||||||
|
|
||||||
const completedSignUpChallenge = await models.SignUpChallengeCompleted.create(
|
|
||||||
{
|
|
||||||
uuid: uuid.v7(),
|
|
||||||
nostr_challenge_completed_uuid: completedNostrChallenge.uuid,
|
|
||||||
app_invite_uuid: signUpChallenge.app_invite_uuid,
|
|
||||||
public_key: completedNostrChallenge.public_key,
|
|
||||||
created_at: new Date().toISOString(),
|
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
|
||||||
return completedSignUpChallenge;
|
async function getAppInvite(inviteUuid) {
|
||||||
}
|
const invite = await models.AppInviteCreated.findOne({
|
||||||
|
where: { uuid: inviteUuid },
|
||||||
|
});
|
||||||
|
return invite;
|
||||||
|
}
|
||||||
|
|
||||||
async function isPublicKeySignedUp(publicKey) {
|
async function isAppInviteSpent(appInviteUuid) {
|
||||||
const signUpChallengeCompleted =
|
const signUpChallengeCompleted =
|
||||||
await models.SignUpChallengeCompleted.findOne({
|
await models.SignUpChallengeCompleted.findOne({
|
||||||
where: {
|
where: {
|
||||||
public_key: publicKey,
|
app_invite_uuid: appInviteUuid,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (signUpChallengeCompleted) {
|
if (signUpChallengeCompleted) {
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function createAppInvite(inviterPubKey) {
|
||||||
|
return await models.AppInviteCreated.create({
|
||||||
|
uuid: uuid.v7(),
|
||||||
|
inviter_pub_key: inviterPubKey,
|
||||||
|
created_at: new Date().toISOString(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function createSignUpChallenge(appInviteUuid) {
|
||||||
|
if (!(await appInviteExists(appInviteUuid))) {
|
||||||
|
throw new errors.NotFoundError("Invite doesn't exist.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (await isAppInviteSpent(appInviteUuid)) {
|
||||||
|
throw new errors.AlreadyUsedError('Invite has already been used.');
|
||||||
|
}
|
||||||
|
|
||||||
|
const nostrChallenge = await nostrService.createNostrChallenge();
|
||||||
|
|
||||||
|
return await models.SignUpChallengeCreated.create({
|
||||||
|
uuid: uuid.v7(),
|
||||||
|
nostr_challenge_uuid: nostrChallenge.uuid,
|
||||||
|
app_invite_uuid: appInviteUuid,
|
||||||
|
created_at: new Date().toISOString(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function verifySignUpChallenge(signedEvent) {
|
||||||
|
const challengeTag = signedEvent.tags.find(
|
||||||
|
(tag) => tag[0] === 'challenge'
|
||||||
|
);
|
||||||
|
const challenge = challengeTag[1];
|
||||||
|
|
||||||
|
const nostrChallenge = await nostrService.getNostrChallenge(
|
||||||
|
null,
|
||||||
|
challenge
|
||||||
|
);
|
||||||
|
|
||||||
|
const signUpChallenge = await models.SignUpChallengeCreated.findOne({
|
||||||
|
where: {
|
||||||
|
nostr_challenge_uuid: nostrChallenge.uuid,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (await nostrService.hasNostrChallengeBeenCompleted(challenge)) {
|
||||||
|
throw new errors.AlreadyUsedError(
|
||||||
|
'This challenge has already been used.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const completedNostrChallenge =
|
||||||
|
await nostrService.verifyNostrChallenge(signedEvent);
|
||||||
|
|
||||||
|
const completedSignUpChallenge =
|
||||||
|
await models.SignUpChallengeCompleted.create({
|
||||||
|
uuid: uuid.v7(),
|
||||||
|
nostr_challenge_completed_uuid: completedNostrChallenge.uuid,
|
||||||
|
app_invite_uuid: signUpChallenge.app_invite_uuid,
|
||||||
|
public_key: completedNostrChallenge.public_key,
|
||||||
|
created_at: new Date().toISOString(),
|
||||||
|
});
|
||||||
|
|
||||||
|
return completedSignUpChallenge;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function isPublicKeySignedUp(publicKey) {
|
||||||
|
const signUpChallengeCompleted =
|
||||||
|
await models.SignUpChallengeCompleted.findOne({
|
||||||
|
where: {
|
||||||
|
public_key: publicKey,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (signUpChallengeCompleted) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
appInviteExists,
|
||||||
|
getAppInvite,
|
||||||
|
isAppInviteSpent,
|
||||||
|
createAppInvite,
|
||||||
|
createSignUpChallenge,
|
||||||
|
verifySignUpChallenge,
|
||||||
|
isPublicKeySignedUp,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = InvitesServiceProvider;
|
||||||
appInviteExists,
|
|
||||||
getAppInvite,
|
|
||||||
isAppInviteSpent,
|
|
||||||
createAppInvite,
|
|
||||||
createSignUpChallenge,
|
|
||||||
verifySignUpChallenge,
|
|
||||||
isPublicKeySignedUp,
|
|
||||||
};
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue