[HOTFIX] Auth Credential Failure check
This commit is contained in:
parent
40f020d27b
commit
626ebf9d1d
2 changed files with 23 additions and 5 deletions
|
@ -16,7 +16,10 @@ const getMclName = (host, id) =>
|
||||||
`${host.toLowerCase().replaceAll(".", "-")}-${id}`;
|
`${host.toLowerCase().replaceAll(".", "-")}-${id}`;
|
||||||
|
|
||||||
export async function checkAuthorization(serverId, cairoId) {
|
export async function checkAuthorization(serverId, cairoId) {
|
||||||
if(!cairoId) return false;
|
console.log(
|
||||||
|
`Checking Authorization for user ${cairoId} for serverId ${serverId}`,
|
||||||
|
);
|
||||||
|
if (!cairoId) return false;
|
||||||
const q = selectWhereAllQuery(table, {
|
const q = selectWhereAllQuery(table, {
|
||||||
id: serverId,
|
id: serverId,
|
||||||
owner_cairo_id: cairoId,
|
owner_cairo_id: cairoId,
|
||||||
|
|
|
@ -9,16 +9,31 @@ const cairoAuthMiddleware = Router();
|
||||||
|
|
||||||
const cairoAuthenticate = async (token) => {
|
const cairoAuthenticate = async (token) => {
|
||||||
const config = { headers: { Authorization: `Bearer ${token}` } };
|
const config = { headers: { Authorization: `Bearer ${token}` } };
|
||||||
return fetch(`${MCL_CAIRO_URL}/api/user/info`, config).then((res) =>
|
return fetch(`${MCL_CAIRO_URL}/api/user/info`, config).then(async (res) => {
|
||||||
res.json(),
|
if (res.status >= 300) {
|
||||||
|
const errorMessage = await res
|
||||||
|
.json()
|
||||||
|
.then((data) => JSON.stringify(data))
|
||||||
|
.catch(() => res.statusText);
|
||||||
|
throw Error(
|
||||||
|
`Could not authenticate with user, receieved message: ${errorMessage}`,
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.json();
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// Middleware
|
// Middleware
|
||||||
const cairoAuthHandler = (req, res, next) => {
|
const cairoAuthHandler = (req, res, next) => {
|
||||||
if (!req.token) return res.status(401).send("Cairo auth required!");
|
if (!req.token) return res.status(401).send("Cairo auth required!");
|
||||||
cairoAuthenticate(req.token)
|
cairoAuthenticate(req.token)
|
||||||
.then((authData) => (req.cairoId = authData.id))
|
.then((authData) => {
|
||||||
|
console.log(authData);
|
||||||
|
if (!authData.id)
|
||||||
|
throw Error(`Cairo didn't return the expected data! ${authData.id}`);
|
||||||
|
req.id = authData.id;
|
||||||
|
})
|
||||||
.then(() => next())
|
.then(() => next())
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
ERR("AUTH", err.response ? err.response.data : err.message);
|
ERR("AUTH", err.response ? err.response.data : err.message);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue