Skip to content

Commit 2c3b411

Browse files
authored
Merge pull request #172933 from henrymbuguakiarie/msid-content-health
[msid][refresh]reference-third-party-cookies-spas (ADO-1869142)
2 parents 8c479b5 + df7e81d commit 2c3b411

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

articles/active-directory/develop/reference-third-party-cookies-spas.md

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,59 +9,60 @@ manager: CelesteDG
99
ms.service: active-directory
1010
ms.subservice: develop
1111
ms.workload: identity
12-
ms.topic: how-to
13-
ms.date: 05/19/2020
12+
ms.topic: conceptual
13+
ms.date: 10/06/2021
1414
ms.author: hirsin
1515
ms.reviewer: kkrishna
1616
ms.custom: aaddev
1717
---
18+
1819
# Handle ITP in Safari and other browsers where third-party cookies are blocked
1920

20-
Many browsers today are blocking third-party cookies - cookies on requests to domains that are not the same as the one showing in the browser bar. This breaks the implicit flow and requires new authentication patterns to successfully sign in users. In the Microsoft identity platform, we use the authorization flow with PKCE and refresh tokens to keep users signed in when third-party cookies are blocked.
21+
Many browsers today are blocking third-party cookies - cookies on requests to domains that aren't the same as the one showing in the browser bar. This breaks the implicit flow and requires new authentication patterns to successfully sign in users. In the Microsoft identity platform, we use the authorization flow with Proof Key for Code Exchange (PKCE) and refresh tokens to keep users signed in when third-party cookies are blocked.
2122

2223
## What is Intelligent Tracking Protection (ITP)?
2324

24-
Apple Safari has an on-by-default privacy protection feature called [Intelligent Tracking Protection](https://webkit.org/tracking-prevention-policy/), or *ITP*. ITP blocks "third-party" cookies, cookies on requests that cross domains.
25+
Apple Safari has an on-by-default privacy protection feature called [Intelligent Tracking Protection](https://webkit.org/tracking-prevention-policy/), or _ITP_. ITP blocks "third-party" cookies, cookies on requests that cross domains.
2526

2627
A common form of user tracking is done by loading an iframe to third-party site in the background and using cookies to correlate the user across the Internet. Unfortunately, this pattern is also the standard way of implementing the [implicit flow](v2-oauth2-implicit-grant-flow.md) in single-page apps (SPAs). When a browser blocks third-party cookies to prevent user tracking, SPAs are also broken.
2728

28-
Safari isn't alone in blocking third-party cookies to enhance user privacy. Brave has blocked third-party cookies by default, and Chromium (the platform behind Google Chrome and Microsoft Edge) has announced that they as well will stop supporting third-party cookies in the future.
29+
Safari isn't alone in blocking third-party cookies to enhance user privacy. Brave blocks third-party cookies by default, and Chromium (the platform behind Google Chrome and Microsoft Edge) has announced that they as well will stop supporting third-party cookies in the future.
2930

3031
The solution outlined in this article works in all of these browsers, or anywhere third-party cookies are blocked.
3132

3233
## Overview of the solution
3334

34-
To continue authenticating users in SPAs, app developers must use the [authorization code flow](v2-oauth2-auth-code-flow.md). In the auth code flow, the identity provider issues a code, and the SPA redeems the code for an access token and a refresh token. When the app requires additional tokens, it can use the [refresh token flow](v2-oauth2-auth-code-flow.md#refresh-the-access-token) to get new tokens. MSAL.js 2.0, the Microsoft identity platform library for SPAs, implements the authorization code flow for SPAs and, with minor updates, is a drop-in replacement for MSAL.js 1.x.
35+
To continue authenticating users in SPAs, app developers must use the [authorization code flow](v2-oauth2-auth-code-flow.md). In the auth code flow, the identity provider issues a code, and the SPA redeems the code for an access token and a refresh token. When the app requires additional tokens, it can use the [refresh token flow](v2-oauth2-auth-code-flow.md#refresh-the-access-token) to get new tokens. Microsoft Authentication Library (MSAL) for JavaScript v2.0, implements the authorization code flow for SPAs and, with minor updates, is a drop-in replacement for MSAL.js 1.x.
3536

3637
For the Microsoft identity platform, SPAs and native clients follow similar protocol guidance:
3738

38-
* Use of a [PKCE code challenge](https://tools.ietf.org/html/rfc7636)
39-
* PKCE is *required* for SPAs on the Microsoft identity platform. PKCE is *recommended* for native and confidential clients.
40-
* No use of a client secret
39+
- Use of a [PKCE code challenge](https://tools.ietf.org/html/rfc7636)
40+
- PKCE is _required_ for SPAs on the Microsoft identity platform. PKCE is _recommended_ for native and confidential clients.
41+
- No use of a client secret
4142

4243
SPAs have two additional restrictions:
4344

44-
* [The redirect URI must be marked as type `spa`](v2-oauth2-auth-code-flow.md#redirect-uri-setup-required-for-single-page-apps) to enable CORS on login endpoints.
45-
* Refresh tokens issued through the authorization code flow to `spa` redirect URIs have a 24-hour lifetime rather than a 90-day lifetime.
45+
- [The redirect URI must be marked as type `spa`](v2-oauth2-auth-code-flow.md#redirect-uri-setup-required-for-single-page-apps) to enable CORS on login endpoints.
46+
- Refresh tokens issued through the authorization code flow to `spa` redirect URIs have a 24-hour lifetime rather than a 90-day lifetime.
4647

4748
:::image type="content" source="media/v2-oauth-auth-code-spa/active-directory-oauth-code-spa.svg" alt-text="Diagram showing the OAuth 2 authorization code flow between a single-page app and the security token service endpoint." border="false":::
4849

4950
## Performance and UX implications
5051

51-
Some applications using the implicit flow attempt sign-in without redirecting by opening a login iframe using `prompt=none`. In most browsers, this request will respond with tokens for the currently signed-in user (assuming consent has already been granted). This pattern meant applications did not need a full page redirect to sign the user in, improving performance and user experience - the user visits the web page and is signed in already. Because `prompt=none` in an iframe is no longer an option when third-party cookies are blocked, applications must visit the login page in a top-level frame to have an authorization code issued.
52+
Some applications using the implicit flow attempt sign-in without redirecting by opening a login iframe using `prompt=none`. In most browsers, this request will respond with tokens for the currently signed-in user (assuming consent has already been granted). This pattern meant applications didn't need a full page redirect to sign the user in, improving performance and user experience - the user visits the web page and is signed in already. Because `prompt=none` in an iframe is no longer an option when third-party cookies are blocked, applications must visit the login page in a top-level frame to have an authorization code issued.
5253

5354
There are two ways of accomplishing sign-in:
5455

55-
* **Full page redirects**
56-
* On the first load of the SPA, redirect the user to the sign-in page if no session already exists (or if the session is expired). The user's browser will visit the login page, present the cookies containing the user session, and then redirect back to the application with the code and tokens in a fragment.
57-
* The redirect does result in the SPA being loaded twice. Follow best practices for caching of SPAs so that the app is not downloaded in-full twice.
58-
* Consider having a pre-load sequence in the app that checks for a login session and redirects to the login page before the app fully unpacks and executes the JavaScript payload.
59-
* **Popups**
60-
* If the user experience (UX) of a full page redirect doesn't work for the application, consider using a popup to handle authentication.
61-
* When the popup finishes redirecting to the application after authentication, code in the redirect handler will store the code and tokens in local storage for the application to use. MSAL.js supports popups for authentication, as do most libraries.
62-
* Browsers are decreasing support for popups, so they may not be the most reliable option. User interaction with the SPA before creating the popup may be needed to satisfy browser requirements.
56+
- **Full page redirects**
57+
- On the first load of the SPA, redirect the user to the sign-in page if no session already exists (or if the session is expired). The user's browser will visit the login page, present the cookies containing the user session, and then redirect back to the application with the code and tokens in a fragment.
58+
- The redirect does result in the SPA being loaded twice. Follow best practices for caching of SPAs so that the app isn't downloaded in-full twice.
59+
- Consider having a pre-load sequence in the app that checks for a login session and redirects to the login page before the app fully unpacks and executes the JavaScript payload.
60+
- **Popups**
61+
- If the user experience (UX) of a full page redirect doesn't work for the application, consider using a popup to handle authentication.
62+
- When the popup finishes redirecting to the application after authentication, code in the redirect handler will store the code and tokens in local storage for the application to use. MSAL.js supports popups for authentication, as do most libraries.
63+
- Browsers are decreasing support for popups, so they may not be the most reliable option. User interaction with the SPA before creating the popup may be needed to satisfy browser requirements.
6364

64-
>[!NOTE]
65+
> [!NOTE]
6566
> Apple [describes a popup method](https://webkit.org/blog/8311/intelligent-tracking-prevention-2-0/) as a temporary compatibility fix to give the original window access to third-party cookies. While Apple may remove this transferral of permissions in the future, it will not impact the guidance here. Here, the popup is being used as a first party navigation to the login page so that a session is found and an auth code can be provided. This should continue working into the future.
6667
6768
### A note on iframe apps
@@ -76,6 +77,7 @@ This limited-lifetime refresh token pattern was chosen as a balance between secu
7677

7778
## Next steps
7879

79-
Learn more about the [authorization code flow](v2-oauth2-auth-code-flow.md).
80+
For more information about authorization code flow and Microsoft Authentication Library (MSAL) for JavaScript v2.0, see:
8081

81-
Try out the authorization code flow with the [MSAL.js 2.0 quickstart](quickstart-v2-javascript-auth-code.md).
82+
- [Authorization code flow](v2-oauth2-auth-code-flow.md).
83+
- [MSAL.js 2.0 quickstart](quickstart-v2-javascript-auth-code.md).

0 commit comments

Comments
 (0)