mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-09 10:34:17 +00:00
Remove unused classes
This commit is contained in:
parent
9d84e776d0
commit
c79f000ea3
@ -1,9 +0,0 @@
|
||||
import createClient from "comty.js"
|
||||
|
||||
export default (params = {}) => {
|
||||
return createClient({
|
||||
...params,
|
||||
accessKey: process.env.COMTY_ACCESS_KEY,
|
||||
privateKey: process.env.COMTY_PRIVATE_KEY,
|
||||
})
|
||||
}
|
@ -1,84 +0,0 @@
|
||||
export class AuthorizationError extends Error {
|
||||
constructor(req, res, message = "This endpoint requires authorization") {
|
||||
super(message)
|
||||
this.name = "AuthorizationError"
|
||||
|
||||
if (req && res) {
|
||||
return res.status(this.constructor.statusCode).json({
|
||||
error: message,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
static get statusCode() {
|
||||
return 401
|
||||
}
|
||||
}
|
||||
|
||||
export class NotFoundError extends Error {
|
||||
constructor(req, res, message = "Not found") {
|
||||
super(message)
|
||||
this.name = "NotFoundError"
|
||||
|
||||
if (req && res) {
|
||||
return res.status(this.constructor.statusCode).json({
|
||||
error: message,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
static get statusCode() {
|
||||
return 404
|
||||
}
|
||||
}
|
||||
|
||||
export class PermissionError extends Error {
|
||||
constructor(req, res, message = "You don't have permission to do this") {
|
||||
super(message)
|
||||
this.name = "PermissionError"
|
||||
|
||||
if (req && res) {
|
||||
return res.status(this.constructor.statusCode).json({
|
||||
error: message,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
static get statusCode() {
|
||||
return 403
|
||||
}
|
||||
}
|
||||
|
||||
export class BadRequestError extends Error {
|
||||
constructor(req, res, message = "Bad request") {
|
||||
super(message)
|
||||
this.name = "BadRequestError"
|
||||
|
||||
if (req && res) {
|
||||
return res.status(this.constructor.statusCode).json({
|
||||
error: message,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
static get statusCode() {
|
||||
return 400
|
||||
}
|
||||
}
|
||||
|
||||
export class InternalServerError extends Error {
|
||||
constructor(req, res, message = "Internal server error") {
|
||||
super(message)
|
||||
this.name = "InternalServerError"
|
||||
|
||||
if (req && res) {
|
||||
return res.status(this.constructor.statusCode).json({
|
||||
error: message,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
static get statusCode() {
|
||||
return 500
|
||||
}
|
||||
}
|
@ -1,133 +0,0 @@
|
||||
import crypto from "crypto"
|
||||
|
||||
export default class SecureEntry {
|
||||
constructor(model, params = {}) {
|
||||
this.params = params
|
||||
|
||||
if (!model) {
|
||||
throw new Error("Missing model")
|
||||
}
|
||||
|
||||
this.model = model
|
||||
}
|
||||
|
||||
static get encrytionAlgorithm() {
|
||||
return "aes-256-cbc"
|
||||
}
|
||||
|
||||
async set(key, value, {
|
||||
keyName = "key",
|
||||
valueName = "value",
|
||||
}) {
|
||||
if (!keyName) {
|
||||
throw new Error("Missing keyName")
|
||||
}
|
||||
|
||||
if (!valueName) {
|
||||
throw new Error("Missing valueName")
|
||||
}
|
||||
|
||||
if (!key) {
|
||||
throw new Error("Missing key")
|
||||
}
|
||||
|
||||
if (!value) {
|
||||
throw new Error("Missing value")
|
||||
}
|
||||
|
||||
let entry = await this.model.findOne({
|
||||
[keyName]: key,
|
||||
[valueName]: value,
|
||||
}).catch(() => null)
|
||||
|
||||
const encryptionKey = Buffer.from(process.env.SYNC_ENCRIPT_SECRET, "hex")
|
||||
const iv = crypto.randomBytes(16)
|
||||
|
||||
const cipher = crypto.createCipheriv(SecureEntry.encrytionAlgorithm, encryptionKey, iv)
|
||||
|
||||
let encryptedData
|
||||
|
||||
try {
|
||||
encryptedData = cipher.update(value)
|
||||
}
|
||||
catch (error) {
|
||||
console.error(error)
|
||||
}
|
||||
|
||||
encryptedData = Buffer.concat([encryptedData, cipher.final()])
|
||||
|
||||
value = iv.toString("hex") + ":" + encryptedData.toString("hex")
|
||||
|
||||
if (entry) {
|
||||
entry[valueName] = value
|
||||
|
||||
await entry.save()
|
||||
|
||||
return entry
|
||||
}
|
||||
|
||||
entry = new this.model({
|
||||
[keyName]: key,
|
||||
[valueName]: value,
|
||||
})
|
||||
|
||||
await entry.save()
|
||||
|
||||
return entry
|
||||
}
|
||||
|
||||
async get(key, value, {
|
||||
keyName = "key",
|
||||
valueName = "value",
|
||||
}) {
|
||||
if (!keyName) {
|
||||
throw new Error("Missing keyName")
|
||||
}
|
||||
if (!key) {
|
||||
throw new Error("Missing key")
|
||||
}
|
||||
|
||||
const searchQuery = {
|
||||
[keyName]: key,
|
||||
}
|
||||
|
||||
if (value) {
|
||||
searchQuery[valueName] = value
|
||||
}
|
||||
|
||||
const entry = await this.model.findOne(searchQuery).catch(() => null)
|
||||
|
||||
if (!entry || !entry[valueName]) {
|
||||
return null
|
||||
}
|
||||
|
||||
const encryptionKey = Buffer.from(process.env.SYNC_ENCRIPT_SECRET, "hex")
|
||||
|
||||
const iv = Buffer.from(entry[valueName].split(":")[0], "hex")
|
||||
const encryptedText = Buffer.from(entry[valueName].split(":")[1], "hex")
|
||||
|
||||
const decipher = crypto.createDecipheriv(SecureEntry.encrytionAlgorithm, encryptionKey, iv)
|
||||
|
||||
let decrypted = decipher.update(encryptedText)
|
||||
|
||||
decrypted = Buffer.concat([decrypted, decipher.final()])
|
||||
|
||||
return decrypted.toString()
|
||||
}
|
||||
|
||||
async deleteByID(_id) {
|
||||
if (!_id) {
|
||||
throw new Error("Missing _id")
|
||||
}
|
||||
|
||||
const entry = await this.model.findById(_id).catch(() => null)
|
||||
|
||||
if (!entry) {
|
||||
return null
|
||||
}
|
||||
|
||||
await entry.delete()
|
||||
|
||||
return entry
|
||||
}
|
||||
}
|
@ -1,134 +0,0 @@
|
||||
import { SyncEntry } from "../../db_models"
|
||||
|
||||
import crypto from "crypto"
|
||||
|
||||
export default class SecureSyncEntry {
|
||||
static get encrytionAlgorithm() {
|
||||
return "aes-256-cbc"
|
||||
}
|
||||
|
||||
static async set(user_id, key, value) {
|
||||
if (!user_id) {
|
||||
throw new Error("Missing user_id")
|
||||
}
|
||||
|
||||
if (!key) {
|
||||
throw new Error("Missing key")
|
||||
}
|
||||
|
||||
if (!value) {
|
||||
throw new Error("Missing value")
|
||||
}
|
||||
|
||||
let entry = await SyncEntry.findOne({
|
||||
user_id,
|
||||
key,
|
||||
}).catch(() => null)
|
||||
|
||||
const encryptionKey = Buffer.from(process.env.SYNC_ENCRIPT_SECRET, "hex")
|
||||
const iv = crypto.randomBytes(16)
|
||||
|
||||
const cipher = crypto.createCipheriv(SecureSyncEntry.encrytionAlgorithm, encryptionKey, iv)
|
||||
|
||||
let encrypted
|
||||
|
||||
try {
|
||||
encrypted = cipher.update(value)
|
||||
}
|
||||
catch (error) {
|
||||
console.error(error)
|
||||
}
|
||||
|
||||
encrypted = Buffer.concat([encrypted, cipher.final()])
|
||||
|
||||
if (entry) {
|
||||
entry.value = iv.toString("hex") + ":" + encrypted.toString("hex")
|
||||
|
||||
await entry.save()
|
||||
|
||||
return entry
|
||||
}
|
||||
|
||||
entry = new SyncEntry({
|
||||
user_id,
|
||||
key,
|
||||
value: iv.toString("hex") + ":" + encrypted.toString("hex"),
|
||||
})
|
||||
|
||||
await entry.save()
|
||||
|
||||
return entry
|
||||
}
|
||||
|
||||
static async get(user_id, key) {
|
||||
if (!user_id) {
|
||||
throw new Error("Missing user_id")
|
||||
}
|
||||
|
||||
if (!key) {
|
||||
throw new Error("Missing key")
|
||||
}
|
||||
|
||||
const entry = await SyncEntry.findOne({
|
||||
user_id,
|
||||
key,
|
||||
}).catch(() => null)
|
||||
|
||||
if (!entry) {
|
||||
return null
|
||||
}
|
||||
|
||||
const encryptionKey = Buffer.from(process.env.SYNC_ENCRIPT_SECRET, "hex")
|
||||
|
||||
const iv = Buffer.from(entry.value.split(":")[0], "hex")
|
||||
const encryptedText = Buffer.from(entry.value.split(":")[1], "hex")
|
||||
|
||||
const decipher = crypto.createDecipheriv(SecureSyncEntry.encrytionAlgorithm, encryptionKey, iv)
|
||||
|
||||
let decrypted = decipher.update(encryptedText)
|
||||
|
||||
decrypted = Buffer.concat([decrypted, decipher.final()])
|
||||
|
||||
return decrypted.toString()
|
||||
}
|
||||
|
||||
static async delete(user_id, key) {
|
||||
if (!user_id) {
|
||||
throw new Error("Missing user_id")
|
||||
}
|
||||
|
||||
if (!key) {
|
||||
throw new Error("Missing key")
|
||||
}
|
||||
|
||||
const entry = await SyncEntry.findOne({
|
||||
user_id,
|
||||
key,
|
||||
}).catch(() => null)
|
||||
|
||||
if (!entry) {
|
||||
return null
|
||||
}
|
||||
|
||||
await entry.delete()
|
||||
|
||||
return entry
|
||||
}
|
||||
|
||||
static async has(user_id, key) {
|
||||
if (!user_id) {
|
||||
throw new Error("Missing user_id")
|
||||
}
|
||||
|
||||
if (!key) {
|
||||
throw new Error("Missing key")
|
||||
}
|
||||
|
||||
const entry = await SyncEntry.findOne({
|
||||
user_id,
|
||||
key,
|
||||
}).catch(() => null)
|
||||
|
||||
return !!entry
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user