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

feat: support refresh callable on google.oauth2.credentials.Credentials #812

Merged
merged 3 commits into from Jul 22, 2021
Merged

feat: support refresh callable on google.oauth2.credentials.Credentials #812

merged 3 commits into from Jul 22, 2021

Conversation

bojeil-google
Copy link
Contributor

This is an optional parameter that can be set via the constructor.
It is used to provide the credentials with new tokens and their
expiration time on refresh() call.

def refresh_handler(request, scopes):
    # Generate a new token for the requested scopes by calling
    # an external process.
    return (
        "ACCESS_TOKEN",
        _helpers.utcnow() + datetime.timedelta(seconds=3600))

creds = google.oauth2.credentials.Credentials(
    scopes=scopes,
    refresh_handler=refresh_handler)
creds.refresh(request)

It is useful in the following cases:

  • Useful in general when tokens are obtained by calling some
    external process on demand.
  • Useful in particular for retrieving downscoped tokens from a
    token broker.

This should have no impact on existing behavior. Refresh tokens
will still have higher priority over refresh handlers.

A getter and setter is exposed to make it easy to set the callable
on unpickled credentials as the callable cannot be easily serialized.

unpickled = pickle.loads(pickle.dumps(oauth_creds))
unpickled.refresh_handler = refresh_handler

…redentials

This is an optional parameter that can be set via the constructor.
It is used to provide the credentials with new tokens and their
expiration time on `refresh()` call.

```
def refresh_handler(request, scopes):
    # Generate a new token for the requested scopes by calling
    # an external process.
    return (
        "ACCESS_TOKEN",
        _helpers.utcnow() + datetime.timedelta(seconds=3600))

creds = google.oauth2.credentials.Credentials(
    scopes=scopes,
    refresh_handler=refresh_handler)
creds.refresh(request)
```

It is useful in the following cases:
- Useful in general when tokens are obtained by calling some
  external process on demand.
- Useful in particular for retrieving downscoped tokens from a
  token broker.

This should have no impact on existing behavior. Refresh tokens
will still have higher priority over refresh handlers.

A getter and setter is exposed to make it easy to set the callable
on unpickled credentials as the callable cannot be easily serialized.

```
unpickled = pickle.loads(pickle.dumps(oauth_creds))
unpickled.refresh_handler = refresh_handler
```
@bojeil-google bojeil-google requested a review from a team as a code owner July 21, 2021 05:21
@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label Jul 21, 2021
tests/oauth2/test_credentials.py Outdated Show resolved Hide resolved
tests/oauth2/test_credentials.py Outdated Show resolved Hide resolved
google/oauth2/credentials.py Show resolved Hide resolved
Copy link
Contributor

@tseaver tseaver left a comment

Choose a reason for hiding this comment

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

Your changes for my comments LGTM. I don't really know the flow being used, so I'll hold off on a final "approve" until those who do can chime in.

@bojeil-google
Copy link
Contributor Author

Your changes for my comments LGTM. I don't really know the flow being used, so I'll hold off on a final "approve" until those who do can chime in.

Thanks @tseaver for the review. @arithmetic1728 has reviewed the design doc. He should be able to review too.

@tseaver tseaver merged commit ec2fb18 into googleapis:master Jul 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants