OAuth 2.0 PKCE, the Proof Key for Code Exchange

In addition to the OAuth 2.0 grant types that differ by use case, such as authorization code, implicit, or password credentials, custom extensions can also be integrated into the protocol. One example is the security extension PKCE (Proof Key for Code Exchange). It enables a special technique with which “public” clients, such as a mobile app, can protect themselves so that an authorization code in transit cannot easily be intercepted and misused.

The core function of PKCE is that the client first creates a secret key (secret) and then uses it again when applying for an access token. This makes it possible to ensure that even in the event of an attack and an interception of the token, the token cannot be used, because the token request is based on the secret.

When Should the PKCE Extension Be Used?

After this brief explanation, the question arises as to which scenarios the PKCE extension is best suited for. The following two examples present the area of use and describe it in more detail.

  • Whenever you use a native OAuth 2.0 client, you should rely on the OAuth2 extension PKCE. The client can be a normal application on an ordinary PC system or take the form of an app for a mobile device.
  • As soon as the OAuth 2.0 client is “public” and therefore has no secured credentials for authenticating at the token endpoint, you should consider applying the PKCE extension and, if possible, install it as well.

The Standard OAuth 2.0 Authorization Flow in Detail

With OAuth 2.0, it is very easy to authorize a mobile app, for example, to access the API endpoints. The following authorization flow describes an exemplary context with its basic functions.

  • When you install a new mobile app on the device, this application registers with the mobile operating system as a handler for special URL schemes. These could be registered according to the scheme com.example-mobileapp://, for example.
  • After the mobile app is opened for the first time, the user is generally asked to authenticate for further use of the app and as part of the data exchange.
  • As a rule, the mobile app automatically starts the system browser for this purpose and opens a page on the AS (authorization server) provided for it.
  • On the AS, the user is now received with a corresponding dialog and asked to enter their user data. In addition, special permissions of the app are generally shown at this point and the user is asked to approve this access. Because it runs in a browser, the authorization itself uses either SSO or two-factor authorization.
  • As soon as the user completes the authorization, the AS creates a corresponding URL according to a scheme that matches the native application, such as com.example-mobileapp://…oauth?code=a5P3s7z15FF9a558. The mobile app is then instructed to log in again at this URL.
  • The mobile browser now asks the operating system how to proceed with this URL and then passes it to the corresponding application.
  • The native app now analyzes the URL it has been passed and extracts the encoded authorization code for authentication.
  • In the next step, the native app calls the AS and passes on the authorization code for verification.
  • The AS in turn now validates the authorization code and creates one or more valid access tokens from it. These are sent back to the native app.
  • The native app on the PC or mobile device receives this validated token and places it in secure storage for further access and authorizations.

Integrating OAuth 2.0 PKCE into the Authorization Flow

At present, however, there is a high risk that steps 6 to 8 of the authorization flow shown above can be manipulated. This opens a security gap and allows outside access to the authorization. If outside access has taken place, the manipulation is passed on to the AS in the eighth step and thus remains undetected. By integrating OAuth 2.0 PKCE, however, a native application or app can create a short-lived secret and add it to the authorization code. The authorization flow then looks as follows.

  • First, steps 1 to 3 of the standard authorization flow from the example above are carried out.
  • When the native app loads the AS page in the browser, a “code_verifier” string is created and passed to the URL as a parameter. The AS stores this string.
  • As soon as the native app exchanges the code for the access token (see step 8 in the standard authorization flow), the request contains the string again. If this string is missing or has been replaced through outside access, the AS will neither create nor send the requested access token.
  • Even if the attacker manages to obtain a code without the “code_verifier,” they cannot convert it into a valid access token.

Additional Security with PKCE

OAuth 2.0 with PKCE offers a significant security improvement, above all for native and mobile applications. The changed authorization sequence and the additional client secret counteract manipulated access to important private or business data. The ability of PKCE to integrate transient client secrets into token generation offers effective protection against outside access and hacked API calls. The complete OAuth PKCE flow and its detailed application are defined in full in RFC 7636.

Share LinkedIn X Email