Merge pull request #204 from VermiumSifell/201-fix-code-smells
Encryption
This commit is contained in:
commit
d01e29636f
5 changed files with 81 additions and 31 deletions
|
@ -27,6 +27,7 @@
|
|||
"reputation": {
|
||||
"timeout": 86400000
|
||||
},
|
||||
"secretKey": "SET A LONG RANDOM PASSWORD HERE, ITS FOR USE OF ENCRYPTION WITH LENGTH OF 32",
|
||||
"importToDB": false,
|
||||
"clearUnused": false
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import logger from "../../../../handlers/logger";
|
|||
|
||||
// Models
|
||||
import apiSchema from "../../../../helpers/database/models/apiSchema";
|
||||
import encryption from "../../../../handlers/encryption";
|
||||
|
||||
// Function
|
||||
export default async (interaction: CommandInteraction) => {
|
||||
|
@ -17,7 +18,7 @@ export default async (interaction: CommandInteraction) => {
|
|||
|
||||
// Get options
|
||||
const url = options?.getString("url");
|
||||
const token = options?.getString("token");
|
||||
const token = encryption.encrypt(options?.getString("token"));
|
||||
|
||||
// Update API credentials
|
||||
await apiSchema
|
||||
|
|
|
@ -8,6 +8,7 @@ import config from "../../../../config.json";
|
|||
|
||||
// Handlers
|
||||
import logger from "../../../handlers/logger";
|
||||
import encryption from "../../../handlers/encryption";
|
||||
|
||||
// Helpers
|
||||
import creditNoun from "../../../helpers/creditNoun";
|
||||
|
@ -127,7 +128,9 @@ export default async (interaction: CommandInteraction) => {
|
|||
// Create a api instance
|
||||
const api = axios?.create({
|
||||
baseURL: apiCredentials?.url,
|
||||
headers: { Authorization: `Bearer ${apiCredentials?.token}` },
|
||||
headers: {
|
||||
Authorization: `Bearer ${encryption.decrypt(apiCredentials?.token)}`,
|
||||
},
|
||||
});
|
||||
|
||||
// Get shop URL
|
||||
|
|
36
src/handlers/encryption.ts
Normal file
36
src/handlers/encryption.ts
Normal file
|
@ -0,0 +1,36 @@
|
|||
import crypto from "crypto";
|
||||
import config from "../../config.json";
|
||||
|
||||
const algorithm = "aes-256-ctr";
|
||||
const iv = crypto.randomBytes(16);
|
||||
|
||||
const encrypt = (text: any) => {
|
||||
const cipher = crypto.createCipheriv(algorithm, config.secretKey, iv);
|
||||
|
||||
const encrypted = Buffer.concat([cipher.update(text), cipher.final()]);
|
||||
|
||||
return {
|
||||
iv: iv.toString("hex"),
|
||||
content: encrypted.toString("hex"),
|
||||
};
|
||||
};
|
||||
|
||||
const decrypt = (hash: any) => {
|
||||
const decipher = crypto.createDecipheriv(
|
||||
algorithm,
|
||||
config.secretKey,
|
||||
Buffer.from(hash.iv, "hex")
|
||||
);
|
||||
|
||||
const decrypted = Buffer.concat([
|
||||
decipher.update(Buffer.from(hash.content, "hex")),
|
||||
decipher.final(),
|
||||
]);
|
||||
|
||||
return decrypted.toString();
|
||||
};
|
||||
|
||||
export default {
|
||||
encrypt,
|
||||
decrypt,
|
||||
};
|
|
@ -1,4 +1,4 @@
|
|||
import mongoose from 'mongoose';
|
||||
import mongoose from "mongoose";
|
||||
|
||||
const apiSchema = new mongoose.Schema(
|
||||
{
|
||||
|
@ -13,17 +13,26 @@ const apiSchema = new mongoose.Schema(
|
|||
required: true,
|
||||
unique: false,
|
||||
index: true,
|
||||
default: 'https://localhost/api/',
|
||||
default: "https://localhost/api/",
|
||||
},
|
||||
token: {
|
||||
iv: {
|
||||
type: mongoose.SchemaTypes.String,
|
||||
required: true,
|
||||
unique: false,
|
||||
index: true,
|
||||
default: 'token',
|
||||
default: "token",
|
||||
},
|
||||
content: {
|
||||
type: mongoose.SchemaTypes.String,
|
||||
required: true,
|
||||
unique: false,
|
||||
index: true,
|
||||
default: "token",
|
||||
},
|
||||
},
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
export default mongoose.model('api', apiSchema);
|
||||
export default mongoose.model("api", apiSchema);
|
||||
|
|
Loading…
Add table
Reference in a new issue