finished all services
This commit is contained in:
parent
f8a185e879
commit
b680ede093
2 changed files with 70 additions and 47 deletions
|
|
@ -2,7 +2,11 @@ class ServicesProvider {
|
||||||
constructor() {}
|
constructor() {}
|
||||||
|
|
||||||
provide() {
|
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 NostrServiceProvider = require('../services/nostrService');
|
||||||
const nostrService = new NostrServiceProvider().provide();
|
const nostrService = new NostrServiceProvider().provide();
|
||||||
|
|
@ -11,6 +15,12 @@ class ServicesProvider {
|
||||||
nostrService,
|
nostrService,
|
||||||
}).provide();
|
}).provide();
|
||||||
|
|
||||||
|
const LoginServiceProvider = require('../services/loginService');
|
||||||
|
const loginService = new LoginServiceProvider({
|
||||||
|
nostrService,
|
||||||
|
invitesService,
|
||||||
|
}).provide();
|
||||||
|
|
||||||
const SessionServiceProvider = require('../services/sessionService');
|
const SessionServiceProvider = require('../services/sessionService');
|
||||||
const sessionService = new SessionServiceProvider({
|
const sessionService = new SessionServiceProvider({
|
||||||
invitesService,
|
invitesService,
|
||||||
|
|
|
||||||
|
|
@ -1,57 +1,70 @@
|
||||||
const uuid = require('uuid');
|
const uuid = require('uuid');
|
||||||
|
|
||||||
const nostrService = require('./nostrService');
|
|
||||||
const invitesService = require('./invitesService');
|
|
||||||
const models = require('../models');
|
const models = require('../models');
|
||||||
|
|
||||||
const errors = require('../errors');
|
const errors = require('../errors');
|
||||||
|
|
||||||
async function createLoginChallenge() {
|
class LoginServiceProvider {
|
||||||
const nostrChallenge = await nostrService.createNostrChallenge();
|
constructor({ nostrService, invitesService }) {
|
||||||
|
this.nostrService = nostrService;
|
||||||
return await models.LoginChallengeCreated.create({
|
this.invitesService = invitesService;
|
||||||
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 =
|
provide() {
|
||||||
await nostrService.verifyNostrChallenge(signedEvent);
|
const createLoginChallenge = async () => {
|
||||||
|
const nostrChallenge = await this.nostrService.createNostrChallenge();
|
||||||
|
|
||||||
if (
|
return await models.LoginChallengeCreated.create({
|
||||||
!(await invitesService.isPublicKeySignedUp(
|
uuid: uuid.v7(),
|
||||||
completedNostrChallenge.public_key
|
nostr_challenge_uuid: nostrChallenge.uuid,
|
||||||
))
|
created_at: new Date().toISOString(),
|
||||||
) {
|
});
|
||||||
console.log('helo4');
|
};
|
||||||
throw new errors.ForbiddenError(
|
|
||||||
`Public key ${completedNostrChallenge.public_key} is not authorized.`
|
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 = LoginServiceProvider;
|
||||||
module.exports = {
|
|
||||||
createLoginChallenge,
|
|
||||||
verifyLoginChallenge,
|
|
||||||
};
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue