Skip to content

Commit 492092e

Browse files
committed
chore: wip
1 parent c66c2d1 commit 492092e

File tree

24 files changed

+1941
-226
lines changed

24 files changed

+1941
-226
lines changed

storage/framework/core/api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@
4545
"devDependencies": {
4646
"@stacksjs/development": "workspace:*",
4747
"@stacksjs/utils": "workspace:*",
48-
"ts-open-api": "workspace:*"
48+
"ts-open-api": "link:ts-open-api"
4949
}
5050
}

storage/framework/core/auth/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@
3434
"typecheck": "bun tsc --noEmit",
3535
"prepublishOnly": "bun run build"
3636
},
37+
"dependencies": {
38+
"ts-auth": "link:ts-auth"
39+
},
3740
"devDependencies": {
38-
"@simplewebauthn/browser": "^13.1.2",
39-
"@simplewebauthn/server": "^13.1.2",
4041
"@stacksjs/development": "workspace:*",
4142
"@stacksjs/error-handling": "workspace:*",
42-
"@stacksjs/router": "workspace:*",
43-
"otplib": "^12.0.1"
43+
"@stacksjs/router": "workspace:*"
4444
}
4545
}
Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,49 @@
11
import { handleError } from '@stacksjs/error-handling'
2-
import { authenticator } from 'otplib'
3-
import qrcode from 'qrcode'
2+
import { generate, generateQRCodeDataURL, generateSecret, keyuri, QRErrorCorrection, verify } from 'ts-auth'
43

54
export function generateTwoFactorSecret(): string {
6-
const secret = authenticator.generateSecret()
7-
8-
return secret
5+
return generateSecret()
96
}
107

118
export type Token = string
129
export type Secret = string
13-
export function generateTwoFactorToken(): Token {
14-
return authenticator.generate(generateTwoFactorSecret())
10+
11+
export function generateTwoFactorToken(secret: Secret): Token {
12+
return generate({ secret })
1513
}
1614

1715
export function verifyTwoFactorCode(token: Token, secret: Secret): boolean {
18-
const isValid = authenticator.verify({ token, secret })
19-
20-
return isValid
16+
return verify(token, { secret })
2117
}
2218

23-
export function generateQrCode(): void {
24-
const user = 'johndoe@example.com'
25-
const service = 'StacksJS 2fa'
26-
const secret = generateTwoFactorSecret()
27-
const otpauth = authenticator.keyuri(user, service, secret)
19+
/**
20+
* Generate a QR code for two-factor authentication
21+
*
22+
* @param user - User identifier (email or username)
23+
* @param service - Service name (e.g., 'StacksJS 2FA')
24+
* @param secret - Optional secret (will be generated if not provided)
25+
* @returns Promise resolving to the data URL of the QR code
26+
*/
27+
export async function generateQrCode(
28+
user?: string,
29+
service?: string,
30+
secret?: Secret
31+
): Promise<string> {
32+
const userIdentifier = user || 'johndoe@example.com'
33+
const serviceName = service || 'StacksJS 2fa'
34+
const otpSecret = secret || generateTwoFactorSecret()
35+
const otpauth = keyuri(userIdentifier, serviceName, otpSecret)
2836

29-
qrcode.toDataURL(otpauth, (err: any) => {
30-
// qrcode.toDataURL(otpauth, (err: any, imageUrl: any) => {
31-
if (err) {
32-
handleError('Error with QR', err)
33-
}
34-
})
37+
try {
38+
return await generateQRCodeDataURL({
39+
text: otpauth,
40+
width: 256,
41+
height: 256,
42+
correctLevel: QRErrorCorrection.H,
43+
})
44+
}
45+
catch (error) {
46+
handleError('Error generating QR code', error)
47+
throw error
48+
}
3549
}

storage/framework/core/security/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@
3434
"typecheck": "bun tsc --noEmit",
3535
"prepublishOnly": "bun run build"
3636
},
37+
"dependencies": {
38+
"ts-security-crypto": "link:ts-security-crypto"
39+
},
3740
"devDependencies": {
3841
"@stacksjs/config": "workspace:*",
3942
"@stacksjs/development": "workspace:*",
4043
"@stacksjs/env": "workspace:*",
4144
"@stacksjs/types": "workspace:*",
42-
"@stacksjs/validation": "workspace:*",
43-
"crypto-js": "^4.2.0",
44-
"js-base64": "^3.7.8"
45+
"@stacksjs/validation": "workspace:*"
4546
}
4647
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import { config } from '@stacksjs/config'
2-
import aes from 'crypto-js/aes'
3-
import utf8 from 'crypto-js/enc-utf8'
2+
import { decrypt as cryptoDecrypt, encrypt as cryptoEncrypt } from 'ts-security-crypto'
43

5-
function encrypt(message: string, customPassphrase?: string): string {
4+
async function encrypt(message: string, customPassphrase?: string): Promise<string> {
65
const passphrase = customPassphrase || config.app.key
76

87
if (!passphrase)
98
throw new Error('APP_KEY is not defined')
109

11-
return aes.encrypt(message, passphrase).toString()
10+
const result = await cryptoEncrypt(message, passphrase)
11+
return result.encrypted
1212
}
1313

14-
function decrypt(encrypted: string, customPassphrase?: string): string {
14+
async function decrypt(encrypted: string, customPassphrase?: string): Promise<string> {
1515
const passphrase = customPassphrase || config.app.key
1616

1717
if (!passphrase)
1818
throw new Error('APP_KEY is not defined')
1919

20-
return aes.decrypt(encrypted, passphrase).toString(utf8)
20+
return await cryptoDecrypt(encrypted, passphrase)
2121
}
2222

2323
export { decrypt, encrypt }

storage/framework/core/security/src/hash.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { hashing } from '@stacksjs/config'
2-
import md5 from 'crypto-js/md5'
3-
import { Base64 } from 'js-base64'
2+
import { base64Decode, base64Encode, hashPassword, md5, verifyPassword } from 'ts-security-crypto'
43

54
interface MakeOptions {
65
algorithm?: 'bcrypt' | 'base64' | 'argon2'
@@ -9,7 +8,7 @@ interface MakeOptions {
98

109
async function make(password: string, options?: MakeOptions): Promise<string> {
1110
if (options?.algorithm === 'argon2')
12-
return await argon2Encode(password, { type: 'argon2id' })
11+
return await argon2Encode(password, { type: options.type || 'argon2id' })
1312
if (options?.algorithm === 'bcrypt')
1413
return await bcryptEncode(password)
1514
if (options?.algorithm === 'base64')
@@ -35,12 +34,10 @@ export async function bcryptEncode(password: string): Promise<string> {
3534
if (!hashing.bcrypt)
3635
throw new Error('Bcrypt hashing is not configured')
3736

38-
const bcryptHash = await Bun.password.hash(password, {
37+
return await hashPassword(password, {
3938
algorithm: 'bcrypt',
4039
cost: hashing.bcrypt.cost,
4140
})
42-
43-
return bcryptHash
4441
}
4542

4643
export async function argon2Encode(
@@ -50,33 +47,27 @@ export async function argon2Encode(
5047
if (!hashing.argon2)
5148
throw new Error('Argon2 hashing is not configured')
5249

53-
const argon2Hash = await Bun.password.hash(password, {
50+
return await hashPassword(password, {
5451
algorithm: options?.type || 'argon2id',
5552
memoryCost: hashing.argon2.memory,
5653
timeCost: hashing.argon2.time,
5754
})
58-
59-
return argon2Hash
6055
}
6156

6257
export async function argon2Verify(password: string, hash: string): Promise<boolean> {
63-
return await Bun.password.verify(password, hash)
58+
return await verifyPassword(password, hash)
6459
}
6560

6661
export async function bcryptVerify(password: string, hash: string): Promise<boolean> {
67-
return await Bun.password.verify(password, hash)
68-
}
69-
70-
export function base64Encode(password: string): string {
71-
return Base64.encode(password)
62+
return await verifyPassword(password, hash)
7263
}
7364

7465
export function base64Verify(password: string, hash: string): boolean {
75-
return Base64.decode(hash) === password
66+
return base64Decode(hash) === password
7667
}
7768

78-
export function md5Encode(password: string): CryptoJS.lib.WordArray {
69+
export function md5Encode(password: string): string {
7970
return md5(password)
8071
}
8172

82-
export { make as makeHash, verify as verifyHash }
73+
export { make as makeHash, verify as verifyHash, base64Encode }
Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
import { getRandomValues } from 'node:crypto'
2-
import base64 from 'crypto-js/enc-base64'
3-
import utf8 from 'crypto-js/enc-utf8'
1+
import { generateKey } from 'ts-security-crypto'
42

53
export function generateAppKey(): string {
6-
const random = getRandomValues(new Uint8Array(32))
7-
const encodedWord = utf8.parse(random.toString())
8-
const key = base64.stringify(encodedWord)
9-
10-
return `base64:${key}`
4+
return generateKey(32)
115
}

storage/framework/core/server/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
"@stacksjs/path": "workspace:*",
4949
"@stacksjs/router": "workspace:*",
5050
"@stacksjs/validation": "workspace:*",
51-
"bun-plugin-auto-imports": "^0.3.1",
52-
"vite": "npm:rolldown-vite@latest"
51+
"bun-plugin-auto-imports": "^0.3.1"
5352
}
5453
}

storage/framework/core/socials/package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,8 @@
3535
"prepublishOnly": "bun run build"
3636
},
3737
"devDependencies": {
38-
"@simplewebauthn/browser": "^13.1.2",
39-
"@simplewebauthn/server": "^13.1.2",
4038
"@stacksjs/development": "workspace:*",
4139
"@stacksjs/error-handling": "workspace:*",
42-
"@stacksjs/router": "workspace:*",
43-
"otplib": "^12.0.1"
40+
"@stacksjs/router": "workspace:*"
4441
}
4542
}

storage/framework/core/storage/package.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,11 @@
4747
"typecheck": "bun tsc --noEmit",
4848
"prepublishOnly": "bun run build"
4949
},
50-
"devDependencies": {
50+
"dependencies": {
5151
"@aws-sdk/client-s3": "^3.876.0",
52-
"@flystorage/aws-s3": "^1.2.0",
53-
"@flystorage/chaos": "^1.1.0",
54-
"@flystorage/file-storage": "^1.1.0",
55-
"@flystorage/in-memory": "^1.1.0",
56-
"@flystorage/local-fs": "^1.1.0",
52+
"@aws-sdk/s3-request-presigner": "^3.876.0"
53+
},
54+
"devDependencies": {
5755
"@stacksjs/arrays": "workspace:*",
5856
"@stacksjs/development": "workspace:*",
5957
"@stacksjs/error-handling": "workspace:*",

0 commit comments

Comments
 (0)