Initial commit

This commit is contained in:
Torsten Schulz (notebook)
2024-08-16 16:34:23 +02:00
commit 31ca0979ce
4216 changed files with 499206 additions and 0 deletions

21
backend/utils/encrypt.js Normal file
View File

@@ -0,0 +1,21 @@
import crypto from 'crypto';
const createHash = (value) => {
return crypto.createHash('sha256').update((typeof value === 'string' ? value : value.toString())).digest('hex');
}
function encryptData(data) {
const cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(process.env.ENCRYPTION_KEY, 'hex'), Buffer.alloc(16, 0));
let encrypted = cipher.update(data, 'utf8', 'hex');
encrypted += cipher.final('hex');
return encrypted;
}
function decryptData(data) {
const decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(process.env.ENCRYPTION_KEY, 'hex'), Buffer.alloc(16, 0));
let decrypted = decipher.update(data, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
export default { createHash, encryptData, decryptData };

View File

@@ -0,0 +1,28 @@
import { User, UserClub } from "../models";
export const getUserByToken = async(token) => {
const user = await User.findOne({
where: [
{hashedId: token}
]
});
return user;
}
export const hasUserClubAccess = async(userId, clubId) => {
const userClub = UserClub.findOne({
where: {
userId: userId,
clubId: clubId,
approved: true
}
});
return userClub !== null;
}
export const checkAccess = async(userToken, clubId) => {
const user = getUserByToken(userToken);
if (!hasUserClubAccess(user.id, clubId)) {
throw new Error('noaccess');
}
}