Commit a12e8dc4 authored by Kamal Thennakoon's avatar Kamal Thennakoon

validate git users

parent 83c0705f
const { checkUserValidity } = require("../services/fetchers/stats-fetcher");
const blacklist = require("../services/common/blacklist");
const { clampValue, CONSTANTS } = require("../services/common/utils");
require("dotenv").config();
const allowCors = fn => async (req, res) => {
res.setHeader('Access-Control-Allow-Credentials', true)
res.setHeader('Access-Control-Allow-Origin', '*')
// another common pattern
// res.setHeader('Access-Control-Allow-Origin', req.headers.origin);
res.setHeader('Access-Control-Allow-Methods', 'GET,OPTIONS,PATCH,DELETE,POST,PUT')
res.setHeader(
'Access-Control-Allow-Headers',
'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version'
)
if (req.method === 'OPTIONS') {
res.status(200).end()
return
}
return await fn(req, res)
}
// check git user exists
const checkUserExists = async (req, res) => {
const { username,cache_seconds } = req.query;
console.log('check user validity...');
if (blacklist.includes(username)) {
return res.send(renderError("User has been blacklisted"));
}
try{
user=await checkUserValidity(username);
const cacheSeconds = clampValue(
parseInt(cache_seconds || CONSTANTS.TWO_HOURS, 10),
CONSTANTS.TWO_HOURS,
CONSTANTS.ONE_DAY,
);
res.setHeader("Cache-Control", `public, max-age=${cacheSeconds}`);
return res.status(200).json({user:user});
}catch(err){
console.log('Error:',err);
return res.status(301).json(err);
}
};
module.exports = allowCors(checkUserExists)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment