Merge pull request #204 from VermiumSifell/201-fix-code-smells

Encryption
This commit is contained in:
Axel Olausson Holtenäs 2022-04-10 22:59:20 +02:00 committed by GitHub
commit d01e29636f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 81 additions and 31 deletions

View file

@ -27,6 +27,7 @@
"reputation": { "reputation": {
"timeout": 86400000 "timeout": 86400000
}, },
"secretKey": "SET A LONG RANDOM PASSWORD HERE, ITS FOR USE OF ENCRYPTION WITH LENGTH OF 32",
"importToDB": false, "importToDB": false,
"clearUnused": false "clearUnused": false
} }

View file

@ -9,6 +9,7 @@ import logger from "../../../../handlers/logger";
// Models // Models
import apiSchema from "../../../../helpers/database/models/apiSchema"; import apiSchema from "../../../../helpers/database/models/apiSchema";
import encryption from "../../../../handlers/encryption";
// Function // Function
export default async (interaction: CommandInteraction) => { export default async (interaction: CommandInteraction) => {
@ -17,7 +18,7 @@ export default async (interaction: CommandInteraction) => {
// Get options // Get options
const url = options?.getString("url"); const url = options?.getString("url");
const token = options?.getString("token"); const token = encryption.encrypt(options?.getString("token"));
// Update API credentials // Update API credentials
await apiSchema await apiSchema

View file

@ -8,6 +8,7 @@ import config from "../../../../config.json";
// Handlers // Handlers
import logger from "../../../handlers/logger"; import logger from "../../../handlers/logger";
import encryption from "../../../handlers/encryption";
// Helpers // Helpers
import creditNoun from "../../../helpers/creditNoun"; import creditNoun from "../../../helpers/creditNoun";
@ -127,7 +128,9 @@ export default async (interaction: CommandInteraction) => {
// Create a api instance // Create a api instance
const api = axios?.create({ const api = axios?.create({
baseURL: apiCredentials?.url, baseURL: apiCredentials?.url,
headers: { Authorization: `Bearer ${apiCredentials?.token}` }, headers: {
Authorization: `Bearer ${encryption.decrypt(apiCredentials?.token)}`,
},
}); });
// Get shop URL // Get shop URL

View 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,
};

View file

@ -1,29 +1,38 @@
import mongoose from 'mongoose'; import mongoose from "mongoose";
const apiSchema = new mongoose.Schema( const apiSchema = new mongoose.Schema(
{ {
guildId: { guildId: {
type: mongoose.SchemaTypes.Decimal128, type: mongoose.SchemaTypes.Decimal128,
required: true, required: true,
unique: false, unique: false,
index: true, index: true,
}, },
url: { url: {
type: mongoose.SchemaTypes.String, type: mongoose.SchemaTypes.String,
required: true, required: true,
unique: false, unique: false,
index: true, index: true,
default: 'https://localhost/api/', default: "https://localhost/api/",
}, },
token: { token: {
type: mongoose.SchemaTypes.String, iv: {
required: true, type: mongoose.SchemaTypes.String,
unique: false, required: true,
index: true, unique: false,
default: 'token', index: true,
}, default: "token",
}, },
{ timestamps: true } content: {
); type: mongoose.SchemaTypes.String,
required: true,
export default mongoose.model('api', apiSchema); unique: false,
index: true,
default: "token",
},
},
},
{ timestamps: true }
);
export default mongoose.model("api", apiSchema);