2021-07-13 22:08:56 +03:00
|
|
|
import * as Data from "~/node_common/data";
|
|
|
|
import * as Strings from "~/common/strings";
|
|
|
|
import * as Utilities from "~/node_common/utilities";
|
|
|
|
|
|
|
|
export const checkAuthorizationInternal = async (req, res) => {
|
|
|
|
const id = Utilities.getIdFromCookie(req);
|
|
|
|
if (!id) {
|
|
|
|
return res.status(401).send({ decorator: "SERVER_NOT_AUTHENTICATED", error: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
const user = await Data.getUserById({
|
|
|
|
id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
return res.status(404).send({
|
|
|
|
decorator: "SERVER_USER_NOT_FOUND",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user.error) {
|
|
|
|
return res.status(500).send({
|
|
|
|
decorator: "SERVER_USER_NOT_FOUND",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return { id, user };
|
|
|
|
};
|
|
|
|
|
|
|
|
export const checkAuthorizationExternal = async (req, res) => {
|
|
|
|
if (Strings.isEmpty(req.headers.authorization)) {
|
|
|
|
return res.status(404).send({
|
|
|
|
decorator: "NO_API_KEY_PROVIDED",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const parsed = Strings.getKey(req.headers.authorization);
|
|
|
|
|
|
|
|
const key = await Data.getAPIKeyByKey({
|
|
|
|
key: parsed,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!key) {
|
|
|
|
return res.status(403).send({
|
|
|
|
decorator: "NO_MATCHING_API_KEY_FOUND",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key.error) {
|
|
|
|
return res.status(500).send({
|
|
|
|
decorator: "ERROR_WHILE_VERIFYING_API_KEY",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const user = await Data.getUserById({
|
|
|
|
id: key.ownerId,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
return res.status(404).send({
|
|
|
|
decorator: "API_KEY_OWNER_NOT_FOUND",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user.error) {
|
|
|
|
return res.status(500).send({
|
|
|
|
decorator: "ERROR_WHILE_LOCATING_API_KEY_OWNER",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-14 01:52:21 +03:00
|
|
|
return { id: user.id, key, user };
|
2021-07-13 22:08:56 +03:00
|
|
|
};
|