mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-11-04 15:11:31 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			38 lines
		
	
	
		
			934 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			38 lines
		
	
	
		
			934 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
"use strict";
 | 
						|
 | 
						|
const utils = require('./utils');
 | 
						|
const session = {};
 | 
						|
 | 
						|
function setDataKey(req, decryptedDataKey) {
 | 
						|
    session.decryptedDataKey = Array.from(decryptedDataKey); // can't store buffer in session
 | 
						|
    session.protectedSessionId = utils.randomSecureToken(32);
 | 
						|
 | 
						|
    return session.protectedSessionId;
 | 
						|
}
 | 
						|
 | 
						|
function getProtectedSessionId(req) {
 | 
						|
    return req.headers.protected_session_id;
 | 
						|
}
 | 
						|
 | 
						|
function getDataKey(req) {
 | 
						|
    const protectedSessionId = getProtectedSessionId(req);
 | 
						|
 | 
						|
    if (protectedSessionId && session.protectedSessionId === protectedSessionId) {
 | 
						|
        return session.decryptedDataKey;
 | 
						|
    }
 | 
						|
    else {
 | 
						|
        return null;
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
function isProtectedSessionAvailable(req) {
 | 
						|
    const protectedSessionId = getProtectedSessionId(req);
 | 
						|
 | 
						|
    return protectedSessionId && session.protectedSessionId === protectedSessionId;
 | 
						|
}
 | 
						|
 | 
						|
module.exports = {
 | 
						|
    setDataKey,
 | 
						|
    getDataKey,
 | 
						|
    isProtectedSessionAvailable
 | 
						|
}; |