mirror of
https://github.com/filecoin-project/slate.git
synced 2024-11-09 20:28:29 +03:00
80 lines
1.6 KiB
JavaScript
80 lines
1.6 KiB
JavaScript
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,
|
|
});
|
|
}
|
|
|
|
return { id: user.id, key, user };
|
|
};
|