I am trying to figure out the best way to implement cross domain authentication.
Scenario:
As a user I log in to youtube.com, when I visit Google.com by typing the domain name https://www.google.com in a new browser tab, my session is preserved.
Problem:
As a developer, how do I implement the cross domain authentication in away that is secure, future proof and user friendly?
I will use the example of Google and YouTube to explain the problem. The parties that will be evolved in this examples are:
- Authentication service: accounts.google.com
- First domain: google.com
- Second domain: youtube.com
Possible solution 1
Use the SameSite attribute to create a Third-Party Cookie
that will be used for authentication. This cookie will be used to store and maintain the user's session cross the websites.
Example
- The user visits YouTube.com and log in.
- The service accounts.google.com issues Third-Party Session Cookie where the SameSite is set to non.
- When user visits Google.com, an Ajax request is made to accounts.google.com that includes the Session Cookie issued in Step 2.
- accounts.google.com will return the user profile and Google.com will see him as authenticated.
Pros:
- Easy to implement and requires minimum changes to websites.
- User friendly since the experience does not require any redirections.
- Secure since the websites that call the Auth API (accouts.google.com in the example above) can be listed as part of the CORS policy.
Cons:
As of 2022 such cookies are blocked on most browsers:
1.1 The usage of Third-Party Cookies is blocked in Safari since version 13.1.
1.2 This year Firefox introduced Total Cookie Protection, which “ensures that no cookies can be used to track you from site to site as you browse the web.”
1.3 Google is planning to phase out third party Cookies by 2023 and replace them with FLoC.
Possible solution 2
- The user visits YouTube.com.
- Before the YouTube is rendered, the user is redirected to accounts.google.com to check his authentication status and then he get redirected to YouTube.com
- The user log in to YouTube.com by first Getting redirected to accounts.google.com where he logs in and then he is sent back to YouTube.com
- When the user visits Google.com, he gets redirected to accounts.google.com first and issues an authentication cookie and redirects back to google.com.
- Google.com uses the cookie to authenticate the user and load his profile.
Pros:
- Requires more work than the first solution.
- Secure since we can use first-party cookies and a while list of the domains that Authentication service redirects to.
- Secure since the websites that call the Auth API (accounts.google.com in the example above) can be listed as part of the CORS policy.
Cons:
- User experience might be impacted because of the redirection. For example, if the Authentication service is slow.
- If the authentication service is down then the websites will not work as they redirect it to it as the start of the user visit the check the authentication status.
- The need to clear all cookies from all websites when the user logs out.
Questions
- Are my assumptions about the solutions 1 and 2 correct?
- Is there another recommended way to share the session across multiple domains?
- Do identity providers like Azure (OAuth2) solve this problem? How to manage the access and refresh tokens across multiple domains?