Skip to content

Commit

Permalink
Merge pull request #13456 from Expensify/nikki-remove-deprecated-shor…
Browse files Browse the repository at this point in the history
…tauthtoken-calls

Update signInWithShortLivedToken to use API.Write instead of DeprecateAPI commands
  • Loading branch information
iwiznia authored Dec 26, 2022
2 parents 7f276b6 + 45b35b9 commit 276f177
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 170 deletions.
22 changes: 0 additions & 22 deletions src/libs/LoginUtils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _ from 'underscore';
import CONST from '../CONST';

/**
Expand All @@ -21,28 +20,7 @@ function getPhoneNumberWithoutUSCountryCodeAndSpecialChars(phone) {
return getPhoneNumberWithoutSpecialChars(phone.replace(/^\+1/, ''));
}

/**
* Filter out all non-Expensify partners from login list
*
* @param {Object} loginList
* @returns {Object}
*/
function keepExpensifyPartners(loginList = {}) {
return _.pick(loginList, login => login.partnerName === CONST.EXPENSIFY_PARTNER_NAME);
}

/**
* Cleans login list that came from the server by only keeping logins with Expensify partner name
*
* @param {Object} loginList
* @returns {Object}
*/
function cleanLoginListServerResponse(loginList = {}) {
return keepExpensifyPartners(loginList);
}

export {
getPhoneNumberWithoutSpecialChars,
getPhoneNumberWithoutUSCountryCodeAndSpecialChars,
cleanLoginListServerResponse,
};
2 changes: 1 addition & 1 deletion src/libs/Network/NetworkStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Onyx.connect({
Onyx.connect({
key: ONYXKEYS.CREDENTIALS,
callback: (val) => {
credentials = val || null;
credentials = val || {};
checkRequiredData();
},
});
Expand Down
9 changes: 8 additions & 1 deletion src/libs/actions/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Navigation from '../Navigation/Navigation';
import ROUTES from '../../ROUTES';
import * as SessionUtils from '../SessionUtils';
import getCurrentUrl from '../Navigation/currentUrl';
import * as Session from './Session';

let currentUserAccountID;
let currentUserEmail = '';
Expand Down Expand Up @@ -203,8 +204,14 @@ function setUpPoliciesAndNavigate(session) {
const makeMeAdmin = url.searchParams.get('makeMeAdmin');
const policyName = url.searchParams.get('policyName');

// Sign out the current user if we're transitioning from oldDot with a different user
const isTransitioningFromOldDot = Str.startsWith(url.pathname, Str.normalizeUrl(ROUTES.TRANSITION_FROM_OLD_DOT));
if (isLoggingInAsNewUser && isTransitioningFromOldDot) {
Session.signOut();
}

const shouldCreateFreePolicy = !isLoggingInAsNewUser
&& Str.startsWith(url.pathname, Str.normalizeUrl(ROUTES.TRANSITION_FROM_OLD_DOT))
&& isTransitioningFromOldDot
&& exitTo === ROUTES.WORKSPACE_NEW;
if (shouldCreateFreePolicy) {
Policy.createWorkspace(ownerEmail, makeMeAdmin, policyName, true);
Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/ReimbursementAccount/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ let credentials;
Onyx.connect({
key: ONYXKEYS.CREDENTIALS,
callback: (val) => {
credentials = val;
credentials = val || {};
},
});

Expand Down
137 changes: 39 additions & 98 deletions src/libs/actions/Session/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import Onyx from 'react-native-onyx';
import Str from 'expensify-common/lib/str';
import _ from 'underscore';
import lodashGet from 'lodash/get';
import ONYXKEYS from '../../../ONYXKEYS';
import redirectToSignIn from '../SignInRedirect';
import * as DeprecatedAPI from '../../deprecatedAPI';
import CONFIG from '../../../CONFIG';
import Log from '../../Log';
import PushNotification from '../../Notification/PushNotification';
Expand All @@ -14,7 +12,6 @@ import * as Localize from '../../Localize';
import UnreadIndicatorUpdater from '../../UnreadIndicatorUpdater';
import Timers from '../../Timers';
import * as Pusher from '../../Pusher/pusher';
import * as User from '../User';
import * as Authentication from '../../Authentication';
import * as Welcome from '../Welcome';
import * as API from '../../API';
Expand All @@ -24,26 +21,9 @@ import DateUtils from '../../DateUtils';
let credentials = {};
Onyx.connect({
key: ONYXKEYS.CREDENTIALS,
callback: val => credentials = val,
callback: val => credentials = val || {},
});

/**
* Sets API data in the store when we make a successful "Authenticate"/"CreateLogin" request
*
* @param {Object} data
* @param {String} data.accountID
* @param {String} data.authToken
* @param {String} data.email
*/
function setSuccessfulSignInData(data) {
PushNotification.register(data.accountID);
Onyx.merge(ONYXKEYS.SESSION, {
errors: null,
..._.pick(data, 'authToken', 'accountID', 'email', 'encryptedAuthToken'),
});
Onyx.set(ONYXKEYS.SHOULD_SHOW_COMPOSE_INPUT, true);
}

/**
* Clears the Onyx store and redirects user to the sign in page
*/
Expand All @@ -59,7 +39,7 @@ function signOut() {
{
onyxMethod: CONST.ONYX.METHOD.SET,
key: ONYXKEYS.CREDENTIALS,
value: null,
value: {},
},
];
API.write('LogOut', {
Expand Down Expand Up @@ -159,64 +139,48 @@ function beginSignIn(login) {
}

/**
*
* Will create a temporary login for the user in the passed authenticate response which is used when
* re-authenticating after an authToken expires.
*
* @param {String} authToken
* @param {String} email
* @return {Promise}
* @param {String} authToken
*/
function createTemporaryLogin(authToken, email) {
const autoGeneratedLogin = Str.guid('expensify.cash-');
const autoGeneratedPassword = Str.guid();
function signInWithShortLivedAuthToken(email, authToken) {
const optimisticData = [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
...CONST.DEFAULT_ACCOUNT_DATA,
isLoading: true,
},
},
];

return DeprecatedAPI.CreateLogin({
authToken,
partnerName: CONFIG.EXPENSIFY.PARTNER_NAME,
partnerPassword: CONFIG.EXPENSIFY.PARTNER_PASSWORD,
partnerUserID: autoGeneratedLogin,
partnerUserSecret: autoGeneratedPassword,
shouldRetry: false,
forceNetworkRequest: true,
email,
includeEncryptedAuthToken: true,
})
.then((createLoginResponse) => {
if (createLoginResponse.jsonCode !== 200) {
Onyx.merge(ONYXKEYS.ACCOUNT, {errors: {[DateUtils.getMicroseconds()]: Localize.translate('createLoginResponse.message')}});
return createLoginResponse;
}

setSuccessfulSignInData(createLoginResponse);

// If we have an old generated login for some reason
// we should delete it before storing the new details
if (credentials && credentials.autoGeneratedLogin) {
DeprecatedAPI.DeleteLogin({
partnerUserID: credentials.autoGeneratedLogin,
partnerName: CONFIG.EXPENSIFY.PARTNER_NAME,
partnerPassword: CONFIG.EXPENSIFY.PARTNER_PASSWORD,
shouldRetry: false,
})
.then((response) => {
if (response.jsonCode === CONST.JSON_CODE.SUCCESS) {
return;
}

Log.hmmm('[Session] Unable to delete login', false, {message: response.message, jsonCode: response.jsonCode});
});
}

Onyx.merge(ONYXKEYS.CREDENTIALS, {
autoGeneratedLogin,
autoGeneratedPassword,
});
return createLoginResponse;
})
.finally(() => {
Onyx.merge(ONYXKEYS.ACCOUNT, {isLoading: false});
});
const successData = [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
isLoading: false,
},
},
];

const failureData = [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
isLoading: false,
},
},
];

// If the user is transitioning to newDot from a different account on oldDot the credentials may be tied to
// the old account. If so, should not pass the auto-generated login as it is not tied to the account being signed in
const oldPartnerUserID = credentials.login === email ? credentials.autoGeneratedLogin : '';
API.write('SignInWithShortLivedAuthToken', {authToken, oldPartnerUserID}, {optimisticData, successData, failureData});
}

/**
Expand Down Expand Up @@ -262,29 +226,6 @@ function signIn(password, twoFactorAuthCode) {
API.write('SigninUser', {email: credentials.login, password, twoFactorAuthCode}, {optimisticData, successData, failureData});
}

/**
* Uses a short lived authToken to continue a user's session from OldDot
*
* @param {String} email
* @param {String} shortLivedAuthToken
* @param {String} exitTo
*/
function signInWithShortLivedAuthToken(email, shortLivedAuthToken) {
Onyx.merge(ONYXKEYS.ACCOUNT, {...CONST.DEFAULT_ACCOUNT_DATA, isLoading: true});

createTemporaryLogin(shortLivedAuthToken, email)
.then((response) => {
if (response.jsonCode !== CONST.JSON_CODE.SUCCESS) {
return;
}

User.getUserDetails();
Onyx.merge(ONYXKEYS.ACCOUNT, {success: true});
}).finally(() => {
Onyx.merge(ONYXKEYS.ACCOUNT, {isLoading: false});
});
}

/**
* User forgot the password so let's send them the link to reset their password
*/
Expand Down Expand Up @@ -361,7 +302,7 @@ function invalidateAuthToken() {
function clearSignInData() {
Onyx.multiSet({
[ONYXKEYS.ACCOUNT]: null,
[ONYXKEYS.CREDENTIALS]: null,
[ONYXKEYS.CREDENTIALS]: {},
});
}

Expand Down
46 changes: 1 addition & 45 deletions src/libs/actions/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ import NetworkConnection from '../NetworkConnection';
import Growl from '../Growl';
import * as Localize from '../Localize';
import * as Link from './Link';
import getSkinToneEmojiFromIndex from '../../components/EmojiPicker/getSkinToneEmojiFromIndex';
import * as SequentialQueue from '../Network/SequentialQueue';
import PusherUtils from '../PusherUtils';
import * as LoginUtils from '../LoginUtils';

let currentUserAccountID = '';
Onyx.connect({
Expand Down Expand Up @@ -89,46 +87,6 @@ function closeAccount(message) {
});
}

/**
* Fetches the data needed for user settings
*/
function getUserDetails() {
DeprecatedAPI.Get({
returnValueList: 'account, loginList, nameValuePairs',
nvpNames: [
CONST.NVP.PAYPAL_ME_ADDRESS,
CONST.NVP.PREFERRED_EMOJI_SKIN_TONE,
CONST.NVP.FREQUENTLY_USED_EMOJIS,
CONST.NVP.BLOCKED_FROM_CONCIERGE,
].join(','),
})
.then((response) => {
// Update the User onyx key
const isSubscribedToNewsletter = lodashGet(response, 'account.subscribed', true);
const validatedStatus = lodashGet(response, 'account.validated', false);
Onyx.merge(ONYXKEYS.USER, {isSubscribedToNewsletter: !!isSubscribedToNewsletter, validated: !!validatedStatus});

// Update login list
const loginList = LoginUtils.cleanLoginListServerResponse(response.loginList);
Onyx.set(ONYXKEYS.LOGIN_LIST, loginList);

// Update the nvp_payPalMeAddress NVP
const payPalMeAddress = lodashGet(response, `nameValuePairs.${CONST.NVP.PAYPAL_ME_ADDRESS}`, '');
Onyx.merge(ONYXKEYS.NVP_PAYPAL_ME_ADDRESS, payPalMeAddress);

// Update the blockedFromConcierge NVP
const blockedFromConcierge = lodashGet(response, `nameValuePairs.${CONST.NVP.BLOCKED_FROM_CONCIERGE}`, {});
Onyx.merge(ONYXKEYS.NVP_BLOCKED_FROM_CONCIERGE, blockedFromConcierge);

const preferredSkinTone = lodashGet(response, `nameValuePairs.${CONST.NVP.PREFERRED_EMOJI_SKIN_TONE}`, {});
Onyx.merge(ONYXKEYS.PREFERRED_EMOJI_SKIN_TONE,
getSkinToneEmojiFromIndex(preferredSkinTone).skinTone);

const frequentlyUsedEmojis = lodashGet(response, `nameValuePairs.${CONST.NVP.FREQUENTLY_USED_EMOJIS}`, []);
Onyx.set(ONYXKEYS.FREQUENTLY_USED_EMOJIS, frequentlyUsedEmojis);
});
}

/**
* Resends a validation link to a given login
*
Expand Down Expand Up @@ -179,8 +137,7 @@ function setSecondaryLoginAndNavigate(login, password) {
password,
}).then((response) => {
if (response.jsonCode === 200) {
const loginList = LoginUtils.cleanLoginListServerResponse(response.loginList);
Onyx.set(ONYXKEYS.LOGIN_LIST, loginList);
Onyx.set(ONYXKEYS.LOGIN_LIST, response.loginList);
Navigation.navigate(ROUTES.SETTINGS_PROFILE);
return;
}
Expand Down Expand Up @@ -481,7 +438,6 @@ function generateStatementPDF(period) {
export {
updatePassword,
closeAccount,
getUserDetails,
resendValidateCode,
updateNewsletterSubscription,
setSecondaryLoginAndNavigate,
Expand Down
2 changes: 1 addition & 1 deletion tests/actions/SessionTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test('Authenticate is called with saved credentials when a session expires', ()
let credentials;
Onyx.connect({
key: ONYXKEYS.CREDENTIALS,
callback: val => credentials = val,
callback: val => credentials = val || {},
});

let session;
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/NetworkTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ describe('NetworkTests', () => {
// Given a simulated a condition where the credentials have not yet been read from storage and we are offline
return Onyx.multiSet({
[ONYXKEYS.NETWORK]: {isOffline: true},
[ONYXKEYS.CREDENTIALS]: null,
[ONYXKEYS.CREDENTIALS]: {},
[ONYXKEYS.SESSION]: null,
})
.then(() => {
Expand Down

0 comments on commit 276f177

Please sign in to comment.