Skip to content
This repository has been archived by the owner on Dec 12, 2018. It is now read-only.

Commit

Permalink
Fixes for custom data strategy
Browse files Browse the repository at this point in the history
* Need to use underscores for namespacing
* Need to convert from flattened keys when constructing accounts
* Need to preserve arrays
  • Loading branch information
Robert Damphousse committed Jun 27, 2017
1 parent 95073a2 commit bb4b28d
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 3 deletions.
46 changes: 43 additions & 3 deletions lib/resource/Account.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var dot = require('dot-object');
var Dot = require('dot-object');
var extend = require('xtend/mutable');
var _ = require('underscore');
var uuid = require('uuid');
Expand All @@ -12,6 +12,44 @@ var getFactorConstructor = FactorInstantiator.getConstructor;
var FactorInstanceCtor = FactorInstantiator.Constructor;
var utils = require('../utils');

/**
* Transforms the output of Dot().object(). By default that library will convert arrays like so:
*
* ['val', 'val2']
*
* becomes..
*
* {
* 'array.0': 'val',
* 'array.1': 'val2'
* }
*
* But we want to preserve arrays, so this function un-does the above.
*/
function dotObjectArrayTransform(obj) {
var r = /(.+)_([0-9])+$/;
Object.keys(obj).forEach(function (key) {
var val = obj[key];
var match = key.match(r);

if (key.match(/stormpathApiKey_/)) {
return;
}

if (val && typeof val === 'object') {
obj[key] = dotObjectArrayTransform(val);
} else if (match) {
var arrayKey = match[1];
if (!obj[arrayKey]) {
obj[arrayKey] = [];
}
obj[arrayKey].push(val);
delete obj[key];
}
});
return obj;
}

/**
* @class Account
*
Expand Down Expand Up @@ -605,7 +643,9 @@ Account.prototype.transformOktaUser = function transformOktaUser() {
};
delete account.profile.emailVerificationToken; // so that the end user can't read it through the /me endpoint
} else {
account.customData[key] = account.profile[key];
var obj = [];
obj[key] = value;
extend(account.customData, new Dot('_').object(obj));
}
return account;
}, account);
Expand Down Expand Up @@ -650,7 +690,7 @@ Account.prototype.toOktaUser = function toOktaUser(configuration) {
if (configuration.customDataStrategy === 'serialize') {
oktaUser.profile.customData = JSON.stringify(value);
} else {
extend(oktaUser.profile, dot.dot(value));
extend(oktaUser.profile, dotObjectArrayTransform(new Dot('_').dot(value)));
}
break;
default:
Expand Down
80 changes: 80 additions & 0 deletions test/okta/custom-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
'use strict';

var Account = require('../../lib/resource/Account');
var common = require('../common');
var assert = common.assert;


describe('Account', function () {
describe('.tranformOktaUser', function () {
it('should map profile data onto custom data', function () {
// toOktaUser is ran when new Accounts are constructed
var account = new Account({
profile: {
prop_a: '1',
nested_property_foo: 'bar'
},
_links: {}
});

assert.deepEqual(account.customData, {
prop: {
a: '1'
},
nested: {
property: {
foo: 'bar'
}
}
});

});
});
describe('.toOktaUser()', function () {
it('should map custom data onto okta profile data properties', function () {
var account = new Account({
_links: {},
profile: {}
});
account.customData.things = [1, 2, 3];
account.customData.simple = 'bar';
account.customData.prop = {
a: '1'
};
account.customData.nested = {
property: {
foo: 'bar'
},
things2: ['a', 'b', 'c']
};
var oktaUser = account.toOktaUser();
assert.deepEqual(oktaUser.profile, {
things: [1, 2, 3],
simple: 'bar',
prop_a: '1',
nested_property_foo: 'bar',
nested_things2: ['a', 'b', 'c']
});
});
});
describe('.toOktaUser({customDataStrategy: stringify})', function () {
it('should map custom data onto okta profile data as a JSON string', function () {
var account = new Account({
_links: {},
profile: {}
});
account.customData.simple = 'bar';
account.customData.prop = {
a: '1'
};
account.customData.nested = {
property: {
foo: 'bar'
},
things2: ['a', 'b', 'c']
};
var oktaUser = account.toOktaUser({ customDataStrategy: 'serialize' });
assert.equal(oktaUser.profile.customData, JSON.stringify(account.customData));
});
});
});

0 comments on commit bb4b28d

Please sign in to comment.