Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement RFC 7636 PKCE in OAuth client and fix redirect URI bug #102

Merged
merged 2 commits into from
Apr 23, 2020

Conversation

mjcheetham
Copy link
Collaborator

Implement the Proof Key for Code Exchange (RFC 7636) specification in the OAuth2 client. This helps prevent hijacking of the authorisation code by malicious applications running on the user's machine.

Also fix a bug that was identified in the use of redirect URIs between the authorisation and token endpoints. If the redirect URI is "localhost" without an explicit port number, the port number we find and open must be included in the redirect URI sent to the token endpoint (i.e, the redirect URI containing the generated port number used in the token endpoint call).

Implement the Proof Key for Code Exchange (RFC 7636) specification in
the OAuth2 client.
Copy link

@bgavrilMS bgavrilMS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PKCE looks good

//
// 96 bytes -> 768 bits -> 128 base64url characters (6 bits per character)
//
var buf = new byte[96];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is also how we do it https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/blob/94e6e2e3baf840b9e06c8afbe2197d8ec0c0871f/src/client/Microsoft.Identity.Client/Platforms/netcore/NetCoreCryptographyManager.cs#L21

But we use 32 bytes... and perform the padding logic - base64url encoding (which is classical base64 + padding) is very common in OAuth ...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for the 32 bytes? (42.6667 base64 chars with a pad that'll always be trimmed)?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember the reason, there was probably a reference implementation somewhere that was used. It did pass security reviews though.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But obviously your argument of using 96 bytes for more entropy is valid, this is a better implementation.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll log a bug on MSAL.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nice thing about 96 bytes is this works out to a base64url string that doens't need padding!
We still trim any padding characters to be doubly sure however 😁

@mjcheetham mjcheetham merged commit 7efe852 into git-ecosystem:master Apr 23, 2020
@mjcheetham mjcheetham deleted the oauth2-pkce branch April 23, 2020 14:58
@mjcheetham mjcheetham mentioned this pull request Apr 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auth:oauth Specific to OAuth2 authentication enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants