Skip to content

Commit 036e929

Browse files
author
Eric Koleda
committed
1.39.0
1 parent 606bcbe commit 036e929

File tree

7 files changed

+443
-35
lines changed

7 files changed

+443
-35
lines changed

dist/OAuth2.gs

+59-19
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ Service_.prototype.refresh = function() {
672672
* Custom values associated with the service can be stored here as well.
673673
* The key <code>null</code> is used to to store the token and should not
674674
* be used.
675-
* @return {Storage} The service's storage.
675+
* @return {Storage_} The service's storage.
676676
*/
677677
Service_.prototype.getStorage = function() {
678678
if (!this.storage_) {
@@ -703,20 +703,35 @@ Service_.prototype.getToken = function(optSkipMemoryCheck) {
703703
};
704704

705705
/**
706-
* Determines if a retrieved token is still valid.
706+
* Determines if a retrieved token is still valid. This will return false if
707+
* either the authorization token or the ID token has expired.
707708
* @param {Object} token The token to validate.
708709
* @return {boolean} True if it has expired, false otherwise.
709710
* @private
710711
*/
711712
Service_.prototype.isExpired_ = function(token) {
713+
var expired = false;
714+
var now = getTimeInSeconds_(new Date());
715+
716+
// Check the authorization token's expiration.
712717
var expiresIn = token.expires_in_sec || token.expires_in || token.expires;
713-
if (!expiresIn) {
714-
return false;
715-
} else {
718+
if (expiresIn) {
716719
var expiresTime = token.granted_time + Number(expiresIn);
717-
var now = getTimeInSeconds_(new Date());
718-
return expiresTime - now < Service_.EXPIRATION_BUFFER_SECONDS_;
720+
if (expiresTime - now < Service_.EXPIRATION_BUFFER_SECONDS_) {
721+
expired = true;
722+
}
719723
}
724+
725+
// Check the ID token's expiration, if it exists.
726+
if (token.id_token) {
727+
var payload = decodeJwt_(token.id_token);
728+
if (payload.exp &&
729+
payload.exp - now < Service_.EXPIRATION_BUFFER_SECONDS_) {
730+
expired = true;
731+
}
732+
}
733+
734+
return expired;
720735
};
721736

722737
/**
@@ -767,10 +782,6 @@ Service_.prototype.createJwt_ = function() {
767782
'Token URL': this.tokenUrl_,
768783
'Issuer or Client ID': this.issuer_ || this.clientId_
769784
});
770-
var header = {
771-
alg: 'RS256',
772-
typ: 'JWT'
773-
};
774785
var now = new Date();
775786
var expires = new Date(now.getTime());
776787
expires.setMinutes(expires.getMinutes() + this.expirationMinutes_);
@@ -792,12 +803,7 @@ Service_.prototype.createJwt_ = function() {
792803
claimSet[key] = additionalClaims[key];
793804
});
794805
}
795-
var toSign = Utilities.base64EncodeWebSafe(JSON.stringify(header)) + '.' +
796-
Utilities.base64EncodeWebSafe(JSON.stringify(claimSet));
797-
var signatureBytes =
798-
Utilities.computeRsaSha256Signature(toSign, this.privateKey_);
799-
var signature = Utilities.base64EncodeWebSafe(signatureBytes);
800-
return toSign + '.' + signature;
806+
return encodeJwt_(claimSet, this.privateKey_);
801807
};
802808

803809
/**
@@ -1001,7 +1007,7 @@ Storage_.prototype.reset = function() {
10011007

10021008
/**
10031009
* Removes a stored value.
1004-
* @param {string} key The key.
1010+
* @param {string} prefixedKey The key.
10051011
*/
10061012
Storage_.prototype.removeValueWithPrefixedKey_ = function(prefixedKey) {
10071013
if (this.properties_) {
@@ -1111,7 +1117,7 @@ function extend_(destination, source) {
11111117
* Gets a copy of an object with all the keys converted to lower-case strings.
11121118
*
11131119
* @param {Object} obj The object to copy.
1114-
* @return {Object} a shallow copy of the object with all lower-case keys.
1120+
* @return {Object} A shallow copy of the object with all lower-case keys.
11151121
*/
11161122
function toLowerCaseKeys_(obj) {
11171123
if (obj === null || typeof obj !== 'object') {
@@ -1125,6 +1131,40 @@ function toLowerCaseKeys_(obj) {
11251131
}, {});
11261132
}
11271133

1134+
/* exported encodeJwt_ */
1135+
/**
1136+
* Encodes and signs a JWT.
1137+
*
1138+
* @param {Object} payload The JWT payload.
1139+
* @param {string} key The key to use when generating the signature.
1140+
* @return {string} The encoded and signed JWT.
1141+
*/
1142+
function encodeJwt_(payload, key) {
1143+
var header = {
1144+
alg: 'RS256',
1145+
typ: 'JWT'
1146+
};
1147+
var toSign = Utilities.base64EncodeWebSafe(JSON.stringify(header)) + '.' +
1148+
Utilities.base64EncodeWebSafe(JSON.stringify(payload));
1149+
var signatureBytes =
1150+
Utilities.computeRsaSha256Signature(toSign, key);
1151+
var signature = Utilities.base64EncodeWebSafe(signatureBytes);
1152+
return toSign + '.' + signature;
1153+
}
1154+
1155+
/* exported decodeJwt_ */
1156+
/**
1157+
* Decodes and returns the parts of the JWT. The signature is not verified.
1158+
*
1159+
* @param {string} jwt The JWT to decode.
1160+
* @return {Object} The decoded payload.
1161+
*/
1162+
function decodeJwt_(jwt) {
1163+
var payload = jwt.split('.')[1];
1164+
var blob = Utilities.newBlob(Utilities.base64DecodeWebSafe(payload));
1165+
return JSON.parse(blob.getDataAsString());
1166+
}
1167+
11281168
/****** code end *********/
11291169
;(
11301170
function copy(src, target, obj) {

docs/Service_.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,7 @@ <h5>Returns:</h5>
10591059

10601060

10611061

1062-
<h4 class="name" id="getStorage"><span class="type-signature"></span>getStorage<span class="signature">()</span><span class="type-signature"> &rarr; {Storage}</span></h4>
1062+
<h4 class="name" id="getStorage"><span class="type-signature"></span>getStorage<span class="signature">()</span><span class="type-signature"> &rarr; {<a href="Storage_.html">Storage_</a>}</span></h4>
10631063

10641064

10651065

@@ -1149,7 +1149,7 @@ <h5>Returns:</h5>
11491149
</dt>
11501150
<dd>
11511151

1152-
<span class="param-type">Storage</span>
1152+
<span class="param-type"><a href="Storage_.html">Storage_</a></span>
11531153

11541154

11551155
</dd>
@@ -5145,13 +5145,13 @@ <h5>Returns:</h5>
51455145
</div>
51465146

51475147
<nav>
5148-
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Service_.html">Service_</a></li><li><a href="Storage_.html">Storage_</a></li></ul><h3>Global</h3><ul><li><a href="global.html#createService">createService</a></li><li><a href="global.html#extend_">extend_</a></li><li><a href="global.html#getRedirectUri">getRedirectUri</a></li><li><a href="global.html#TOKEN_FORMAT">TOKEN_FORMAT</a></li><li><a href="global.html#toLowerCaseKeys_">toLowerCaseKeys_</a></li></ul>
5148+
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Service_.html">Service_</a></li><li><a href="Storage_.html">Storage_</a></li></ul><h3>Global</h3><ul><li><a href="global.html#createService">createService</a></li><li><a href="global.html#decodeJwt_">decodeJwt_</a></li><li><a href="global.html#encodeJwt_">encodeJwt_</a></li><li><a href="global.html#extend_">extend_</a></li><li><a href="global.html#getRedirectUri">getRedirectUri</a></li><li><a href="global.html#TOKEN_FORMAT">TOKEN_FORMAT</a></li><li><a href="global.html#toLowerCaseKeys_">toLowerCaseKeys_</a></li></ul>
51495149
</nav>
51505150

51515151
<br class="clear">
51525152

51535153
<footer>
5154-
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.4</a> on Tue Apr 07 2020 20:19:13 GMT-0400 (Eastern Daylight Time)
5154+
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.4</a> on Mon Dec 14 2020 20:02:02 GMT-0500 (Eastern Standard Time)
51555155
</footer>
51565156

51575157
<script> prettyPrint(); </script>

docs/Storage_.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ <h5>Parameters:</h5>
600600

601601

602602

603-
<h4 class="name" id="removeValueWithPrefixedKey_"><span class="type-signature"></span>removeValueWithPrefixedKey_<span class="signature">(key)</span><span class="type-signature"></span></h4>
603+
<h4 class="name" id="removeValueWithPrefixedKey_"><span class="type-signature"></span>removeValueWithPrefixedKey_<span class="signature">(prefixedKey)</span><span class="type-signature"></span></h4>
604604

605605

606606

@@ -644,7 +644,7 @@ <h5>Parameters:</h5>
644644

645645
<tr>
646646

647-
<td class="name"><code>key</code></td>
647+
<td class="name"><code>prefixedKey</code></td>
648648

649649

650650
<td class="type">
@@ -1029,13 +1029,13 @@ <h5>Parameters:</h5>
10291029
</div>
10301030

10311031
<nav>
1032-
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Service_.html">Service_</a></li><li><a href="Storage_.html">Storage_</a></li></ul><h3>Global</h3><ul><li><a href="global.html#createService">createService</a></li><li><a href="global.html#extend_">extend_</a></li><li><a href="global.html#getRedirectUri">getRedirectUri</a></li><li><a href="global.html#TOKEN_FORMAT">TOKEN_FORMAT</a></li><li><a href="global.html#toLowerCaseKeys_">toLowerCaseKeys_</a></li></ul>
1032+
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Service_.html">Service_</a></li><li><a href="Storage_.html">Storage_</a></li></ul><h3>Global</h3><ul><li><a href="global.html#createService">createService</a></li><li><a href="global.html#decodeJwt_">decodeJwt_</a></li><li><a href="global.html#encodeJwt_">encodeJwt_</a></li><li><a href="global.html#extend_">extend_</a></li><li><a href="global.html#getRedirectUri">getRedirectUri</a></li><li><a href="global.html#TOKEN_FORMAT">TOKEN_FORMAT</a></li><li><a href="global.html#toLowerCaseKeys_">toLowerCaseKeys_</a></li></ul>
10331033
</nav>
10341034

10351035
<br class="clear">
10361036

10371037
<footer>
1038-
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.4</a> on Tue Apr 07 2020 20:19:13 GMT-0400 (Eastern Daylight Time)
1038+
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.4</a> on Mon Dec 14 2020 20:02:02 GMT-0500 (Eastern Standard Time)
10391039
</footer>
10401040

10411041
<script> prettyPrint(); </script>

0 commit comments

Comments
 (0)