Skip to content

Commit 688e3bc

Browse files
author
Eric Koleda
committed
1.28.0
1 parent e757598 commit 688e3bc

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

dist/OAuth2.gs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ Service_.prototype.hasAccess = function() {
463463
return this.lockable_(function() {
464464
var token = this.getToken();
465465
if (!token || this.isExpired_(token)) {
466-
if (token && token.refresh_token) {
466+
if (token && this.canRefresh_(token)) {
467467
try {
468468
this.refresh();
469469
} catch (e) {
@@ -678,6 +678,24 @@ Service_.prototype.isExpired_ = function(token) {
678678
}
679679
};
680680

681+
/**
682+
* Determines if a retrieved token can be refreshed.
683+
* @param {Object} token The token to inspect.
684+
* @return {boolean} True if it can be refreshed, false otherwise.
685+
* @private
686+
*/
687+
Service_.prototype.canRefresh_ = function(token) {
688+
if (!token.refresh_token) return false;
689+
var expiresIn = token.refresh_token_expires_in;
690+
if (!expiresIn) {
691+
return true;
692+
} else {
693+
var expiresTime = token.granted_time + Number(expiresIn);
694+
var now = getTimeInSeconds_(new Date());
695+
return expiresTime - now > Service_.EXPIRATION_BUFFER_SECONDS_;
696+
}
697+
};
698+
681699
/**
682700
* Uses the service account flow to exchange a signed JSON Web Token (JWT) for
683701
* an access token.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "apps-script-oauth2",
3-
"version": "1.27.0",
3+
"version": "1.28.0",
44
"description": "OAuth2 for Apps Script is a library for Google Apps Script that provides the ability to create and authorize OAuth2 tokens as well as refresh them when they expire.",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)