-
Notifications
You must be signed in to change notification settings - Fork 384
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. #242
Conversation
src/auth/computeclient.ts
Outdated
@@ -33,8 +33,8 @@ export class Compute extends OAuth2Client { | |||
* Retrieve access token from the metadata server. | |||
* See: https://developers.google.com/compute/docs/authentication | |||
*/ | |||
constructor() { | |||
super(); | |||
constructor(refreshTokenEarlyMillis?: number) { |
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.
c5fcb09
to
cc95246
Compare
PTAL |
src/auth/googleauth.ts
Outdated
/** | ||
* @param {number=} refreshTokenEarlyMillis The token should be refreshed if it will expire within this many milliseconds. | ||
*/ | ||
constructor(public refreshTokenEarlyMillis?: number) {} |
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.
src/auth/googleauth.ts
Outdated
@@ -87,6 +87,11 @@ export class GoogleAuth { | |||
*/ | |||
static DefaultTransporter = DefaultTransporter; | |||
|
|||
/** | |||
* @param {number=} refreshTokenEarlyMillis 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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/computeclient.ts
Outdated
@@ -20,6 +20,8 @@ import {RequestError} from './../transporters'; | |||
import {CredentialRequest, Credentials} from './credentials'; | |||
import {GetTokenResponse, OAuth2Client} from './oauth2client'; | |||
|
|||
export interface ComputeOptions { refreshTokenEarlyMillis?: number; } |
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.
src/auth/refreshclient.ts
Outdated
constructor( | ||
clientId?: string, clientSecret?: string, refreshToken?: string, | ||
refreshTokenEarlyMillis?: number); | ||
|
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.
test/test.compute.ts
Outdated
@@ -66,6 +66,19 @@ describe('Compute auth client', () => { | |||
}); | |||
}); | |||
|
|||
it('should refresh if access token has expired', (done) => { |
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.
027962d
to
bb554a9
Compare
a47221c
to
5338263
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.
LGTM once nits are addressed.
src/auth/oauth2client.ts
Outdated
clientId?: string; | ||
clientSecret?: string; | ||
redirectUri?: string; | ||
authBaseUrl?: string; | ||
tokenUrl?: string; | ||
} | ||
|
||
export interface RefreshOptions { | ||
// 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.
Sorry, something went wrong.
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.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
import {GetTokenResponse, OAuth2Client} from './oauth2client'; | ||
import {GetTokenResponse, OAuth2Client, RefreshOptions} from './oauth2client'; | ||
|
||
export interface ComputeOptions extends RefreshOptions {} |
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.
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.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/computeclient.ts
Outdated
super(); | ||
constructor(options?: ComputeOptions) { | ||
options = options || {}; | ||
super({refreshTokenEarlyMillis: options.refreshTokenEarlyMillis}); |
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.
src/auth/googleauth.ts
Outdated
@@ -58,6 +58,8 @@ interface CredentialResult { | |||
default: {email: string;}; | |||
} | |||
|
|||
export interface GoogleAuthOptions extends RefreshOptions {} |
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.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/googleauth.ts
Outdated
/** | ||
* Export DefaultTransporter as a static property of the class. | ||
*/ | ||
static DefaultTransporter = DefaultTransporter; | ||
|
||
constructor(options?: GoogleAuthOptions) { |
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.
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.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/oauth2client.ts
Outdated
@@ -936,4 +932,16 @@ export class OAuth2Client extends AuthClient { | |||
const buffer = new Buffer(b64String, 'base64'); | |||
return buffer.toString('utf8'); | |||
} | |||
|
|||
/** | |||
* Returns true iff a token is expired or will expire within |
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.
test/test.compute.ts
Outdated
compute = new Compute({refreshTokenEarlyMillis: 10000}); | ||
compute.credentials.access_token = 'initial-access-token'; | ||
compute.credentials.expiry_date = (new Date()).getTime() + 5000; | ||
compute.request({url: 'http://foo'}, () => { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
test/test.compute.ts
Outdated
.reply(200, {access_token: 'abc123', expires_in: 10000}); | ||
compute.credentials.access_token = 'initial-access-token'; | ||
compute.credentials.expiry_date = (new Date()).getTime() + 11000; | ||
compute.request({url: 'http://foo'}, () => { |
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.
}; | ||
|
||
createGTokenMock({access_token: 'abc123'}); | ||
jwt.request({url: 'http://bar'}, () => { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
expiry_date: (new Date()).getTime() + 5000 | ||
}; | ||
|
||
jwt.request({url: 'http://bar'}, () => { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/oauth2client.ts
Outdated
* refreshTokenEarlyMillis milliseconds. | ||
* If there is no expiry time, assumes the token is not expired or expiring. | ||
*/ | ||
protected isTokenExpiring(): boolean { |
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.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Does this address #67? |
31c482c
to
affe774
Compare
Somehow the comment about expiry buffer got lost in the fold, but it should be 5 minutes. See the canonical implementation: |
ed32d20
to
9303172
Compare
PTAL |
LGTM! Please rebase to master, and I'll merge this in. Thank you! |
…ctions in googleAuth
9303172
to
63ab7cc
Compare
Just rebased against master. |
Is there any update to the docs? How should this change be used? |
Greetings! You can learn how to us this in the reference docs: |
404 |
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
For earlier discussion of these changes, see PR #208.