@raketa-cloud/admin 0.0.99 → 0.0.100
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/package.json +1 -1
- package/src/cipher.js +49 -0
- package/src/cloud/Settings.js +2 -8
- package/src/setupAuthentication.js +6 -21
- package/src/simpleCipher.js +0 -58
package/package.json
CHANGED
package/src/cipher.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import {
|
|
2
|
+
pbkdf2Sync,
|
|
3
|
+
createCipheriv,
|
|
4
|
+
createDecipheriv,
|
|
5
|
+
randomBytes,
|
|
6
|
+
} from "node:crypto";
|
|
7
|
+
|
|
8
|
+
const ALGORITHM = "aes-256-cbc";
|
|
9
|
+
const IV = randomBytes(16);
|
|
10
|
+
const SALT = "some string to have a salt value..";
|
|
11
|
+
|
|
12
|
+
function deriveKey(password) {
|
|
13
|
+
return pbkdf2Sync(password, SALT, 100000, 32, "sha256");
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function encrypt(plaintext, config) {
|
|
17
|
+
const cipher = createCipheriv(
|
|
18
|
+
ALGORITHM,
|
|
19
|
+
deriveKey(config.secrets.crypto_secret),
|
|
20
|
+
IV
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
let encrypted = cipher.update(plaintext, "utf8", "hex");
|
|
24
|
+
|
|
25
|
+
encrypted += cipher.final("hex");
|
|
26
|
+
|
|
27
|
+
return `${IV.toString("hex")}:${encrypted}`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function decrypt(encryptedText, config) {
|
|
31
|
+
const [ivHex, encrypted] = encryptedText.split(":");
|
|
32
|
+
const iv = Buffer.from(ivHex, "hex");
|
|
33
|
+
|
|
34
|
+
const decipher = createDecipheriv(
|
|
35
|
+
ALGORITHM,
|
|
36
|
+
deriveKey(config.secrets.crypto_secret),
|
|
37
|
+
iv
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
let decrypted = decipher.update(encrypted, "hex", "utf8");
|
|
41
|
+
decrypted += decipher.final("utf8");
|
|
42
|
+
|
|
43
|
+
return decrypted;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export default {
|
|
47
|
+
encrypt,
|
|
48
|
+
decrypt,
|
|
49
|
+
};
|
package/src/cloud/Settings.js
CHANGED
|
@@ -6,17 +6,11 @@ import {
|
|
|
6
6
|
|
|
7
7
|
export default class Settings extends RaketaClient {
|
|
8
8
|
async find(locale) {
|
|
9
|
-
const {
|
|
10
|
-
setting,
|
|
11
|
-
} = await this.query(SettingDocument, {
|
|
9
|
+
const { setting } = await this.query(SettingDocument, {
|
|
12
10
|
locale,
|
|
13
11
|
});
|
|
14
12
|
|
|
15
|
-
|
|
16
|
-
this.logError(`Cannot find settings for ${locale} locale.`)
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return setting.value;
|
|
13
|
+
return setting?.value || {};
|
|
20
14
|
}
|
|
21
15
|
|
|
22
16
|
async update(locale, params) {
|
|
@@ -1,45 +1,30 @@
|
|
|
1
|
-
import { createHmac, randomBytes } from "node:crypto";
|
|
2
1
|
import ejs from "ejs";
|
|
3
2
|
import { deleteCookie, getCookie, setCookie } from "hono/cookie";
|
|
4
3
|
import currentPath from "./currentPath.cjs";
|
|
5
4
|
import AuthHelper from "./helpers/AuthHelper";
|
|
6
|
-
|
|
7
|
-
import simpleCipher from "./simpleCipher";
|
|
5
|
+
import cipher from "./cipher";
|
|
8
6
|
|
|
9
|
-
// Generate a random signing secret on every server restart
|
|
10
|
-
// const SIGNING_SECRET = randomBytes(64).toString("hex");
|
|
11
7
|
const DELIMITER = ",";
|
|
12
8
|
|
|
13
|
-
function calculateHash(text, config) {
|
|
14
|
-
return createHmac("sha256", config.secrets.signing_secret)
|
|
15
|
-
.update(text)
|
|
16
|
-
.digest("hex");
|
|
17
|
-
}
|
|
18
|
-
|
|
19
9
|
function obfuscateToken(token, config) {
|
|
20
10
|
const date = new Date();
|
|
21
11
|
const timestamp = date.getTime() + 2 * 60000;
|
|
22
|
-
const
|
|
23
|
-
const hash = calculateHash(timestamped, config);
|
|
24
|
-
const signed = [hash, timestamped].join(DELIMITER);
|
|
12
|
+
const data = [token, timestamp].join(DELIMITER);
|
|
25
13
|
|
|
26
|
-
return Buffer.from(
|
|
14
|
+
return Buffer.from(cipher.encrypt(data, config)).toString("base64");
|
|
27
15
|
}
|
|
28
16
|
|
|
29
17
|
function extractToken(text, config) {
|
|
30
18
|
const encrypted = Buffer.from(text, "base64").toString("ascii");
|
|
31
|
-
const
|
|
32
|
-
const [
|
|
19
|
+
const data = cipher.decrypt(encrypted, config);
|
|
20
|
+
const [token, timestamp] = data.split(DELIMITER);
|
|
21
|
+
|
|
33
22
|
const tDate = new Date(Number.parseInt(timestamp, 10));
|
|
34
23
|
|
|
35
24
|
if (tDate < new Date()) {
|
|
36
25
|
return null;
|
|
37
26
|
}
|
|
38
27
|
|
|
39
|
-
if (hash !== calculateHash([token, timestamp].join(DELIMITER), config)) {
|
|
40
|
-
return null;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
28
|
return token;
|
|
44
29
|
}
|
|
45
30
|
|
package/src/simpleCipher.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
const ALPHABET =
|
|
2
|
-
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789,.";
|
|
3
|
-
|
|
4
|
-
function generateEncryptionLookup(key) {
|
|
5
|
-
const lookup = {};
|
|
6
|
-
|
|
7
|
-
ALPHABET.split("").forEach((char, index) => {
|
|
8
|
-
lookup[char] = key[index];
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
return lookup;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function generateDecryptionLookup(key) {
|
|
15
|
-
const lookup = {};
|
|
16
|
-
|
|
17
|
-
key.split("").forEach((char, index) => {
|
|
18
|
-
lookup[char] = ALPHABET[index];
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
return lookup;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// function generateKey() {
|
|
25
|
-
// const shuffled = ALPHABET.split("")
|
|
26
|
-
// .sort(() => Math.random() - 0.5)
|
|
27
|
-
// .join("");
|
|
28
|
-
|
|
29
|
-
// return shuffled;
|
|
30
|
-
// }
|
|
31
|
-
|
|
32
|
-
// Scramble lookup maps on every server restart
|
|
33
|
-
// const CIPHER_KEY = generateKey();
|
|
34
|
-
// const ENCRYPT_LOOKUP = generateEncryptionLookup(CIPHER_KEY);
|
|
35
|
-
// const DECRYPT_LOOKUP = generateDecryptionLookup(CIPHER_KEY);
|
|
36
|
-
|
|
37
|
-
function encrypt(plaintext, config) {
|
|
38
|
-
const ecryptLookup = generateEncryptionLookup(config.secrets.crypto_secret);
|
|
39
|
-
|
|
40
|
-
return plaintext
|
|
41
|
-
.split("")
|
|
42
|
-
.map((char) => ecryptLookup[char] || char)
|
|
43
|
-
.join("");
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function decrypt(ciphertext, config) {
|
|
47
|
-
const decryptLookup = generateDecryptionLookup(config.secrets.crypto_secret);
|
|
48
|
-
|
|
49
|
-
return ciphertext
|
|
50
|
-
.split("")
|
|
51
|
-
.map((char) => decryptLookup[char] || char)
|
|
52
|
-
.join("");
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export default {
|
|
56
|
-
encrypt,
|
|
57
|
-
decrypt,
|
|
58
|
-
};
|