|
1 | 1 | ---
|
2 | 2 | title: Microsoft identity platform and OAuth 2.0 authorization code flow
|
3 |
| -description: Build web applications using the Microsoft identity platform implementation of the OAuth 2.0 authentication protocol. |
4 |
| -services: active-directory |
| 3 | +description: Protocol reference for the Microsoft identity platform's implementation of the OAuth 2.0 authorization code grant |
5 | 4 | author: davidmu1
|
6 | 5 | manager: CelesteDG
|
7 | 6 |
|
8 | 7 | ms.service: active-directory
|
9 | 8 | ms.subservice: develop
|
10 |
| -ms.workload: identity |
11 |
| -ms.topic: conceptual |
12 |
| -ms.date: 02/02/2022 |
| 9 | +ms.topic: reference |
| 10 | +ms.date: 07/29/2022 |
13 | 11 | ms.author: davidmu
|
14 | 12 | ms.reviewer: ludwignick
|
15 | 13 | ms.custom: aaddev, identityplatformtop40
|
16 | 14 | ---
|
17 | 15 |
|
18 | 16 | # Microsoft identity platform and OAuth 2.0 authorization code flow
|
19 | 17 |
|
20 |
| -The OAuth 2.0 authorization code grant can be used in apps that are installed on a device to gain access to protected resources, such as web APIs. Using the Microsoft identity platform implementation of OAuth 2.0 and Open ID Connect (OIDC), you can add sign in and API access to your mobile and desktop apps. |
| 18 | +The OAuth 2.0 authorization code grant type, or _auth code flow_, enables a client application to obtain authorized access to protected resources like web APIs. The auth code flow requires a user-agent that supports redirection from the authorization server (the Microsoft identity platform) back to your application. For example, a web browser, desktop, or mobile application operated by a user to sign in to your app and access their data. |
21 | 19 |
|
22 |
| -This article describes how to program directly against the protocol in your application using any language. When possible, we recommend you use the supported Microsoft Authentication Libraries (MSAL) to [acquire tokens and call secured web APIs](authentication-flows-app-scenarios.md#scenarios-and-supported-authentication-flows). For more information, look at [sample apps that use MSAL](sample-v2-code.md). |
| 20 | +This article describes low-level protocol details usually required only when manually crafting and issuing raw HTTP requests to execute the flow, which we do **not** recommend. Instead, use a [Microsoft-built and supported authentication library](reference-v2-libraries.md) to get security tokens and call protected web APIs in your apps. |
23 | 21 |
|
24 |
| -The OAuth 2.0 authorization code flow is described in [section 4.1 of the OAuth 2.0 specification](https://tools.ietf.org/html/rfc6749). With OIDC, this flow does authentication and authorization for most app types. These types include [single page apps](v2-app-types.md#single-page-apps-javascript), [web apps](v2-app-types.md#web-apps), and [natively installed apps](v2-app-types.md#mobile-and-native-apps). The flow enables apps to securely acquire an `access_token` that can be used to access resources secured by the Microsoft identity platform. Apps can refresh tokens to get other access tokens and ID tokens for the signed in user. |
| 22 | +## Applications that support the auth code flow |
25 | 23 |
|
26 |
| -[!INCLUDE [try-in-postman-link](includes/try-in-postman-link.md)] |
| 24 | +Use the auth code flow paired with Proof Key for Code Exchange (PKCE) and OpenID Connect (OIDC) to get access tokens and ID tokens in these types of apps: |
| 25 | + |
| 26 | +- [Single-page web application (SPA)](v2-app-types.md#single-page-apps-javascript) |
| 27 | +- [Standard (server-based) web application](v2-app-types.md#web-apps) |
| 28 | +- [Desktop and mobile apps](v2-app-types.md#mobile-and-native-apps) |
| 29 | + |
| 30 | +## Protocol details |
27 | 31 |
|
28 |
| -## Protocol diagram |
| 32 | +The OAuth 2.0 authorization code flow is described in [section 4.1 of the OAuth 2.0 specification](https://tools.ietf.org/html/rfc6749). Apps using the OAuth 2.0 authorization code flow acquire an `access_token` to include in requests to resources protected by the Microsoft identity platform (typically APIs). Apps can also request new ID and access tokens for previously authenticated entities by using a refresh mechanism. |
29 | 33 |
|
30 |
| -This diagram provides a high-level overview of the authentication flow for an application: |
| 34 | +[!INCLUDE [try-in-postman-link](includes/try-in-postman-link.md)] |
| 35 | + |
| 36 | +This diagram shows a high-level view of the authentication flow: |
31 | 37 |
|
32 | 38 | 
|
33 | 39 |
|
34 |
| -## Redirect URI setup required for single-page apps |
| 40 | +## Redirect URIs for single-page apps (SPAs) |
| 41 | + |
| 42 | +Redirect URIs for SPAs that use the auth code flow require special configuration. |
35 | 43 |
|
36 |
| -The authorization code flow for single page applications requires additional setup. Follow the instructions for [creating your single-page application](scenario-spa-app-registration.md#redirect-uri-msaljs-20-with-auth-code-flow) to correctly mark your redirect URI as enabled for Cross-Origin Resource Sharing (CORS). To update an existing redirect URI to enable CORS, open the manifest editor and set the `type` field for your redirect URI to `spa` in the `replyUrlsWithType` section. Or, you can select the redirect URI in **Authentication** > **Web** and select URIs to migrate to using the authorization code flow. |
| 44 | +- **Add a redirect URI** that supports auth code flow with PKCE and cross-origin resource sharing (CORS): Follow the steps in [Redirect URI: MSAL.js 2.0 with auth code flow](scenario-spa-app-registration.md#redirect-uri-msaljs-20-with-auth-code-flow). |
| 45 | +- **Update a redirect URI**: Set the redirect URI's `type` to `spa` by using the [application manifest editor](reference-app-manifest.md) in the Azure portal. |
37 | 46 |
|
38 |
| -The `spa` redirect type is backwards compatible with the implicit flow. Apps currently using the implicit flow to get tokens can move to the `spa` redirect URI type without issues and continue using the implicit flow. |
| 47 | +The `spa` redirect type is backward-compatible with the implicit flow. Apps currently using the implicit flow to get tokens can move to the `spa` redirect URI type without issues and continue using the implicit flow. |
39 | 48 |
|
40 | 49 | If you attempt to use the authorization code flow without setting up CORS for your redirect URI, you will see this error in the console:
|
41 | 50 |
|
@@ -323,7 +332,7 @@ This example is an Error response:
|
323 | 332 | |`invalid_scope` | The scope requested by the app is invalid. | Update the value of the `scope` parameter in the authentication request to a valid value. |
|
324 | 333 |
|
325 | 334 | > [!NOTE]
|
326 |
| -> Single page apps may receive an `invalid_request` error indicating that cross-origin token redemption is permitted only for the 'Single-Page Application' client-type. This indicates that the redirect URI used to request the token has not been marked as a `spa` redirect URI. Review the [application registration steps](#redirect-uri-setup-required-for-single-page-apps) on how to enable this flow. |
| 335 | +> Single page apps may receive an `invalid_request` error indicating that cross-origin token redemption is permitted only for the 'Single-Page Application' client-type. This indicates that the redirect URI used to request the token has not been marked as a `spa` redirect URI. Review the [application registration steps](#redirect-uris-for-single-page-apps-spas) on how to enable this flow. |
327 | 336 |
|
328 | 337 | ## Use the access token
|
329 | 338 |
|
|
0 commit comments