mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 15:48:04 +01:00
v3: fallbackto authWithHeaders if wuthWithSession or authWithUrl fails
This commit is contained in:
@@ -40,6 +40,7 @@ describe('GET /challenges/:challengeId/export/csv', () => {
|
||||
|
||||
it('fails if challenge doesn\'t exists', async () => {
|
||||
user = await generateUser();
|
||||
user.get('/user');
|
||||
await expect(user.get(`/challenges/${generateUUID()}/export/csv`)).to.eventually.be.rejected.and.eql({
|
||||
code: 404,
|
||||
error: 'NotFound',
|
||||
@@ -49,6 +50,7 @@ describe('GET /challenges/:challengeId/export/csv', () => {
|
||||
|
||||
it('fails if user doesn\'t have access to the challenge', async () => {
|
||||
user = await generateUser();
|
||||
user.get('/user');
|
||||
|
||||
await expect(user.get(`/challenges/${challenge._id}/export/csv`)).to.eventually.be.rejected.and.eql({
|
||||
code: 404,
|
||||
|
||||
@@ -64,13 +64,6 @@ function _requestMaker (user, method, additionalSets = {}) {
|
||||
return reject(parsedError);
|
||||
}
|
||||
|
||||
// if any cookies was sent, save it for the next request
|
||||
if (response.headers['set-cookie']) {
|
||||
additionalSets.cookie = response.headers['set-cookie'].map(cookieString => {
|
||||
return cookieString.split(';')[0];
|
||||
}).join('; ');
|
||||
}
|
||||
|
||||
resolve(_parseRes(response));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -41,7 +41,14 @@ export function authWithHeaders (optional = false) {
|
||||
export function authWithSession (req, res, next) {
|
||||
let userId = req.session.userId;
|
||||
|
||||
if (!userId) return next(new NotAuthorized(res.t('invalidCredentials')));
|
||||
// Always allow authentication with headers
|
||||
if (!userId) {
|
||||
if (!req.header('x-api-user') || !req.header('x-api-key')) {
|
||||
return next(new NotAuthorized(res.t('invalidCredentials')));
|
||||
} else {
|
||||
return authWithHeaders()(req, res, next);
|
||||
}
|
||||
}
|
||||
|
||||
return User.findOne({
|
||||
_id: userId,
|
||||
@@ -60,8 +67,13 @@ export function authWithUrl (req, res, next) {
|
||||
let userId = req.query._id;
|
||||
let apiToken = req.query.apiToken;
|
||||
|
||||
// Always allow authentication with headers
|
||||
if (!userId || !apiToken) {
|
||||
throw new NotAuthorized(res.t('missingAuthParams'));
|
||||
if (!req.header('x-api-user') || !req.header('x-api-key')) {
|
||||
return next(new NotAuthorized(res.t('missingAuthParams')));
|
||||
} else {
|
||||
return authWithHeaders()(req, res, next);
|
||||
}
|
||||
}
|
||||
|
||||
return User.findOne({ _id: userId, apiToken }).exec()
|
||||
|
||||
Reference in New Issue
Block a user