@rpcbase/server 0.198.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.
@@ -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
@@ -12,3 +12,5 @@ module.exports = {
12
12
  client_router,
13
13
  rpc_router,
14
14
  }
15
+
16
+ console.log("ICI SERRRRRRR", process.env.CUSTOMER_PREFIX)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/server",
3
- "version": "0.198.0",
3
+ "version": "0.199.0",
4
4
  "license": "SSPL-1.0",
5
5
  "main": "./index.js",
6
6
  "bin": {
@@ -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
  })
@@ -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
  })