Skip to content

Encrypted Current User in browser #1036

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 7 commits into from
Dec 24, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
769 changes: 475 additions & 294 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"dependencies": {
"@babel/runtime": "7.7.4",
"@babel/runtime-corejs3": "7.7.4",
"crypto-js": "^3.1.9-1",
"uuid": "3.3.3",
"ws": "7.2.0",
"xmlhttprequest": "1.8.0"
Expand Down
4 changes: 3 additions & 1 deletion src/CoreManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,15 @@ const config: Config & { [key: string]: mixed } = {
SERVER_AUTH_TYPE: null,
SERVER_AUTH_TOKEN: null,
LIVEQUERY_SERVER_URL: null,
ENCRYPTED_KEY: null,
VERSION: 'js' + require('../package.json').version,
APPLICATION_ID: null,
JAVASCRIPT_KEY: null,
MASTER_KEY: null,
USE_MASTER_KEY: false,
PERFORM_USER_REWRITE: true,
FORCE_REVOCABLE_SESSION: false
FORCE_REVOCABLE_SESSION: false,
ENCRYPTED_USER: false
};

function requireMethods(name: string, methods: Array<string>, controller: any) {
Expand Down
47 changes: 47 additions & 0 deletions src/Parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,34 @@ Object.defineProperty(Parse, 'liveQueryServerURL', {
CoreManager.set('LIVEQUERY_SERVER_URL', value);
}
});

/**
* @member Parse.encryptedUser
* @type boolean
* @static
*/
Object.defineProperty(Parse, 'encryptedUser', {
get() {
return CoreManager.get('ENCRYPTED_USER');
},
set(value) {
CoreManager.set('ENCRYPTED_USER', value);
}
});

/**
* @member Parse.encryptedKey
* @type string
* @static
*/
Object.defineProperty(Parse, 'encryptedKey', {
get() {
return CoreManager.get('ENCRYPTED_KEY');
},
set(value) {
CoreManager.set('ENCRYPTED_KEY', value);
}
});
/* End setters */

Parse.ACL = require('./ParseACL').default;
Expand Down Expand Up @@ -255,6 +283,25 @@ Parse.dumpLocalDatastore = function() {
return Parse.LocalDatastore._getAllContents();
}
}

/**
* Enable the current user encryption.
* This must be called before login any user.
*
* @static
*/
Parse.enableEncryptedUser = function() {
Parse.encryptedUser = true;
}
/**
* Flag that indicates whether Encrypted User is enabled.
*
* @static
*/
Parse.isEncryptedUserEnabled = function() {
return Parse.encryptedUser;
}

CoreManager.setInstallationController(InstallationController);
CoreManager.setRESTController(RESTController);

Expand Down
16 changes: 15 additions & 1 deletion src/ParseUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import type { RequestOptions, FullOptions } from './RESTController';

export type AuthData = ?{ [key: string]: mixed };

let CryptoJS = null;
if (process.env.PARSE_BUILD === 'browser') {
CryptoJS = require('crypto-js');
}

const CURRENT_USER_KEY = 'currentUser';
let canUseCurrentUser = !CoreManager.get('IS_NODE');
let currentUserCacheMatchesDisk = false;
Expand Down Expand Up @@ -873,7 +878,10 @@ const DefaultController = {

json.className = user.constructor.name === ParseUser.name ? '_User' : user.constructor.name;
return Storage.setItemAsync(
path, JSON.stringify(json)
path,
(!CoreManager.get('ENCRYPTED_USER') && process.env.PARSE_BUILD !== 'browser')
? JSON.stringify(json)
: CryptoJS.AES.encrypt(JSON.stringify(json), CoreManager.get('ENCRYPTED_KEY'))
).then(() => {
return user;
});
Expand Down Expand Up @@ -918,6 +926,9 @@ const DefaultController = {
currentUserCache = null;
return null;
}
if (CoreManager.get('ENCRYPTED_USER') && process.env.PARSE_BUILD === 'browser') {
userData = CryptoJS.AES.decrypt(userData.toString(), CoreManager.get('ENCRYPTED_KEY')).toString(CryptoJS.enc.Utf8);
}
userData = JSON.parse(userData);
if (!userData.className) {
userData.className = '_User';
Expand Down Expand Up @@ -954,6 +965,9 @@ const DefaultController = {
currentUserCache = null;
return Promise.resolve(null);
}
if (CoreManager.get('ENCRYPTED_USER') && process.env.PARSE_BUILD === 'browser') {
userData = CryptoJS.AES.decrypt(userData.toString(), CoreManager.get('ENCRYPTED_KEY')).toString(CryptoJS.enc.Utf8)
}
userData = JSON.parse(userData);
if (!userData.className) {
userData.className = '_User';
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/Cloud-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jest.dontMock('../Cloud');
jest.dontMock('../CoreManager');
jest.dontMock('../decode');
jest.dontMock('../encode');
jest.dontMock('crypto-js');

const Cloud = require('../Cloud');
const CoreManager = require('../CoreManager');
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/Hooks-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jest.dontMock('../ParseHooks');
jest.dontMock('../CoreManager');
jest.dontMock('../decode');
jest.dontMock('../encode');
jest.dontMock('crypto-js');

const Hooks = require('../ParseHooks');
const CoreManager = require('../CoreManager');
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/LiveQueryClient-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jest.dontMock('../ParseACL');
jest.dontMock('../ParseQuery');
jest.dontMock('../LiveQuerySubscription');
jest.dontMock('../LocalDatastore');
jest.dontMock('crypto-js');

jest.useFakeTimers();

Expand Down
1 change: 1 addition & 0 deletions src/__tests__/ObjectStateMutations-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jest.dontMock('../ParseGeoPoint');
jest.dontMock('../ParseOp');
jest.dontMock('../ParseRelation');
jest.dontMock('../TaskQueue');
jest.dontMock('crypto-js');

const mockObject = function(className) {
this.className = className;
Expand Down
15 changes: 15 additions & 0 deletions src/__tests__/Parse-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
jest.dontMock('../CoreManager');
jest.dontMock('../Parse');
jest.dontMock('../LocalDatastore');
jest.dontMock('crypto-js');

const CoreManager = require('../CoreManager');
const Parse = require('../Parse');
Expand Down Expand Up @@ -109,4 +110,18 @@ describe('Parse module', () => {
LDS = await Parse.dumpLocalDatastore();
expect(LDS).toEqual({ key: 'value' });
});

it('can enable encrypter CurrentUser', () => {
jest.spyOn(console, 'log').mockImplementationOnce(() => {});
Parse.encryptedUser = false;
Parse.enableEncryptedUser();
expect(Parse.encryptedUser).toBe(true);
expect(Parse.isEncryptedUserEnabled()).toBe(true);
});

it('can set an encrypt token as String', () => {
Parse.encryptedKey = 'My Super secret key';
expect(CoreManager.get('ENCRYPTED_KEY')).toBe('My Super secret key');
expect(Parse.encryptedKey).toBe('My Super secret key');
});
});
1 change: 1 addition & 0 deletions src/__tests__/ParseACL-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

jest.dontMock('../ParseACL');
jest.dontMock('crypto-js');

const mockRole = function(name) {
this.name = name;
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/ParseConfig-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jest.dontMock('../ParseGeoPoint');
jest.dontMock('../RESTController');
jest.dontMock('../Storage');
jest.dontMock('../StorageController.default');
jest.dontMock('crypto-js');

const CoreManager = require('../CoreManager');
const ParseConfig = require('../ParseConfig').default;
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/ParseLiveQuery-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jest.dontMock('../ParseObject');
jest.dontMock('../ParseQuery');
jest.dontMock('../EventEmitter');
jest.dontMock('../promiseUtils');
jest.dontMock('crypto-js');

// Forces the loading
const LiveQuery = require('../ParseLiveQuery').default;
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/ParseObject-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jest.dontMock('../UniqueInstanceStateController');
jest.dontMock('../unsavedChildren');
jest.dontMock('../ParseACL');
jest.dontMock('../LocalDatastore');
jest.dontMock('crypto-js');

jest.mock('uuid/v4', () => {
let value = 0;
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/ParseOp-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jest.dontMock('../arrayContainsObject');
jest.dontMock('../encode');
jest.dontMock('../ParseOp');
jest.dontMock('../unique');
jest.dontMock('crypto-js');

let localCount = 0;
const mockObject = function(className, id) {
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/ParseQuery-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jest.dontMock('../ObjectStateMutations');
jest.dontMock('../LocalDatastore');
jest.dontMock('../OfflineQuery');
jest.dontMock('../LiveQuerySubscription');
jest.dontMock('crypto-js');

jest.mock('uuid/v4', () => {
let value = 0;
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/ParseRelation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jest.dontMock('../encode');
jest.dontMock('../ParseRelation');
jest.dontMock('../ParseOp');
jest.dontMock('../unique');
jest.dontMock('crypto-js');

const mockStore = {};
const mockObject = function(className) {
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/ParseRole-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jest.dontMock('../ParseOp');
jest.dontMock('../ParseRole');
jest.dontMock('../SingleInstanceStateController');
jest.dontMock('../UniqueInstanceStateController');
jest.dontMock('crypto-js');

const ParseACL = require('../ParseACL').default;
const ParseError = require('../ParseError').default;
Expand Down
23 changes: 23 additions & 0 deletions src/__tests__/ParseUser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jest.dontMock('../StorageController.default');
jest.dontMock('../TaskQueue');
jest.dontMock('../unique');
jest.dontMock('../UniqueInstanceStateController');
jest.dontMock('crypto-js');

jest.mock('uuid/v4', () => {
let value = 0;
Expand Down Expand Up @@ -1009,4 +1010,26 @@ describe('ParseUser', () => {
const authProvider = user.linkWith.mock.calls[0][0];
expect(authProvider).toBe('testProvider');
});

it('can get the current user even encrypted', async () => {
CoreManager.set('ENCRYPTED_USER', true);
CoreManager.set('ENCRYPTED_KEY', 'secret');
let u = new ParseUser();
expect(u.isCurrent()).toBe(false);
expect(u.className).toBe('_User');
expect(u instanceof ParseObject).toBe(true);

u = new ParseUser({
username: 'andrew',
password: 'secret'
});
expect(u.get('username')).toBe('andrew');
expect(u.get('password')).toBe('secret');

expect(function() {
new ParseUser({
$$$: 'invalid'
});
}).toThrow('Can\'t create an invalid Parse User');
});
});
1 change: 1 addition & 0 deletions src/__tests__/SingleInstanceStateController-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jest.dontMock('../ParseGeoPoint');
jest.dontMock('../ParseOp');
jest.dontMock('../SingleInstanceStateController');
jest.dontMock('../TaskQueue');
jest.dontMock('crypto-js');

const mockObject = function() {};
mockObject.registerSubclass = function() {};
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/UniqueInstanceStateController-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jest.dontMock('../ParseOp');
jest.dontMock('../UniqueInstanceStateController');
jest.dontMock('../TaskQueue');
jest.dontMock('../promiseUtils');
jest.dontMock('crypto-js');
jest.useFakeTimers();

const mockObject = function(className) {
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/canBeSerialized-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

jest.dontMock('../canBeSerialized');
jest.dontMock('crypto-js');

function mockObject(id, attributes) {
this.id = id;
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/decode-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
jest.dontMock('../decode');
jest.dontMock('../ParseFile');
jest.dontMock('../ParseGeoPoint');
jest.dontMock('crypto-js');

const decode = require('../decode').default;

Expand Down
1 change: 1 addition & 0 deletions src/__tests__/encode-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jest.dontMock('../encode');
jest.dontMock('../ParseACL');
jest.dontMock('../ParseFile');
jest.dontMock('../ParseGeoPoint');
jest.dontMock('crypto-js');

const mockObject = function(className) {
this.className = className;
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/unsavedChildren-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

jest.dontMock('../ParseFile');
jest.dontMock('../unsavedChildren');
jest.dontMock('crypto-js');

function mockObject({ className, localId, id, attributes, dirty }) {
this.className = className;
Expand Down