From 5d0bcc29bb498e7cb0eaa1162ad3c1645bdc6c25 Mon Sep 17 00:00:00 2001 From: Evan Lucas Date: Mon, 31 Aug 2015 16:47:33 -0500 Subject: [PATCH] initial commit --- .gitignore | 1 + .travis.yml | 6 ++++ LICENSE | 21 +++++++++++++ README.md | 29 ++++++++++++++++++ index.js | 1 + lib/index.js | 24 +++++++++++++++ package.json | 23 +++++++++++++++ test/test.js | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 188 insertions(+) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 index.js create mode 100644 lib/index.js create mode 100644 package.json create mode 100644 test/test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9132f3b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.nyc_output/ diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..136dd9f --- /dev/null +++ b/.travis.yml @@ -0,0 +1,6 @@ +language: node_js +node_js: + - "0.10" + - "0.12" + - "iojs" +sudo: false diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e95fd6d --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 Help.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..cfe17fe --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# strip-passwords + +[![Build Status](https://travis-ci.org/helpdotcom/strip-passwords.svg)](https://travis-ci.org/helpdotcom/strip-passwords) +[![Coverage Status](https://coveralls.io/repos/helpdotcom/strip-passwords/badge.svg?branch=master&service=github)](https://coveralls.io/github/helpdotcom/strip-passwords?branch=master) + +Strip passwords from an object + +## Install + +```bash +$ npm install --save strip-passwords +``` + +## Usage + +```js +var clean = require('strip-passwords') + +clean({ password: 'test' }) +// => { password: '****' } +``` + +## Author + +Evan Lucas + +## License + +MIT (See `LICENSE` for more info) diff --git a/index.js b/index.js new file mode 100644 index 0000000..ce914ea --- /dev/null +++ b/index.js @@ -0,0 +1 @@ +module.exports = require('./lib') diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..17abb0a --- /dev/null +++ b/lib/index.js @@ -0,0 +1,24 @@ +'use strict' + +module.exports = clean + +function clean(msg) { + if (typeof msg !== 'object') { + return msg + } + var keys = Object.keys(msg) + for (var i = 0, len = keys.length; i < len; i++) { + var key = keys[i] + if (key === 'password') { + msg[key] = '****' + } else if (Array.isArray(msg[key])) { + msg[key] = msg[key].map(function(item) { + return clean(item) + }); + } else if (typeof msg[key] === 'object') { + msg[key] = clean(msg[key]) + } + } + + return msg +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..5aa8d04 --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "strip-passwords", + "version": "1.0.0", + "description": "Strip passwords from an object", + "main": "index.js", + "scripts": { + "test": "tap test/test.js --cov" + }, + "dependencies": {}, + "devDependencies": { + "tap": "~1.3.4" + }, + "license": "MIT", + "author": "Evan Lucas ", + "repository": { + "type": "git", + "url": "https://github.com/helpdotcom/strip-passwords" + }, + "homepage": "https://github.com/helpdotcom/strip-passwords", + "bugs": { + "url": "https://github.com/helpdotcom/strip-passwords/issues" + } +} diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..aab8ad6 --- /dev/null +++ b/test/test.js @@ -0,0 +1,83 @@ +'use strict'; + +var test = require('tap').test +var clean = require('../') + +var tests = [ + { + name: 'should clean out passwords' + , test: { + meta: {} + , data: { + email: 'biscuits' + , password: 'password' + } + } + , expected: { + meta: {} + , data: { + email: 'biscuits' + , password: '****' + } + } + } +, { + name: 'should work recursively' + , test: { + meta: {} + , data: { + meta: {} + , data: { + email: 'biscuits' + , password: 'password' + } + } + } + , expected: { + meta: {} + , data: { + meta: {} + , data: { + email: 'biscuits' + , password: '****' + } + } + } + } +, { + name: 'should return msg if string' + , test: 'str' + , expected: 'str' + } +, { + name: 'should return msg if bool' + , test: true + , expected: true + } +, { + name: 'should work for arrays' + , test: { + data: [{ + meta: { password: '1234' } + } + , { + meta: { password: '1234' } + }] + } + , expected: { + data: [{ + meta: { password: '****' } + } + , { + meta: { password: '****' } + }] + } + } +] + +tests.forEach(function(tester) { + test(tester.name, function(t) { + t.deepEqual(clean(tester.test), tester.expected) + t.end() + }) +})