session service

This commit is contained in:
counterweight 2025-03-06 01:46:17 +01:00
parent a2939a7f2e
commit 69290f4c7a
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
2 changed files with 91 additions and 76 deletions

View file

@ -5,7 +5,12 @@ class ServicesProvider {
const invitesService = require('../services/invitesService');
const nostrService = require('../services/nostrService');
const loginService = require('../services/loginService');
const sessionService = require('../services/sessionService');
const SessionServiceProvider = require('../services/sessionService');
const sessionService = new SessionServiceProvider({
invitesService,
}).provide();
const ProfileServiceProvider = require('../services/profileService');
const profileService = new ProfileServiceProvider().provide();
const OfferServiceProvider = require('../services/offerService');

View file

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