22// that writes to the database.
33// This could be either a "create" or an "update".
44
5- var crypto = require ( 'crypto' ) ;
65var deepcopy = require ( 'deepcopy' ) ;
7- var rack = require ( 'hat' ) . rack ( ) ;
86
97var Auth = require ( './Auth' ) ;
108var cache = require ( './cache' ) ;
119var Config = require ( './Config' ) ;
10+ var cryptoUtils = require ( './cryptoUtils' ) ;
1211var passwordCrypto = require ( './password' ) ;
1312var facebook = require ( './facebook' ) ;
1413var Parse = require ( 'parse/node' ) ;
@@ -56,7 +55,7 @@ function RestWrite(config, auth, className, query, data, originalData) {
5655 this . data . updatedAt = this . updatedAt ;
5756 if ( ! this . query ) {
5857 this . data . createdAt = this . updatedAt ;
59- this . data . objectId = newStringId ( 10 ) ;
58+ this . data . objectId = cryptoUtils . newObjectId ( ) ;
6059 }
6160 }
6261}
@@ -252,7 +251,7 @@ RestWrite.prototype.handleFacebookAuthData = function() {
252251 throw new Parse . Error ( Parse . Error . ACCOUNT_ALREADY_LINKED ,
253252 'this auth is already used' ) ;
254253 } else {
255- this . data . username = rack ( ) ;
254+ this . data . username = cryptoUtils . newToken ( ) ;
256255 }
257256
258257 // This FB auth does not already exist, so transform it to a
@@ -273,7 +272,7 @@ RestWrite.prototype.transformUser = function() {
273272 var promise = Promise . resolve ( ) ;
274273
275274 if ( ! this . query ) {
276- var token = 'r:' + rack ( ) ;
275+ var token = 'r:' + cryptoUtils . newToken ( ) ;
277276 this . storage [ 'token' ] = token ;
278277 promise = promise . then ( ( ) => {
279278 var expiresAt = new Date ( ) ;
@@ -319,7 +318,7 @@ RestWrite.prototype.transformUser = function() {
319318 // Check for username uniqueness
320319 if ( ! this . data . username ) {
321320 if ( ! this . query ) {
322- this . data . username = newStringId ( 25 ) ;
321+ this . data . username = cryptoUtils . randomString ( 25 ) ;
323322 }
324323 return ;
325324 }
@@ -412,7 +411,7 @@ RestWrite.prototype.handleSession = function() {
412411 }
413412
414413 if ( ! this . query && ! this . auth . isMaster ) {
415- var token = 'r:' + rack ( ) ;
414+ var token = 'r:' + cryptoUtils . newToken ( ) ;
416415 var expiresAt = new Date ( ) ;
417416 expiresAt . setFullYear ( expiresAt . getFullYear ( ) + 1 ) ;
418417 var sessionData = {
@@ -713,20 +712,4 @@ RestWrite.prototype.objectId = function() {
713712 return this . data . objectId || this . query . objectId ;
714713} ;
715714
716- // Returns a unique string that's usable as an object or other id.
717- function newStringId ( size ) {
718- var chars = ( 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
719- 'abcdefghijklmnopqrstuvwxyz' +
720- '0123456789' ) ;
721- var objectId = '' ;
722- var bytes = crypto . randomBytes ( size ) ;
723- for ( var i = 0 ; i < bytes . length ; ++ i ) {
724- // Note: there is a slight modulo bias, because chars length
725- // of 62 doesn't divide the number of all bytes (256) evenly.
726- // It is acceptable for our purposes.
727- objectId += chars [ bytes . readUInt8 ( i ) % chars . length ] ;
728- }
729- return objectId ;
730- }
731-
732715module . exports = RestWrite ;
0 commit comments