Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix automatic authentication with magic link. #19637

Merged
merged 4 commits into from
May 31, 2023

Conversation

cristipaval
Copy link
Contributor

@cristipaval cristipaval commented May 25, 2023

Details

Fixes the automatic authentication with magic links on accounts with 2fa enabled.

Fixed Issues

$ #19636

Tests

Note: Automatic authentication with the magic link is disabled for the desktop App.

  1. Have an account with 2fa enabled
  2. Start sign-in
  3. Tap on the link in the email that you automatically received
  4. Verify that the magic code is automatically validated in the App and you now have to enter the 2fa code
  5. Enter the 2fa code
  6. Verify that you are now signed in
  • Verify that no errors appear in the JS console

Offline tests

N/A

QA Steps

Same as Tests

  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android / native
    • Android / Chrome
    • iOS / native
    • iOS / Safari
    • MacOS / Chrome / Safari
    • MacOS / Desktop
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
      • If any non-english text was added/modified, I verified the translation was requested/reviewed in #expensify-open-source and it was approved by an internal Expensify engineer. Link to Slack message:
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG))
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR author checklist, including those that don't apply to this PR.

Screenshots/Videos

Web
web.mov
Mobile Web - Chrome
mobile.chrome.mov
Mobile Web - Safari
mobile.safari.mov
Desktop N/A
iOS
Screen.Recording.2023-05-26.at.02.01.41.mov
Android
Screen.Recording.2023-05-26.at.02.08.54.mov

@cristipaval cristipaval requested a review from NikkiWines May 25, 2023 23:06
@cristipaval cristipaval self-assigned this May 25, 2023
@cristipaval cristipaval marked this pull request as ready for review May 25, 2023 23:10
@cristipaval cristipaval requested a review from a team as a code owner May 25, 2023 23:10
@melvin-bot melvin-bot bot removed the request for review from a team May 25, 2023 23:10
@melvin-bot
Copy link

melvin-bot bot commented May 25, 2023

@NikkiWines @parasharrajat One of you needs to copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@melvin-bot melvin-bot bot requested a review from parasharrajat May 25, 2023 23:10
src/libs/actions/Session/index.js Outdated Show resolved Hide resolved
Co-authored-by: Rajat Parashar <parasharrajat@users.noreply.github.com>
@cristipaval cristipaval requested a review from parasharrajat May 25, 2023 23:28
Copy link
Contributor

@NikkiWines NikkiWines left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One suggestion that'll cause a bunch of changes

Comment on lines 466 to 468
if (twoFactorAuthCode) {
params.twoFactorAuthCode = twoFactorAuthCode;
}
Copy link
Contributor

@NikkiWines NikkiWines May 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we're explicitly passing null for the twoFactorAuthCode in several places (but that we often pass a preferredLocale, which is the param after it).

Maybe a cleaner solution would be to update this function (and also signInWithValidateCodeAndNavigate which calls this) so that twoFactorAuthCode is the last param and defaults to ''. Then we don't even need this if

It's more changes but ultimately I think it's better than passing null in a bunch of places

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cristipaval Are you going to make this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm on them atm

Comment on lines 465 to 468
// Pass twoFactorAuthCode to server only if it has a valid value, otherwise php might convert it to "null" as a string.
if (twoFactorAuthCode) {
params.twoFactorAuthCode = twoFactorAuthCode;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this a sign that this is an internal bug? Let's add the filter logic in php, if the twoFactorAuthCode is empty/unset, ignore it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that we should have better validation/sanitization on backend @cristipaval.

@parasharrajat
Copy link
Member

@cristipaval Are we going make requested changes in the PR?

@cristipaval
Copy link
Contributor Author

@cristipaval Are we going make requested changes in the PR?

on it! I like Nikki's suggestion

@cristipaval cristipaval requested a review from NikkiWines May 26, 2023 13:09
@cristipaval
Copy link
Contributor Author

Ready for a new round, @parasharrajat @NikkiWines @s77rt

@parasharrajat
Copy link
Member

@cristipaval because we refactored a few parts in this PR, can you please extend the QA steps to cover those components/pages as well?

Also, it will be a great help to me if you can tell me what pages are affected by these refactors other than then main login page/validation page.

@NikkiWines
Copy link
Contributor

Also, it will be a great help to me if you can tell me what pages are affected by these refactors other than then main login page/validation page.

I don't believe any other pages are impacted, since this logic is only used for signing into the app but maybe @cristipaval knows better than I do here.

We will need to be sure to test signing in on all platforms on accounts with 2FA enabled and without 2FA enabled. Also, we'll need to test manually entering the magic code and also logging in by clicking on the magic link.

@parasharrajat
Copy link
Member

Thanks for that help. Starting testing.

Copy link
Contributor

@NikkiWines NikkiWines left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@parasharrajat
Copy link
Member

parasharrajat commented May 26, 2023

Screenshots

🔲 iOS / native

Screen.Recording.2023-05-27.at.1.37.17.AM.mov

🔲 iOS / Safari

Screen.Recording.2023-05-27.at.2.01.52.AM.mov

🔲 MacOS / Desktop

🔲 MacOS / Chrome

Screen.Recording.2023-05-27.at.2.03.34.AM.mov

🔲 Android / Chrome

Screen.Recording.2023-05-27.at.2.31.55.AM.mov

🔲 Android / native

Screen.Recording.2023-05-27.at.2.28.38.AM.mov

@parasharrajat
Copy link
Member

parasharrajat commented May 26, 2023

I saw one issue, Where I am unable to go back from 2fa screen. This might be related to this issue. But it is one.

Steps:

  1. Open the app on safari ios.
  2. Enter email.
  3. open the magic link on the browser itself in a new tab.
  4. Go back to previous tab where you were trying to log in.
  5. Press go back.
Screen.Recording.2023-05-27.at.1.40.35.AM.mov

Even if the page is refreshed/reopened, if I put real 2fa code, It logs me in.

@parasharrajat
Copy link
Member

I am receiving incorrect 2FA code with following steps even if the 2fa is correct.

  1. Open the app on Safari IOS.
  2. Enter the email.
  3. open the magic link on the same tab where you are trying to log in.
  4. Go back to login screen.
  5. Open the magic link in new tab.
  6. Go to the login tab.
  7. enter the 2fa.
  8. Notice the error.
Screen.Recording.2023-05-27.at.1.44.45.AM.mov

Copy link
Member

@parasharrajat parasharrajat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from the above reported funky bugs, it's working. I am expecting those are unrelated and will be follow-up. let me know if I am wrong.

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified tests pass on all platforms & I tested again on:
    • Android / native
    • Android / Chrome
    • iOS / native
    • iOS / Safari
    • MacOS / Chrome / Safari
    • MacOS / Desktop
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick).
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG)
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

cc: @NikkiWines

🎀 👀 🎀 C+ reviewed

@parasharrajat
Copy link
Member

One more Bug: Opening magic on the browser where you haven't started login(haven't entered the email), asks you for a password.

Steps:

  1. Open the app on Android. Enter the email and continue.
  2. Copy the received magic link.
  3. Open it in the web browser on your laptop. (You should have not started login on the web browser i.e. you are on the first username input page before doing this step).
  4. Notice that it asks for a password.

@parasharrajat
Copy link
Member

parasharrajat commented May 26, 2023

I approved as the main issue is solved in this PR and in case we want to look into these issues in separate issues.

@parasharrajat
Copy link
Member

How do we open the magic link on desktop app in dev?

@parasharrajat
Copy link
Member

parasharrajat commented May 26, 2023

At 0.40 seconds you refreshed the page, but instead, you have to go back to the login page and then open the magic link in a new tab.

in my video, it is 0.41-0.43 sec.

@NikkiWines
Copy link
Contributor

NikkiWines commented May 26, 2023

For not being able to go back to the magic code view from the 2FA code view I think this is intentional since to get to the 2FA view you will have had to "use up" a correct magic code. Going back to that view wouldn't make sense.

If we do decide that's a bug, I think it should be separate from this issue.

How do we open the magic link on desktop app in dev?

I don't believe we have a flow for that, as it added a lot of complexity. There is no "automatic" authentication for desktop

@NikkiWines
Copy link
Contributor

At 0.40 seconds you refreshed the page, but instead, you have to go back to the login page and then open the magic link in a new tab.

I could've sworn I didn't refresh anything but it does look like that from the video 😅 I'll retest once more

@NikkiWines
Copy link
Contributor

Ok, I was able to reproduce the issue from here. It seems like in this case we don't end up passing the validateCode anymore so we seem to be throwing in the backend because we're not passing a password or validateCode, only the 2FA code

@NikkiWines NikkiWines self-requested a review May 27, 2023 00:40
@NikkiWines
Copy link
Contributor

Rescinding my approval since we should probably fix that issue to ensure we're passing the magic code and 2FA when applicable

@cristipaval
Copy link
Contributor Author

It looks like I can't reproduce your issue @parasharrajat

Screen.Recording.2023-05-29.at.23.23.10.mov

@parasharrajat
Copy link
Member

@cristipaval I also explained the reproduction in the above discussion. Does that help? There is also a video for it #19637 (comment)?

@cristipaval
Copy link
Contributor Author

@cristipaval I also explained the reproduction in the above discussion. Does that help? There is also a video for it #19637 (comment)?

Could you please look at my screen recording? I think I am doing the exact same steps 🤷

@parasharrajat
Copy link
Member

You are not doing the exact same steps. Instead of pasting the link you navigated away by changing the URL at 0.24 secs.

@NikkiWines
Copy link
Contributor

NikkiWines commented May 30, 2023

@parasharrajat it doesn't seem like i'm able to reproduce this anymore. Can you try once again?

screencap.mov

Since this is a more niche flow involving going back on the login flow, maybe we can make a separate issue once this is consistently reproducible. It would be good to get this PR out since we know the main flow is broken

@parasharrajat
Copy link
Member

Back to work. Retesting.

@parasharrajat
Copy link
Member

I am stuck in this loop. How can I reset it?

Screen.Recording.2023-06-01.at.12.12.50.AM.mov

@NikkiWines
Copy link
Contributor

@parasharrajat if entering the authenticator code doesn't log you in, you can try opening the JSContext for the simulator in safari and deleting onyxDB and clearing your cache.

@parasharrajat
Copy link
Member

Let me do that.

@parasharrajat
Copy link
Member

Ok, so I cleared the data and tried again and I can still reproduce this issue.

@NikkiWines
Copy link
Contributor

NikkiWines commented May 31, 2023

I still think we should possibly handle that as a separate issue. The majority of users aren't going to be going through a flow on mWeb where they manually paste the link over the original tab where they began signing in. It's definitely a bug, but a much less severe one than the main link not working at all on mobile devices when the user has 2FA enabled.

We should definitely fix it, but I think it's best to patch the more severe bug first, then handle the smaller bugs (the one you've identified and also that bug where you get caught in the login <-> 2FA code loop) separately

@parasharrajat
Copy link
Member

That's why I also approved the PR.

Copy link
Contributor

@NikkiWines NikkiWines left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@NikkiWines
Copy link
Contributor

Cool, @parasharrajat do you want to post that bug in #expensify-bugs since you found it in this PR? If not, I can spin up a new issue for it today

@NikkiWines NikkiWines merged commit 79f3377 into main May 31, 2023
@NikkiWines NikkiWines deleted the cristi_fix-signin-with-magic-link branch May 31, 2023 19:43
@OSBotify
Copy link
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@OSBotify
Copy link
Contributor

OSBotify commented Jun 1, 2023

🚀 Deployed to staging by https://github.com/NikkiWines in version: 1.3.22-0 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

@OSBotify
Copy link
Contributor

OSBotify commented Jun 2, 2023

🚀 Deployed to production by https://github.com/Julesssss in version: 1.3.22-1 🚀

platform result
🤖 android 🤖 cancelled 🔪
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 failure ❌
🕸 web 🕸 cancelled 🔪

2 similar comments
@OSBotify
Copy link
Contributor

OSBotify commented Jun 2, 2023

🚀 Deployed to production by https://github.com/Julesssss in version: 1.3.22-1 🚀

platform result
🤖 android 🤖 cancelled 🔪
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 failure ❌
🕸 web 🕸 cancelled 🔪

@OSBotify
Copy link
Contributor

OSBotify commented Jun 2, 2023

🚀 Deployed to production by https://github.com/Julesssss in version: 1.3.22-1 🚀

platform result
🤖 android 🤖 cancelled 🔪
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 failure ❌
🕸 web 🕸 cancelled 🔪

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants