Skip to content

add support for http basic auth #1706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions spec/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,30 @@ describe('server', () => {
expect(setServerConfiguration.bind(undefined, { appId: 'myId', masterKey: 'mk' })).toThrow('You must provide a serverURL!');
done();
});

it('support http basic authentication with masterkey', done => {
request.get({
url: 'http://localhost:8378/1/classes/TestObject',
headers: {
'Authorization': 'Basic ' + new Buffer('test:' + 'test').toString('base64')
}
}, (error, response, body) => {
expect(response.statusCode).toEqual(200);
done();
});
});

it('support http basic authentication with javascriptKey', done => {
request.get({
url: 'http://localhost:8378/1/classes/TestObject',
headers: {
'Authorization': 'Basic ' + new Buffer('test:javascript-key=' + 'test').toString('base64')
}
}, (error, response, body) => {
expect(response.statusCode).toEqual(200);
done();
});
});

it('fails if database is unreachable', done => {
setServerConfiguration({
Expand Down
47 changes: 47 additions & 0 deletions src/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ function handleParseHeaders(req, res, next) {
dotNetKey: req.get('X-Parse-Windows-Key'),
restAPIKey: req.get('X-Parse-REST-API-Key')
};

var basicAuth = httpAuth(req);

if (basicAuth) {
info.appId = basicAuth.appId
info.masterKey = basicAuth.masterKey || info.masterKey;
info.javascriptKey = basicAuth.javascriptKey || info.javascriptKey;
}

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

function httpAuth(req) {
if (!(req.req || req).headers.authorization)
return ;

var header = (req.req || req).headers.authorization;
var appId, masterKey, javascriptKey;

// parse header
var authPrefix = 'basic ';

var match = header.toLowerCase().indexOf(authPrefix);

if (match == 0) {
var encodedAuth = header.substring(authPrefix.length, header.length);
var credentials = decodeBase64(encodedAuth).split(':');

if (credentials.length == 2) {
appId = credentials[0];
var key = credentials[1];

var jsKeyPrefix = 'javascript-key=';

var matchKey = key.indexOf(jsKeyPrefix)
if (matchKey == 0) {
javascriptKey = key.substring(jsKeyPrefix.length, key.length);
}
else {
masterKey = key;
}
}
}

return {appId: appId, masterKey: masterKey, javascriptKey: javascriptKey};
}

function decodeBase64(str) {
return new Buffer(str, 'base64').toString()
}

var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
Expand Down