diff options
| author | Adam Soltys <[email protected]> | 2018-01-29 19:39:52 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2018-01-29 19:58:22 -0800 |
| commit | 49ec609f5b4dc0c5577c398ff229d710db902d4d (patch) | |
| tree | d6fa26daa1420b801a307ee767a68e2fdcb1edcd | |
| parent | 0feb98d1b31acca2a24e9c253a06e45cf4ee3e63 (diff) | |
| download | coinos-server-49ec609f5b4dc0c5577c398ff229d710db902d4d.tar.gz coinos-server-49ec609f5b4dc0c5577c398ff229d710db902d4d.zip | |
jwt auth
| -rw-r--r-- | app.js | 39 | ||||
| -rw-r--r-- | db.js | 3 | ||||
| -rw-r--r-- | package.json | 2 | ||||
| -rw-r--r-- | passport.js | 12 | ||||
| -rw-r--r-- | users.js | 46 | ||||
| -rw-r--r-- | yarn.lock | 68 |
6 files changed, 118 insertions, 52 deletions
@@ -9,6 +9,8 @@ import express from 'express' import fs from 'fs' import grpc from 'grpc' import passport from './passport' +import jwt from 'jsonwebtoken' +import bcrypt from 'bcrypt' import { graphqlExpress, graphiqlExpress } from 'apollo-server-express' dotenv.config() @@ -47,6 +49,43 @@ const l = console.log res.json(app.get('rates')) }) + app.post('/login', (req, res) => { + console.log('logging in') + db.User.findOne({ + where: { + username: req.body.username + } + }).then((user) => { + bcrypt.compare(req.body.password, user.password).then((result) => { + console.log('password was good') + if (result) { + let payload = { username: user.username } + let token = jwt.sign(payload, process.env.SECRET) + res.cookie('token', token, { expires: new Date(Date.now() + 9999999) }) + res.json({ token: token }) + } else { + res.status(401).end() + } + }).catch((err) => { + console.log(err) + res.status(401).end() + }) + }).catch((err) => { + console.log(err) + res.status(401).end() + }) + }) + + app.get('/secret', passport.authenticate('jwt', { session: false }), (req, res) => { + res.json({message: 'Success! You can not see this without a token'}) + }) + + app.get('/api/users/me', + passport.authenticate('basic', { session: false }), + function(req, res) { + res.json({ id: req.user.id, username: req.user.username }); + }); + app.use('/graphql', bodyParser.json(), graphqlExpress({ schema: db.gqlschema, context: {}, @@ -32,7 +32,8 @@ const db = new Sequelize( host: config.development.host, dialect: config.development.dialect, logging: false, - dialectOptions: { multipleStatements: true } + dialectOptions: { multipleStatements: true }, + operatorsAliases: false, }, ) const gqlfields = {} diff --git a/package.json b/package.json index 32fc4d5b..8ffeccd0 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "async": "2.1.1", "babel-cli": "^6.26.0", "babel-preset-env": "^1.6.1", + "bcrypt": "^1.0.3", "bip39": "^2.2.0", "bitcoinaverage": "^0.1.4", "bitcoinjs-lib": "^2.3.0", @@ -34,6 +35,7 @@ "graphql-tools": "^2.18.0", "hogan-express": "0.5.2", "http-proxy-middleware": "^0.17.2", + "jsonwebtoken": "^8.1.1", "less": "2.7.1", "lnrpc": "^0.0.3", "lodash": "^4.17.4", diff --git a/passport.js b/passport.js index d0fe0599..c85c9a52 100644 --- a/passport.js +++ b/passport.js @@ -1,17 +1,14 @@ const passport = require('passport') -const db = require('./redis') const jwt = require('passport-jwt') const dotenv = require('dotenv') dotenv.config() - require('dotenv').config() const cookieExtractor = function(req) { let token = null if (req && req.cookies) { token = req.cookies['token'] - console.log('set') } return token } @@ -19,8 +16,13 @@ const cookieExtractor = function(req) { passport.use(new jwt.Strategy({ jwtFromRequest: jwt.ExtractJwt.fromExtractors([cookieExtractor]), secretOrKey: process.env.SECRET - }, function (payload, next) { - db.hgetallAsync('user:' + payload.username).then((user) => { + }, async function (payload, next) { + const db = await require('./db') + db.User.findOne({ + where: { + username: payload.username + } + }).then((user) => { next(null, user) }).catch((err) => { next(null, false) diff --git a/users.js b/users.js deleted file mode 100644 index 3a18d38e..00000000 --- a/users.js +++ /dev/null @@ -1,46 +0,0 @@ -import { resolver, defaultArgs, defaultListArgs, attributeFields } from 'graphql-sequelize' -import { - graphql, - GraphQLSchema, - GraphQLObjectType, - GraphQLInputObjectType, - GraphQLString, - GraphQLInt, - GraphQLNonNull, - GraphQLList -} from 'graphql' - -module.exports = (db, fields, types, options) => { - options.after = (results, args) => { - return results.sort((a, b) => { - if (a.name === b.name) return 0 - return a.name > b.name ? 1 : -1 - }) - } - - db['User'].Membership = db['User'].hasOne(db['Membership']) - - db['RoleMap'].Users = db['RoleMap'].belongsToMany(db['User'], { - as: 'users', - through: db['UserRoles'], - foreignKey: 'roleId', - otherKey: 'userId' - }) - - db['User'].Roles = db['User'].belongsToMany(db['RoleMap'], { - as: 'roles', - through: db['UserRoles'], - foreignKey: 'userId', - otherKey: 'roleId' - }) - - fields.programs = { - type: new GraphQLList(types['rolemap']), - resolve: resolver(db['User'].Roles) - } - - fields.membership = { - type: types['my_aspnet_membership'], - resolve: resolver(db['User'].Membership) - } -} @@ -1003,6 +1003,13 @@ bcrypt-pbkdf@^1.0.0: dependencies: tweetnacl "^0.14.3" +bcrypt@^1.0.3: + version "1.0.3" + resolved "https://registry.yarnpkg.com/bcrypt/-/bcrypt-1.0.3.tgz#b02ddc6c0b52ea16b8d3cf375d5a32e780dab548" + dependencies: + nan "2.6.2" + node-pre-gyp "0.6.36" + better-assert@~1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/better-assert/-/better-assert-1.0.2.tgz#40866b9e1b9e0b55b481894311e68faffaebc522" @@ -3498,6 +3505,21 @@ jsonwebtoken@^7.0.0: ms "^0.7.1" xtend "^4.0.1" +jsonwebtoken@^8.1.1: + version "8.1.1" + resolved "https://registry.yarnpkg.com/jsonwebtoken/-/jsonwebtoken-8.1.1.tgz#b04d8bb2ad847bc93238c3c92170ffdbdd1cb2ea" + dependencies: + jws "^3.1.4" + lodash.includes "^4.3.0" + lodash.isboolean "^3.0.3" + lodash.isinteger "^4.0.4" + lodash.isnumber "^3.0.3" + lodash.isplainobject "^4.0.6" + lodash.isstring "^4.0.1" + lodash.once "^4.0.0" + ms "^2.1.1" + xtend "^4.0.1" + jsprim@^1.2.2: version "1.4.0" resolved "https://registry.yarnpkg.com/jsprim/-/jsprim-1.4.0.tgz#a3b87e40298d8c380552d8cc7628a0bb95a22918" @@ -3648,6 +3670,14 @@ lodash.foreach@^4.5.0: version "4.5.0" resolved "https://registry.yarnpkg.com/lodash.foreach/-/lodash.foreach-4.5.0.tgz#1a6a35eace401280c7f06dddec35165ab27e3e53" +lodash.includes@^4.3.0: + version "4.3.0" + resolved "https://registry.yarnpkg.com/lodash.includes/-/lodash.includes-4.3.0.tgz#60bb98a87cb923c68ca1e51325483314849f553f" + +lodash.isboolean@^3.0.3: + version "3.0.3" + resolved "https://registry.yarnpkg.com/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz#6c2e171db2a257cd96802fd43b01b20d5f5870f6" + lodash.isempty@^4.4.0: version "4.4.0" resolved "https://registry.yarnpkg.com/lodash.isempty/-/lodash.isempty-4.4.0.tgz#6f86cbedd8be4ec987be9aaf33c9684db1b31e7e" @@ -3656,6 +3686,22 @@ lodash.isfinite@^3.3.2: version "3.3.2" resolved "https://registry.yarnpkg.com/lodash.isfinite/-/lodash.isfinite-3.3.2.tgz#fb89b65a9a80281833f0b7478b3a5104f898ebb3" +lodash.isinteger@^4.0.4: + version "4.0.4" + resolved "https://registry.yarnpkg.com/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz#619c0af3d03f8b04c31f5882840b77b11cd68343" + +lodash.isnumber@^3.0.3: + version "3.0.3" + resolved "https://registry.yarnpkg.com/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz#3ce76810c5928d03352301ac287317f11c0b1ffc" + +lodash.isplainobject@^4.0.6: + version "4.0.6" + resolved "https://registry.yarnpkg.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz#7c526a52d89b45c45cc690b88163be0497f550cb" + +lodash.isstring@^4.0.1: + version "4.0.1" + resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451" + lodash.keys@^4.2.0: version "4.2.0" resolved "https://registry.yarnpkg.com/lodash.keys/-/lodash.keys-4.2.0.tgz#a08602ac12e4fb83f91fc1fb7a360a4d9ba35205" @@ -3952,6 +3998,10 @@ [email protected]: version "2.0.0" resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8" +ms@^2.1.1: + version "2.1.1" + resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.1.tgz#30a5864eb3ebb0a66f2ebe6d727af06a09d86e0a" + version "0.0.7" resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.7.tgz#3075ce93bc21b8fab43e1bc4da7e8115ed1e7bab" @@ -3988,6 +4038,10 @@ [email protected]: dependencies: lru-cache "2.5.0" + version "2.6.2" + resolved "https://registry.yarnpkg.com/nan/-/nan-2.6.2.tgz#e4ff34e6c95fdfb5aecc08de6596f43605a7db45" + nan@^2.3.0: version "2.4.0" resolved "https://registry.yarnpkg.com/nan/-/nan-2.4.0.tgz#fb3c59d45fe4effe215f0b890f8adf6eb32d2232" @@ -4036,6 +4090,20 @@ node-http-proxy@^0.2.3: pm "2.2.5" request "2.51.0" + version "0.6.36" + resolved "https://registry.yarnpkg.com/node-pre-gyp/-/node-pre-gyp-0.6.36.tgz#db604112cb74e0d477554e9b505b17abddfab786" + dependencies: + mkdirp "^0.5.1" + nopt "^4.0.1" + npmlog "^4.0.2" + rc "^1.1.7" + request "^2.81.0" + rimraf "^2.6.1" + semver "^5.3.0" + tar "^2.2.1" + tar-pack "^3.4.0" + node-pre-gyp@^0.6.29: version "0.6.34" resolved "https://registry.yarnpkg.com/node-pre-gyp/-/node-pre-gyp-0.6.34.tgz#94ad1c798a11d7fc67381b50d47f8cc18d9799f7" |
