Skip to content

[Feature Request] During auth code redemption, send code_verifier to /token endpoint as part of auth code + pkceΒ #470

Closed

Description

@jennyf19

Background
For details, see the spec for auth code + pkce for public clients.

Auth code + pkce was originally implemented for public clients as they are susceptible to having the auth code intercepted. However, more and more confidential clients are wanting to implement pkce:
image

ASP NET Core surfaces the code_verifier in the AuthenticationProperties collection on the event context.

Part of this work is adding a client secret for the web app, in the templates, now as the secret will be used to verify the client.

Next Steps
MSAL .NET needs to support auth code + pkce for confidential clients first, so that we can use it during auth code redemption. See issue here.

ASP NET starts the first leg of the auth code flow, and includes code_challenge and code_challenge_method to the /authorize endpoint.

Then, the second part, MS Identity Web, using MSAL, will need to send the code + code_verifier (which we will get in the AuthenticationProperties) so that the server can verify the original code_challenge.

We need to complete the work in MSAL .NET first.
We can use the MSAL Java implementation for this.

Here is where the developer provides the code_challenge and code_challenge_method:
https://github.com/AzureAD/microsoft-authentication-library-for-java/blob/dev/src/main/java/com/microsoft/aad/msal4j/AuthorizationRequestUrlParameters.java#L84

And where the developer passes in the code_verifier.
https://github.com/AzureAD/microsoft-authentication-library-for-java/blob/dev/src/main/java/com/microsoft/aad/msal4j/AuthorizationCodeParameters.java#L47

Activity

  1. jmprieur commented on Mar 17, 2021

    @jmprieur
    Collaborator
  2. jennyf19 commented on Mar 23, 2021

    @jennyf19
    CollaboratorAuthor

    What I'd like to see for this:
    A new override of AcquireTokenByAuthorizationCode that takes the code_verifier. The changes to Id Web should be minimal, 1-2 lines. Just getting the code_verifier from the event context on the AuthenticationProperties in ASP.NET and giving to the AcquireTokenByAuthorizationCode method.
    Like this for example:

    public async Task AddAccountToCacheFromAuthorizationCodeAsync(
        AuthorizationCodeReceivedContext context,
        IEnumerable<string> scopes)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }
    
        if (scopes == null)
        {
            throw new ArgumentNullException(nameof(scopes));
        }
    
        try
        {
            _application = await GetOrBuildConfidentialClientApplicationAsync().ConfigureAwait(false);
    
            context.TokenEndpointRequest.Parameters.TryGetValue(OAuthConstants.CodeVerifierKey, out string? codeVerifier);
            var builder = _application
                .AcquireTokenByAuthorizationCode(
                           scopes.Except(_scopesRequestedByMsal),
                           context.ProtocolMessage.Code)
                .WithSendX5C(_microsoftIdentityOptions.SendX5C)
                .WithPkceCodeVerifier(codeVerifier);
    ....
    //more here
  3. self-assigned this
    on Apr 20, 2021
  4. added this to the 1.10.0 milestone on Apr 20, 2021
  5. jmprieur commented on Apr 22, 2021

    @jmprieur
    Collaborator

    Bloked until MSAL.NET 4.30 releases

  6. removed this from the 1.10.0 milestone on May 1, 2021
  7. 5 remaining items

  8. tedvanderveen commented on May 6, 2021

    @tedvanderveen

    Jean-Marc Prieur (@jmprieur) jennyf19 for PKCE flows in B2C, you need a "Single Page application" platform, although the name is a bit confusing here. As stated in the initial post of this Issue, PKCE can/should now also be used by confidential web applications. So I assume selecting "Single Page application" is what we need to get Auth code + PKCE in B2C play nice with the new bits introduced with latest version (1.9.2) of this library?

  9. jmprieur commented on May 6, 2021

    @jmprieur
    Collaborator

    Ted van der Veen (@tedvanderveen), I think that this is because you don't need to do anything in the B2C app registration, in order to enable PKCE in a B2C web app.

    So, no. don't add "Single Page application". It would only work with blazorwasm applications or MSAL.js applications.

  10. jennyf19 commented on May 6, 2021

    @jennyf19
    CollaboratorAuthor

    Jean-Marc Prieur (@jmprieur) is correct, you shouldn't have to do anything as app developer using Microsoft Identity Web to enable PCKE, it's on by default and your app should just work as it did before, with no code or registration changes.

  11. tedvanderveen commented on May 6, 2021

    @tedvanderveen

    jennyf19 Jean-Marc Prieur (@jmprieur) FYI: I got it working finally with the B2C Single Page application.
    Trick was to add an additional scope (see last line) for the client_id:

                    .AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
                    .AddMicrosoftIdentityWebApp(options =>
                    {
                        options.Instance = "https://tenantnamehere.b2clogin.com";
                        options.ClientId = "put-client-id-here";
                        options.Domain = "tenantnamehere.onmicrosoft.com";
                        options.TenantId = "put-tenantid-here";
                        options.SignUpSignInPolicyId = "b2c_1_signup_signin";
                        options.ResetPasswordPolicyId = "b2c_1_passwordreset";
                        options.ResponseType = OpenIdConnectResponseType.Code;
                        options.Scope.Add(options.ClientId);
                    });
    
  12. Ponant commented on May 11, 2021

    @Ponant
    Contributor

    Ted van der Veen (@tedvanderveen) , I have the same question, so you did register as SPA + the configuration above, and you can get pkce?

  13. tedvanderveen commented on May 11, 2021

    @tedvanderveen

    Ponant yes it just works!

  14. Ponant commented on May 11, 2021

    @Ponant
    Contributor

    Razor pages? It does not work locally, but I just implemented so I will check further.

  15. tedvanderveen commented on May 11, 2021

    @tedvanderveen

    Did you set B2C up with the settings like what I posted here in the screenshot?

  16. Ponant commented on May 11, 2021

    @Ponant
    Contributor

    Ted van der Veen (@tedvanderveen) , that is what I was doing because I am receiving an error, which I believe is telling me switch from web to spa on the app registration. I also have ot change the call backs. What I would like to ask you is why you need the

      options.ResponseType = OpenIdConnectResponseType.Code;
                        options.Scope.Add(options.ClientId);
    
    
  17. Ponant commented on May 11, 2021

    @Ponant
    Contributor

    I mean, it seems the UsePkce is on by default, so I wonder why not everything is wired on by default. When I put the two options above, I get the code_challenge in the url, so it is good news (then it crashes because I need to configure to spa in azure, but that is ok). If I remove the two options, I do not see the code challenge. Any thought?

  18. Ponant commented on May 11, 2021

    @Ponant
    Contributor

    for your screenshot, I think it should work if you leave the public client switch off, correct? I have another app which is blazor wasm and it uses b2c with pkce, and it is basically your screenshot but with public/native switched off, and this makes sense to me.

  19. jmprieur commented on May 11, 2021

    @jmprieur
    Collaborator

    Ponant, Ted van der Veen (@tedvanderveen) : you don't need to do anything to activate PKCE. This is done on the web app (code) side. the app registration does not change at all. In particular the redirect URI for a webapp, should be Web, not SPA, unless you're building a blazorwasm application on .NET 5.0 or later

  20. Ponant commented on May 11, 2021

    @Ponant
    Contributor

    If I do this (with razor pages .net 5 b2c, upgraded to latest nuget 1.9.2), then I do not get the code_challenge sent out

  21. Ponant commented on May 11, 2021

    @Ponant
    Contributor

    I can confirm anew. I tested several options (even knowingly that it won't work).

    In the portal, with a web option:

    If I disable implicit grant (Access and idtokens checkboxes) I get the error (before login flow). And Url has no code_challenge

    AADB2C90057: The provided application is not configured to allow the 'OAuth' Implicit flow.

    If I enable only ID tokens (used for implicit and hybrid flows), the same error.

    If I add to the startup.cs
    options.ResponseType = OpenIdConnectResponseType.Code; options.Scope.Add(options.ClientId);
    Then I see a code_challenge in the url
    Then I can go through the login flow but then it throws
    'AADB2C90079: Clients must send a client_secret when redeeming a confidential grant.

    If I remove the code above, which is essentially the base template, then the app works but no code_challenge

  22. Ponant commented on May 11, 2021

    @Ponant
    Contributor

    Jean-Marc Prieur (@jmprieur) , I checked out further, and the only way to make it work from the base template (updated nuget) is to do on the portal a spa registration, so Ted van der Veen (@tedvanderveen) is right. So I do not understand how you get it to work with the web option in azure?
    The portal even hapily displays "Your Redirect URI is eligible for the Authorization Code Flow with PKCE."
    Here is the output manifest

    "replyUrlsWithType": [
    		{
    			"url": "https://localhost:44339/signin-oidc",
    			"type": "Spa"
    		}
    

    Edit: Furthermore, the options.ResponseType = OpenIdConnectResponseType.Code; options.Scope.Add(options.ClientId); need to be added in startup. SO I believe we disagree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions