decentralized-internet
Version:
An NPM library of programs to create decentralized web and distributed computing projects
1 lines • 3.61 kB
JavaScript
;var fs=require("fs"),path=require("path"),qs=require("querystring"),os=require("os"),request=require("request"),Promise=require("bluebird"),_=require("underscore"),prompt=require("prompt"),jws=require("jsonwebtoken");module.exports=class{constructor(){this.auth={},this.uri="",this.agentOptions={},this.setServerUri=this.setServer}getAuth(){return this.auth}setServer(e){_.isObject(e)?_.extend(this,e):this.uri=e}getserver(){return this.uri}getServer(){return this.uri}setAgentOptions(e){this.agentOptions=e}promptUsernamePassword(){return new Promise(function(e,t){prompt.get({properties:{email:{pattern:/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,message:"Email address",required:!0},password:{hidden:!0,required:!0}}},function(r,n){r?t(r):e(n)})})}promptServer(){return new Promise(function(e,t){prompt.get({properties:{uri:{message:"Please enter the server uri",required:!0}}},function(r,n){r?t(r):e(n)})})}start(){var e=this;return e.promptServer().then(function(t){return e.setServer(t),e.promptUsernamePassword()}).then(function(t){return e.userLogin(t)})}testUserToken(e){var t=jws.decode(e.token);return!(t.exp&&t.exp<Date.now()/1e3)&&(void 0===t.exp&&console.log("WARNING! The token does not have an expiry date. Tokens without expiry date were deprecated. The server could be running an old version. Please contact the server administrator."),!0)}setUserToken(e){if(_.isObject(e)){if(!e.token)throw console.error("self.setUserToken: ",JSON.stringify(e)),"Invalid token set for auth mechanism, must be an object {'token': 'someAuthToken'}";this.auth.bearer=e.token}else this.auth.bearer=e}getUserToken(){return{token:this.auth.bearer}}createUser(e){var t=this;return new Promise(function(r,n){var s={url:t.getServer()+"/auth/user",method:"POST",json:e,agentOptions:t.agentOptions};request(s,function(e,t,s){e?n(e):r(s)})})}resetPassword(e){var t=this;return new Promise(function(r,n){var s={url:t.getServer()+"/auth/reset",method:"POST",json:e,agentOptions:t.agentOptions};request(s,function(e,t,s){e?n(e):r(s)})})}userLogin(e){var t=this;return new Promise(function(r,n){var s={url:t.getServer()+"/auth/login",method:"POST",json:e,agentOptions:t.agentOptions};request(s,function(e,s,o){e?n(e):o&&o.token?(t.auth.bearer=o.token,r(o)):n(o)})})}getUser(){var e=this;return new Promise(function(t,r){var n={url:e.getServer()+"/auth/user",method:"GET",auth:e.auth,agentOptions:e.agentOptions};request(n,function(e,n,s){e?r(e):t(JSON.parse(s))})})}getUsers(){var e=this;return new Promise(function(t,r){var n={url:e.getServer()+"/auth/users",method:"GET",auth:e.auth,agentOptions:e.agentOptions};request(n,function(e,n,s){e?r(e):t(s)})})}updateUser(e){var t=this;return new Promise(function(r,n){var s=_.clone(e);s.scope&&delete s.scope;var o={url:t.getServer()+"/auth/user",method:"PUT",json:s,auth:t.auth,agentOptions:t.agentOptions};request(o,function(e,t,s){e?n(e):r(s)})})}updateUsers(e){var t=this;return new Promise(function(r,n){var s={url:t.getServer()+"/auth/users",method:"PUT",json:e,auth:t.auth,agentOptions:t.agentOptions};request(s,function(e,t,s){e?n(e):r(s)})})}deleteUser(){var e=this;return new Promise(function(t,r){var n={url:e.getServer()+"/auth/user",method:"DELETE",agentOptions:e.agentOptions,auth:e.auth};request(n,function(e,n,s){e?r(e):t(s)})})}deleteUsers(e){var t=this;return new Promise(function(r,n){var s={url:t.getServer()+"/auth/users",method:"DELETE",agentOptions:t.agentOptions,auth:t.auth,json:e};request(s,function(e,t,s){e?n(e):r(s)})})}};