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

lab-shiv #4

Open
wants to merge 2 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
22 changes: 22 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"rules": {
"no-console": "off",
"indent": [ "error", 2 ],
"quotes": [ "error", "single" ],
"semi": ["error", "always"],
"linebreak-style": [ "error", "unix" ],
"no-unused-vars": [2, {"vars": "local", "args": "after-used"}]
},
"env": {
"es6": true,
"node": true,
"mocha": true,
"jasmine": true
},
"ecmaFeatures": {
"modules": true,
"experimentalObjectRestSpread": true,
"impliedStrict": true
},
"extends": "eslint:recommended"
}
116 changes: 116 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Created by https://www.gitignore.io/api/osx,windows,node

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

###
.env

# 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

### 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,windows,node
34 changes: 34 additions & 0 deletions lib/basic-auth-middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

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

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

var authHeader = req.headers.authorization;
if (!authHeader) {
return next(createError(401, 'authorization header required'));
}

var base64str = authHeader.split('Basic ')[1];
if (!base64str) {
return next(createError(401, 'username and password required'));
}

var utf8string = new Buffer(base64str, 'base64').toString();
var authArray = utf8string.split(':');

req.auth = {
username: authArray[0],
password: authArray[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 lib/bearer-auth-middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const jwt = require('jsonwebtoken');
const createError = require('http-errors');
const debug = require('debug')('quiver:bearer-auth-middleware');

const User = require('../model/user.js');

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

var authHeader = req.headers.authorization;
if(!authHeader) return next(createError(401, 'authorization headers required'));

var token = authHeader.split('Bearer ')[1];
if(!token) return next(createError(401, 'token required'));

jwt.verify(token, process.env.APP_SECRET, (err, decoded) => {
if(err) return next(err);

User.findOne({ findHash: decoded.token })
.then( user => {
req.user = user;
next();
})
.catch(err => next(err));
});
};
29 changes: 29 additions & 0 deletions lib/error-middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

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


module.exports = function(err, req, res, next) {
debug('error middleware');

console.error('msg:', err.message);
console.error('name:', err.name);

if(err.status) {
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;
}

err = createError(500, err.message);
res.status(err.status).send(err.name);
next();
};
16 changes: 16 additions & 0 deletions model/pic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const picSchema = Schema({
name: { type: String, required: true },
description: { type: String, required: true },
userID: { type: Schema.Types.ObjectId, required: true },
venueID: { type: Schema.Types.ObjectId, required: true },
imageURI: { type: String, required: true, unique: true },
objectKey: { type: String, required: true, unique: true },
created: { type: Date, default: Date.now }
});

module.exports = mongoose.model('pic', picSchema);
77 changes: 77 additions & 0 deletions model/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'use strict';

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const createError = require('http-errors');
const Promise = require('bluebird');
const debug = require('debug')('quiver:user');

const userSchema = Schema({
username: { type: String, required: true, unique: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
isArtist: { type: Boolean, required: true },
findHash: { type: String, unique: true }
});

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

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

userSchema.methods.comparePasswordHash = function(password) {
debug('comparePasswordHash');

return new Promise((resolve, reject) => {
bcrypt.compare(password, this.password, (err, valid) => {
if(err) return reject(createError(400, 'bad request'));
if(valid === false) {
return reject(createError(401, 'wrong password'));
}
resolve(this);
});
});
};

userSchema.methods.generateFindHash = function() {
debug('generateFindHash');

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

_generateFindHash.call(this);

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

userSchema.methods.generateToken = function () {
debug('generateToken');

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

module.exports = mongoose.model('user', userSchema);
14 changes: 14 additions & 0 deletions model/venue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const venueSchema = Schema({
name: { type: String, required: true, unique: true },
address: { type: String, required: true, unique: true },
profPic: { type: Schema.Types.ObjectId, ref: 'pic' },
pics: [{ type: Schema.Types.ObjectId, ref: 'pic' }],
userID: { type: Schema.Types.ObjectId, required: true, unique: true },
})

module.exports = mongoose.model('venue', venueSchema);
48 changes: 48 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "18-aws_s3",
"version": "1.0.0",
"description": "![cf](https://i.imgur.com/7v5ASc8.png) Lab 18 - AWS S3 ======",
"main": "server.js",
"directories": {
"test": "test"
},
"scripts": {
"start": "node server.js",
"test": "DEBUG='AWS-S3*' mocha"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are telling debug to look at 'AWS-S3*'. But all of your files are using 'quiver' for debug.

},
"repository": {
"type": "git",
"url": "git+https://github.com/shivprogrammer/18-aws_s3.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/shivprogrammer/18-aws_s3/issues"
},
"homepage": "https://github.com/shivprogrammer/18-aws_s3#readme",
"dependencies": {
"aws-sdk": "^2.31.0",
"bcrypt": "^1.0.2",
"bluebird": "^3.5.0",
"body-parser": "^1.17.1",
"cors": "^2.8.1",
"debug": "^2.6.3",
"del": "^2.2.2",
"dotenv": "^4.0.0",
"express": "^4.15.2",
"http-errors": "^1.6.1",
"jsonwebtoken": "^7.3.0",
"mongoose": "^4.9.1",
"morgan": "^1.8.1",
"multer": "^1.3.0"
},
"devDependencies": {
"aws-sdk-mock": "^1.6.1",
"chai": "^3.5.0",
"coveralls": "^2.12.0",
"istanbul": "^0.4.5",
"mocha": "^3.2.0",
"superagent": "^3.5.2"
}
}
Loading