@rpcbase/server 0.197.0 → 0.199.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/get_object_id.js +32 -0
- package/index.js +2 -0
- package/package.json +1 -1
- package/src/auth/reset_password.js +2 -0
- package/src/auth/sign_up.js +3 -0
- package/src/client/client_router.js +1 -0
package/get_object_id.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/* @flow */
|
|
2
|
+
const assert = require("assert")
|
|
3
|
+
const crypto = require("crypto")
|
|
4
|
+
|
|
5
|
+
const isHexadecimal = require("validator/lib/isHexadecimal")
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
const {CUSTOMER_PREFIX} = process.env
|
|
9
|
+
|
|
10
|
+
// TODO: WARNING: DANGER: this code is duplicated in the client
|
|
11
|
+
// TODO: move it to iso
|
|
12
|
+
|
|
13
|
+
// Validation
|
|
14
|
+
// is defined
|
|
15
|
+
assert(CUSTOMER_PREFIX, "expected CUSTOMER_PREFIX to be defined")
|
|
16
|
+
// is hexadecimal
|
|
17
|
+
assert(isHexadecimal(CUSTOMER_PREFIX), "expected CUSTOMER_PREFIX to be a hexadecimal")
|
|
18
|
+
// is 4 bytes
|
|
19
|
+
assert(CUSTOMER_PREFIX.length === 8, "CUSTOMER_PREFIX must be exactly bytes long ie: 8 hex chars")
|
|
20
|
+
// is lower than
|
|
21
|
+
const max_val = parseInt("6387427d", 16)
|
|
22
|
+
const customer_prefix_int = parseInt(CUSTOMER_PREFIX, 16)
|
|
23
|
+
assert(max_val - customer_prefix_int > 0, "CUSTOMER_PREFIX must be lower than 6387427d")
|
|
24
|
+
|
|
25
|
+
// generates a 12 bytes mongodb object id using the org id prefix or custom customer id
|
|
26
|
+
const get_object_id = () => {
|
|
27
|
+
const random_bytes = crypto.randomBytes(8)
|
|
28
|
+
const obj_id = `${CUSTOMER_PREFIX}${random_bytes.toString("hex")}`
|
|
29
|
+
return obj_id
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
module.exports = get_object_id
|
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -10,6 +10,7 @@ const get_random_str = require("@rpcbase/std/crypto/get_random_str")
|
|
|
10
10
|
|
|
11
11
|
const mailer = require("../../mailer")
|
|
12
12
|
const mongoose = require("../../mongoose")
|
|
13
|
+
const get_object_id = require("../../get_object_id")
|
|
13
14
|
const ResetPasswordToken = require("../models/ResetPasswordToken")
|
|
14
15
|
|
|
15
16
|
const log = debug("rb:auth:reset_password")
|
|
@@ -40,6 +41,7 @@ const reset_password = async({email}, ctx) => {
|
|
|
40
41
|
const token_hash = await hash_password(token)
|
|
41
42
|
|
|
42
43
|
const reset_token = new ResetPasswordToken({
|
|
44
|
+
_id: get_object_id(),
|
|
43
45
|
user_id: user._id,
|
|
44
46
|
token_hash,
|
|
45
47
|
})
|
package/src/auth/sign_up.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
/* @flow */
|
|
2
2
|
const {hash_password} = require("@rpcbase/std/crypto/hash")
|
|
3
3
|
|
|
4
|
+
const get_object_id = require("../../get_object_id")
|
|
5
|
+
|
|
4
6
|
const mongoose = require("../../mongoose")
|
|
5
7
|
|
|
6
8
|
const sign_up = async({email, password}, ctx) => {
|
|
@@ -40,6 +42,7 @@ const sign_up = async({email, password}, ctx) => {
|
|
|
40
42
|
const hash = await hash_password(password)
|
|
41
43
|
|
|
42
44
|
const user = new User({
|
|
45
|
+
_id: get_object_id(),
|
|
43
46
|
email,
|
|
44
47
|
password_hash: hash
|
|
45
48
|
})
|