use-promise

This commit is contained in:
Untone 2024-03-04 19:25:01 +03:00
parent 311a95ed97
commit f599f49949

150
index.mjs
View File

@ -17,89 +17,91 @@ const server = Server.configure({
connection.requiresAuthentication = true; connection.requiresAuthentication = true;
}, },
onAuthenticate(data) { onAuthenticate(data) {
// console.debug(data) return new Promise((resolve, reject) => {
if (!data.requestHeaders) { const headers = data.requestHeaders
console.error('Request headers not found'); if (!headers) {
return null; console.error('Request headers not found');
} return Promise.reject('required header is not present')
const shout_id = parseInt(data.documentName.replace('shout-', ''), 10) }
console.debug(`shout_id extracted: ${shout_id}`); const shout_id = parseInt(data.documentName.replace('shout-', ''), 10)
console.debug(`shout_id extracted: ${shout_id}`);
const token = data.token || data.requestHeaders['authorization'] || '' const token = data.token || headers['authorization'] || ''
const params = { const params = {
token_type: 'access_token', token_type: 'access_token',
token token
}; };
if (!token) { if (!token) {
console.error('Authorization token not found'); console.error('Authorization token not found');
return null; return Promise.reject('token is not found')
} }
authorizer.validateJWTToken(params) authorizer.validateJWTToken(params)
.then(response => { .then(response => {
if (!response?.data?.is_valid) { if (!response?.data?.is_valid) {
console.error('Invalid authorization token'); console.error('Invalid authorization token');
return null; return Promise.reject('token is invalid')
}
const { sub: user_id, allowed_roles: roles } = response.data.claims
console.debug(`user_id: ${user_id} roles: ${roles}`)
if (roles.includes('editor')) {
return {
id: user_id,
roles: Array.isArray(roles) ? roles : roles.split(',')
} }
}
authorizer.getProfile(params).then((r) => { const { sub: user_id, allowed_roles: roles } = response.data.claims
console.debug(r) console.debug(`user_id: ${user_id} roles: ${roles}`)
const { profile: author } = r.data.app_data
const author_id = author.get('id') if (roles.includes('editor')) {
if(author_id) { return Promise.resolve({
const query = ` id: user_id,
query { roles: Array.isArray(roles) ? roles : roles.split(',')
get_shout(shout_id: $shout_id) {
id
slug
authors
}
}
`;
fetch('https://core.discours.io/graphql', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query, variables: { shout_id } }),
}) })
.then(res => res.json())
.then(data => {
console.debug(data)
const { authors } = data.get_shout;
if (authors.includes(author_id)) {
return {
id: user_id,
author: author_id,
roles: Array.isArray(roles) ? roles : roles.split(','),
};
}
return null;
})
.catch(e => {
console.error('Error fetching shout data:', e.message);
console.error(e.stack);
return null;
});
} }
authorizer.getProfile(params).then((r) => {
console.debug(r)
const { profile: author } = r.data.app_data
const author_id = author.get('id')
if(author_id) {
const query = `
query {
get_shout(shout_id: $shout_id) {
id
slug
authors
}
}
`;
fetch('https://core.discours.io/graphql', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query, variables: { shout_id } }),
})
.then(res => res.json())
.then(data => {
console.debug(data)
const { authors } = data.get_shout;
if (authors.includes(author_id)) {
return {
id: user_id,
author: author_id,
roles: Array.isArray(roles) ? roles : roles.split(','),
};
}
return Promise.reject('not in authors list')
})
.catch(e => {
console.error('Error fetching shout data:', e.message);
console.error(e.stack);
return Promise.reject('error fetching shout data')
});
}
})
}) })
}) .catch(e => {
.catch(e => { console.error('Error validating authorization token:', e.message);
console.error('Error validating authorization token:', e.message); console.error(e.stack);
console.error(e.stack); return Promise.reject('token is invalid')
return null; });
}); })
}, }
}); });
server.listen().then(r => console.info('started')); server.listen().then(r => console.info('started'));