@@ -42,16 +42,12 @@ class EntryStorage {
4242 return newData ;
4343 }
4444
45- private static isOTPStorage ( entry : object ) {
46- const properties = [
47- 'account' , 'hash' , 'index' , 'issuer' , 'type' , 'counter' , 'secret' ,
48- 'encrypted'
49- ] ;
50- for ( let i = 0 ; i < properties . length ; i ++ ) {
51- if ( ! entry . hasOwnProperty ( properties [ i ] ) ) {
52- return false ;
53- }
45+ /* tslint:disable-next-line:no-any */
46+ private static isOTPStorage ( entry : any ) {
47+ if ( ! entry . hasOwnProperty ( 'secret' ) ) {
48+ return false ;
5449 }
50+
5551 return true ;
5652 }
5753
@@ -252,98 +248,110 @@ class EntryStorage {
252248 ( resolve : ( value : OTPEntry [ ] ) => void ,
253249 reject : ( reason : Error ) => void ) => {
254250 try {
255- chrome . storage . sync . get ( ( _data : { [ hash : string ] : OTPStorage } ) => {
256- const data : OTPEntry [ ] = [ ] ;
257- for ( let hash of Object . keys ( _data ) ) {
258- if ( ! this . isValidEntry ( _data , hash ) ) {
259- continue ;
260- }
261- const entryData = _data [ hash ] ;
262- let needMigrate = false ;
251+ chrome . storage . sync . get (
252+ async ( _data : { [ hash : string ] : OTPStorage } ) => {
253+ const data : OTPEntry [ ] = [ ] ;
254+ for ( let hash of Object . keys ( _data ) ) {
255+ if ( ! this . isValidEntry ( _data , hash ) ) {
256+ continue ;
257+ }
258+ const entryData = _data [ hash ] ;
259+ let needMigrate = false ;
263260
264- if ( ! entryData . type ) {
265- entryData . type = OTPType [ OTPType . totp ] ;
266- needMigrate = true ;
267- }
261+ if ( ! entryData . hash ) {
262+ entryData . hash = hash ;
263+ needMigrate = true ;
264+ }
268265
269- let type : OTPType ;
270- switch ( entryData . type ) {
271- case 'totp' :
272- case 'hotp' :
273- case 'battle' :
274- case 'steam' :
275- type = OTPType [ entryData . type ] ;
276- break ;
277- default :
278- // we need correct the type here
279- // and save it
280- type = OTPType . totp ;
281- entryData . type = OTPType [ OTPType . totp ] ;
282- needMigrate = true ;
283- }
284- entryData . secret = entryData . encrypted ?
285- encryption . getDecryptedSecret ( entryData . secret ) :
286- entryData . secret ;
287-
288- const entry = new OTPEntry (
289- type , entryData . issuer , entryData . secret , entryData . account ,
290- entryData . index , entryData . counter ) ;
291- data . push ( entry ) ;
266+ if ( ! entryData . type ) {
267+ entryData . type = OTPType [ OTPType . totp ] ;
268+ needMigrate = true ;
269+ }
292270
293- // we need migrate secret in old format here
294- if ( / ^ ( b l z \- | b l i z \- ) / . test ( entryData . secret ) ) {
295- const secretMatches =
296- entryData . secret . match ( / ^ ( b l z \- | b l i z \- ) ( .* ) / ) ;
297- if ( secretMatches && secretMatches . length >= 3 ) {
271+ let type : OTPType ;
272+ switch ( entryData . type ) {
273+ case 'totp' :
274+ case 'hotp' :
275+ case 'battle' :
276+ case 'steam' :
277+ type = OTPType [ entryData . type ] ;
278+ break ;
279+ default :
280+ // we need correct the type here
281+ // and save it
282+ type = OTPType . totp ;
283+ entryData . type = OTPType [ OTPType . totp ] ;
284+ needMigrate = true ;
285+ }
298286 entryData . secret = entryData . encrypted ?
299- secretMatches [ 2 ] :
300- encryption . getEncryptedSecret ( entry . secret ) ;
301- entryData . type = OTPType [ OTPType . battle ] ;
302- needMigrate = true ;
303- }
304- }
287+ encryption . getDecryptedSecret ( entryData . secret ) :
288+ entryData . secret ;
305289
306- if ( / ^ s t m \- / . test ( entryData . secret ) ) {
307- const secretMatches = entryData . secret . match ( / ^ s t m \- ( . * ) / ) ;
308- if ( secretMatches && secretMatches . length >= 2 ) {
309- entryData . secret = entryData . encrypted ?
310- secretMatches [ 2 ] :
311- encryption . getEncryptedSecret ( entry . secret ) ;
312- entryData . type = OTPType [ OTPType . steam ] ;
313- needMigrate = true ;
314- }
315- }
290+ // we need migrate secret in old format here
291+ if ( / ^ ( b l z \- | b l i z \- ) / . test ( entryData . secret ) ) {
292+ const secretMatches =
293+ entryData . secret . match ( / ^ ( b l z \- | b l i z \- ) ( . * ) / ) ;
294+ if ( secretMatches && secretMatches . length >= 3 ) {
295+ entryData . secret = secretMatches [ 2 ] ;
296+ entryData . type = OTPType [ OTPType . battle ] ;
297+ needMigrate = true ;
298+ }
299+ }
316300
317- // we need correct the hash
318- if ( entry . secret !== 'Encrypted' ) {
319- const _hash = CryptoJS . MD5 ( entry . secret ) . toString ( ) ;
320- if ( hash !== _hash ) {
321- chrome . storage . sync . remove ( hash ) ;
322- hash = _hash ;
323- entryData . hash = hash ;
324- needMigrate = true ;
325- }
326- }
301+ if ( / ^ s t m \- / . test ( entryData . secret ) ) {
302+ const secretMatches =
303+ entryData . secret . match ( / ^ s t m \- ( .* ) / ) ;
304+ if ( secretMatches && secretMatches . length >= 2 ) {
305+ entryData . secret = secretMatches [ 1 ] ;
306+ entryData . type = OTPType [ OTPType . steam ] ;
307+ needMigrate = true ;
308+ }
309+ }
327310
328- if ( needMigrate ) {
329- const _entry : { [ hash : string ] : OTPStorage } = { } ;
330- _entry [ hash ] = entryData ;
331- this . import ( encryption , _entry ) ;
332- }
333- }
311+ const entry = new OTPEntry (
312+ type , entryData . issuer , entryData . secret ,
313+ entryData . account , entryData . index , entryData . counter ) ;
334314
335- data . sort ( ( a , b ) => {
336- return a . index - b . index ;
337- } ) ;
338- return resolve ( data ) ;
339- } ) ;
315+ data . push ( entry ) ;
316+
317+ // we need correct the hash
318+ if ( entry . secret !== 'Encrypted' ) {
319+ const _hash = CryptoJS . MD5 ( entryData . secret ) . toString ( ) ;
320+ if ( hash !== _hash ) {
321+ await this . remove ( hash ) ;
322+ hash = _hash ;
323+ entryData . hash = hash ;
324+ needMigrate = true ;
325+ }
326+ }
327+
328+ if ( needMigrate ) {
329+ const _entry : { [ hash : string ] : OTPStorage } = { } ;
330+ _entry [ hash ] = entryData ;
331+ _entry [ hash ] . encrypted = false ;
332+ this . import ( encryption , _entry ) ;
333+ }
334+ }
335+
336+ data . sort ( ( a , b ) => {
337+ return a . index - b . index ;
338+ } ) ;
339+ return resolve ( data ) ;
340+ } ) ;
340341 return ;
341342 } catch ( error ) {
342343 return reject ( error ) ;
343344 }
344345 } ) ;
345346 }
346347
348+ static async remove ( hash : string ) {
349+ return new Promise (
350+ ( resolve : ( ) => void , reject : ( reason : Error ) => void ) => {
351+ return chrome . storage . sync . remove ( hash , resolve ) ;
352+ } ) ;
353+ }
354+
347355 static async delete ( entry : OTPEntry ) {
348356 return new Promise (
349357 ( resolve : ( ) => void , reject : ( reason : Error ) => void ) => {
0 commit comments