_pkce.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Copyright 2016 Google Inc. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """
  15. Utility functions for implementing Proof Key for Code Exchange (PKCE) by OAuth
  16. Public Clients
  17. See RFC7636.
  18. """
  19. import base64
  20. import hashlib
  21. import os
  22. def code_verifier(n_bytes=64):
  23. """
  24. Generates a 'code_verifier' as described in section 4.1 of RFC 7636.
  25. This is a 'high-entropy cryptographic random string' that will be
  26. impractical for an attacker to guess.
  27. Args:
  28. n_bytes: integer between 31 and 96, inclusive. default: 64
  29. number of bytes of entropy to include in verifier.
  30. Returns:
  31. Bytestring, representing urlsafe base64-encoded random data.
  32. """
  33. verifier = base64.urlsafe_b64encode(os.urandom(n_bytes)).rstrip(b'=')
  34. # https://tools.ietf.org/html/rfc7636#section-4.1
  35. # minimum length of 43 characters and a maximum length of 128 characters.
  36. if len(verifier) < 43:
  37. raise ValueError("Verifier too short. n_bytes must be > 30.")
  38. elif len(verifier) > 128:
  39. raise ValueError("Verifier too long. n_bytes must be < 97.")
  40. else:
  41. return verifier
  42. def code_challenge(verifier):
  43. """
  44. Creates a 'code_challenge' as described in section 4.2 of RFC 7636
  45. by taking the sha256 hash of the verifier and then urlsafe
  46. base64-encoding it.
  47. Args:
  48. verifier: bytestring, representing a code_verifier as generated by
  49. code_verifier().
  50. Returns:
  51. Bytestring, representing a urlsafe base64-encoded sha256 hash digest,
  52. without '=' padding.
  53. """
  54. digest = hashlib.sha256(verifier).digest()
  55. return base64.urlsafe_b64encode(digest).rstrip(b'=')