PKCE Parameter Support

Created on 23 February 2022, over 2 years ago
Updated 17 June 2024, 8 days ago

Problem/Motivation

A security scan run by the IT department of one of our clients detected that our openid_connect based SSO didn't use PKCE/the code_verify parameter. It flagged this as a high priority issue so we're trying to add it. We're using this with Azure's Open ID which does recommend using it wherever possible: https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth...

Steps to reproduce/test

  1. Select or configure a site with this module enabled.
  2. Select or create an IdP that supports PKCE. E.g. configure a Google IDP: https://developers.google.com/identity/protocols/oauth2/openid-connect
  3. Navigate to /admin/config/people/openid-connect
  4. Add an OpenID client of the appropriate type, e.g. Google
  5. Populate all relevant details - name, client ID, client secret etc.
  6. Navigate to /admin/config/people/openid-connect/settings
  7. Navigate to /user/login
  8. Open the web browser's developer tools and switch to the network tab
  9. Click the login link for the client you just added
  10. Observe the request to the authorize endpoint - see that it does not include the code_challenge and code_challenge_method parameters (or that it does if you're testing the change instead of reproducing the issue)
  11. If testing this change instead of reproducing the issue complete the login process and make sure it works as expected

Proposed resolution

Add support for the relevant parameters.

Remaining tasks

It might make sense to override some other plugins to support this. E.g. the generic handler could potentially use PKCE based on autodiscovery or a form option.

User interface changes

None yet, but we may want to add an option to the generic plugin as above.

API changes

OpenIDConnectGenericClient and OpenIDConnectClientBase now have a usesPkce() method which by default returns FALSE. If this method is implemented and returns TRUE the plugin will add the code_challenge and code_challenge_method parameters in the initial authorization and the code_verifier parameter when posting to the token endpoint.

Feature request
Status

Needs review

Version

3.0

Component

Code

Created by

🇨🇦Canada Dylan Donkersgoed London, Ontario

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇨🇭Switzerland bircher 🇨🇿

    This works for us. Though our custom plugin needs to unset the client secret and disable the checkbox on the form because for security reasons PKCE is required and sending the secret is prohibited.

    But this is easily achieved with:

      /**
       * {@inheritdoc}
       */
      protected function getTokenRequestOptions(string $authorization_code, string $redirect_uri): array {
        $options = parent::getTokenRequestOptions($authorization_code, $redirect_uri);
        unset($options['form_params']['client_secret']);
    
        return $options;
      }
    
    

    So this patch works for us, and since it is opt in, I can RTBC it.

  • 🇧🇷Brazil carolpettirossi Campinas - SP

    I'm a little confused about how to make the PKCE work with the OAuth2 since client_secret is required. Should I create a custom plugin?

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update 8 months ago
    100 pass
  • 🇧🇷Brazil carolpettirossi Campinas - SP

    Attaching patch from MR#59 to use with composer.

  • Status changed to Needs work 8 months ago
  • 🇵🇹Portugal jcnventura

    I don't really like the explosion of settings in the settings form. Can we move the "On/Off" switch of this into the transformation settings, please? So the transformation options would be "Off / SHA-256 / Plain".

    Also this needs a hook_update to add this setting to existing installs.

  • 🇧🇪Belgium lorenzs

    In the meantime I can confirm this is working on a D10/PHP8 site with PKCE/SHA256 enabled.
    For me the use of the setting in the form is very clear (off, on - then choose transformation) but fine if it would be 1 toggle. That also prevents having 'no' transformation on existing installs when enabling the config only via code - without saving the settings form.

  • First commit to issue fork.
  • 🇧🇪Belgium interX Flanders

    I rerolled the 2.x branch into 3266205-pkce-flow-2022-10--3.x.

    I am using this version and everything works as expected here.
    The update hook has been added to set the default config of existing plugins.

    Note that the new session service that gets injected into the base client will require a change in other contrib modules that provide a client (like openid_connect_windows_aad).

  • 🇧🇪Belgium interX Flanders

    interX changed the visibility of the branch revert-ec71b33d to hidden.

  • Merge request !96Issue #3266205 PKCE Parameter Support → (Closed) created by interX
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.1 + Environment: PHP 8.1 & MySQL 5.7
    last update 5 months ago
    100 pass
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.1 + Environment: PHP 8.1 & MySQL 5.7
    last update 5 months ago
    100 pass
  • 🇪🇸Spain abelass

    I can't apply patch from MR 96 to 3.0.0-alpha2 with composer. Anybody has a working patch for 3. or 2. ?

  • 🇧🇪Belgium interX Flanders

    You have to use the dev-version to test the patch. composer.json should have an entry like:

    "drupal/openid_connect": "^3.x-dev"

  • Status changed to RTBC 2 months ago
  • 🇧🇪Belgium lorenzs

    Tested.. again.. PKCE is a widely used standard and this issue is now open for more than 2 years keeping us busy with MR's and patches that were tested and reviewed... I do not think anyone is offended by an extra setting in the mentioned config form so please make another alpha release - it is alpha for a reason and setting can easily be skipped if not needed.

  • 🇮🇳India Tilottama11

    Can this pkce patch be released in next alpha release?

  • Status changed to Needs work 24 days ago
  • 🇵🇹Portugal jcnventura

    Several problems still stand.

    1. Nobody did anything regarding my comment on #23
    2. The MRs are broken at the moment. Among others they reintroduce file_save_data() which was removed in Drupal 10. Merging this MR would remove support for Drupal 10. I'm closing the MRs, as I believe it might be best to start from the patch in #22
    3. Not sure what the RTBCs are for, if for the #22 patch or the MRs. Hopefully it's for the patch. It looks good. As long as it is simplified somewhat to have a single toggle as I requested in #23.

    This only needs someone to improve on #22 (maybe open a new MR with the contents of that patch as first commit, and then do the changes I requested in #23 as a second commit). And then someone else marks it as RTBC and it should get merged fast.

  • Merge request !113PKCE integration → (Open) created by interX
  • Pipeline finished with Success
    9 days ago
    Total: 142s
    #200865
  • 🇧🇪Belgium interX Flanders

    I have recreated a MR from 3.x with remarks from #35. The property name pkce_challenge_transformation was kept, so anyone using one of the previous patches can update without issue. The property use_pkce can be removed from config.

  • Pipeline finished with Success
    9 days ago
    Total: 147s
    #200869
  • Status changed to Needs review 8 days ago
  • 🇵🇹Portugal jcnventura

    Nice. Hopefully someone can also test and review it.

Production build 0.69.0 2024