Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eddie #6

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 0 additions & 63 deletions README.md

This file was deleted.

6 changes: 6 additions & 0 deletions lab-eddie/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

**/node_modules/*
**/vendor/*
**/*.min.js
**/coverage/*
**/build/*
21 changes: 21 additions & 0 deletions lab-eddie/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"rules": {
"no-console": "off",
"indent": [ "error", 2 ],
"quotes": [ "error", "single" ],
"semi": ["error", "always"],
"linebreak-style": [ "error", "unix" ]
},
"env": {
"es6": true,
"node": true,
"mocha": true,
"jasmine": true
},
"ecmaFeatures": {
"modules": true,
"experimentalObjectRestSpread": true,
"impliedStrict": true
},
"extends": "eslint:recommended"
}
129 changes: 129 additions & 0 deletions lab-eddie/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@

# Created by https://www.gitignore.io/api/osx,vim,node,windows

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env


### OSX ###
*.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### Vim ###
# swap
[._]*.s[a-v][a-z]
[._]*.sw[a-p]
[._]s[a-v][a-z]
[._]sw[a-p]
# session
Session.vim
# temporary
.netrwhist
*~
# auto-generated tag files
tags

### Windows ###
# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk

# End of https://www.gitignore.io/api/osx,vim,node,windows
data
.env
18 changes: 18 additions & 0 deletions lab-eddie/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Welcome, welcome.

This is my lab for day 16 of CF 401...
Yayyyyyyyyyyyyyyyyyyyyyy.

So this is short and simple. To the point.

Gotta get that server running. We can do that by typing npm run start in the lab-eddie dir.
Make sure the dependancies are installed first!!! npm i that s***.

Next, we open a browser or a terminal window and do some httpie magic. OR whatever medium you prefer

we do an
http POST :3000/api/newuser userName=='Ted Bundy' email=='[email protected]' password=='ted4life'

This will give us back an authentication token :D and a 200 code. It means it worked. Hooray!!!!


27 changes: 27 additions & 0 deletions lab-eddie/lib/basic-auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

const debug = require('debug')('app: basic-auth');
const createError = require('http-errors');

module.exports = function(req, res, next) {
debug('basic-auth');

let authHeader = req.headers.authorization;
if(!authHeader) return next(createError(401, 'Authorization Header'));

let base64auth = authHeader.split('Basic ')[1];
if(!base64auth) return next(createError(401, 'Must Enter Username and Password'));

let utf8auth = new Buffer(base64auth, 'base64').toString();
let loginfo = utf8auth.split(':');
console.log(authHeader)
req.auth = {
userName: loginfo[0],
passWord: loginfo[1]
};

if(!req.auth.userName) return next(createError(401, 'Username Required'));
if(!req.auth.passWord) return next(createError(401, 'Password Required'));

next();
};
28 changes: 28 additions & 0 deletions lab-eddie/lib/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const createError = require('http-errors');
const debug = require('debug')('app:error');

module.exports = function(err, req, res, next) {
console.error(err.message);
if (err.status) {
debug('user error');

res.status(err.status).send(err.name);
next();
return;
}

if (err.name === 'ValidationError') {
err = createError(400, err.message);
res.status(err.status).send(err.name);
next();
return;
}

debug('server error');
err = createError(500, err.message);
res.status(err.status).send(err.name);
next();
};

76 changes: 76 additions & 0 deletions lab-eddie/model/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'use strict';

const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const creatError = require('http-errors');
const debug = require('debug')('app: user');
const crypto = require('crypto');
const Promise = require('bluebird');
const jwt = require('jsonwebtoken');

const Schema = mongoose.Schema;

const User = Schema({
userName: {type: String, required: true, unique: true},
email: {type: String, required: true, unique: true},
passWord: {type: String, required: true},
findHash: {type: String, unique: true}
});

User.methods.generatePasswordHash = function(password) {
debug('generatePasswordHash');

return new Promise((resolve, reject) => {
bcrypt.hash(password, 5, (err, hash) => {
if(err) return reject(err);
this.passWord = hash;
resolve(this);
})
})
}

User.methods.confirmPass = function(password) {
debug('comparePass');


return new Promise((resolve, reject) => {
bcrypt.compare(password, this.passWord, (err, valid) => {
if(err) return reject(err);
if(!valid) return reject(creatError(401, 'Invalid Password!'));
resolve(this);
})
});
}

User.methods.generateHash = function() {
debug('generateHash');

return new Promise((resolve, reject) => {
let tries = 0;

_generateHash.call(this);

function _generateHash() {
this.findHash = crypto.randomBytes(32).toString('hex');
this.save()
.then(() => resolve(this.findHash))
.catch(err => {
if(tries >2) return reject(err);
tries++;
_generateHash.call(this);
});
};
});
};

User.methods.tokenGen = function() {
debug('tokenGen');

return new Promise((resolve, reject) => {
this.generateHash()
.then(hash => resolve(jwt.sign({token: hash}, process.env.APP_SECRET)))
.catch(err => reject(err));
});
};

module.exports = mongoose.model('user', User);
Loading