From e314c421b60fd2d7a0140cce09ee5c2338699ade Mon Sep 17 00:00:00 2001 From: Eduard Kuzmenko Date: Tue, 8 Jun 2021 15:24:30 +0300 Subject: [PATCH] Support current WebK auth --- src/lib/appManagers/appDialogsManager.ts | 2 ++ src/lib/appManagers/appStateManager.ts | 24 +++++++++++++++++++++- src/lib/mtproto/apiManager.ts | 2 +- src/test_webk_auth_migration.js | 26 ++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 src/test_webk_auth_migration.js diff --git a/src/lib/appManagers/appDialogsManager.ts b/src/lib/appManagers/appDialogsManager.ts index f3c48dbe..d0c2e090 100644 --- a/src/lib/appManagers/appDialogsManager.ts +++ b/src/lib/appManagers/appDialogsManager.ts @@ -710,6 +710,8 @@ export class AppDialogsManager { private onListLengthChange = () => { //return; + + if(this.filterId < 2) return; const emptyFolder = this.chatList.parentElement.querySelector('.empty-folder'); if(this.scroll.loadedAll.bottom && !this.chatList.childElementCount) { diff --git a/src/lib/appManagers/appStateManager.ts b/src/lib/appManagers/appStateManager.ts index ba022f00..b7e41407 100644 --- a/src/lib/appManagers/appStateManager.ts +++ b/src/lib/appManagers/appStateManager.ts @@ -199,9 +199,10 @@ export class AppStateManager extends EventListenerBase<{ const promises: Promise[] = ALL_KEYS.map(key => stateStorage.get(key)) .concat(sessionStorage.get('user_auth')) + .concat(stateStorage.get('user_auth' as any)) // support old webk format .concat(storagesPromises); - Promise.all(promises).then((arr) => { + Promise.all(promises).then(async(arr) => { /* const self = this; const skipHandleKeys = new Set(['isProxy', 'filters', 'drafts']); const getHandler = (path?: string) => { @@ -275,6 +276,27 @@ export class AppStateManager extends EventListenerBase<{ } } + let shiftedWebKAuth = arr.shift() as UserAuth | number; + if(!auth && shiftedWebKAuth) { // support old webk auth + auth = shiftedWebKAuth; + const keys: string[] = ['dc', 'server_time_offset', 'xt_instance']; + for(let i = 1; i <= 5; ++i) { + keys.push(`dc${i}_server_salt`); + keys.push(`dc${i}_auth_key`); + } + + const values = await Promise.all(keys.map(key => stateStorage.get(key as any))); + keys.push('user_auth'); + values.push(typeof(auth) === 'number' ? {dcID: values[0] || App.baseDcId, id: auth} : auth); + + let obj: any = {}; + keys.forEach((key, idx) => { + obj[key] = values[idx]; + }); + + await sessionStorage.set(obj); + } + if(auth) { // ! Warning ! DON'T delete this state.authState = {_: 'authStateSignedIn'}; diff --git a/src/lib/mtproto/apiManager.ts b/src/lib/mtproto/apiManager.ts index dfba437a..8be23d7e 100644 --- a/src/lib/mtproto/apiManager.ts +++ b/src/lib/mtproto/apiManager.ts @@ -160,7 +160,7 @@ export class ApiManager { // WebPushApiManager.forceUnsubscribe(); // WARNING const storageResult = await Promise.all(storageKeys.map(key => sessionStorage.get(key as any))); - const logoutPromises = []; + const logoutPromises: Promise[] = []; for(let i = 0; i < storageResult.length; i++) { if(storageResult[i]) { logoutPromises.push(this.invokeApi('auth.logOut', {}, {dcId: i + 1, ignoreErrors: true})); diff --git a/src/test_webk_auth_migration.js b/src/test_webk_auth_migration.js new file mode 100644 index 00000000..b6fe9a80 --- /dev/null +++ b/src/test_webk_auth_migration.js @@ -0,0 +1,26 @@ +(async function asd() { + const auth = 1; + const keys = ['dc', 'server_time_offset', 'xt_instance']; + for(let i = 1; i <= 5; ++i) { + keys.push(`dc${i}_server_salt`); + keys.push(`dc${i}_auth_key`); + } + + const values = await Promise.all(keys.map(key => appStorage.get(key))); + keys.push('user_auth'); + values.push(typeof(auth) === 'number' ? {dcID: values[0] || 2, id: auth} : auth); + + let obj = {}; + keys.forEach((key, idx) => { + obj[key] = values[idx]; + }); + + window.exported = obj; + console.log(obj); +})(); + +copy(JSON.stringify(window.exported)); + + +var obj = JSON.parse(); +appStorage.set(obj); \ No newline at end of file