Update create_code_verifier to output the proper length #876
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Previously,
create_code_verifier
would callsecrets.token_urlsafe(nbytes=length)
to produce the code verifier, but this leads to incorrect behaviour because the string returned bytoken_urlsafe
doesn't have lengthnbytes
. Instead it has lengthnbytes * (4/3)
since the output is base64-encoded.This means that, for example, you could call
create_code_verifier(length=128)
and you would get a result with length 171, which exceeds the 128-character maximum length of the code verifier allowed by the spec. But this wouldn't be caught by the checks in the code, since they only check the input length parameter and not the actual output length.This PR fixes the issue by using the
generate_token
utility function instead, which does output strings of the exact length you specify. It also adds a test to make sure the length of the outputted string is equal to the specified length.