Skip to content

Commit f40bb09

Browse files
committed
Use middleware instead of custom checks inside SchemasRouter.
1 parent b90f3c9 commit f40bb09

File tree

2 files changed

+13
-37
lines changed

2 files changed

+13
-37
lines changed

spec/schemas.spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ describe('schemas', () => {
9898
json: true,
9999
headers: restKeyHeaders,
100100
}, (error, response, body) => {
101-
expect(response.statusCode).toEqual(401);
102-
expect(body.error).toEqual('master key not specified');
101+
expect(response.statusCode).toEqual(403);
102+
expect(body.error).toEqual('unauthorized: master key is required');
103103
done();
104104
});
105105
});
@@ -110,8 +110,8 @@ describe('schemas', () => {
110110
json: true,
111111
headers: restKeyHeaders,
112112
}, (error, response, body) => {
113-
expect(response.statusCode).toEqual(401);
114-
expect(body.error).toEqual('master key not specified');
113+
expect(response.statusCode).toEqual(403);
114+
expect(body.error).toEqual('unauthorized: master key is required');
115115
done();
116116
});
117117
});
@@ -206,8 +206,8 @@ describe('schemas', () => {
206206
className: 'MyClass',
207207
},
208208
}, (error, response, body) => {
209-
expect(response.statusCode).toEqual(401);
210-
expect(body.error).toEqual('master key not specified');
209+
expect(response.statusCode).toEqual(403);
210+
expect(body.error).toEqual('unauthorized: master key is required');
211211
done();
212212
});
213213
});

src/Routers/SchemasRouter.js

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,7 @@ var express = require('express'),
55
Schema = require('../Schema');
66

77
import PromiseRouter from '../PromiseRouter';
8-
9-
// TODO: refactor in a SchemaController at one point...
10-
function masterKeyRequiredResponse() {
11-
return Promise.resolve({
12-
status: 401,
13-
response: {error: 'master key not specified'},
14-
})
15-
}
8+
import * as middleware from "../middlewares";
169

1710
function classNameMismatchResponse(bodyClass, pathClass) {
1811
return Promise.resolve({
@@ -45,9 +38,6 @@ function mongoSchemaToSchemaAPIResponse(schema) {
4538
}
4639

4740
function getAllSchemas(req) {
48-
if (!req.auth.isMaster) {
49-
return masterKeyRequiredResponse();
50-
}
5141
return req.config.database.collection('_SCHEMA')
5242
.then(coll => coll.find({}).toArray())
5343
.then(schemas => ({response: {
@@ -56,9 +46,6 @@ function getAllSchemas(req) {
5646
}
5747

5848
function getOneSchema(req) {
59-
if (!req.auth.isMaster) {
60-
return masterKeyRequiredResponse();
61-
}
6249
return req.config.database.collection('_SCHEMA')
6350
.then(coll => coll.findOne({'_id': req.params.className}))
6451
.then(schema => ({response: mongoSchemaToSchemaAPIResponse(schema)}))
@@ -72,9 +59,6 @@ function getOneSchema(req) {
7259
}
7360

7461
function createSchema(req) {
75-
if (!req.auth.isMaster) {
76-
return masterKeyRequiredResponse();
77-
}
7862
if (req.params.className && req.body.className) {
7963
if (req.params.className != req.body.className) {
8064
return classNameMismatchResponse(req.body.className, req.params.className);
@@ -100,10 +84,6 @@ function createSchema(req) {
10084
}
10185

10286
function modifySchema(req) {
103-
if (!req.auth.isMaster) {
104-
return masterKeyRequiredResponse();
105-
}
106-
10787
if (req.body.className && req.body.className != req.params.className) {
10888
return classNameMismatchResponse(req.body.className, req.params.className);
10989
}
@@ -168,10 +148,6 @@ var removeJoinTables = (database, mongoSchema) => {
168148
};
169149

170150
function deleteSchema(req) {
171-
if (!req.auth.isMaster) {
172-
return masterKeyRequiredResponse();
173-
}
174-
175151
if (!Schema.classNameIsValid(req.params.className)) {
176152
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, Schema.invalidClassNameMessage(req.params.className));
177153
}
@@ -214,11 +190,11 @@ function deleteSchema(req) {
214190

215191
export class SchemasRouter extends PromiseRouter {
216192
mountRoutes() {
217-
this.route('GET', '/schemas', getAllSchemas);
218-
this.route('GET', '/schemas/:className', getOneSchema);
219-
this.route('POST', '/schemas', createSchema);
220-
this.route('POST', '/schemas/:className', createSchema);
221-
this.route('PUT', '/schemas/:className', modifySchema);
222-
this.route('DELETE', '/schemas/:className', deleteSchema);
193+
this.route('GET', '/schemas', middleware.promiseEnforceMasterKeyAccess, getAllSchemas);
194+
this.route('GET', '/schemas/:className', middleware.promiseEnforceMasterKeyAccess, getOneSchema);
195+
this.route('POST', '/schemas', middleware.promiseEnforceMasterKeyAccess, createSchema);
196+
this.route('POST', '/schemas/:className', middleware.promiseEnforceMasterKeyAccess, createSchema);
197+
this.route('PUT', '/schemas/:className', middleware.promiseEnforceMasterKeyAccess, modifySchema);
198+
this.route('DELETE', '/schemas/:className', middleware.promiseEnforceMasterKeyAccess, deleteSchema);
223199
}
224200
}

0 commit comments

Comments
 (0)