session upgrades work
This commit is contained in:
parent
bee8218e40
commit
74019e97a6
7 changed files with 146 additions and 40 deletions
|
|
@ -73,22 +73,17 @@ async function verifySignUpChallenge(signedEvent) {
|
|||
null, challenge
|
||||
);
|
||||
|
||||
console.log(`Found this nostr challenge: ${nostrChallenge}`);
|
||||
|
||||
const signUpChallenge = await SignUpChallengeCreated.findOne({
|
||||
where: {
|
||||
nostr_challenge_uuid: nostrChallenge.uuid
|
||||
}
|
||||
})
|
||||
|
||||
console.log(`Found this signup challenge: ${signUpChallenge}`);
|
||||
|
||||
if (await nostrService.hasNostrChallengeBeenCompleted(challenge)) {
|
||||
throw new errors.AlreadyUsedError("This challenge has already been used.");
|
||||
}
|
||||
console.log(`I'm gonna verify the nostr challenge`);
|
||||
|
||||
const completedNostrChallenge = await nostrService.verifyNostrChallenge(signedEvent);
|
||||
console.log(`Verified the NostrChallenge: ${completedNostrChallenge}`);
|
||||
|
||||
const completedSignUpChallenge = await SignUpChallengeCompleted.create(
|
||||
{
|
||||
|
|
@ -99,17 +94,33 @@ async function verifySignUpChallenge(signedEvent) {
|
|||
created_at: new Date().toISOString()
|
||||
}
|
||||
);
|
||||
console.log(`Verified the SignUpChallenge: ${completedSignUpChallenge}`);
|
||||
|
||||
|
||||
return completedSignUpChallenge;
|
||||
}
|
||||
|
||||
async function isPublicKeySignedUp(publicKey) {
|
||||
const signUpChallengeCompleted = await SignUpChallengeCompleted.findOne(
|
||||
{
|
||||
where: {
|
||||
public_key: publicKey
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
if (signUpChallengeCompleted) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
appInviteExists,
|
||||
getAppInvite,
|
||||
isAppInviteSpent,
|
||||
createAppInvite,
|
||||
createSignUpChallenge,
|
||||
verifySignUpChallenge
|
||||
verifySignUpChallenge,
|
||||
isPublicKeySignedUp
|
||||
};
|
||||
|
|
@ -1,4 +1,9 @@
|
|||
const uuid = require('uuid');
|
||||
|
||||
const SessionCreated = require('../models/SessionCreated');
|
||||
const SessionRelatedToPublickey = require('../models/SessionRelatedToPublickey');
|
||||
|
||||
const invitesService = require('./invitesService');
|
||||
|
||||
const constants = require('../constants');
|
||||
|
||||
|
|
@ -14,7 +19,7 @@ async function createSession(sessionUuid) {
|
|||
});
|
||||
}
|
||||
|
||||
async function isSessionExpired(sessionUuid) {
|
||||
async function isSessionValid(sessionUuid) {
|
||||
const currentSession = await SessionCreated.findOne({
|
||||
where: {
|
||||
'uuid': sessionUuid
|
||||
|
|
@ -22,15 +27,36 @@ async function isSessionExpired(sessionUuid) {
|
|||
});
|
||||
|
||||
if (!currentSession) {
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (currentSession.expires_at <= new Date()) {
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
exports.createSession = createSession;
|
||||
exports.isSessionExpired = isSessionExpired;
|
||||
async function relateSessionToPublicKey(sessionUuid, publicKey) {
|
||||
if (!(await isSessionValid(sessionUuid))) {
|
||||
throw Error("Session is not valid anymore.");
|
||||
}
|
||||
|
||||
if (!(await invitesService.isPublicKeySignedUp(publicKey))) {
|
||||
throw Error("Public key is not signed up.");
|
||||
}
|
||||
|
||||
return SessionRelatedToPublickey.create({
|
||||
'uuid': uuid.v7(),
|
||||
session_uuid: sessionUuid,
|
||||
public_key: publicKey,
|
||||
created_at: new Date().toISOString()
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createSession,
|
||||
isSessionValid,
|
||||
relateSessionToPublicKey
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue