-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
113 additions
and
191 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
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 |
---|---|---|
@@ -1,31 +1,29 @@ | ||
// FUNCTIONAL PROMISES (aka Functional River) | ||
const Promise = require('bluebird') | ||
const {hashStringAsync} = require('./lib/crypto') | ||
const {logEventAsync} = require('./lib/log') | ||
const {getModelAsync} = require('./lib/db') | ||
|
||
module.exports = {auth} | ||
// Functional River Pattern | ||
const {hashString} = require('./lib/crypto') | ||
const users = require('./lib/users') | ||
|
||
function auth({username = '', password = ''}) { | ||
return Promise.resolve({username, password}) | ||
.then(_checkArgs) | ||
.tap(logEventAsync({event: 'login', username})) | ||
.then(_loginUser) | ||
.then(_checkUser) | ||
} | ||
|
||
function _checkArgs({username = '', password = ''}) { | ||
if (username.length < 1 || password.length < 6) throw new Error('Check args') | ||
if (username.length < 1) throw new Error('Enter valid username') | ||
if (password.length < 6) throw new Error('Enter valid Password') | ||
return {username, password} | ||
} | ||
|
||
function _loginUser({username, password}) { | ||
return Promise.props({ | ||
Users: getModelAsync('users'), | ||
password: hashStringAsync(password) | ||
}).then(({Users, password}) => Users.findOneAsync({username, password})) | ||
return Promise.resolve(password) | ||
.then(hashString) | ||
.then(password => users.getOne({username, password})) | ||
} | ||
|
||
function _checkUser(user) { | ||
return user && user._id ? user : Promise.reject(new Error('User Not found!')) | ||
if (user && user._id) return user | ||
throw new Error('No User found. Check credentials and try again.') | ||
} | ||
|
||
module.exports = {auth, _checkArgs, _loginUser, _checkUser} |
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 |
---|---|---|
@@ -1,46 +1,49 @@ | ||
const test = require('ava') | ||
const {auth, _checkArgs, _loginUser, _checkUser} = require('./auth.fp') | ||
|
||
test.before('create user records', t => { | ||
const {addUserAsync} = require('./lib/db') | ||
return addUserAsync({id: 1, username: 'alice', email: 'alice@nsa.gov', password: 'superSecret1'}) | ||
.tap(result => { | ||
// console.log('\ntest user:', result, '\n') | ||
const users = require('./lib/users') | ||
const {hashString} = require('./lib/crypto') | ||
const password = hashString('superSecret1') | ||
return users.create({username: 'alice', email: 'alice@nsa.gov', password}) | ||
.then(result => { | ||
t.is(typeof result, 'object') | ||
t.pass() | ||
}) | ||
}) | ||
|
||
test.cb('callback: login successful', t => { | ||
test('fun. river: _checkArgs', t => { | ||
t.plan(2) | ||
const {auth} = require('./auth.callbacks') | ||
return auth('alice', 'superSecret1', (err, result) => { | ||
t.is(result.username, 'alice') | ||
t.falsy(err) | ||
t.end() | ||
}) | ||
const args = _checkArgs({username: 'alice', password: 'superSecret1'}) | ||
t.truthy(args.username) | ||
t.truthy(args.password) | ||
}) | ||
|
||
test.cb('callback: login failed', t => { | ||
t.plan(1) | ||
const {auth} = require('./auth.callbacks') | ||
auth('eve', 'fooBar', (err, result) => t.truthy(err) || t.end()) | ||
test('fun. river: _loginUser', t => { | ||
t.plan(2) | ||
const result = t.notThrows(() => _loginUser({username: 'alice', password: 'superSecret1'})) | ||
t.truthy(result == undefined) | ||
}) | ||
|
||
test('fp/river: login successful', t => { | ||
test('fun. river: _checkUser', t => { | ||
t.plan(2) | ||
const result = t.throws(() => _checkUser(null)) | ||
t.truthy(/No User found/.test(result.message)) | ||
}) | ||
|
||
test('fun. river: login successful', t => { | ||
t.plan(2) | ||
const {auth} = require('./auth.fp') | ||
return auth({username: 'alice', password: 'superSecret1'}) | ||
.tap(result => { | ||
t.is(result.username, 'alice') | ||
t.pass() | ||
}) | ||
.then(result => { | ||
t.is(result.username, 'alice') | ||
t.pass() | ||
}) | ||
}) | ||
|
||
test('fp/river: login failed', t => { | ||
test('fun. river: login failed', t => { | ||
t.plan(1) | ||
const {auth} = require('./auth.fp') | ||
return auth({username: 'eve', password: 'fooBar'}) | ||
.then(result => t.fail()) | ||
.catch(err => t.pass()) | ||
.then(result => t.fail()) | ||
.catch(err => t.pass()) | ||
}) | ||
|
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 |
---|---|---|
@@ -1,15 +1,9 @@ | ||
const Promise = require('bluebird') | ||
const crypto = require('crypto') | ||
const crypto = require('crypto') | ||
|
||
module.exports = { | ||
hashString, | ||
hashStringAsync: Promise.promisify(hashString) | ||
} | ||
module.exports = {hashString} | ||
|
||
function hashString(s, callback = (x, y) => y || x) { | ||
try { | ||
return callback(null, crypto.createHash('sha256').update(s).digest('hex')) | ||
} catch (e) { | ||
return callback(e) | ||
} | ||
function hashString(s, callback) { | ||
s = crypto.createHash('sha256').update(s).digest('hex') | ||
if (callback) callback(null, s) | ||
return s | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.