session service
This commit is contained in:
parent
a2939a7f2e
commit
69290f4c7a
2 changed files with 91 additions and 76 deletions
|
|
@ -5,7 +5,12 @@ class ServicesProvider {
|
||||||
const invitesService = require('../services/invitesService');
|
const invitesService = require('../services/invitesService');
|
||||||
const nostrService = require('../services/nostrService');
|
const nostrService = require('../services/nostrService');
|
||||||
const loginService = require('../services/loginService');
|
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 ProfileServiceProvider = require('../services/profileService');
|
||||||
const profileService = new ProfileServiceProvider().provide();
|
const profileService = new ProfileServiceProvider().provide();
|
||||||
const OfferServiceProvider = require('../services/offerService');
|
const OfferServiceProvider = require('../services/offerService');
|
||||||
|
|
|
||||||
|
|
@ -2,92 +2,102 @@ const uuid = require('uuid');
|
||||||
|
|
||||||
const models = require('../models');
|
const models = require('../models');
|
||||||
|
|
||||||
const invitesService = require('./invitesService');
|
|
||||||
const constants = require('../constants');
|
const constants = require('../constants');
|
||||||
|
|
||||||
async function createSession(sessionUuid) {
|
class SessionServiceProvider {
|
||||||
const currentTimestamp = new Date();
|
constructor({ invitesService }) {
|
||||||
const expiryTimestamp = new Date(currentTimestamp.getTime());
|
this.invitesService = invitesService;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (currentSession.expires_at <= new Date()) {
|
provide() {
|
||||||
return false;
|
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) {
|
async function isSessionValid(sessionUuid) {
|
||||||
if (!(await isSessionValid(sessionUuid))) {
|
const currentSession = await models.SessionCreated.findOne({
|
||||||
throw Error('Session is not valid anymore.');
|
where: {
|
||||||
}
|
uuid: sessionUuid,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
if (!(await invitesService.isPublicKeySignedUp(publicKey))) {
|
if (!currentSession) {
|
||||||
throw Error('Public key is not signed up.');
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return models.SessionRelatedToPublickey.create({
|
if (currentSession.expires_at <= new Date()) {
|
||||||
uuid: uuid.v7(),
|
return false;
|
||||||
session_uuid: sessionUuid,
|
}
|
||||||
public_key: publicKey,
|
|
||||||
created_at: new Date().toISOString(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function isSessionAuthorized(sessionUuid) {
|
return true;
|
||||||
const isSessionRelatedToPublicKey =
|
}
|
||||||
await models.SessionRelatedToPublickey.findOne({
|
|
||||||
where: {
|
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,
|
session_uuid: sessionUuid,
|
||||||
},
|
public_key: publicKey,
|
||||||
});
|
created_at: new Date().toISOString(),
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
if (isSessionRelatedToPublicKey) {
|
async function isSessionAuthorized(sessionUuid) {
|
||||||
return true;
|
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) {
|
module.exports = SessionServiceProvider;
|
||||||
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,
|
|
||||||
};
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue