-
Notifications
You must be signed in to change notification settings - Fork 310
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
feat: asyncio http request logic and asynchronous credentials logic #572
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ef6d872
feat: asyncio http request logic and asynchronous credentials log c
anibadde 06606ff
feat: added the private scope for Response class
anibadde cd57fad
feat: added docstring for Auth Session request method
anibadde a38e333
fix: Changed initialization of client session to within an async cont…
anibadde 57d6d10
changed aiohttp_requests abbreviation for the async authorized sessio…
anibadde aa6ece2
fix: comments on PR regarding shared data between requests and aiohtt…
anibadde d88839b
fix: fixed noxfile test dependency sharing
anibadde c62dd1a
fix: fixed the noxfile dependencies between sync and async unit tests
anibadde 7080d14
fix: cover async dependency
anibadde File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
"""Interfaces for credentials.""" | ||
|
||
import abc | ||
|
||
import six | ||
|
||
from google.auth import credentials | ||
|
||
|
||
@six.add_metaclass(abc.ABCMeta) | ||
class Credentials(credentials.Credentials): | ||
"""Async inherited credentials class from google.auth.credentials. | ||
The added functionality is the before_request call which requires | ||
async/await syntax. | ||
All credentials have a :attr:`token` that is used for authentication and | ||
may also optionally set an :attr:`expiry` to indicate when the token will | ||
no longer be valid. | ||
|
||
Most credentials will be :attr:`invalid` until :meth:`refresh` is called. | ||
Credentials can do this automatically before the first HTTP request in | ||
:meth:`before_request`. | ||
|
||
Although the token and expiration will change as the credentials are | ||
:meth:`refreshed <refresh>` and used, credentials should be considered | ||
immutable. Various credentials will accept configuration such as private | ||
keys, scopes, and other options. These options are not changeable after | ||
construction. Some classes will provide mechanisms to copy the credentials | ||
with modifications such as :meth:`ScopedCredentials.with_scopes`. | ||
""" | ||
|
||
async def before_request(self, request, method, url, headers): | ||
"""Performs credential-specific before request logic. | ||
|
||
Refreshes the credentials if necessary, then calls :meth:`apply` to | ||
apply the token to the authentication header. | ||
|
||
Args: | ||
request (google.auth.transport.Request): The object used to make | ||
HTTP requests. | ||
method (str): The request's HTTP method or the RPC method being | ||
invoked. | ||
url (str): The request's URI or the RPC service's URI. | ||
headers (Mapping): The request's headers. | ||
""" | ||
# pylint: disable=unused-argument | ||
# (Subclasses may use these arguments to ascertain information about | ||
# the http request.) | ||
|
||
if not self.valid: | ||
self.refresh(request) | ||
self.apply(headers) | ||
|
||
|
||
class AnonymousCredentials(credentials.AnonymousCredentials, Credentials): | ||
"""Credentials that do not provide any authentication information. | ||
|
||
These are useful in the case of services that support anonymous access or | ||
local service emulators that do not use credentials. This class inherits | ||
from the sync anonymous credentials file, but is kept if async credentials | ||
is initialized and we would like anonymous credentials. | ||
""" | ||
|
||
|
||
@six.add_metaclass(abc.ABCMeta) | ||
class ReadOnlyScoped(credentials.ReadOnlyScoped): | ||
"""Interface for credentials whose scopes can be queried. | ||
|
||
OAuth 2.0-based credentials allow limiting access using scopes as described | ||
in `RFC6749 Section 3.3`_. | ||
If a credential class implements this interface then the credentials either | ||
use scopes in their implementation. | ||
|
||
Some credentials require scopes in order to obtain a token. You can check | ||
if scoping is necessary with :attr:`requires_scopes`:: | ||
|
||
if credentials.requires_scopes: | ||
# Scoping is required. | ||
credentials = credentials_async.with_scopes(scopes=['one', 'two']) | ||
|
||
Credentials that require scopes must either be constructed with scopes:: | ||
|
||
credentials = SomeScopedCredentials(scopes=['one', 'two']) | ||
|
||
Or must copy an existing instance using :meth:`with_scopes`:: | ||
|
||
scoped_credentials = credentials_async.with_scopes(scopes=['one', 'two']) | ||
|
||
Some credentials have scopes but do not allow or require scopes to be set, | ||
these credentials can be used as-is. | ||
|
||
.. _RFC6749 Section 3.3: https://tools.ietf.org/html/rfc6749#section-3.3 | ||
""" | ||
|
||
|
||
class Scoped(credentials.Scoped): | ||
"""Interface for credentials whose scopes can be replaced while copying. | ||
|
||
OAuth 2.0-based credentials allow limiting access using scopes as described | ||
in `RFC6749 Section 3.3`_. | ||
If a credential class implements this interface then the credentials either | ||
use scopes in their implementation. | ||
|
||
Some credentials require scopes in order to obtain a token. You can check | ||
if scoping is necessary with :attr:`requires_scopes`:: | ||
|
||
if credentials.requires_scopes: | ||
# Scoping is required. | ||
credentials = credentials_async.create_scoped(['one', 'two']) | ||
|
||
Credentials that require scopes must either be constructed with scopes:: | ||
|
||
credentials = SomeScopedCredentials(scopes=['one', 'two']) | ||
|
||
Or must copy an existing instance using :meth:`with_scopes`:: | ||
|
||
scoped_credentials = credentials.with_scopes(scopes=['one', 'two']) | ||
|
||
Some credentials have scopes but do not allow or require scopes to be set, | ||
these credentials can be used as-is. | ||
|
||
.. _RFC6749 Section 3.3: https://tools.ietf.org/html/rfc6749#section-3.3 | ||
""" | ||
|
||
|
||
def with_scopes_if_required(credentials, scopes): | ||
"""Creates a copy of the credentials with scopes if scoping is required. | ||
|
||
This helper function is useful when you do not know (or care to know) the | ||
specific type of credentials you are using (such as when you use | ||
:func:`google.auth.default`). This function will call | ||
:meth:`Scoped.with_scopes` if the credentials are scoped credentials and if | ||
the credentials require scoping. Otherwise, it will return the credentials | ||
as-is. | ||
|
||
Args: | ||
credentials (google.auth.credentials.Credentials): The credentials to | ||
scope if necessary. | ||
scopes (Sequence[str]): The list of scopes to use. | ||
|
||
Returns: | ||
google.auth.credentials_async.Credentials: Either a new set of scoped | ||
credentials, or the passed in credentials instance if no scoping | ||
was required. | ||
""" | ||
if isinstance(credentials, Scoped) and credentials.requires_scopes: | ||
return credentials.with_scopes(scopes) | ||
else: | ||
return credentials | ||
|
||
|
||
@six.add_metaclass(abc.ABCMeta) | ||
class Signing(credentials.Signing): | ||
"""Interface for credentials that can cryptographically sign messages.""" |
Oops, something went wrong.
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.
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.
Should six be needed for this? this surface is only usable for 3.6+ correct?
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 used six to change the exception types (line 143, 147) so that we could raise our defined transport error.
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.
This module uses
six
only for thesix.add_metaclass(abc.ABCMeta)
class decorator. Given that it will only ever be imported under Python >= 3.6, it would be clearer to just use the direct Py3 syntax, e.g.: