deploy.sh 0.2.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +1 -0
- package/.eslintrc +7 -9
- package/.github/workflows/ci.yml +48 -0
- package/.husky/pre-commit +5 -0
- package/{test/fixtures/unknown/.gitkeep → .prettierrc} +0 -0
- package/.release-it.json +5 -0
- package/CHANGELOG.md +26 -0
- package/README.md +5 -6
- package/__tests__/fixtures/unknown/.gitkeep +0 -0
- package/__tests__/lib/classifier.test.js +49 -0
- package/__tests__/lib/helpers/util.test.js +57 -0
- package/bin/deploy-delete.js +14 -0
- package/bin/deploy-deploy.js +34 -59
- package/bin/deploy-list.js +25 -52
- package/bin/deploy-login.js +36 -52
- package/bin/deploy-logout.js +9 -40
- package/bin/deploy-logs.js +22 -48
- package/bin/deploy-open.js +14 -37
- package/bin/deploy-register.js +37 -50
- package/bin/deploy-server.js +8 -5
- package/bin/deploy-whoami.js +8 -37
- package/bin/deploy.js +145 -19
- package/{test/fixtures → examples}/docker/Dockerfile +0 -0
- package/examples/docker/index.js +12 -0
- package/examples/node/index.js +8 -0
- package/{test/fixtures → examples}/node/package.json +0 -0
- package/examples/static/index.html +22 -0
- package/{test/fixtures → examples}/static/main.css +4 -2
- package/examples/static/out.gifcd +0 -0
- package/generate-docs.js +55 -0
- package/index.js +54 -55
- package/jsdoc.json +6 -3
- package/lib/classifier.js +24 -17
- package/lib/deploy.js +56 -64
- package/lib/helpers/cli.js +178 -207
- package/lib/helpers/util.js +94 -21
- package/lib/models/deployment.js +415 -474
- package/lib/models/request.js +77 -66
- package/lib/models/user.js +119 -144
- package/lib/server.js +187 -97
- package/lib/static/not-found.html +30 -0
- package/lib/static/page-could-not-load.html +30 -0
- package/lib/static/static-server.js +70 -0
- package/package.json +47 -34
- package/website/README.md +41 -0
- package/website/babel.config.js +3 -0
- package/website/docs/api/_category_.yml +1 -0
- package/website/docs/api/lib/classifier.js.md +11 -0
- package/website/docs/api/lib/deploy.js.md +13 -0
- package/website/docs/api/lib/helpers/cli.js.md +193 -0
- package/website/docs/api/lib/helpers/util.js.md +65 -0
- package/website/docs/api/lib/models/deployment.js.md +171 -0
- package/website/docs/api/lib/models/request.js.md +67 -0
- package/website/docs/api/lib/models/user.js.md +92 -0
- package/website/docs/api/lib/server.js.md +0 -0
- package/website/docs/api/lib/static/static-server.js.md +0 -0
- package/website/docs/intro.md +57 -0
- package/website/docusaurus.config.js +82 -0
- package/website/package-lock.json +25218 -0
- package/website/package.json +39 -0
- package/website/sidebars.js +31 -0
- package/website/src/components/HomepageFeatures/index.js +79 -0
- package/website/src/components/HomepageFeatures/styles.module.css +11 -0
- package/website/src/css/custom.css +39 -0
- package/website/src/pages/index.js +57 -0
- package/website/src/pages/index.module.css +23 -0
- package/website/static/.nojekyll +0 -0
- package/website/static/example.gif +0 -0
- package/website/static/example.mov +0 -0
- package/website/static/img/favicon.ico +0 -0
- package/website/static/img/intro/deploy.png +0 -0
- package/website/static/img/intro/logs.png +0 -0
- package/{docs → website/static/img}/logo.png +0 -0
- package/{docs → website/static/img}/logo.pxm +0 -0
- package/{docs → website/static/img}/logo@2x.png +0 -0
- package/.npmignore +0 -9
- package/.travis.yml +0 -9
- package/TODO.md +0 -12
- package/docs/code/classifier.js.html +0 -116
- package/docs/code/deploy.js.html +0 -138
- package/docs/code/fonts/OpenSans-Bold-webfont.eot +0 -0
- package/docs/code/fonts/OpenSans-Bold-webfont.svg +0 -1830
- package/docs/code/fonts/OpenSans-Bold-webfont.woff +0 -0
- package/docs/code/fonts/OpenSans-BoldItalic-webfont.eot +0 -0
- package/docs/code/fonts/OpenSans-BoldItalic-webfont.svg +0 -1830
- package/docs/code/fonts/OpenSans-BoldItalic-webfont.woff +0 -0
- package/docs/code/fonts/OpenSans-Italic-webfont.eot +0 -0
- package/docs/code/fonts/OpenSans-Italic-webfont.svg +0 -1830
- package/docs/code/fonts/OpenSans-Italic-webfont.woff +0 -0
- package/docs/code/fonts/OpenSans-Light-webfont.eot +0 -0
- package/docs/code/fonts/OpenSans-Light-webfont.svg +0 -1831
- package/docs/code/fonts/OpenSans-Light-webfont.woff +0 -0
- package/docs/code/fonts/OpenSans-LightItalic-webfont.eot +0 -0
- package/docs/code/fonts/OpenSans-LightItalic-webfont.svg +0 -1835
- package/docs/code/fonts/OpenSans-LightItalic-webfont.woff +0 -0
- package/docs/code/fonts/OpenSans-Regular-webfont.eot +0 -0
- package/docs/code/fonts/OpenSans-Regular-webfont.svg +0 -1831
- package/docs/code/fonts/OpenSans-Regular-webfont.woff +0 -0
- package/docs/code/fonts/OpenSans-Semibold-webfont.eot +0 -0
- package/docs/code/fonts/OpenSans-Semibold-webfont.svg +0 -1830
- package/docs/code/fonts/OpenSans-Semibold-webfont.ttf +0 -0
- package/docs/code/fonts/OpenSans-Semibold-webfont.woff +0 -0
- package/docs/code/fonts/OpenSans-SemiboldItalic-webfont.eot +0 -0
- package/docs/code/fonts/OpenSans-SemiboldItalic-webfont.svg +0 -1830
- package/docs/code/fonts/OpenSans-SemiboldItalic-webfont.ttf +0 -0
- package/docs/code/fonts/OpenSans-SemiboldItalic-webfont.woff +0 -0
- package/docs/code/global.html +0 -325
- package/docs/code/helpers_cli.js.html +0 -351
- package/docs/code/helpers_util.js.html +0 -127
- package/docs/code/index.html +0 -81
- package/docs/code/models_deployment.js.html +0 -593
- package/docs/code/models_request.js.html +0 -150
- package/docs/code/models_user.js.html +0 -232
- package/docs/code/module-lib_classifier.html +0 -246
- package/docs/code/module-lib_deploy.html +0 -350
- package/docs/code/module-lib_helpers_cli.html +0 -2067
- package/docs/code/module-lib_helpers_util.html +0 -608
- package/docs/code/module-models_deployment-Deployment.html +0 -359
- package/docs/code/module-models_deployment-DeploymentSchema.html +0 -359
- package/docs/code/module-models_deployment.Deployment.html +0 -359
- package/docs/code/module-models_deployment.html +0 -1851
- package/docs/code/module-models_request-Request.html +0 -409
- package/docs/code/module-models_request-RequestSchema.html +0 -359
- package/docs/code/module-models_request.html +0 -446
- package/docs/code/module-models_user-User.html +0 -306
- package/docs/code/module-models_user-UserSchema.html +0 -306
- package/docs/code/module-models_user.User.html +0 -306
- package/docs/code/module-models_user.html +0 -1200
- package/docs/code/scripts/linenumber.js +0 -25
- package/docs/code/scripts/prettify/Apache-License-2.0.txt +0 -202
- package/docs/code/scripts/prettify/lang-css.js +0 -2
- package/docs/code/scripts/prettify/prettify.js +0 -28
- package/docs/code/styles/jsdoc-default.css +0 -692
- package/docs/code/styles/prettify-jsdoc.css +0 -111
- package/docs/code/styles/prettify-tomorrow.css +0 -132
- package/docs/example-docker.gif +0 -0
- package/docs/example-node.gif +0 -0
- package/docs/example-static.gif +0 -0
- package/docs/index.html +0 -50
- package/docs/main.css +0 -12
- package/docs/psychic-min.css +0 -1
- package/lib/helpers/static-server.js +0 -69
- package/test/fixtures/docker/index.js +0 -12
- package/test/fixtures/node/index.js +0 -8
- package/test/fixtures/static/index.html +0 -14
- package/test/lib/classifier.js +0 -51
- package/test/lib/helpers/util.js +0 -30
package/lib/models/request.js
CHANGED
|
@@ -1,31 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
* @module models/request
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
const mongoose = require('mongoose');
|
|
1
|
+
import mongoose from "mongoose";
|
|
6
2
|
|
|
7
3
|
const Schema = mongoose.Schema;
|
|
8
4
|
|
|
9
|
-
const RequestSchema = new Schema(
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
5
|
+
const RequestSchema = new Schema(
|
|
6
|
+
{
|
|
7
|
+
url: String,
|
|
8
|
+
time: Number,
|
|
9
|
+
subdomain: String,
|
|
10
|
+
statusCode: String,
|
|
11
|
+
userAgent: String,
|
|
12
|
+
referer: String,
|
|
13
|
+
method: String,
|
|
14
|
+
acceptLanguage: String,
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
minimize: false,
|
|
18
|
+
timestamps: {
|
|
19
|
+
createdAt: "created_at",
|
|
20
|
+
updatedAt: "updated_at",
|
|
21
|
+
},
|
|
23
22
|
}
|
|
24
|
-
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
const RequestModel = mongoose.model("Request", RequestSchema);
|
|
25
26
|
|
|
26
27
|
/**
|
|
27
28
|
* Request definition
|
|
28
|
-
* @class
|
|
29
|
+
* @class Request
|
|
29
30
|
* @property {String} subdomain - the subdomain of the request
|
|
30
31
|
* @property {String} url - the url that was being accessed
|
|
31
32
|
* @property {Number} time - the time it took to access the resource
|
|
@@ -35,56 +36,66 @@ const RequestSchema = new Schema({
|
|
|
35
36
|
* @property {String} referer - the referer that the user was at before accessing the address
|
|
36
37
|
* @property {String} acceptLanguage - browser based language preferences
|
|
37
38
|
*/
|
|
38
|
-
|
|
39
|
+
class Request {
|
|
40
|
+
/**
|
|
41
|
+
* express middleware that logs requests
|
|
42
|
+
* @method log
|
|
43
|
+
* @memberof Request
|
|
44
|
+
* @param {Object} req - express request object
|
|
45
|
+
* @param {Object} res - express response object
|
|
46
|
+
* @param {Function} next - callback function
|
|
47
|
+
*/
|
|
48
|
+
static log(req, res, next) {
|
|
49
|
+
const { url, headers, method } = req;
|
|
50
|
+
const { host } = headers;
|
|
39
51
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
* @method log
|
|
43
|
-
* @param {Object} req - express request object
|
|
44
|
-
* @param {Object} res - express response object
|
|
45
|
-
* @param {Function} next - callback function
|
|
46
|
-
*/
|
|
47
|
-
module.exports.log = function log(req, res, next) {
|
|
48
|
-
const { url, headers, method } = req;
|
|
49
|
-
const { host } = headers;
|
|
52
|
+
const subdomain = host.split(".")[0];
|
|
53
|
+
const startAt = process.hrtime();
|
|
50
54
|
|
|
51
|
-
|
|
52
|
-
|
|
55
|
+
res.on("finish", () => {
|
|
56
|
+
const diff = process.hrtime(startAt);
|
|
57
|
+
const time = diff[0] * 1e3 + diff[1] * 1e-6;
|
|
53
58
|
|
|
54
|
-
|
|
55
|
-
const diff = process.hrtime(startAt);
|
|
56
|
-
const time = diff[0] * 1e3 + diff[1] * 1e-6;
|
|
59
|
+
const { statusCode } = res;
|
|
57
60
|
|
|
58
|
-
|
|
61
|
+
RequestModel.create({
|
|
62
|
+
url,
|
|
63
|
+
time,
|
|
64
|
+
subdomain,
|
|
65
|
+
method,
|
|
66
|
+
statusCode,
|
|
67
|
+
userAgent: headers["user-agent"],
|
|
68
|
+
referer: headers["referer"],
|
|
69
|
+
acceptLanguage: headers["accept-language"],
|
|
70
|
+
});
|
|
71
|
+
});
|
|
59
72
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
73
|
+
next();
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* returns the amount of requests for the specified subdomain
|
|
77
|
+
* @method count
|
|
78
|
+
* @memberof Request
|
|
79
|
+
* @param {string} subdomain - the subdomain of which to get the count of requests for
|
|
80
|
+
* @return {Promise}
|
|
81
|
+
*/
|
|
82
|
+
static async count({ subdomain }) {
|
|
83
|
+
return await RequestModel.count({
|
|
63
84
|
subdomain,
|
|
64
|
-
method,
|
|
65
|
-
statusCode,
|
|
66
|
-
userAgent: headers['user-agent'],
|
|
67
|
-
referer: headers['referer'],
|
|
68
|
-
acceptLanguage: headers['accept-language']
|
|
69
85
|
});
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
module.exports.count = function count({ subdomain }) {
|
|
82
|
-
return new Promise(function(resolve, reject) {
|
|
83
|
-
Request.count({
|
|
84
|
-
subdomain
|
|
85
|
-
}, (err, count) => {
|
|
86
|
-
if(err) return reject(err);
|
|
87
|
-
return resolve(count);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* removes all entries associated with a particular subdomain
|
|
89
|
+
* @method del
|
|
90
|
+
* @memberof Request
|
|
91
|
+
* @param {String} subdomain - the subdomain that the entries are related to
|
|
92
|
+
* @return {Promise}
|
|
93
|
+
*/
|
|
94
|
+
static async del({ subdomain }) {
|
|
95
|
+
return await RequestModel.deleteMany({
|
|
96
|
+
subdomain,
|
|
88
97
|
});
|
|
89
|
-
}
|
|
90
|
-
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export default Request;
|
package/lib/models/user.js
CHANGED
|
@@ -1,172 +1,147 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
const mongoose = require('mongoose');
|
|
6
|
-
const crypto = require('crypto');
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
|
+
import crypto from "crypto";
|
|
7
3
|
|
|
8
4
|
const Schema = mongoose.Schema;
|
|
9
5
|
|
|
10
|
-
const UserSchema = new Schema(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
6
|
+
const UserSchema = new Schema(
|
|
7
|
+
{
|
|
8
|
+
_id: String,
|
|
9
|
+
username: String,
|
|
10
|
+
password: String,
|
|
11
|
+
token: String,
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
minimize: false,
|
|
15
|
+
timestamps: {
|
|
16
|
+
createdAt: "created_at",
|
|
17
|
+
updatedAt: "updated_at",
|
|
18
|
+
},
|
|
20
19
|
}
|
|
21
|
-
|
|
20
|
+
);
|
|
22
21
|
|
|
23
|
-
const
|
|
22
|
+
const UserModel = mongoose.model("User", UserSchema);
|
|
24
23
|
|
|
25
24
|
/**
|
|
26
25
|
* User definition
|
|
27
|
-
* @class
|
|
26
|
+
* @class User
|
|
28
27
|
* @property {String} username - a string that defines the user's accounts
|
|
29
28
|
* @property {String} password - a password for the user
|
|
30
29
|
* @property {String=} token - an access token
|
|
31
30
|
*/
|
|
32
|
-
|
|
31
|
+
class User {
|
|
32
|
+
/**
|
|
33
|
+
* middleware to verify the username and token are valid, will then set the user to req.user
|
|
34
|
+
* @function authenticateMiddleware
|
|
35
|
+
* @memberof User
|
|
36
|
+
* @param {Object} req - express request
|
|
37
|
+
* @param {Object} res - express response
|
|
38
|
+
* @param {Function} next - callback to go to next middleware
|
|
39
|
+
*/
|
|
40
|
+
static async authenticateMiddleware(req, res, next) {
|
|
41
|
+
const username = req.headers["x-deploy-username"];
|
|
42
|
+
const token = req.headers["x-deploy-token"];
|
|
33
43
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
* @param {Object} req - express request
|
|
38
|
-
* @param {Object} res - express response
|
|
39
|
-
* @param {Function} next - callback to go to next middleware
|
|
40
|
-
*/
|
|
41
|
-
module.exports.authenticateMiddleware = function authenticateMiddleware(req, res, next) {
|
|
42
|
-
const { headers } = req;
|
|
43
|
-
const username = headers['x-deploy-username'];
|
|
44
|
-
const token = headers['x-deploy-token'];
|
|
45
|
-
if(!username && !token) {
|
|
46
|
-
res.status(500).send({ error: 'authentication necessary' });
|
|
47
|
-
}
|
|
48
|
-
authenticate(username, token)
|
|
49
|
-
.then((user) => {
|
|
50
|
-
req.user = user;
|
|
51
|
-
next();
|
|
52
|
-
})
|
|
53
|
-
.catch((error) => res.status(500).send({ error }));
|
|
54
|
-
};
|
|
44
|
+
if (!username && !token) {
|
|
45
|
+
res.status(500).send({ error: "authentication necessary" });
|
|
46
|
+
}
|
|
55
47
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
48
|
+
req.user = await User.authenticate(username, token);
|
|
49
|
+
|
|
50
|
+
next();
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* verify the username and token are valid
|
|
54
|
+
* @method authenticate
|
|
55
|
+
* @memberof User
|
|
56
|
+
* @param {String} token - the token associated with the username
|
|
57
|
+
* @param {String} username - the username of the user
|
|
58
|
+
* @return {Promise}
|
|
59
|
+
*/
|
|
60
|
+
static async authenticate(username, token) {
|
|
61
|
+
let user = await UserModel.findOne({
|
|
67
62
|
_id: username,
|
|
68
|
-
token
|
|
69
|
-
}, function(err, user) {
|
|
70
|
-
if(err || !user) return reject('not authenticated');
|
|
71
|
-
return resolve({
|
|
72
|
-
username: user.username,
|
|
73
|
-
token: user.token,
|
|
74
|
-
depoyments: user.deployments
|
|
75
|
-
});
|
|
63
|
+
token,
|
|
76
64
|
});
|
|
77
|
-
});
|
|
78
|
-
};
|
|
79
65
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
66
|
+
if (!user) throw new Error("not authenticated");
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
username: user.username,
|
|
70
|
+
token: user.token,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* logs out a user by deleting their token
|
|
75
|
+
* @method logout
|
|
76
|
+
* @memberof User
|
|
77
|
+
* @param {String} token - the token associated with the username
|
|
78
|
+
* @param {String} username - the username of the user
|
|
79
|
+
* @return {Promise}
|
|
80
|
+
*/
|
|
81
|
+
static async logout({ token, username }) {
|
|
82
|
+
let user = await UserModel.findOne({
|
|
90
83
|
_id: username,
|
|
91
|
-
token
|
|
92
|
-
}, function(err, user) {
|
|
93
|
-
if(err || !user) return reject('token and username not valid');
|
|
94
|
-
user.token = '';
|
|
95
|
-
user.save((err) => {
|
|
96
|
-
if(err) return reject('erro removing token from the database');
|
|
97
|
-
return resolve();
|
|
98
|
-
});
|
|
84
|
+
token,
|
|
99
85
|
});
|
|
100
|
-
});
|
|
101
|
-
};
|
|
102
86
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
87
|
+
if (!user) throw new Error("token and username not valid");
|
|
88
|
+
|
|
89
|
+
user.token = "";
|
|
90
|
+
|
|
91
|
+
return await user.save();
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* logs in a user and returns
|
|
95
|
+
* @method login
|
|
96
|
+
* @memberof User
|
|
97
|
+
* @param {String} username - the username of the user who is logging in
|
|
98
|
+
* @param {String} password - the password associated with the user
|
|
99
|
+
* @return {Promise}
|
|
100
|
+
*/
|
|
101
|
+
static async login({ username, password }) {
|
|
102
|
+
let user = await UserModel.findOne({
|
|
113
103
|
_id: username,
|
|
114
|
-
password: crypto.createHash(
|
|
115
|
-
}, function(err, user) {
|
|
116
|
-
if(err || !user) return reject('password username combination not correct');
|
|
117
|
-
user.token = crypto.createHash('sha256').update(Date.now() + password + username).digest('hex');
|
|
118
|
-
user.save((err) => {
|
|
119
|
-
if(err) return reject('erro persisting token to database');
|
|
120
|
-
return resolve({
|
|
121
|
-
username: user.username,
|
|
122
|
-
token: user.token
|
|
123
|
-
});
|
|
124
|
-
});
|
|
104
|
+
password: crypto.createHash("sha256").update(password).digest("hex"),
|
|
125
105
|
});
|
|
126
|
-
});
|
|
127
|
-
};
|
|
128
106
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
107
|
+
if (!user) throw new Error("password username combination not correct");
|
|
108
|
+
|
|
109
|
+
user.token = crypto
|
|
110
|
+
.createHash("sha256")
|
|
111
|
+
.update(Date.now() + password + username)
|
|
112
|
+
.digest("hex");
|
|
113
|
+
await user.save();
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
username: user.username,
|
|
117
|
+
token: user.token,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* registers a user
|
|
122
|
+
* @method register
|
|
123
|
+
* @memberof User
|
|
124
|
+
* @param {String} username - the username of the user who is logging in
|
|
125
|
+
* @param {String} password - the password associated with the user
|
|
126
|
+
* @return {Promise}
|
|
127
|
+
*/
|
|
128
|
+
static async register({ username, password }) {
|
|
129
|
+
let user = await UserModel.create({
|
|
139
130
|
_id: username,
|
|
140
131
|
username,
|
|
141
|
-
password: crypto.createHash(
|
|
142
|
-
token: crypto
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
username: user.username,
|
|
147
|
-
token: user.token,
|
|
148
|
-
depoyments: user.deployments
|
|
149
|
-
});
|
|
132
|
+
password: crypto.createHash("sha256").update(password).digest("hex"),
|
|
133
|
+
token: crypto
|
|
134
|
+
.createHash("sha256")
|
|
135
|
+
.update(Date.now() + password + username)
|
|
136
|
+
.digest("hex"),
|
|
150
137
|
});
|
|
151
|
-
});
|
|
152
|
-
};
|
|
153
138
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
module.exports.get = function get({ token, username }) {
|
|
162
|
-
return new Promise(function(resolve, reject) {
|
|
163
|
-
User.findOne({
|
|
164
|
-
_id: username,
|
|
165
|
-
token
|
|
166
|
-
}, function(err, user) {
|
|
167
|
-
if(err || !user) return reject('token is no longer valid');
|
|
168
|
-
return resolve(user);
|
|
169
|
-
});
|
|
139
|
+
return {
|
|
140
|
+
username: user.username,
|
|
141
|
+
token: user.token,
|
|
142
|
+
depoyments: user.deployments,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
}
|
|
170
146
|
|
|
171
|
-
|
|
172
|
-
};
|
|
147
|
+
export default User;
|