-
-
Notifications
You must be signed in to change notification settings - Fork 644
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a password set/reset script (#186)
Co-authored-by: Matiss Janis Aboltins <[email protected]>
- Loading branch information
1 parent
bc93604
commit 104c980
Showing
7 changed files
with
230 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { needsBootstrap, bootstrap, changePassword } from '../account-db.js'; | ||
import { promptPassword } from '../util/prompt.js'; | ||
|
||
if (needsBootstrap()) { | ||
console.log( | ||
'It looks like you don’t have a password set yet. Let’s set one up now!', | ||
); | ||
|
||
promptPassword().then((password) => { | ||
let { error } = bootstrap(password); | ||
if (error) { | ||
console.log('Error setting password:', error); | ||
console.log( | ||
'Please report this as an issue: https://github.com/actualbudget/actual-server/issues', | ||
); | ||
} else { | ||
console.log('Password set!'); | ||
} | ||
}); | ||
} else { | ||
console.log('It looks like you already have a password set. Let’s reset it!'); | ||
promptPassword().then((password) => { | ||
let { error } = changePassword(password); | ||
if (error) { | ||
console.log('Error changing password:', error); | ||
console.log( | ||
'Please report this as an issue: https://github.com/actualbudget/actual-server/issues', | ||
); | ||
} else { | ||
console.log('Password changed!'); | ||
console.log( | ||
'Note: you will need to log in with the new password on any browsers or devices that are currently logged in.', | ||
); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { createInterface, cursorTo } from 'node:readline'; | ||
|
||
export async function prompt(message) { | ||
let rl = createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}); | ||
|
||
let promise = new Promise((resolve) => { | ||
rl.question(message, (answer) => { | ||
resolve(answer); | ||
rl.close(); | ||
}); | ||
}); | ||
|
||
let answer = await promise; | ||
|
||
return answer; | ||
} | ||
|
||
export async function promptPassword() { | ||
let password = await askForPassword('Enter a password, then press enter: '); | ||
|
||
if (password === '') { | ||
console.log('Password cannot be empty.'); | ||
return promptPassword(); | ||
} | ||
|
||
let password2 = await askForPassword( | ||
'Enter the password again, then press enter: ', | ||
); | ||
|
||
if (password !== password2) { | ||
console.log('Passwords do not match.'); | ||
return promptPassword(); | ||
} | ||
|
||
return password; | ||
} | ||
|
||
async function askForPassword(prompt) { | ||
let dataListener, endListener; | ||
|
||
let promise = new Promise((resolve) => { | ||
let result = ''; | ||
process.stdout.write(prompt); | ||
process.stdin.setRawMode(true); | ||
process.stdin.resume(); | ||
dataListener = (key) => { | ||
switch (key[0]) { | ||
case 0x03: // ^C | ||
process.exit(); | ||
break; | ||
case 0x0d: // Enter | ||
process.stdin.setRawMode(false); | ||
process.stdin.pause(); | ||
resolve(result); | ||
break; | ||
case 0x7f: // Backspace | ||
case 0x08: // Delete | ||
if (result) { | ||
result = result.slice(0, -1); | ||
cursorTo(process.stdout, prompt.length + result.length); | ||
process.stdout.write(' '); | ||
cursorTo(process.stdout, prompt.length + result.length); | ||
} | ||
break; | ||
default: | ||
result += key; | ||
process.stdout.write('*'); | ||
break; | ||
} | ||
}; | ||
process.stdin.on('data', dataListener); | ||
|
||
endListener = () => resolve(result); | ||
process.stdin.on('end', endListener); | ||
}); | ||
|
||
let answer = await promise; | ||
|
||
process.stdin.off('data', dataListener); | ||
process.stdin.off('end', endListener); | ||
|
||
process.stdout.write('\n'); | ||
|
||
return answer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
category: Features | ||
authors: [j-f1] | ||
--- | ||
|
||
Add an `npm run reset-password` script to set or reset the server password. |