Skip to content

Commit 05acb01

Browse files
committed
Return 401 instead and fix some tests
1 parent 7d4a72c commit 05acb01

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

lib/api/middlewares/authorization.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ module.exports = function authorization (metadataBackend, forceToBeMaster = fals
88
const { user } = res.locals;
99
const credentials = getCredentialsFromRequest(req);
1010

11-
if (!userMatches(credentials, user) || !credentials.apiKeyToken) {
11+
if (!userMatches(credentials, user)) {
1212
req.profiler.done('authorization');
1313

1414
return next(new Error('permission denied'));
15+
} else if (!credentials.apiKeyToken) {
16+
req.profiler.done('authorization');
17+
18+
return next(new Error('unauthorized'));
1519
}
1620

1721
res.locals.api_key = credentials.apiKeyToken;

lib/services/error-handler.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class ErrorHandler extends Error {
3232
getHttpStatus (httpStatus = 400) {
3333
if (this.message.includes('permission denied')) {
3434
return 403;
35+
} else if (this.message.includes('unauthorized')) {
36+
return 401;
3537
}
3638

3739
return httpStatus;

test/acceptance/app-auth-test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ var assert = require('../support/assert');
88
describe('app.auth', function () {
99
var scenarios = [
1010
{
11-
desc: 'no api key should fallback to default api key',
11+
desc: 'no api key should return 401',
1212
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4',
13-
statusCode: 200
13+
statusCode: 401
1414
},
1515
{
1616
desc: 'invalid api key should return 401',
@@ -35,12 +35,12 @@ describe('app.auth', function () {
3535
{
3636
desc: 'no api key should NOT allow insert in protected tables',
3737
url: "/api/v1/sql?q=INSERT%20INTO%20private_table%20(name)%20VALUES%20('RAMBO')",
38-
statusCode: 403
38+
statusCode: 401
3939
},
4040
{
4141
desc: 'no api key should NOT allow insert in public tables',
4242
url: "/api/v1/sql?q=INSERT%20INTO%20untitle_table_4%20(name)%20VALUES%20('RAMBO')",
43-
statusCode: 403
43+
statusCode: 401
4444
}
4545
];
4646

0 commit comments

Comments
 (0)