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: Add ability to refresh token automatically before expiration. #208

Closed
wants to merge 20 commits into from

Conversation

nolanmar511
Copy link
Contributor

@nolanmar511 nolanmar511 commented Dec 15, 2017

This pull request makes it possible to refresh the token when it is not expired but will expire in some period of time.
This can be used to address the issues described in #133, by allowing users to specify that tokens should be refreshed if they will expire soon. This is also necessary to fix googleapis/cloud-profiler-nodejs#94

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Dec 15, 2017
constructor() {
super();
constructor(refreshTokenEarlyMillis?: number) {
super(undefined, undefined, undefined, undefined, refreshTokenEarlyMillis);

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

@nolanmar511
Copy link
Contributor Author

If I run the the travis tests on my fork of the master branch, without modifying anything, the travis tests fail in the same way.
The PRs which currently have passing tests seem to have different scripts in their package.json files.

How should I address the failing tests?

@@ -39,9 +39,11 @@ export class Compute extends OAuth2Client {
*
* Retrieve access token from the metadata server.
* See: https://developers.google.com/compute/docs/authentication
*
* @param {number=} refreshTokeEarlyMillis The token should be refreshed if it will expire within this many milliseconds.

This comment was marked as spam.

This comment was marked as spam.

Copy link
Contributor

@JustinBeckwith JustinBeckwith left a comment

Choose a reason for hiding this comment

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

Biggest feedback - this should be rebased and submitted against the next branch, and we should make the constructor changes Ali suggested. I'm happy to submit the constructor change as part of a separate PR if that's helpful.

constructor() {
super();
constructor(refreshTokenEarlyMillis?: number) {
super(undefined, undefined, undefined, undefined, refreshTokenEarlyMillis);

This comment was marked as spam.

@@ -105,6 +105,11 @@ export class GoogleAuth {
*/
public static DefaultTransporter = DefaultTransporter;

/**
* @param {number=} refreshTokeEarlyMillis The token should be refreshed if it will expire within this many milliseconds.

This comment was marked as spam.

This comment was marked as spam.

@@ -400,9 +406,12 @@ export class GoogleAuth {
// Set the JSON contents
this.jsonContent = json;
if (json.type === 'authorized_user') {
client = new UserRefreshClient();
client = new UserRefreshClient(
undefined, undefined, undefined, this.refreshTokenEarlyMillis);

This comment was marked as spam.

/**
* @param {number=} refreshTokeEarlyMillis The token should be refreshed if it will expire within this many milliseconds.
*/
constructor(private refreshTokenEarlyMillis?: number) {}

This comment was marked as spam.

This comment was marked as spam.

@@ -44,12 +44,13 @@ export class JWT extends OAuth2Client {
* @param {string=} key value of key
* @param {(string|array)=} scopes list of requested scopes or a single scope.
* @param {string=} subject impersonated account's email address.
* @param {number=} refreshTokeEarlyMillis The token should be refreshed if it will expire within this many milliseconds.

This comment was marked as spam.

This comment was marked as spam.

@@ -52,17 +53,19 @@ export class OAuth2Client extends AuthClient {
* @param {string=} clientSecret The authentication client secret.
* @param {string=} redirectUri The URI to redirect to after completing the auth request.
* @param {Object=} opt_opts optional options for overriding the given parameters.
* @param {number=} refreshTokeEarlyMillis The token should be refreshed if it will expire within this many milliseconds.

This comment was marked as spam.

This comment was marked as spam.

@@ -29,10 +29,14 @@ export class UserRefreshClient extends OAuth2Client {
* @param {string} clientId The authentication client ID.
* @param {string} clientSecret The authentication client secret.
* @param {string} refreshToken The authentication refresh token.
* @param {number=} refreshTokeEarlyMillis The token should be refreshed if it will expire within this many milliseconds.

This comment was marked as spam.

This comment was marked as spam.

expiry_date: (new Date()).getTime() + 3000
};

oauth2client.request({uri: 'http://example.com'}, () => {

This comment was marked as spam.

@JustinBeckwith
Copy link
Contributor

Instead of trying to get the tests to pass on the master branch, please take a look at rebasing against the next branch.

@googlebot
Copy link

So there's good news and bad news.

👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

😕 The bad news is that it appears that one or more commits were authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this State. It's up to you to confirm consent of the commit author(s) and merge this pull request when appropriate.

@googlebot googlebot added cla: no This human has *not* signed the Contributor License Agreement. and removed cla: yes This human has signed the Contributor License Agreement. labels Jan 3, 2018
@nolanmar511 nolanmar511 closed this Jan 3, 2018
@nolanmar511
Copy link
Contributor Author

Opened PR #227, which has these changes on top of the next branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: no This human has *not* signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"Unauthorized" error when credentials expire
5 participants