Skip to content

Commit 03108e6

Browse files
hmoqhimdrew-gross
authored andcommitted
add support for http basic auth (#1706)
* add support for http basic auth * update http auth per flovilmart feedback
1 parent 4d43614 commit 03108e6

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

spec/index.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,30 @@ describe('server', () => {
1212
expect(setServerConfiguration.bind(undefined, { appId: 'myId', masterKey: 'mk' })).toThrow('You must provide a serverURL!');
1313
done();
1414
});
15+
16+
it('support http basic authentication with masterkey', done => {
17+
request.get({
18+
url: 'http://localhost:8378/1/classes/TestObject',
19+
headers: {
20+
'Authorization': 'Basic ' + new Buffer('test:' + 'test').toString('base64')
21+
}
22+
}, (error, response, body) => {
23+
expect(response.statusCode).toEqual(200);
24+
done();
25+
});
26+
});
27+
28+
it('support http basic authentication with javascriptKey', done => {
29+
request.get({
30+
url: 'http://localhost:8378/1/classes/TestObject',
31+
headers: {
32+
'Authorization': 'Basic ' + new Buffer('test:javascript-key=' + 'test').toString('base64')
33+
}
34+
}, (error, response, body) => {
35+
expect(response.statusCode).toEqual(200);
36+
done();
37+
});
38+
});
1539

1640
it('fails if database is unreachable', done => {
1741
setServerConfiguration({

src/middlewares.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ function handleParseHeaders(req, res, next) {
2727
dotNetKey: req.get('X-Parse-Windows-Key'),
2828
restAPIKey: req.get('X-Parse-REST-API-Key')
2929
};
30+
31+
var basicAuth = httpAuth(req);
32+
33+
if (basicAuth) {
34+
info.appId = basicAuth.appId
35+
info.masterKey = basicAuth.masterKey || info.masterKey;
36+
info.javascriptKey = basicAuth.javascriptKey || info.javascriptKey;
37+
}
3038

3139
if (req.body) {
3240
// Unity SDK sends a _noBody key which needs to be removed.
@@ -144,6 +152,45 @@ function handleParseHeaders(req, res, next) {
144152
});
145153
}
146154

155+
function httpAuth(req) {
156+
if (!(req.req || req).headers.authorization)
157+
return ;
158+
159+
var header = (req.req || req).headers.authorization;
160+
var appId, masterKey, javascriptKey;
161+
162+
// parse header
163+
var authPrefix = 'basic ';
164+
165+
var match = header.toLowerCase().indexOf(authPrefix);
166+
167+
if (match == 0) {
168+
var encodedAuth = header.substring(authPrefix.length, header.length);
169+
var credentials = decodeBase64(encodedAuth).split(':');
170+
171+
if (credentials.length == 2) {
172+
appId = credentials[0];
173+
var key = credentials[1];
174+
175+
var jsKeyPrefix = 'javascript-key=';
176+
177+
var matchKey = key.indexOf(jsKeyPrefix)
178+
if (matchKey == 0) {
179+
javascriptKey = key.substring(jsKeyPrefix.length, key.length);
180+
}
181+
else {
182+
masterKey = key;
183+
}
184+
}
185+
}
186+
187+
return {appId: appId, masterKey: masterKey, javascriptKey: javascriptKey};
188+
}
189+
190+
function decodeBase64(str) {
191+
return new Buffer(str, 'base64').toString()
192+
}
193+
147194
var allowCrossDomain = function(req, res, next) {
148195
res.header('Access-Control-Allow-Origin', '*');
149196
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');

0 commit comments

Comments
 (0)