Loading
Identify Your Users and Manage Access
Table of Contents
Select Filters

          No results
          No results
          Here are some search tips

          Check the spelling of your keywords.
          Use more general search terms.
          Select fewer filters to broaden your search.

          Search all of Salesforce Help
          Proof Key for Code Exchange (PKCE) Extension

          Proof Key for Code Exchange (PKCE) Extension

          To improve the security of your OAuth and authentication provider implementations, use the OAuth 2.0 Proof Key for Code Exchange (PKCE) extension. You can require PKCE at an org-wide level, require it for a specific connected app, and automatically enable it for supported authentication providers.

          Required Editions

          Available in: all editions

          During the OAuth authorization code flow and its derivatives, the PKCE extension helps ensure that the client that initiates the flow is the same client that completes the flow. During these flows, an app requests an authorization code from Salesforce, which it then exchanges for an access token. Traditionally, the app can secure the token request by including its connected app consumer secret. Because the consumer secret is information that only the app and Salesforce know, it functions as a password for accessing the Salesforce token endpoint. Even if an attacker somehow gains access to an authorization code, they can’t use it to get an access token unless they have the consumer secret.

          But public clients like mobile apps and single-page apps can’t keep the consumer secret confidential. Unlike private client apps with a traditional client-server architecture, public clients don’t have a private backend where they can store the consumer secret. If a public client sends the consumer secret in the token request, it risks leaking it. For this reason, we never recommend including the consumer secret in token requests for a public client.

          With no consumer secret, the app is vulnerable to an attacker intercepting the authorization code and attempting to exchange it for an access token. PKCE helps fill the consumer secret gap with a set of hashed parameters that only your app and Salesforce can understand. During a flow that implements PKCE, the app creates a random string, known as the code_verifier parameter. It then hashes this value with an SHA-256 algorithm. This hashed value is the code_challenge parameter. A key concept of SHA-256 hashing is that it only works one way. You can get the code_challenge from the code_verifier by running the SHA-256 algorithm. But you can’t run it backwards to get the code_verifier from the code_challenge.

          The concept of a one-way function plays an important role in why PKCE works. When the app sends its initial request for an authorization code, it includes the hashed code_challenge parameter. Then, when it requests an access token, it includes the code_verifier parameter. Now, Salesforce has both parameters, and it can run the SHA-256 algorithm on the code_verifier value to compare it to the code_challenge. If the values match, Salesforce can verify that the same app sent the authorization request and the token request. Even if an attacker has the authorization code, they can’t exchange it for an access token because they don’t have the PKCE parameters that only your app and Salesforce know.

          We recommend that you always implement PKCE for public clients. For private clients, we recommend that you implement PKCE and include the consumer secret in token requests.

          You can optionally implement PKCE by including code_challenge and code_verifier parameters when you build variations of the authorization code flow with Salesforce. These flows support PKCE.

          To ensure that PKCE is enforced, we recommend that you take these security measures.

          • Generate PKCE Parameters
            Use the PKCE Generator endpoint to quickly generate code_challenge and code_verifier parameters that you can implement in variations of the authorization code flow.

          Generate PKCE Parameters

          Use the PKCE Generator endpoint to quickly generate code_challenge and code_verifier parameters that you can implement in variations of the authorization code flow.

          1. Enable Cross-Origin Resource Sharing (CORS) for OAuth endpoints, which automatically enables CORS for the /services/oauth2/pkce/generator endpoint.
          2. To generate the code_challenge and code_verifier parameters, send an HTTP GET request to the /services/oauth2/pkce/generator endpoint on your My Domain login URL or Experience Cloud site URL. Don’t include any headers or body parameters in this request.
            Here’s an example request that uses a My Domain login URL.
            GET /services/oauth2/pkce/generator? HTTP 1.1
            Host: MyDomainName.my.site.salesforce.com
            Here’s an example request that uses an Experience Cloud site URL.
            GET /services/oauth2/pkce/generator? HTTP 1.1
            Host: MyDomainName.my.site.com
            Salesforce responds with these PKCE parameters.
            ParameterDescription
            code_challenge_method The method used to compute the code_challenge from the code_verifier. Salesforce uses the S256 method, which computes the code_challenge from the code_verifier using an SHA256 algorithm. For more information, see Section 4.2 in RFC 7636: Proof Key for Code Exchange by OAuth Public Clients.
            code_challenge

            Specifies the SHA256 hash value of the code_verifier value in the token request. The value is Base-64 URL encoded as defined in Section 5 of RFC 4648: The Base16, Base32, and Base64 Data Encodings.

            When you configure your authorization flow, include this value in the authorization request.

            code_verifier

            Specifies 128 bytes of random data with high entropy to make guessing the code value difficult. Set this parameter to help prevent authorization code interception attacks. The value is Base-64 URL encoded as defined in Section 5 of RFC 4648: The Base16, Base32, and Base64 Data Encodings.

            When you configure your authorization flow, include this value in the token request.

            Here’s an example response.
            {
            “code_challenge_method”:“S256”
            “code_challenge”:“JB7nT*************”
            “code_verifier”:“GkLvw**************”
            }

          When you configure the web server flow, hybrid web server flow, and variations of the Authorization Code and Credentials Flow, include the code_challenge and code_verifier parameters in your requests as directed.

           
          Loading
          Salesforce Help | Article