Skip to content

Commit

Permalink
feat: prompt user for secure string
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Dec 12, 2017
1 parent 6a5cc46 commit e0b854c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ the help of [semantic-release](https://github.com/semantic-release/semantic-rele

- The text is stored in an encrypted buffer
- The plain text is only available in a `callback`
- Prompting the user for a secure string

## Getting started

Expand All @@ -27,6 +28,7 @@ Install with [npm](http://blog.npmjs.org/post/85484771375/how-to-install-npm)

```js
const SecureString = require('secure-string')

const password = new SecureString()
password.appendCodePoint(0x41)
password.value(plainText => {
Expand All @@ -36,6 +38,21 @@ password.value(plainText => {

See the [spec](./test/secure-string.spec.js) for more examples.

### Ask

`SecureString.ask` prompts the user for some data

```js
const SecureString = require('secure-string')

SecureString.ask('password', (err, answer) => {
if (err) return console.log(err)
answer.value(plainText => {
console.log('the passowrd is', plainText.toString())
})
})
```

# License
The [MIT license](./LICENSE).

Expand Down
35 changes: 35 additions & 0 deletions src/ask.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
`use strict`

const process = require('process')
const readline = require('readline')
const SecureString = require('./secure-string')

function ask (prompt, callback) {
const secret = new SecureString()
const stdin = process.stdin
const stdout = process.stdout
const rl = readline.createInterface({
input: stdin,
output: null
})

function keypress(s, key) {
if (s === '\r' || s === '\n') {
stdin.removeListener('keypress', keypress)
callback(null, secret)
} else if (s !== undefined) {
secret.appendCodePoint(s.codePointAt(0))
}
}

stdin.on('keypress', keypress)
readline.emitKeypressEvents(stdin)
if (stdin.isTTY)
process.stdin.setRawMode(true)

if (stdout)
stdout.write(prompt)
stdin.resume()
}

module.exports = ask
30 changes: 30 additions & 0 deletions test/ask.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* eslint-env mocha */

'use strict'

const chai = require('chai')
const expect = chai.expect
chai.use(require('dirty-chai'))
const SecureString = require('..')
const Readable = require('stream').Readable;

describe('ask', () => {
it('static', () => {
const password = new SecureString()
expect(SecureString.ask).to.exist()
})

it.skip('returns a secure string', done => {
SecureString.ask('password', (err, answer) => {
expect(err).to.not.exist()
expect(answer).to.exist()
expect(answer).to.be.instanceOf(SecureString)
answer.value(plainText => {
expect(plainText).to.exist();
expect(plainText).to.be.instanceOf(Buffer)
expect(plainText.toString()).to.equal('')
done()
})
})
})
})

0 comments on commit e0b854c

Please sign in to comment.