Update: nueva version Horux Despachos
This commit is contained in:
26
packages/core/package.json
Normal file
26
packages/core/package.json
Normal file
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"name": "@horux/core",
|
||||
"version": "0.0.1",
|
||||
"private": true,
|
||||
"main": "./src/index.ts",
|
||||
"types": "./src/index.ts",
|
||||
"scripts": {
|
||||
"lint": "eslint src/",
|
||||
"typecheck": "tsc --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"@horux/shared": "workspace:*",
|
||||
"bcryptjs": "^2.4.3",
|
||||
"jsonwebtoken": "^9.0.2",
|
||||
"nodemailer": "^8.0.0",
|
||||
"express": "^4.21.0",
|
||||
"zod": "^3.23.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/bcryptjs": "^2.4.6",
|
||||
"@types/jsonwebtoken": "^9.0.7",
|
||||
"@types/nodemailer": "^6.4.17",
|
||||
"@types/express": "^5.0.0",
|
||||
"typescript": "^5.3.0"
|
||||
}
|
||||
}
|
||||
2
packages/core/src/auth/index.ts
Normal file
2
packages/core/src/auth/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './token';
|
||||
export * from './password';
|
||||
11
packages/core/src/auth/password.ts
Normal file
11
packages/core/src/auth/password.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import bcrypt from 'bcryptjs';
|
||||
|
||||
const SALT_ROUNDS = 12;
|
||||
|
||||
export async function hashPassword(password: string): Promise<string> {
|
||||
return bcrypt.hash(password, SALT_ROUNDS);
|
||||
}
|
||||
|
||||
export async function verifyPassword(password: string, hash: string): Promise<boolean> {
|
||||
return bcrypt.compare(password, hash);
|
||||
}
|
||||
37
packages/core/src/auth/token.ts
Normal file
37
packages/core/src/auth/token.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import jwt, { type SignOptions } from 'jsonwebtoken';
|
||||
import { randomBytes } from 'crypto';
|
||||
import type { JWTPayload } from '@horux/shared';
|
||||
|
||||
export interface TokenConfig {
|
||||
secret: string;
|
||||
accessExpiresIn: string;
|
||||
refreshExpiresIn: string;
|
||||
}
|
||||
|
||||
export function generateAccessToken(payload: Omit<JWTPayload, 'iat' | 'exp'>, config: TokenConfig): string {
|
||||
const options: SignOptions = {
|
||||
expiresIn: config.accessExpiresIn as SignOptions['expiresIn'],
|
||||
jwtid: randomBytes(8).toString('hex'),
|
||||
};
|
||||
return jwt.sign(payload, config.secret, options);
|
||||
}
|
||||
|
||||
export function generateRefreshToken(payload: Omit<JWTPayload, 'iat' | 'exp'>, config: TokenConfig): string {
|
||||
const options: SignOptions = {
|
||||
expiresIn: config.refreshExpiresIn as SignOptions['expiresIn'],
|
||||
jwtid: randomBytes(8).toString('hex'),
|
||||
};
|
||||
return jwt.sign(payload, config.secret, options);
|
||||
}
|
||||
|
||||
export function verifyToken(token: string, secret: string): JWTPayload {
|
||||
return jwt.verify(token, secret) as JWTPayload;
|
||||
}
|
||||
|
||||
export function decodeToken(token: string): JWTPayload | null {
|
||||
try {
|
||||
return jwt.decode(token) as JWTPayload;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
30
packages/core/src/crypto/aes-gcm.ts
Normal file
30
packages/core/src/crypto/aes-gcm.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { createCipheriv, createDecipheriv, randomBytes, createHash } from 'crypto';
|
||||
|
||||
const ALGORITHM = 'aes-256-gcm';
|
||||
const IV_LENGTH = 16;
|
||||
|
||||
export function deriveAesKey(secret: string): Buffer {
|
||||
return createHash('sha256').update(secret).digest();
|
||||
}
|
||||
|
||||
export function encryptAesGcm(data: Buffer, key: Buffer): { encrypted: Buffer; iv: Buffer; tag: Buffer } {
|
||||
const iv = randomBytes(IV_LENGTH);
|
||||
const cipher = createCipheriv(ALGORITHM, key, iv);
|
||||
const encrypted = Buffer.concat([cipher.update(data), cipher.final()]);
|
||||
const tag = cipher.getAuthTag();
|
||||
return { encrypted, iv, tag };
|
||||
}
|
||||
|
||||
export function decryptAesGcm(encrypted: Buffer, iv: Buffer, tag: Buffer, key: Buffer): Buffer {
|
||||
const decipher = createDecipheriv(ALGORITHM, key, iv);
|
||||
decipher.setAuthTag(tag);
|
||||
return Buffer.concat([decipher.update(encrypted), decipher.final()]);
|
||||
}
|
||||
|
||||
export function encryptStringAesGcm(text: string, key: Buffer): { encrypted: Buffer; iv: Buffer; tag: Buffer } {
|
||||
return encryptAesGcm(Buffer.from(text, 'utf-8'), key);
|
||||
}
|
||||
|
||||
export function decryptToStringAesGcm(encrypted: Buffer, iv: Buffer, tag: Buffer, key: Buffer): string {
|
||||
return decryptAesGcm(encrypted, iv, tag, key).toString('utf-8');
|
||||
}
|
||||
1
packages/core/src/crypto/index.ts
Normal file
1
packages/core/src/crypto/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './aes-gcm';
|
||||
1
packages/core/src/email/index.ts
Normal file
1
packages/core/src/email/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './transport';
|
||||
60
packages/core/src/email/transport.ts
Normal file
60
packages/core/src/email/transport.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { createTransport, type Transporter } from 'nodemailer';
|
||||
|
||||
export interface SmtpConfig {
|
||||
host: string;
|
||||
port: number;
|
||||
user: string;
|
||||
pass: string;
|
||||
from: string;
|
||||
}
|
||||
|
||||
export interface EmailTransport {
|
||||
send(to: string, subject: string, html: string): Promise<void>;
|
||||
}
|
||||
|
||||
export function createEmailTransport(config: SmtpConfig | null): EmailTransport {
|
||||
let transporter: Transporter | null = null;
|
||||
|
||||
function getTransporter(): Transporter {
|
||||
if (!transporter) {
|
||||
if (!config || !config.user || !config.pass) {
|
||||
console.warn('[EMAIL] SMTP not configured. Emails will be logged to console.');
|
||||
return {
|
||||
sendMail: async (opts: any) => {
|
||||
console.log('[EMAIL] Would send:', { to: opts.to, subject: opts.subject });
|
||||
return { messageId: 'mock' };
|
||||
},
|
||||
} as any;
|
||||
}
|
||||
|
||||
transporter = createTransport({
|
||||
host: config.host,
|
||||
port: config.port,
|
||||
secure: false,
|
||||
requireTLS: true,
|
||||
auth: {
|
||||
user: config.user,
|
||||
pass: config.pass,
|
||||
},
|
||||
});
|
||||
}
|
||||
return transporter;
|
||||
}
|
||||
|
||||
return {
|
||||
async send(to: string, subject: string, html: string) {
|
||||
const transport = getTransporter();
|
||||
try {
|
||||
await transport.sendMail({
|
||||
from: config?.from ?? 'noreply@example.com',
|
||||
to,
|
||||
subject,
|
||||
html,
|
||||
text: html.replace(/<[^>]*>/g, ''),
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('[EMAIL] Error sending email:', error);
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
3
packages/core/src/index.ts
Normal file
3
packages/core/src/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from './auth';
|
||||
export * from './email';
|
||||
export * from './crypto';
|
||||
17
packages/core/tsconfig.json
Normal file
17
packages/core/tsconfig.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "bundler",
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"declaration": true,
|
||||
"resolveJsonModule": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"isolatedModules": true
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["node_modules", "dist"]
|
||||
}
|
||||
Reference in New Issue
Block a user