remove unused controller

This commit is contained in:
SrGooglo 2023-07-27 00:09:05 +00:00
parent c6e55f4f2e
commit 4d59accafe
3 changed files with 0 additions and 319 deletions

View File

@ -1,117 +0,0 @@
import { SyncEntry } from "@shared-classes/DbModels"
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
}
}

View File

@ -1,202 +0,0 @@
import { Controller } from "linebridge/dist/server"
import SecureSyncEntry from "./classes/secureSyncEntry"
import axios from "axios"
export default class SyncController extends Controller {
static refName = "SyncController"
static useRoute = "/sync"
httpEndpoints = {
post: {
"/spotify/auth": {
middlewares: ["withAuthentication"],
fn: async (req, res) => {
const { code, redirect_uri } = req.body
if (!code) {
return res.status(400).json({
message: "Missing code",
})
}
if (!redirect_uri) {
return res.status(400).json({
message: "Missing redirect_uri",
})
}
const response = await axios({
url: "https://accounts.spotify.com/api/token",
method: "post",
params: {
grant_type: "authorization_code",
code: code,
redirect_uri: redirect_uri
},
headers: {
"Authorization": `Basic ${(Buffer.from(process.env.SPOTIFY_CLIENT_ID + ":" + process.env.SPOTIFY_CLIENT_SECRET).toString("base64"))}`,
"Content-Type": "application/x-www-form-urlencoded"
}
})
if (!response) {
return res.status(400).json({
message: "Missing data",
})
}
await SecureSyncEntry.set(req.user._id.toString(), "spotify_access_token", response.data.access_token)
await SecureSyncEntry.set(req.user._id.toString(), "spotify_refresh_token", response.data.refresh_token)
return res.json({
message: "ok"
})
}
},
"/spotify/unlink": {
middlewares: ["withAuthentication"],
fn: async (req, res) => {
await SecureSyncEntry.delete(req.user._id.toString(), "spotify_access_token", "")
await SecureSyncEntry.delete(req.user._id.toString(), "spotify_refresh_token", "")
return res.json({
message: "ok"
})
}
}
},
get: {
"/spotify/client_id": async (req, res) => {
return res.json({
client_id: process.env.SPOTIFY_CLIENT_ID,
})
},
"/spotify/is_authorized": {
middlewares: ["withAuthentication"],
fn: async (req, res) => {
const user_id = req.user._id.toString()
const authToken = await SecureSyncEntry.get(user_id, "spotify_access_token")
if (!authToken) {
return res.json({
is_authorized: false,
})
}
return res.json({
is_authorized: true,
})
}
},
"/spotify/data": {
middlewares: ["withAuthentication"],
fn: async (req, res) => {
const user_id = req.user._id.toString()
const authToken = await SecureSyncEntry.get(user_id, "spotify_access_token")
if (!authToken) {
return res.status(400).json({
message: "Missing auth token",
})
}
const response = await axios.get("https://api.spotify.com/v1/me", {
headers: {
"Authorization": `Bearer ${authToken}`
},
}).catch((error) => {
console.error(error.response.data)
res.status(error.response.status).json(error.response.data)
return null
})
if (response) {
return res.json(response.data)
}
}
},
"/spotify/currently_playing": {
middlewares: ["withAuthentication"],
fn: async (req, res) => {
const user_id = req.user._id.toString()
const authToken = await SecureSyncEntry.get(user_id, "spotify_access_token")
if (!authToken) {
return res.status(400).json({
message: "Missing auth token",
})
}
const response = await axios.get("https://api.spotify.com/v1/me/player", {
headers: {
"Authorization": `Bearer ${authToken}`
},
}).catch((error) => {
console.error(error.response.data)
res.status(error.response.status).json(error.response.data)
return null
})
if (response) {
return res.json(response.data)
}
}
},
"/spotify/search": {
middlewares: ["withAuthentication"],
fn: async (req, res) => {
const user_id = req.user._id.toString()
const authToken = await SecureSyncEntry.get(user_id, "spotify_access_token")
if (!authToken) {
return res.status(400).json({
message: "Missing auth token",
})
}
const { query, type, limit, offset } = req.query
if (!query) {
return res.status(400).json({
message: "Missing query",
})
}
if (!type) {
return res.status(400).json({
message: "Missing type",
})
}
const response = await axios.get("https://api.spotify.com/v1/search", {
headers: {
"Authorization": `Bearer ${authToken}`
},
params: {
q: query,
type: type,
limit: limit,
offset: offset,
}
}).catch((error) => {
console.error(error.response.data)
res.status(error.response.status).json(error.response.data)
return null
})
if (response) {
return res.json(response.data)
}
}
}
},
}
}