feat: initialize microservice architecture with auth, api, realtime, copernicus, ml, and console modules
This commit is contained in:
54
auth/src/tools/security.js
Normal file
54
auth/src/tools/security.js
Normal file
@@ -0,0 +1,54 @@
|
||||
const bcrypt = require('bcrypt');
|
||||
const crypto = require('crypto');
|
||||
|
||||
const saltRounds = 12;
|
||||
|
||||
/**
|
||||
* Genera un hash di una password
|
||||
* @param {string} password - Password da hashare
|
||||
* @returns {string} - Hash della password
|
||||
*/
|
||||
function hashPassword(password) {
|
||||
return bcrypt.hashSync(password, saltRounds);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifica una password
|
||||
* @param {string} password - Password da verificare
|
||||
* @param {string} hash - Hash della password
|
||||
* @returns {boolean} - True se la password è corretta, false altrimenti
|
||||
*/
|
||||
function verifyPassword(password, hash) {
|
||||
return bcrypt.compareSync(password, hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a session token from code and username
|
||||
* Format: XXXXXXXX-base64_username
|
||||
* @param {string} sessionCode
|
||||
* @param {string} username
|
||||
* @returns {string} Session token
|
||||
*/
|
||||
function generateSessionCode() {
|
||||
return crypto.randomBytes(32).toString('base64url');
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a session token
|
||||
* @param {string} token
|
||||
* @returns {string|null} The session token itself if valid
|
||||
*/
|
||||
function parseSessionToken(token) {
|
||||
if (!token || typeof token !== 'string' || token.length < 32 || token.length > 64) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
hashPassword,
|
||||
verifyPassword,
|
||||
generateSessionCode,
|
||||
parseSessionToken
|
||||
};
|
||||
Reference in New Issue
Block a user