-
Notifications
You must be signed in to change notification settings - Fork 309
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
Add JWT credentials #21
Conversation
01c1ea8
to
bf19834
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love how test-able this code is
|
||
The constructor arguments determine the claims for the JWT that is | ||
sent with requests. Usually, you'll construct these credentials with | ||
one of the helper constructors. |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Note that JWT credentials will also set the audience claim on demand. If no | ||
audience is specified when creating the credentials, then whenever a | ||
request is made the credentials will automatically generate a one-time | ||
JWT with the request URI as the audience. |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
signer = crypt.Signer.from_string(private_key, key_id) | ||
|
||
kwargs.setdefault('subject', email) | ||
return cls(signer, issuer=email, **kwargs) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
issuer=issuer if issuer is not None else self._issuer, | ||
subject=subject if subject is not None else self._subject, | ||
audience=audience if audience is not None else self._audience, | ||
additional_claims=dict(self._additional_claims).update( |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
'sub': self._subject or self._issuer, | ||
'iat': _helpers.datetime_to_secs(now), | ||
'exp': _helpers.datetime_to_secs(expiry), | ||
'aud': audience or self._audience |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
def test_sign_bytes(self): | ||
to_sign = b'123' | ||
signature = self.credentials.sign_bytes(to_sign) | ||
crypt.verify_signature(to_sign, signature, PUBLIC_CERT_BYTES) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
assert not self.credentials.expired | ||
|
||
with mock.patch('google.auth._helpers.utcnow') as now: | ||
one_day_from_now = datetime.timedelta(days=1) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
mock.Mock(), 'GET', 'http://example.com?a=1#3', headers) | ||
|
||
header_value = headers['authorization'] | ||
token = header_value.split().pop() |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
None, 'GET', 'http://example.com?a=1#3', headers) | ||
|
||
header_value = headers['authorization'] | ||
token = header_value.split().pop() |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
credentials = self.credentials.with_claims(audience='test') | ||
credentials.refresh(None) | ||
credentials.before_request( | ||
None, 'GET', 'http://example.com?a=1#3', headers) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
SUBJECT = 'subject' | ||
AUDIENCE = 'audience' | ||
ADDITIONAL_CLAIMS = {'meta': 'data'} | ||
credentials = None |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@@ -302,7 +306,7 @@ def test_before_request_one_time_token(self): | |||
mock.Mock(), 'GET', 'http://example.com?a=1#3', headers) | |||
|
|||
header_value = headers['authorization'] | |||
token = header_value.split().pop() | |||
token = header_value.split('Bearer ').pop() |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
LGTM |
(Has #8 as a diffbase)