Skip to content

Commit feac126

Browse files
committed
Updated library to support custom JWT header and signature encodings for Service Account authorization.
1 parent eebdf78 commit feac126

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

src/Service.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,20 @@ Service_.prototype.setAdditionalClaims = function(additionalClaims) {
288288
return this;
289289
};
290290

291+
/**
292+
* Sets custom JWT encoding options to use for Service Account authorization.
293+
* @param {Object} customJWTEncodingOptions Custom JWT encoding options, as key-value pairs.
294+
* @param {Object} [customJWTEncodingOptions.header] Custom JWT header properties
295+
* @param {function} [customJWTEncodingOptions.computeJWTSignature] Custom function
296+
* to compute JWT signature.
297+
* @return {!Service_} This service, for chaining.
298+
*/
299+
Service_.prototype.setCustomJWTEncodingOptions = function(customJWTEncodingOptions) {
300+
this.customJWTEncodingOptions_ = customJWTEncodingOptions;
301+
return this;
302+
};
303+
304+
291305
/**
292306
* Sets the subject (sub) value to use for Service Account authorization.
293307
* @param {string} subject This subject value
@@ -748,7 +762,7 @@ Service_.prototype.createJwt_ = function() {
748762
claimSet[key] = additionalClaims[key];
749763
});
750764
}
751-
return encodeJwt_(claimSet, this.privateKey_);
765+
return encodeJwt_(claimSet, this.privateKey_, this.customJWTEncodingOptions_);
752766
};
753767

754768
/**

src/Utilities.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,24 +96,46 @@ function toLowerCaseKeys_(obj) {
9696
}, {});
9797
}
9898

99+
/**
100+
* Default method to compute JWT signature.
101+
*
102+
* @param {string} toSign String to Sign
103+
* @param {string} key Key used to sign string
104+
* @return {string} JWT Signature
105+
*/
106+
function computeJWTSignatureDefault_(toSign, key) {
107+
var signatureBytes =
108+
Utilities.computeRsaSha256Signature(toSign, key);
109+
return Utilities.base64EncodeWebSafe(signatureBytes);
110+
}
111+
99112
/* exported encodeJwt_ */
100113
/**
101114
* Encodes and signs a JWT.
102115
*
103116
* @param {Object} payload The JWT payload.
104117
* @param {string} key The key to use when generating the signature.
118+
* @param {Object} [customOptions] Options to customize JWT encoding
119+
* @param {Object} [customOptions.header] Supply custom header properties
120+
* @param {Function} [customOptions.computeJWTSignature] Custom function
121+
* to compute JWT signature.
105122
* @return {string} The encoded and signed JWT.
106123
*/
107-
function encodeJwt_(payload, key) {
108-
var header = {
124+
function encodeJwt_(payload, key, customOptions) {
125+
var customOptions = customOptions || {};
126+
127+
var header = Object.assign({
109128
alg: 'RS256',
110129
typ: 'JWT'
111-
};
130+
}, customOptions.header || {});
131+
132+
var computeJWTSignature = typeof customOptions.computeJWTSignature === 'function' ?
133+
customOptions.computeJWTSignature : computeJWTSignatureDefault_;
134+
112135
var toSign = Utilities.base64EncodeWebSafe(JSON.stringify(header)) + '.' +
113136
Utilities.base64EncodeWebSafe(JSON.stringify(payload));
114-
var signatureBytes =
115-
Utilities.computeRsaSha256Signature(toSign, key);
116-
var signature = Utilities.base64EncodeWebSafe(signatureBytes);
137+
138+
var signature = computeJWTSignature(toSign, key);
117139
return toSign + '.' + signature;
118140
}
119141

0 commit comments

Comments
 (0)