Skip to content

Commit

Permalink
Merge pull request #61 from Authenticator-Extension/master
Browse files Browse the repository at this point in the history
5.0.5
  • Loading branch information
Sneezry authored Mar 4, 2018
2 parents 45f284f + a9476b0 commit da11c25
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 88 deletions.
2 changes: 1 addition & 1 deletion manifest-chrome.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "__MSG_extName__",
"short_name": "__MSG_extShortName__",
"version": "5.0.4",
"version": "5.0.5",
"default_locale": "en",
"description": "__MSG_extDesc__",
"icons": {
Expand Down
2 changes: 1 addition & 1 deletion manifest-firefox.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "__MSG_extName__",
"short_name": "__MSG_extShortName__",
"version": "5.0.4",
"version": "5.0.5",
"default_locale": "en",
"description": "__MSG_extDesc__",
"applications": {
Expand Down
180 changes: 94 additions & 86 deletions src/models/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,12 @@ class EntryStorage {
return newData;
}

private static isOTPStorage(entry: object) {
const properties = [
'account', 'hash', 'index', 'issuer', 'type', 'counter', 'secret',
'encrypted'
];
for (let i = 0; i < properties.length; i++) {
if (!entry.hasOwnProperty(properties[i])) {
return false;
}
/* tslint:disable-next-line:no-any */
private static isOTPStorage(entry: any) {
if (!entry.hasOwnProperty('secret')) {
return false;
}

return true;
}

Expand Down Expand Up @@ -252,98 +248,110 @@ class EntryStorage {
(resolve: (value: OTPEntry[]) => void,
reject: (reason: Error) => void) => {
try {
chrome.storage.sync.get((_data: {[hash: string]: OTPStorage}) => {
const data: OTPEntry[] = [];
for (let hash of Object.keys(_data)) {
if (!this.isValidEntry(_data, hash)) {
continue;
}
const entryData = _data[hash];
let needMigrate = false;
chrome.storage.sync.get(
async (_data: {[hash: string]: OTPStorage}) => {
const data: OTPEntry[] = [];
for (let hash of Object.keys(_data)) {
if (!this.isValidEntry(_data, hash)) {
continue;
}
const entryData = _data[hash];
let needMigrate = false;

if (!entryData.type) {
entryData.type = OTPType[OTPType.totp];
needMigrate = true;
}
if (!entryData.hash) {
entryData.hash = hash;
needMigrate = true;
}

let type: OTPType;
switch (entryData.type) {
case 'totp':
case 'hotp':
case 'battle':
case 'steam':
type = OTPType[entryData.type];
break;
default:
// we need correct the type here
// and save it
type = OTPType.totp;
entryData.type = OTPType[OTPType.totp];
needMigrate = true;
}
entryData.secret = entryData.encrypted ?
encryption.getDecryptedSecret(entryData.secret) :
entryData.secret;

const entry = new OTPEntry(
type, entryData.issuer, entryData.secret, entryData.account,
entryData.index, entryData.counter);
data.push(entry);
if (!entryData.type) {
entryData.type = OTPType[OTPType.totp];
needMigrate = true;
}

// we need migrate secret in old format here
if (/^(blz\-|bliz\-)/.test(entryData.secret)) {
const secretMatches =
entryData.secret.match(/^(blz\-|bliz\-)(.*)/);
if (secretMatches && secretMatches.length >= 3) {
let type: OTPType;
switch (entryData.type) {
case 'totp':
case 'hotp':
case 'battle':
case 'steam':
type = OTPType[entryData.type];
break;
default:
// we need correct the type here
// and save it
type = OTPType.totp;
entryData.type = OTPType[OTPType.totp];
needMigrate = true;
}
entryData.secret = entryData.encrypted ?
secretMatches[2] :
encryption.getEncryptedSecret(entry.secret);
entryData.type = OTPType[OTPType.battle];
needMigrate = true;
}
}
encryption.getDecryptedSecret(entryData.secret) :
entryData.secret;

if (/^stm\-/.test(entryData.secret)) {
const secretMatches = entryData.secret.match(/^stm\-(.*)/);
if (secretMatches && secretMatches.length >= 2) {
entryData.secret = entryData.encrypted ?
secretMatches[2] :
encryption.getEncryptedSecret(entry.secret);
entryData.type = OTPType[OTPType.steam];
needMigrate = true;
}
}
// we need migrate secret in old format here
if (/^(blz\-|bliz\-)/.test(entryData.secret)) {
const secretMatches =
entryData.secret.match(/^(blz\-|bliz\-)(.*)/);
if (secretMatches && secretMatches.length >= 3) {
entryData.secret = secretMatches[2];
entryData.type = OTPType[OTPType.battle];
needMigrate = true;
}
}

// we need correct the hash
if (entry.secret !== 'Encrypted') {
const _hash = CryptoJS.MD5(entry.secret).toString();
if (hash !== _hash) {
chrome.storage.sync.remove(hash);
hash = _hash;
entryData.hash = hash;
needMigrate = true;
}
}
if (/^stm\-/.test(entryData.secret)) {
const secretMatches =
entryData.secret.match(/^stm\-(.*)/);
if (secretMatches && secretMatches.length >= 2) {
entryData.secret = secretMatches[1];
entryData.type = OTPType[OTPType.steam];
needMigrate = true;
}
}

if (needMigrate) {
const _entry: {[hash: string]: OTPStorage} = {};
_entry[hash] = entryData;
this.import(encryption, _entry);
}
}
const entry = new OTPEntry(
type, entryData.issuer, entryData.secret,
entryData.account, entryData.index, entryData.counter);

data.sort((a, b) => {
return a.index - b.index;
});
return resolve(data);
});
data.push(entry);

// we need correct the hash
if (entry.secret !== 'Encrypted') {
const _hash = CryptoJS.MD5(entryData.secret).toString();
if (hash !== _hash) {
await this.remove(hash);
hash = _hash;
entryData.hash = hash;
needMigrate = true;
}
}

if (needMigrate) {
const _entry: {[hash: string]: OTPStorage} = {};
_entry[hash] = entryData;
_entry[hash].encrypted = false;
this.import(encryption, _entry);
}
}

data.sort((a, b) => {
return a.index - b.index;
});
return resolve(data);
});
return;
} catch (error) {
return reject(error);
}
});
}

static async remove(hash: string) {
return new Promise(
(resolve: () => void, reject: (reason: Error) => void) => {
return chrome.storage.sync.remove(hash, resolve);
});
}

static async delete(entry: OTPEntry) {
return new Promise(
(resolve: () => void, reject: (reason: Error) => void) => {
Expand Down

0 comments on commit da11c25

Please sign in to comment.