mirror of
https://github.com/aljazceru/landscape-template.git
synced 2026-01-24 16:54:23 +01:00
refactor: migrate to jwt sessions instead of store sessions
This commit is contained in:
@@ -27,11 +27,10 @@ function isHashUsed(hash) {
|
||||
return prisma.generatedK1.findFirst({ where: { value: hash } })
|
||||
}
|
||||
|
||||
function addHash(hash, sid) {
|
||||
function addHash(hash) {
|
||||
return prisma.generatedK1.create({
|
||||
data: {
|
||||
value: hash,
|
||||
sid,
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -46,7 +45,7 @@ function removeHash(hash) {
|
||||
|
||||
function removeExpiredHashes() {
|
||||
const now = new Date();
|
||||
const lastHourDate = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours() - 1, now.getMinutes());
|
||||
const lastHourDate = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes() - 10);
|
||||
|
||||
return prisma.generatedK1.deleteMany({
|
||||
where: {
|
||||
@@ -57,20 +56,21 @@ function removeExpiredHashes() {
|
||||
})
|
||||
}
|
||||
|
||||
async function generateAuthUrl(sid) {
|
||||
async function generateAuthUrl() {
|
||||
const hostname = CONSTS.LNURL_AUTH_HOST;
|
||||
const secret = await generateSecret()
|
||||
await addHash(createHash(secret), sid)
|
||||
const secret = await generateSecret();
|
||||
const hash = createHash(secret);
|
||||
await addHash(hash)
|
||||
const url = `${hostname}?tag=login&k1=${secret}`
|
||||
return {
|
||||
url,
|
||||
encoded: lnurl.encode(url).toUpperCase(),
|
||||
secret,
|
||||
secretHash: hash,
|
||||
}
|
||||
}
|
||||
|
||||
async function getSidByK1(k1) {
|
||||
const hash = createHash(k1)
|
||||
async function getAuthTokenByHash(hash) {
|
||||
const data = await prisma.generatedK1.findFirst({
|
||||
where: {
|
||||
value: hash,
|
||||
@@ -79,6 +79,17 @@ async function getSidByK1(k1) {
|
||||
return data.sid;
|
||||
}
|
||||
|
||||
function associateTokenToHash(hash, token) {
|
||||
return prisma.generatedK1.update({
|
||||
where: {
|
||||
value: hash
|
||||
},
|
||||
data: {
|
||||
sid: token
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async function verifySig(sig, k1, key) {
|
||||
if (!lnurl.verifyAuthorizationSignature(sig, k1, key)) {
|
||||
const message = 'Signature verification failed'
|
||||
@@ -105,141 +116,12 @@ function createHash(data) {
|
||||
|
||||
|
||||
|
||||
|
||||
// function setupAuthMiddelwares(app) {
|
||||
// app.use(session({
|
||||
// secret: "12345",
|
||||
// resave: false,
|
||||
// saveUninitialized: true,
|
||||
// store: new SQLiteStore()
|
||||
// }));
|
||||
|
||||
// passport.use(
|
||||
// new lnurlAuth.Strategy(function (linkingPublicKey, done) {
|
||||
// const user = { id: linkingPublicKey };
|
||||
// console.log("Strategy Function");
|
||||
// console.log(user);
|
||||
// // let user = map.user.get(linkingPublicKey);
|
||||
// // if (!user) {
|
||||
// // user = { id: linkingPublicKey };
|
||||
// // map.user.set(linkingPublicKey, user);
|
||||
// // }
|
||||
// done(null, user);
|
||||
// })
|
||||
// );
|
||||
|
||||
// app.use(passport.initialize());
|
||||
// app.use(passport.session());
|
||||
// app.use(passport.authenticate("lnurl-auth"));
|
||||
// passport.serializeUser(function (user, done) {
|
||||
// done(null, user.id);
|
||||
// });
|
||||
|
||||
// passport.deserializeUser(function (id, done) {
|
||||
|
||||
// // done(null, map.user.get(id) || null);
|
||||
// done(null, id || null);
|
||||
// });
|
||||
// return app;
|
||||
// /*
|
||||
// app.get(
|
||||
// "/do-login",
|
||||
// function (req, res, next) {
|
||||
// next();
|
||||
// },
|
||||
// async function (req, res) {
|
||||
|
||||
|
||||
// if (req.query.k1 || req.query.key || req.query.sig) {
|
||||
// // Check signature against provided linking public key.
|
||||
// // This request could originate from a mobile app (ie. not their browser).
|
||||
// let session;
|
||||
// assert.ok(
|
||||
// req.query.k1,
|
||||
// new HttpError('Missing required parameter: "k1"', 400)
|
||||
// );
|
||||
// assert.ok(
|
||||
// req.query.sig,
|
||||
// new HttpError('Missing required parameter: "sig"', 400)
|
||||
// );
|
||||
// assert.ok(
|
||||
// req.query.key,
|
||||
// new HttpError('Missing required parameter: "key"', 400)
|
||||
// );
|
||||
// session = map.session.get(req.query.k1);
|
||||
// assert.ok(
|
||||
// session,
|
||||
// new HttpError("Secret does not match any known session", 400)
|
||||
// );
|
||||
// const { k1, sig, key } = req.query;
|
||||
// assert.ok(
|
||||
// verifyAuthorizationSignature(sig, k1, key),
|
||||
// new HttpError("Invalid signature", 400)
|
||||
// );
|
||||
// session.lnurlAuth = session.lnurlAuth || {};
|
||||
// session.lnurlAuth.linkingPublicKey = req.query.key;
|
||||
|
||||
|
||||
// const result = await session.save();
|
||||
// console.log(result);
|
||||
// res.status(200).json({ status: "OK" });
|
||||
// }
|
||||
|
||||
// req.session = req.session || {};
|
||||
// req.session.lnurlAuth = req.session.lnurlAuth || {};
|
||||
// let k1 = req.session.lnurlAuth.k1 || null;
|
||||
// if (!k1) {
|
||||
// k1 = req.session.lnurlAuth.k1 = generateSecret(32, "hex");
|
||||
// map.session.set(k1, req.session);
|
||||
// }
|
||||
|
||||
// const callbackUrl =
|
||||
// "https://" +
|
||||
// `${req.get("host")}/do-login?${querystring.stringify({
|
||||
// k1,
|
||||
// tag: "login",
|
||||
// })}`;
|
||||
|
||||
// const encoded = lnurl.encode(callbackUrl).toUpperCase();
|
||||
// const qrCode = await qrcode.toDataURL(encoded);
|
||||
// return res.json({
|
||||
// lnurl: encoded,
|
||||
// qrCode: qrCode,
|
||||
// });
|
||||
// }
|
||||
// );
|
||||
// */
|
||||
|
||||
// // app.get("/logout", function (req, res, next) {
|
||||
// // if (req.user) {
|
||||
// // req.session.destroy();
|
||||
// // return res.redirect("/");
|
||||
// // }
|
||||
// // next();
|
||||
// // });
|
||||
|
||||
// // app.get("/me", function (req, res, next) {
|
||||
// // res.json({ user: req.user ? req.user : null });
|
||||
|
||||
// // next();
|
||||
// // });
|
||||
|
||||
// // app.get("/profile", function (req, res, next) {
|
||||
// // if (!req.user) {
|
||||
// // return res.redirect("/login");
|
||||
// // }
|
||||
|
||||
// // res.render("profile", { user: req.user });
|
||||
|
||||
// // next();
|
||||
// // });
|
||||
// }
|
||||
|
||||
module.exports = {
|
||||
generateAuthUrl: generateAuthUrl,
|
||||
verifySig: verifySig,
|
||||
removeHash: removeHash,
|
||||
createHash: createHash,
|
||||
removeExpiredHashes: removeExpiredHashes,
|
||||
getSidByK1: getSidByK1
|
||||
getAuthTokenByHash: getAuthTokenByHash,
|
||||
associateTokenToHash: associateTokenToHash
|
||||
}
|
||||
Reference in New Issue
Block a user