finished all services

This commit is contained in:
counterweight 2025-03-07 12:31:07 +01:00
parent f8a185e879
commit b680ede093
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
2 changed files with 70 additions and 47 deletions

View file

@ -2,7 +2,11 @@ class ServicesProvider {
constructor() {}
provide() {
const loginService = require('../services/loginService');
/*
Up next:
- start passing all service stuff from outside
- also remember to get CLi fixed and working
*/
const NostrServiceProvider = require('../services/nostrService');
const nostrService = new NostrServiceProvider().provide();
@ -11,6 +15,12 @@ class ServicesProvider {
nostrService,
}).provide();
const LoginServiceProvider = require('../services/loginService');
const loginService = new LoginServiceProvider({
nostrService,
invitesService,
}).provide();
const SessionServiceProvider = require('../services/sessionService');
const sessionService = new SessionServiceProvider({
invitesService,

View file

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