mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-01-14 06:28:52 +01:00
refactor(crypto): reorganize token hashing and signing functionality
This commit is contained in:
parent
6f4c8ef114
commit
494c6d2596
10 changed files with 27 additions and 66 deletions
|
|
@ -1,26 +0,0 @@
|
|||
const jwt = require('jsonwebtoken');
|
||||
|
||||
/**
|
||||
* Signs a given payload using either the `jose` library (for Bun runtime) or `jsonwebtoken`.
|
||||
*
|
||||
* @async
|
||||
* @function
|
||||
* @param {Object} options - The options for signing the payload.
|
||||
* @param {Object} options.payload - The payload to be signed.
|
||||
* @param {string} options.secret - The secret key used for signing.
|
||||
* @param {number} options.expirationTime - The expiration time in seconds.
|
||||
* @returns {Promise<string>} Returns a promise that resolves to the signed JWT.
|
||||
* @throws {Error} Throws an error if there's an issue during signing.
|
||||
*
|
||||
* @example
|
||||
* const signedPayload = await signPayload({
|
||||
* payload: { userId: 123 },
|
||||
* secret: 'my-secret-key',
|
||||
* expirationTime: 3600
|
||||
* });
|
||||
*/
|
||||
async function signPayload({ payload, secret, expirationTime }) {
|
||||
return jwt.sign(payload, secret, { expiresIn: expirationTime });
|
||||
}
|
||||
|
||||
module.exports = signPayload;
|
||||
|
|
@ -106,12 +106,6 @@ function decryptV3(encryptedValue) {
|
|||
return decrypted.toString('utf8');
|
||||
}
|
||||
|
||||
async function hashToken(str) {
|
||||
const data = new TextEncoder().encode(str);
|
||||
const hashBuffer = await webcrypto.subtle.digest('SHA-256', data);
|
||||
return Buffer.from(hashBuffer).toString('hex');
|
||||
}
|
||||
|
||||
async function getRandomValues(length) {
|
||||
if (!Number.isInteger(length) || length <= 0) {
|
||||
throw new Error('Length must be a positive integer');
|
||||
|
|
@ -141,7 +135,6 @@ module.exports = {
|
|||
decryptV2,
|
||||
encryptV3,
|
||||
decryptV3,
|
||||
hashToken,
|
||||
hashBackupCode,
|
||||
getRandomValues,
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue