UNPKG

decentralized-internet

Version:

An NPM library of programs to create decentralized web and distributed computing projects

1 lines 6.41 kB
module.exports=function(e,n){var t=require("bluebird"),r=require("jsonwebtoken"),o=require("bcrypt");n.saltRounds;var i=require("@hapi/boom"),a=require("underscore"),u=require("nodemailer"),c=require("couch-provider").couchProvider;c.setConfiguration(n.userdb);var s,d=require("couch-update-views"),l=require("path");(s="nodemailer-stub-transport"===n.mailer.nodemailer?u.createTransport(require(n.mailer.nodemailer)()):u.createTransport(n.mailer.nodemailer)).verify(function(e,n){e&&console.log(e)}),d.migrateUp(c.getCouchDBServer(),l.join(__dirname,"views"),!0).catch(function(e){return d.migrateUp(c.getCouchDBServer(),l.join(__dirname,"views"))}).then(function(){return c.getView("_design/user/_view/scopes")}).then(function(e){if(e&&0===e.length)return console.log("Generating default scopes..."),c.uploadDocuments({type:"scopes",scopes:["default","admin"]}).then(function(){console.log("Done!")})}).catch(function(e){console.error(e)});var h={validationFunctions:[]};e.method({name:"jwtauth.addValidationFunction",method:function(e){h.validationFunctions.push(e)},options:{}});const f=async function(e,n){return h.validateUser(e,n).then(function(e){return e}).catch(function(r){return t.any(a.map(h.validationFunctions,function(t){return t(e,n)})).then(function(e){return e}).catch(t.AggregateError,function(e){return t.reject(i.unauthorized(e))})})};n.algorithms?(console.log("Please modify your configuration. Instead of field 'algorithms' change to 'verifyOptions'"),e.auth.strategy("token","jwt",{key:n.privateKey,validateFunc:f,verifyOptions:n.algorithms})):e.auth.strategy("token","jwt",{key:n.privateKey,validateFunc:f,verifyOptions:n.verifyOptions});e.method({name:"jwtauth.sign",method:function(e,t){var o,i={};return o=t||n.algorithm,i.token=r.sign(e,n.privateKey,o),i},options:{}});e.method({name:"jwtauth.verify",method:function(e){try{var t=r.verify(e,n.privateKey);return m(t)}catch(e){throw i.unauthorized(e)}},options:{}}),e.method({name:"jwt.verify",method:function(e){return r.verify(e,n.privateKey)},options:{}});const m=function(e){return c.getView("_design/user/_view/info?key="+JSON.stringify(e.email)).then(function(e){if((e=a.pluck(e,"value")).length>1)throw i.unauthorized("More than 1 user with same email found in DB!");if(0===e.length)throw i.unauthorized("User not found in db");return e[0]})};h.validateUser=function(e,n){return m(n)};const p=function(e){return new t(function(t,r){o.hash(e,n.saltRounds,function(e,n){e?r(e):t(n)})})};return h.createUser=function(n,t){var r=n.payload.email,o=n.payload.password;return c.getView("_design/user/_view/info?key="+JSON.stringify(r)).then(function(e){if(0===(e=a.pluck(e,"value")).length)return p(o);throw i.conflict("User already exists")}).then(function(t){var r=n.payload;return r.password=t,r.type="user",r.scope=["default"],c.uploadDocuments(r).then(function(n){if((n=n[0]).ok)return e.methods.jwtauth.sign({email:r.email});throw i.badData(n)})}).catch(function(e){return e})},h.getUser=function(e,n){return e.auth.credentials},h.getUsers=function(e,n){return c.getView("_design/user/_view/info").then(function(e){return a.pluck(e,"value")}).catch(function(e){return i.badImplementation(e)})},h.updateUser=function(e,n){var t=e.auth.credentials,r=e.payload;return t.email!==r.email&&-1===t.scope.indexOf("admin")?i.unauthorized("You cannot modify the user information"):c.getDocument(r._id).then(function(e){return r.password=e.password,void 0===r.scope&&(r.scope=e.scope),c.uploadDocuments(r).then(function(e){if((e=e[0]).ok)return e;throw e})}).catch(function(e){return i.badImplementation(e)})},h.updateUserBasic=function(e,n){var t=e.auth.credentials,r=e.payload;return t.email!==r.email||t._id!==r._id?i.unauthorized("You cannot modify the user information"):c.getDocument(r._id).then(function(e){return r.password=e.password,r.scope=e.scope,c.uploadDocuments(r).then(function(e){if((e=e[0]).ok)return e;throw e})}).catch(function(e){return i.badImplementation(e)})},h.login=function(n,r){var u=n.payload.email,s=n.payload.password;return c.getView("_design/user/_view/hash?key="+JSON.stringify(u)).then(function(e){return a.pluck(e,"value")[0]}).then(function(n){return new t(function(t,r){o.compare(s,n,function(n,o){o?t(e.methods.jwtauth.sign({email:u})):r(n)})})}).catch(function(e){return i.unauthorized(e)})},h.loginUpdate=function(n,t){var r=n.auth.credentials,o=(r.email,n.payload.password);return c.getDocument(r._id).then(function(n){return p(o).then(function(t){return n.password=t,c.uploadDocuments(n).then(function(t){return(t=t[0]).ok?e.methods.jwtauth.sign({email:n.email}):i.badImplementation(t)})})}).catch(function(e){return i.unauthorized(e)})},h.deleteUser=function(e,n){var t=e.auth.credentials,r=e.auth.credentials;return e.payload&&(r=e.payload),r.email!==t.email&&-1===t.scope.indexOf("admin")?i.unauthorized("You cannot delete the user"):c.deleteDocument(r).then(n).catch(function(e){return i.conflict(e)})},h.resetPassword=function(r,o){var u=r.payload.email;return c.getView("_design/user/_view/info?key="+JSON.stringify(u)).then(function(r){if(0===(r=a.pluck(r,"value")).length)throw i.unauthorized("I don't know who you are, you need to create an account first!");r=r[0];var o=a.clone(n.algorithm);o.expiresIn="30m";var c=e.methods.jwtauth.sign({email:r.email},o),d="Hello @USERNAME@,<br>Somebody asked me to send you a link to reset your password, hopefully it was you.<br>Follow this <a href='@SERVER@/public/#/login/reset?token=@TOKEN@'>link</a> to reset your password.<br>The link will expire in 30 minutes.<br>Bye.";n.mailer.message&&(d=n.mailer.message);var l=e.info.uri;n.mailer.uri&&(l=n.mailer.uri),d=(d=(d=d.replace("@USERNAME@",r.name)).replace("@SERVER@",l)).replace("@TOKEN@",c.token);var h={from:n.mailer.from,to:u,subject:"Password reset",html:d};return new t(function(e,n){s.sendMail(h,function(t,r){t?n(i.badImplementation(t)):e("An email has been sent to recover your password.")})})}).catch(function(e){return console.log(e),i.wrap(e)})},h.getScopes=function(e,n){return c.getView("_design/user/_view/scopes?include_docs=true").then(function(e){return a.pluck(e,"doc")}).catch(function(e){return i.badImplementation(e)})},h.updateScopes=function(e,n){var t=e.auth.credentials;e.payload;return-1===t.scope.indexOf("admin")?i.unauthorized("You cannot modify the scopes"):c.uploadDocuments(e.payload).then(function(e){return e[0]}).catch(function(e){return i.wrap(e)})},h};