Merge pull request #1997 from ecency/nt/token-refresh

inverted migration logic, support for other secret keys to generate p…
This commit is contained in:
Feruz M 2021-07-16 16:20:30 +03:00 committed by GitHub
commit 217285be95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 18 deletions

View File

@ -92,8 +92,8 @@ const AccountsBottomSheetContainer = ({ navigation }) => {
//migreate account to use access token for master key auth type
let logs = "Realm data: " + JSON.stringify(realmData) + "\n\n"
Alert.alert("Realm data: " + JSON.stringify(realmData) + "\n\nWith PinHash: " + pinHash + "\n\n" )
if (realmData[0].authType === AUTH_TYPE.MASTER_KEY && realmData[0].accessToken === '') {
_currentAccount = await migrateToMasterKeyWithAccessToken(_currentAccount, pinHash);
if (realmData[0].authType !== AUTH_TYPE.STEEM_CONNECT && realmData[0].accessToken === '') {
_currentAccount = await migrateToMasterKeyWithAccessToken(_currentAccount, realmData[0], pinHash);
logs += "Migrated Data: " + JSON.stringify(_currentAccount.local);
Alert.alert("Migrated Data: " + JSON.stringify(_currentAccount.local))
}

View File

@ -3,6 +3,7 @@ import sha256 from 'crypto-js/sha256';
import Config from 'react-native-config';
import get from 'lodash/get';
import { Alert } from 'react-native';
import { getDigitPinCode, getUser } from './dhive';
import {
setUserData,
@ -333,7 +334,7 @@ export const refreshSCToken = async (userData, pinCode) => {
});
return encryptedAccessToken;
} catch (error) {
Alert.alert("Access token refresh failed", JSON.stringify(error));
Alert.alert('Access token refresh failed', JSON.stringify(error));
if (now > expireDate) {
throw error;
} else {
@ -421,25 +422,22 @@ const isLoggedInUser = async (username) => {
* This migration snippet is used to update access token for users logged in using masterKey
* accessToken is required for all ecency api calls even for non hivesigner users.
*/
export const migrateToMasterKeyWithAccessToken = async (account, pinHash) => {
export const migrateToMasterKeyWithAccessToken = async (account, userData, pinHash) => {
//get username, user local data from account;
const username = account.name;
const userData = account.local;
if (userData.accessToken) {
//skipping migration as access token already preset;
Alert.alert("Already have access token: " + JSON.stringify(userData));
return account;
}
//decrypt password from local data
const pinCode = getDigitPinCode(pinHash);
const password = decryptKey(userData.masterKey, pinCode);
const password = decryptKey(
userData.masterKey || userData.activeKey || userData.postingKey || userData.memoKey,
pinCode,
);
// Set private keys of user
const privateKeys = getPrivateKeys(username, password);
const signerPrivateKey = privateKeys.ownerKey || privateKeys.activeKey || privateKeys.postingKey;
const signerPrivateKey =
privateKeys.ownerKey || privateKeys.activeKey || privateKeys.postingKey || privateKeys.memoKey;
const code = await makeHsCode(account.name, signerPrivateKey);
const scTokens = await getSCAccessToken(code);

View File

@ -693,8 +693,8 @@ class ApplicationContainer extends Component {
//cannot migrate or refresh token since pin would null while pin code modal is open
if (!isPinCodeOpen) {
//migration script for previously mast key based logged in user not having access token
if (realmObject.authType === AUTH_TYPE.MASTER_KEY && realmObject.accessToken === '') {
accountData = await migrateToMasterKeyWithAccessToken(accountData, pinCode);
if (realmObject.authType !== AUTH_TYPE.STEEM_CONNECT && realmObject.accessToken === '') {
accountData = await migrateToMasterKeyWithAccessToken(accountData, realmObject, pinCode);
}
//refresh access token
@ -873,8 +873,12 @@ class ApplicationContainer extends Component {
[_currentAccount.local] = realmData;
//migreate account to use access token for master key auth type
if (realmData[0].authType === AUTH_TYPE.MASTER_KEY && realmData[0].accessToken === '') {
_currentAccount = await migrateToMasterKeyWithAccessToken(_currentAccount, pinCode);
if (realmData[0].authType !== AUTH_TYPE.STEEM_CONNECT && realmData[0].accessToken === '') {
_currentAccount = await migrateToMasterKeyWithAccessToken(
_currentAccount,
realmData[0],
pinCode,
);
}
//update refresh token

View File

@ -299,11 +299,12 @@ class PinCodeContainer extends Component {
const pinHash = encryptKey(pin, Config.PIN_KEY);
//migration script for previously mast key based logged in user not having access token
if (
realmData[0].authType === AUTH_TYPE.MASTER_KEY &&
realmData[0].authType !== AUTH_TYPE.STEEM_CONNECT &&
realmData[0].accessToken === ''
) {
_currentAccount = await migrateToMasterKeyWithAccessToken(
_currentAccount,
realmData[0],
pinHash,
);
}