format project

This commit is contained in:
counterweight 2025-02-14 11:13:18 +01:00
parent 90d8e39eb3
commit c02cf8c12e
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
39 changed files with 2062 additions and 909 deletions

View file

@ -8,119 +8,112 @@ const SignUpChallengeCompleted = require('../models/SignUpChallengeCompleted');
const errors = require('../errors');
async function appInviteExists(inviteUuid) {
const invite = await AppInviteCreated.findOne({ where: { uuid: inviteUuid } });
if (invite) {
return true;
}
return false;
const invite = await AppInviteCreated.findOne({
where: { uuid: inviteUuid },
});
if (invite) {
return true;
}
return false;
}
async function getAppInvite(inviteUuid) {
const invite = await AppInviteCreated.findOne({ where: { uuid: inviteUuid } });
return invite;
const invite = await AppInviteCreated.findOne({
where: { uuid: inviteUuid },
});
return invite;
}
async function isAppInviteSpent(appInviteUuid) {
const signUpChallengeCompleted = await SignUpChallengeCompleted.findOne({
where: {
app_invite_uuid: appInviteUuid
}
})
const signUpChallengeCompleted = await SignUpChallengeCompleted.findOne({
where: {
app_invite_uuid: appInviteUuid,
},
});
if (signUpChallengeCompleted) {
return true;
}
return false;
if (signUpChallengeCompleted) {
return true;
}
return false;
}
async function createAppInvite(inviterPubKey) {
return await AppInviteCreated.create({
uuid: uuid.v7(),
inviter_pub_key: inviterPubKey,
created_at: new Date().toISOString()
}
);
return await 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 appInviteExists(appInviteUuid))) {
throw new errors.NotFoundError("Invite doesn't exist.")
}
if (await isAppInviteSpent(appInviteUuid)) {
throw new errors.AlreadyUsedError('Invite has already been used.');
}
if (await isAppInviteSpent(appInviteUuid)) {
throw new errors.AlreadyUsedError("Invite has already been used.")
}
const nostrChallenge = await nostrService.createNostrChallenge();
const nostrChallenge = await nostrService.createNostrChallenge()
return await SignUpChallengeCreated.create({
'uuid': uuid.v7(),
nostr_challenge_uuid: nostrChallenge.uuid,
app_invite_uuid: appInviteUuid,
created_at: new Date().toISOString()
}
)
return await 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 challengeTag = signedEvent.tags.find(tag => tag[0] === "challenge");
const challenge = challengeTag[1];
const nostrChallenge = await nostrService.getNostrChallenge(null, challenge);
const nostrChallenge = await nostrService.getNostrChallenge(
null, challenge
);
const signUpChallenge = await SignUpChallengeCreated.findOne({
where: {
nostr_challenge_uuid: nostrChallenge.uuid,
},
});
const signUpChallenge = await SignUpChallengeCreated.findOne({
where: {
nostr_challenge_uuid: nostrChallenge.uuid
}
})
if (await nostrService.hasNostrChallengeBeenCompleted(challenge)) {
throw new errors.AlreadyUsedError('This challenge has already been used.');
}
if (await nostrService.hasNostrChallengeBeenCompleted(challenge)) {
throw new errors.AlreadyUsedError("This challenge has already been used.");
}
const completedNostrChallenge =
await nostrService.verifyNostrChallenge(signedEvent);
const completedNostrChallenge = await nostrService.verifyNostrChallenge(signedEvent);
const completedSignUpChallenge = await 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(),
});
const completedSignUpChallenge = await 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;
return completedSignUpChallenge;
}
async function isPublicKeySignedUp(publicKey) {
const signUpChallengeCompleted = await SignUpChallengeCompleted.findOne(
{
where: {
public_key: publicKey
}
}
)
const signUpChallengeCompleted = await SignUpChallengeCompleted.findOne({
where: {
public_key: publicKey,
},
});
if (signUpChallengeCompleted) {
return true;
}
return false;
if (signUpChallengeCompleted) {
return true;
}
return false;
}
module.exports = {
appInviteExists,
getAppInvite,
isAppInviteSpent,
createAppInvite,
createSignUpChallenge,
verifySignUpChallenge,
isPublicKeySignedUp
};
appInviteExists,
getAppInvite,
isAppInviteSpent,
createAppInvite,
createSignUpChallenge,
verifySignUpChallenge,
isPublicKeySignedUp,
};

View file

@ -1,115 +1,113 @@
const uuid = require("uuid");
const crypto = require("crypto");
const uuid = require('uuid');
const crypto = require('crypto');
const { Op, TimeoutError } = require('sequelize');
const { verifyEvent } = require("nostr-tools");
const { verifyEvent } = require('nostr-tools');
const NostrChallengeCreated = require('../models/NostrChallengeCreated');
const NostrChallengeCompleted = require("../models/NostrChallengeCompleted");
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 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(),
});
const nostrChallenge = await NostrChallengeCreated.create({
'uuid': uuid.v7(),
challenge: crypto.randomBytes(32).toString("hex"),
expires_at: expiryTimestamp.toISOString(),
created_at: currentTimestamp.toISOString()
});
return nostrChallenge;
return nostrChallenge;
}
async function getNostrChallenge(nostrChallengeUuid = null, challenge = null) {
if (nostrChallengeUuid) {
return await NostrChallengeCreated.findOne({
where: {
uuid: nostrChallengeUuid,
},
});
}
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.')
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];
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 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 NostrChallengeCompleted.create({
'uuid': uuid.v7(),
challenge: challenge,
signed_event: signedEvent,
public_key: signedEvent.pubkey,
created_at: new Date().toISOString()
}
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 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()
}
}
});
const nostrChallenge = await NostrChallengeCreated.findOne({
where: {
challenge: challengeString,
expires_at: {
[Op.gt]: new Date(),
},
},
});
if (nostrChallenge) {
return true;
}
return false;
if (nostrChallenge) {
return true;
}
return false;
}
async function hasNostrChallengeBeenCompleted(challengeString) {
const completedNostrChallenge = await NostrChallengeCompleted.findOne(
{
where: {
challenge: challengeString
}
}
);
const completedNostrChallenge = await NostrChallengeCompleted.findOne({
where: {
challenge: challengeString,
},
});
if (completedNostrChallenge) {
return true;
}
return false;
if (completedNostrChallenge) {
return true;
}
return false;
}
module.exports = {
createNostrChallenge,
getNostrChallenge,
verifyNostrChallenge,
isNostrChallengeFresh,
hasNostrChallengeBeenCompleted
};
createNostrChallenge,
getNostrChallenge,
verifyNostrChallenge,
isNostrChallengeFresh,
hasNostrChallengeBeenCompleted,
};

View file

@ -3,28 +3,24 @@ const ContactDetailsSet = require('../models/ContactDetailsSet');
const NymSet = require('../models/NymSet');
async function setContactDetails(publicKey, encryptedContactDetails) {
return await ContactDetailsSet.create(
{
'uuid': uuid.v7(),
public_key: publicKey,
encrypted_contact_details: encryptedContactDetails,
created_at: new Date().toISOString()
}
)
return await ContactDetailsSet.create({
uuid: uuid.v7(),
public_key: publicKey,
encrypted_contact_details: encryptedContactDetails,
created_at: new Date().toISOString(),
});
}
async function setNym(publicKey, nym) {
return await NymSet.create(
{
'uuid': uuid.v7(),
public_key: publicKey,
nym: nym,
created_at: new Date().toISOString()
}
)
return await NymSet.create({
uuid: uuid.v7(),
public_key: publicKey,
nym: nym,
created_at: new Date().toISOString(),
});
}
module.exports = {
setContactDetails,
setNym
};
setContactDetails,
setNym,
};

View file

@ -7,86 +7,82 @@ const invitesService = require('./invitesService');
const constants = require('../constants');
async function createSession(sessionUuid) {
const currentTimestamp = new Date();
const expiryTimestamp = new Date(currentTimestamp.getTime());
expiryTimestamp.setSeconds(expiryTimestamp.getSeconds() + constants.DEFAULT_SESSION_DURATION_SECONDS);
const currentTimestamp = new Date();
const expiryTimestamp = new Date(currentTimestamp.getTime());
expiryTimestamp.setSeconds(
expiryTimestamp.getSeconds() + constants.DEFAULT_SESSION_DURATION_SECONDS
);
return await SessionCreated.create({
uuid: sessionUuid,
created_at: currentTimestamp.toISOString(),
expires_at: expiryTimestamp.toISOString()
});
return await SessionCreated.create({
uuid: sessionUuid,
created_at: currentTimestamp.toISOString(),
expires_at: expiryTimestamp.toISOString(),
});
}
async function isSessionValid(sessionUuid) {
const currentSession = await SessionCreated.findOne({
where: {
'uuid': sessionUuid
}
});
const currentSession = await SessionCreated.findOne({
where: {
uuid: sessionUuid,
},
});
if (!currentSession) {
return false;
}
if (!currentSession) {
return false;
}
if (currentSession.expires_at <= new Date()) {
return false;
}
if (currentSession.expires_at <= new Date()) {
return false;
}
return true;
return true;
}
async function relateSessionToPublicKey(sessionUuid, publicKey) {
if (!(await isSessionValid(sessionUuid))) {
throw Error("Session is not valid anymore.");
}
if (!(await isSessionValid(sessionUuid))) {
throw Error('Session is not valid anymore.');
}
if (!(await invitesService.isPublicKeySignedUp(publicKey))) {
throw Error("Public key is not signed up.");
}
if (!(await invitesService.isPublicKeySignedUp(publicKey))) {
throw Error('Public key is not signed up.');
}
return SessionRelatedToPublickey.create({
'uuid': uuid.v7(),
session_uuid: sessionUuid,
public_key: publicKey,
created_at: new Date().toISOString()
});
return SessionRelatedToPublickey.create({
uuid: uuid.v7(),
session_uuid: sessionUuid,
public_key: publicKey,
created_at: new Date().toISOString(),
});
}
async function isSessionAuthorized(sessionUuid) {
const isSessionRelatedToPublicKey = await SessionRelatedToPublickey.findOne(
{
where: {
session_uuid: sessionUuid
}
}
);
const isSessionRelatedToPublicKey = await SessionRelatedToPublickey.findOne({
where: {
session_uuid: sessionUuid,
},
});
if (isSessionRelatedToPublicKey) {
return true;
}
if (isSessionRelatedToPublicKey) {
return true;
}
return false;
return false;
}
async function getPublicKeyRelatedToSession(sessionUuid) {
const sessionRelatedToPublickey = await SessionRelatedToPublickey.findOne(
{
where: {
session_uuid: sessionUuid
}
}
);
return sessionRelatedToPublickey.public_key;
const sessionRelatedToPublickey = await SessionRelatedToPublickey.findOne({
where: {
session_uuid: sessionUuid,
},
});
return sessionRelatedToPublickey.public_key;
}
module.exports = {
createSession,
isSessionValid,
relateSessionToPublicKey,
isSessionAuthorized,
getPublicKeyRelatedToSession
}
createSession,
isSessionValid,
relateSessionToPublicKey,
isSessionAuthorized,
getPublicKeyRelatedToSession,
};