@@ -96,24 +96,46 @@ function toLowerCaseKeys_(obj) {
96
96
} , { } ) ;
97
97
}
98
98
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
+
99
112
/* exported encodeJwt_ */
100
113
/**
101
114
* Encodes and signs a JWT.
102
115
*
103
116
* @param {Object } payload The JWT payload.
104
117
* @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.
105
122
* @return {string } The encoded and signed JWT.
106
123
*/
107
- function encodeJwt_ ( payload , key ) {
108
- var header = {
124
+ function encodeJwt_ ( payload , key , customOptions ) {
125
+ var customOptions = customOptions || { } ;
126
+
127
+ var header = Object . assign ( {
109
128
alg : 'RS256' ,
110
129
typ : 'JWT'
111
- } ;
130
+ } , customOptions . header || { } ) ;
131
+
132
+ var computeJWTSignature = typeof customOptions . computeJWTSignature === 'function' ?
133
+ customOptions . computeJWTSignature : computeJWTSignatureDefault_ ;
134
+
112
135
var toSign = Utilities . base64EncodeWebSafe ( JSON . stringify ( header ) ) + '.' +
113
136
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 ) ;
117
139
return toSign + '.' + signature ;
118
140
}
119
141
0 commit comments