Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function isUnauthorizedDueToExpiredAccessToken(response) {
// There are three cases to consider:
// 1) The response body is a Buffer. This indicates that the request was malformed (i.e. malformed url) so return false.
// 2) The status code is UNAUTHORIZED and the response body is an empty object or null. This indicates that the access tokens are expired, so return true.
// 3) The status code is UNAUTHORIZED and the response body is a non-empty object. This indicates that the 401 was returned for some reason other
// than expired tokens, so return false.
if (Buffer.isBuffer(response.body)) {
return false;
}
var isResponseStatusCodeUnauthorized = response.statusCode === httpStatusCodes.UNAUTHORIZED,
isResponseBodyEmpty = !response.body || Object.getOwnPropertyNames(response.body).length === 0;
return isResponseStatusCodeUnauthorized && isResponseBodyEmpty;
}
.catch(() => res.sendStatus(HttpStatus.UNAUTHORIZED));
} else {
static get status() {
return status.UNAUTHORIZED;
}
}
update: async (req, res, next) => {
try {
const job = await Job
.findById(req.params.jobId);
if (!job) {
return next(new APIError('Job not found', httpStatus.NOT_FOUND));
}
if (job.postedUser.toString() !== req.user._id.toString()) {
return next(new APIError('Not allowed to update a job you did not post', httpStatus.UNAUTHORIZED));
}
const today = new Date().getDate();
if (job.isDeleted || (job.expirationDate && job.expirationDate < today)) {
return next(new APIError('Not allowed to update this job as it has been deleted', httpStatus.FORBIDDEN));
}
const updated = Object.assign(job, req.body);
await updated.save();
return res.status(httpStatus.OK).json(transform(updated, true));
} catch (err) {
return next(err);
}
},
async findAndGenerateToken(options) {
const { email, password, refreshObject } = options;
if (!email) throw new APIError({ message: 'An email is required to generate a token' });
const user = await this.findOne({ email }).exec();
const err = {
status: httpStatus.UNAUTHORIZED,
isPublic: true,
};
if (password) {
if (user && await user.passwordMatches(password)) {
return { user, accessToken: user.token() };
}
err.message = 'Incorrect email or password';
} else if (refreshObject && refreshObject.userEmail === email) {
return { user, accessToken: user.token() };
} else {
err.message = 'Incorrect email or refreshToken';
}
throw new APIError(err);
},
static get status() {
return status.UNAUTHORIZED;
}
}
function UnauthorizedError(error) {
Error.call(this, error.message);
Error.captureStackTrace(this, this.constructor);
this.name = 'UnauthorizedError';
this.message = error.message;
this.status = httpStatus.UNAUTHORIZED;
this.inner = error;
}
}, (error) => {
if (error.message.indexOf('Network Error') >= 0) {
Vue.prototype.$notify.error('Network error, please check your network settings!')
} else if (error.response.status === HttpStatus.UNAUTHORIZED) {
localStorage.removeItem('token')
localStorage.removeItem('username')
Vue.prototype.$notify.error('Authorized failed,please login.')
} else if (error.response.status >= HttpStatus.BAD_REQUEST) {
Vue.prototype.$notify.error(error.response.data.message)
}
})
const refreshAuthTokens = async refreshToken => {
try {
const refreshTokenDoc = await tokenService.verifyToken(refreshToken, 'refresh');
const userId = refreshTokenDoc.user;
await userService.getUserById(userId);
await refreshTokenDoc.remove();
return await generateAuthTokens(userId);
} catch (error) {
throw new AppError(httpStatus.UNAUTHORIZED, 'Please authenticate');
}
};
function authFail(req, res) {
res.sendStatus(HTTPStatus.UNAUTHORIZED);
}
exports.authFail = authFail;