login working
This commit is contained in:
parent
48b12b71c2
commit
a35fa7d0dc
9 changed files with 333 additions and 7 deletions
58
src/services/loginService.js
Normal file
58
src/services/loginService.js
Normal 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,
|
||||
};
|
||||
|
|
@ -76,7 +76,11 @@ async function getPublicKeyRelatedToSession(sessionUuid) {
|
|||
},
|
||||
});
|
||||
|
||||
return sessionRelatedToPublickey.public_key;
|
||||
if (sessionRelatedToPublickey) {
|
||||
return sessionRelatedToPublickey.public_key;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue