Skip to content

Commit

Permalink
Merge pull request #261 from auth0/check-standard-fields
Browse files Browse the repository at this point in the history
Add standard fields to parseHash and normalize responses to camelCase
  • Loading branch information
hzalaz authored Dec 7, 2016
2 parents e4af5b0 + 1afbac5 commit 28fd424
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 69 deletions.
2 changes: 1 addition & 1 deletion src/helper/error.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function buildResponse(error, description) {
return {
error: error,
error_description: description
errorDescription: description
};
}

Expand Down
20 changes: 19 additions & 1 deletion src/helper/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,36 @@ function camelToSnake(str) {
return newKey;
}

function snakeToCamel(str) {
var parts = str.split('_');
return parts.reduce(function (p, c) {
return p + c.charAt(0).toUpperCase() + c.slice(1);
}, parts.shift());
}

function toSnakeCase(object, exceptions) {
exceptions = exceptions || [];

return Object.keys(object).reduce(function (p, key) {
var newKey = exceptions.indexOf(key) === -1 ? camelToSnake(key) : key;
p[newKey] = object[key];
p[newKey] = typeof(object[key]) === 'object' ? toSnakeCase(object[key]) : object[key];
return p;
}, {});
}

function toCamelCase(object, exceptions) {
exceptions = exceptions || [];

return Object.keys(object).reduce(function (p, key) {
var newKey = exceptions.indexOf(key) === -1 ? snakeToCamel(key) : key;
p[newKey] = typeof(object[key]) === 'object' ? toCamelCase(object[key]) : object[key];
return p;
}, {});
}

module.exports = {
toSnakeCase: toSnakeCase,
toCamelCase: toCamelCase,
blacklist: blacklist,
merge: merge,
pick: pick,
Expand Down
7 changes: 4 additions & 3 deletions src/helper/response-handler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var error = require('./error');
var objectHelper = require('./object');

function wrapCallback(cb) {
return function (err, data) {
Expand All @@ -14,11 +15,11 @@ function wrapCallback(cb) {
};

if (err.response && err.response.statusCode) {
errObj.status_code = err.response.statusCode;
errObj.statusCode = err.response.statusCode;
}

if (err.response && err.response.statusText) {
errObj.status_text = err.response.statusText;
errObj.statusText = err.response.statusText;
}

if (err.response && err.response.body) {
Expand All @@ -39,7 +40,7 @@ function wrapCallback(cb) {
return cb(errObj);
}

return cb(null, data.body || data.text || data);
return cb(null, objectHelper.toCamelCase(data.body || data.text || data));
};
}

Expand Down
8 changes: 5 additions & 3 deletions src/web-auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,14 @@ WebAuth.prototype.parseHash = function (hash, options) {
}

return {
accessToken: parsedQs.access_token,
accessToken: parsedQs.access_token || null,
idToken: parsedQs.id_token || null,
idTokenPayload: token && token.payload ? token.payload : null,
appStatus: token ? token.appStatus || null : null,
refreshToken: parsedQs.refresh_token,
state: parsedQs.state
refreshToken: parsedQs.refresh_token || null,
state: parsedQs.state || null,
expiresIn: parsedQs.expires_in || null,
tokenType: parsedQs.token_type || null
};
};

Expand Down
14 changes: 7 additions & 7 deletions test/authentication/authentication.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ describe('auth0.authentication', function () {
this.auth0.userInfo('abcd1234', function(err, data) {
expect(err).to.be(null);
expect(data).to.eql({
user_id: '...',
userId: '...',
provider: 'auth0',
connection: 'Username-Password-Authentication',
isSocial: false
Expand Down Expand Up @@ -261,9 +261,9 @@ describe('auth0.authentication', function () {
}, function(err, data) {
expect(err).to.be(null);
expect(data).to.eql({
'token_type': 'Bearer',
'expires_in': 36000,
'id_token': 'eyJ...'
'tokenType': 'Bearer',
'expiresIn': 36000,
'idToken': 'eyJ...'
});
done();
})
Expand Down Expand Up @@ -351,9 +351,9 @@ describe('auth0.authentication', function () {
}, function(err, data) {
expect(err).to.be(null);
expect(data).to.eql({
'token_type': 'Bearer',
'expires_in': 36000,
'id_token': 'eyJ...'
'tokenType': 'Bearer',
'expiresIn': 36000,
'idToken': 'eyJ...'
});
done();
})
Expand Down
12 changes: 6 additions & 6 deletions test/authentication/ro.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ describe('auth0.authentication', function () {
}, function (err, data) {
expect(err).to.be(null);
expect(data).to.eql({
id_token: 'id_token.id_token.id_token',
access_token: 'access_token',
token_type: 'bearer'
idToken: 'id_token.id_token.id_token',
accessToken: 'access_token',
tokenType: 'bearer'
});
done();
});
Expand Down Expand Up @@ -147,9 +147,9 @@ describe('auth0.authentication', function () {
}, function (err, data) {
expect(err).to.be(null);
expect(data).to.eql({
id_token: 'id_token.id_token.id_token',
access_token: 'access_token',
token_type: 'bearer'
idToken: 'id_token.id_token.id_token',
accessToken: 'access_token',
tokenType: 'bearer'
});
done();
});
Expand Down
6 changes: 4 additions & 2 deletions test/helper/iframe-handler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ describe('helpers iframeHandler', function () {
},
appStatus: null,
refreshToken: 'kajshdgfkasdjhgfas',
state: 'theState'
state: 'theState',
expiresIn: null,
tokenType: 'Bearer'
});

done();
Expand All @@ -155,7 +157,7 @@ describe('helpers iframeHandler', function () {

expect(err).to.eql({
error:'some_error',
error_description: 'the+error+description'
errorDescription: 'the+error+description'
});

done();
Expand Down
76 changes: 73 additions & 3 deletions test/helper/object.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,21 +276,33 @@ describe('helpers', function () {
var object = {
attrName1: 'attribute_1',
attrName22: 'attribute_2',
attrNAME3: 'attribute_3'
attrNAME3: 'attribute_3',
someObj: {
objAtt1: 'asd',
objAtt2: '123'
}
};

var newObject = objectHelper.toSnakeCase(object);

expect(object).to.eql({
attrName1: 'attribute_1',
attrName22: 'attribute_2',
attrNAME3: 'attribute_3'
attrNAME3: 'attribute_3',
someObj: {
objAtt1: 'asd',
objAtt2: '123'
}
});

expect(newObject).to.eql({
attr_name_1: 'attribute_1',
attr_name_22: 'attribute_2',
attr_name_3: 'attribute_3'
attr_name_3: 'attribute_3',
some_obj: {
obj_att_1: 'asd',
obj_att_2: '123'
}
});
});

Expand All @@ -316,4 +328,62 @@ describe('helpers', function () {
});
});
});

describe('toCamelCase', function () {
it('should change the casing to all the attributes', function () {
var object = {
attr_name_1: 'attribute_1',
attr_name_22: 'attribute_2',
attr__name_3: 'attribute_3',
some_obj: {
obj_att_1: 'asdf',
obj_att_2: '1234'
}
};

var newObject = objectHelper.toCamelCase(object);

expect(object).to.eql({
attr_name_1: 'attribute_1',
attr_name_22: 'attribute_2',
attr__name_3: 'attribute_3',
some_obj: {
obj_att_1: 'asdf',
obj_att_2: '1234'
}
});

expect(newObject).to.eql({
attrName1: 'attribute_1',
attrName22: 'attribute_2',
attrName3: 'attribute_3',
someObj: {
objAtt1: 'asdf',
objAtt2: '1234'
}
});
});

it('should change the casing to all the attributes that are not blacklisted', function () {
var object = {
attr_name_1: 'attribute_1',
attr_name_22: 'attribute_2',
attr__name_3: 'attribute_3'
};

var newObject = objectHelper.toCamelCase(object, ['attr_name_22']);

expect(object).to.eql({
attr_name_1: 'attribute_1',
attr_name_22: 'attribute_2',
attr__name_3: 'attribute_3'
});

expect(newObject).to.eql({
attrName1: 'attribute_1',
attr_name_22: 'attribute_2',
attrName3: 'attribute_3'
});
});
});
});
6 changes: 3 additions & 3 deletions test/helper/response-handler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('helpers responseHandler', function () {
expect(data).to.be(undefined);
expect(err).to.eql({
error: 'generic_error',
error_description: 'Something went wrong'
errorDescription: 'Something went wrong'
});
done();
})(null, null);
Expand All @@ -31,8 +31,8 @@ describe('helpers responseHandler', function () {
expect(data).to.be(undefined);
expect(err).to.eql({
original: assert_err,
status_code: 400,
status_text: 'Bad request',
statusCode: 400,
statusText: 'Bad request',
code: 'the_error_code',
description: 'The error description.',
name: 'SomeName'
Expand Down
18 changes: 9 additions & 9 deletions test/management/management.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('auth0.Management', function () {
this.auth0.getUser('auth0|123', function(err, user) {
expect(err).to.be(null);
expect(user).to.eql({
'user_id': 'auth0|123',
'userId': 'auth0|123',
'email': 'me@example.com'
});
done();
Expand Down Expand Up @@ -182,9 +182,9 @@ describe('auth0.Management', function () {
this.auth0.patchUserMetadata('auth0|123', {role:'admin'}, function(err, user) {
expect(err).to.be(null);
expect(user).to.eql({
'user_id': 'auth0|123',
'userId': 'auth0|123',
'email': 'me@example.com',
'user_metadata': { role: 'admin' }
'userMetadata': { role: 'admin' }
});
done();
})
Expand Down Expand Up @@ -281,20 +281,20 @@ describe('auth0.Management', function () {
expect(err).to.be(null);
expect(user).to.eql([{
'connection': 'twitter',
'user_id': '191919191919191',
'userId': '191919191919191',
'provider': 'twitter',
'profileData': {
'email': '',
'email_verified': false,
'emailVerified': false,
'name': '',
'username': 'johndoe',
'given_name': '',
'givenName': '',
'phoneNumber': '',
'phone_verified': false,
'family_name': ''
'phoneVerified': false,
'familyName': ''
},
'isSocial': false,
'access_token': ''
'accessToken': ''
}]);
done();
})
Expand Down
Loading

0 comments on commit 28fd424

Please sign in to comment.