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 code verifier (PKCE) #42

Merged
merged 7 commits into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions google_auth_oauthlib/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ def __init__(
redirect_uri (str): The OAuth 2.0 redirect URI if known at flow
creation time. Otherwise, it will need to be set using
:attr:`redirect_uri`.

code_verifier: random string of 43-128 chars used to verify the
key exchange.using PKCE. Auto-generated if not provided.
jay0lee marked this conversation as resolved.
Show resolved Hide resolved
.. _client secrets:
https://developers.google.com/api-client-library/python/guide
/aaa_client_secrets
Expand Down Expand Up @@ -222,9 +223,9 @@ def authorization_url(self, **kwargs):
rnd = SystemRandom()
random_verifier = [rnd.choice(chars) for _ in range(0, 128)]
self.code_verifier = ''.join(random_verifier)
c = hashlib.sha256()
c.update(str.encode(self.code_verifier))
unencoded_challenge = c.digest()
code_hash = hashlib.sha256()
code_hash.update(str.encode(self.code_verifier))
unencoded_challenge = code_hash.digest()
b64_challenge = urlsafe_b64encode(unencoded_challenge)
code_challenge = b64_challenge.decode().split('=')[0]
kwargs.setdefault('code_challenge', code_challenge)
Expand Down
20 changes: 20 additions & 0 deletions tests/test_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from functools import partial
import json
import os
import re

import mock
import pytest
Expand Down Expand Up @@ -123,6 +124,25 @@ def test_authorization_url_access_type(self, instance):
code_challenge='2yN0TOdl0gkGwFOmtfx3f913tgEaLM2d2S0WlmG1Z6Q',
code_challenge_method='S256')

def test_authorization_url_generated_verifier(self, instance):
scope = 'scope_one'
instance.oauth2session.scope = [scope]
authorization_url_path = mock.patch.object(
instance.oauth2session, 'authorization_url',
wraps=instance.oauth2session.authorization_url)

with authorization_url_path as authorization_url_spy:
url, _ = instance.authorization_url()

_, kwargs = authorization_url_spy.call_args_list[0]
Copy link

Choose a reason for hiding this comment

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

The mock has an assert_called_once_with helper for this, e.g.:

    authorization_url_spy..assert_called_once_with(code_challenge_method='S256')

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks, I can do this for checking code_challenge_method but the other values are randomized and I'm just doing size and character type validations but I think I need to unpack the arguments to do those regex comparisons.

Definitely open to suggestions here. Thanks.

assert kwargs['code_challenge_method'] == 'S256'
assert len(instance.code_verifier) == 128
assert len(kwargs['code_challenge']) == 43 # 128 char verifier
valid_verifier = r'^[A-Za-z0-9-._~]*$'
valid_challenge = r'^[A-Za-z0-9-_]*$'
assert re.match(valid_verifier, instance.code_verifier)
assert re.match(valid_challenge, kwargs['code_challenge'])

def test_fetch_token(self, instance):
instance.code_verifier = 'amanaplanacanalpanama'
fetch_token_patch = mock.patch.object(
Expand Down