-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword-reset.js
More file actions
executable file
·109 lines (92 loc) · 3.45 KB
/
Copy pathpassword-reset.js
File metadata and controls
executable file
·109 lines (92 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env bun
/**
* CLI tool to reset user passwords from the command line.
*
* Usage: bun password-reset.js <email> <new-password>
* Example: bun password-reset.js user@example.com newpass123
*
* This tool is for administrative use only and should not be exposed via API.
*/
import { database } from './src/database/index.js'
import { USERS } from './src/database/queries.js'
/**
* Hashes a password using Argon2i algorithm
*/
async function hashPassword(password) {
return await Bun.password.hash(password, {
algorithm: 'argon2i',
memoryCost: Number(process.env.MEMORY_COST || 4),
timeCost: Number(process.env.TIME_COST || 3)
})
}
/**
* Resets a user's password by email
*/
async function resetPassword(email, newPassword) {
try {
// Find the user by email
const findQuery = database.query(USERS.FIND_BY_EMAIL)
const user = findQuery.get({ email })
if (!user) {
console.error(`❌ Error: User with email "${email}" not found.`)
process.exit(1)
}
// Hash the new password
console.log('🔐 Hashing new password...')
const hashedPassword = await hashPassword(newPassword)
// Update the user's password
const updateQuery = database.query(`
UPDATE users
SET password = :password,
updated_at = CURRENT_TIMESTAMP
WHERE email = :email;
`)
updateQuery.run({ email, password: hashedPassword })
console.log(`✅ Password successfully reset for user: ${email}`)
console.log(` User ID: ${user.id}`)
console.log(` Name: ${user.name}`)
console.log(` Role: ${user.role}`)
} catch (error) {
console.error('❌ Error resetting password:', error.message)
process.exit(1)
}
}
// Main execution
function main() {
const args = process.argv.slice(2)
// Show help
if (args.length === 0 || args.includes('--help') || args.includes('-h')) {
console.log('Password Reset CLI Tool')
console.log('=======================\n')
console.log('Reset user passwords from the command line.\n')
console.log('Usage: bun password-reset.js <email> <new-password>')
console.log('Example: bun password-reset.js user@example.com newpass123\n')
console.log('Security Notes:')
console.log(' • This tool is for administrative use only')
console.log(' • Never expose this functionality via an API')
console.log(' • Passwords must be at least 6 characters long')
console.log(' • Passwords are hashed using Argon2i before storage')
process.exit(args.length === 0 ? 1 : 0)
}
if (args.length !== 2) {
console.error('❌ Invalid arguments.')
console.error('\nUsage: bun password-reset.js <email> <new-password>')
console.error('Example: bun password-reset.js user@example.com newpass123')
process.exit(1)
}
const [email, newPassword] = args
// Validate email format
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
if (!emailRegex.test(email)) {
console.error('❌ Error: Invalid email format.')
process.exit(1)
}
// Validate password length
if (newPassword.length < 6) {
console.error('❌ Error: Password must be at least 6 characters long.')
process.exit(1)
}
console.log('🔄 Resetting password...')
resetPassword(email, newPassword)
}
main()