add get api key and check link extension endpoints

This commit is contained in:
Martina 2021-09-14 15:58:00 -07:00
parent d29d770cd7
commit 8be6c257b4
5 changed files with 105 additions and 10 deletions

View File

@ -20,6 +20,7 @@ import updateTwitterToken from "~/node_common/data/methods/update-twitter-token"
// File postgres queries
import createFile from "~/node_common/data/methods/create-file";
import getFileByCid from "~/node_common/data/methods/get-file-by-cid";
import getFileByUrl from "~/node_common/data/methods/get-file-by-url";
import getFilesByCids from "~/node_common/data/methods/get-files-by-cids";
import getFileById from "~/node_common/data/methods/get-file-by-id";
import getFilesByIds from "~/node_common/data/methods/get-files-by-ids";
@ -114,6 +115,7 @@ export {
//NOTE(martina): File operations
createFile,
getFileByCid,
getFileByUrl,
getFilesByCids,
getFileById,
getFilesByIds,

View File

@ -0,0 +1,28 @@
import * as Serializers from "~/node_common/serializers";
import { runQuery } from "~/node_common/data/utilities";
export default async ({ ownerId, url, sanitize = false }) => {
return await runQuery({
label: "GET_FILE_BY_URL",
queryFn: async (DB) => {
let query = await DB.select("*").from("files").where({ ownerId, url }).first();
if (!query || query.error) {
return null;
}
if (sanitize) {
query = Serializers.sanitizeFile(query);
}
return JSON.parse(JSON.stringify(query));
},
errorFn: async (e) => {
return {
error: true,
decorator: "GET_FILE_BY_URL",
};
},
});
};

View File

@ -58,6 +58,19 @@ export const checkTextile = async () => {
return false;
};
export const getIdFromCookieValue = (token) => {
if (!Strings.isEmpty(token)) {
try {
const decoded = JWT.verify(token, Environment.JWT_SECRET);
id = decoded.id;
} catch (e) {
Logging.error(e.message);
}
}
return id;
};
export const getIdFromCookie = (req) => {
let id = null;
if (Strings.isEmpty(req.headers.cookie)) {
@ -69,16 +82,7 @@ export const getIdFromCookie = (req) => {
"$1"
);
if (!Strings.isEmpty(token)) {
try {
const decoded = JWT.verify(token, Environment.JWT_SECRET);
id = decoded.id;
} catch (e) {
Logging.error(e.message);
}
}
return id;
return getIdFromCookieValue(token);
};
export const encryptWithSecret = async (text, secret) => {

View File

@ -0,0 +1,26 @@
import * as Data from "~/node_common/data";
import * as RequestUtilities from "~/node_common/request-utilities";
export default async (req, res) => {
const userInfo = await RequestUtilities.checkAuthorizationExternal(req, res);
if (!userInfo) return;
const { id, key, user } = userInfo;
const url = req.body.data.url;
if (!url) {
return res.status(400).send({ decorator: "SERVER_CHECK_LINK_NO_LINK_PROVIDED", error: true });
}
const existingLink = await Data.getFileByUrl({ ownerId: user.id, url });
if (existingLink) {
return res.status(200).send({
decorator: "LINK_FOUND",
data: existingLink,
});
} else {
return res.status(200).send({
decorator: "LINK_NOT_FOUND",
});
}
};

View File

@ -0,0 +1,35 @@
import * as Data from "~/node_common/data";
import * as Utilities from "~/node_common/utilities";
export default async (req, res) => {
let token = req.body.data?.token;
if (!token) {
return res.status(400).send({ decorator: "NO_TOKEN_PROVIDED", error: true });
}
const id = Utilities.getIdFromCookieValue(token);
if (!id) {
return res.status(401).send({ decorator: "SERVER_NOT_AUTHENTICATED", error: true });
}
const keys = await Data.getAPIKeysByUserId({
userId: id,
});
if (!keys) {
return res.status(404).send({
decorator: "SERVER_USER_NOT_FOUND",
error: true,
});
}
if (keys.error) {
return res.status(500).send({
decorator: "SERVER_USER_NOT_FOUND",
error: true,
});
}
return res.status(200).send({ decorator: "SERVER_GET_API_KEYS", data: keys });
};