login working

This commit is contained in:
counterweight 2025-02-17 01:18:43 +01:00
parent 48b12b71c2
commit a35fa7d0dc
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
9 changed files with 333 additions and 7 deletions

View file

@ -0,0 +1,58 @@
const uuid = require('uuid');
const nostrService = require('./nostrService');
const invitesService = require('./invitesService');
const LoginChallengeCreated = require('../models/LoginChallengeCreated');
const LoginChallengeCompleted = require('../models/LoginChallengeCompleted');
const errors = require('../errors');
async function createLoginChallenge() {
const nostrChallenge = await nostrService.createNostrChallenge();
return await LoginChallengeCreated.create({
uuid: uuid.v7(),
nostr_challenge_uuid: nostrChallenge.uuid,
created_at: new Date().toISOString(),
});
}
async function verifyLoginChallenge(signedEvent) {
const challengeTag = signedEvent.tags.find((tag) => tag[0] === 'challenge');
const challenge = challengeTag[1];
if (await nostrService.hasNostrChallengeBeenCompleted(challenge)) {
throw new errors.AlreadyUsedError('This challenge has already been used.');
}
const completedNostrChallenge =
await nostrService.verifyNostrChallenge(signedEvent);
if (
!(await invitesService.isPublicKeySignedUp(
completedNostrChallenge.public_key
))
) {
console.log('helo4');
throw new errors.ForbiddenError(
`Public key ${completedNostrChallenge.public_key} is not authorized.`
);
}
const completedLoginChallenge = await LoginChallengeCompleted.create({
uuid: uuid.v7(),
nostr_challenge_completed_uuid: completedNostrChallenge.uuid,
public_key: completedNostrChallenge.public_key,
created_at: new Date().toISOString(),
});
console.log('helo3');
console.log(completedLoginChallenge);
return completedLoginChallenge;
}
module.exports = {
createLoginChallenge,
verifyLoginChallenge,
};

View file

@ -76,7 +76,11 @@ async function getPublicKeyRelatedToSession(sessionUuid) {
},
});
return sessionRelatedToPublickey.public_key;
if (sessionRelatedToPublickey) {
return sessionRelatedToPublickey.public_key;
}
return null;
}
module.exports = {