Skip to content

Commit fdd3d0c

Browse files
author
Eric Koleda
committed
1.31.0
1 parent c4f13e6 commit fdd3d0c

File tree

7 files changed

+91
-15
lines changed

7 files changed

+91
-15
lines changed

dist/OAuth2.gs

+12-6
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,11 @@ Service_.prototype.setGrantType = function(grantType) {
389389
* have the user visit this URL and approve the authorization request. The
390390
* user will then be redirected back to your application using callback function
391391
* name specified, so that the flow may continue.
392+
* @param {Object} optAdditionalParameters Additional parameters that should be
393+
* stored in the state token and made available in the callback function.
392394
* @return {string} The authorization URL.
393395
*/
394-
Service_.prototype.getAuthorizationUrl = function() {
396+
Service_.prototype.getAuthorizationUrl = function(optAdditionalParameters) {
395397
validate_({
396398
'Client ID': this.clientId_,
397399
'Script ID': this.scriptId_,
@@ -400,16 +402,20 @@ Service_.prototype.getAuthorizationUrl = function() {
400402
});
401403

402404
var redirectUri = getRedirectUri(this.scriptId_);
403-
var state = eval('Script' + 'App').newStateToken()
405+
var stateTokenBuilder = eval('Script' + 'App').newStateToken()
404406
.withMethod(this.callbackFunctionName_)
405407
.withArgument('serviceName', this.serviceName_)
406-
.withTimeout(3600)
407-
.createToken();
408+
.withTimeout(3600);
409+
if (optAdditionalParameters) {
410+
Object.keys(optAdditionalParameters).forEach(function(key) {
411+
stateTokenBuilder.withArgument(key, optAdditionalParameters[key]);
412+
});
413+
}
408414
var params = {
409415
client_id: this.clientId_,
410416
response_type: 'code',
411417
redirect_uri: redirectUri,
412-
state: state
418+
state: stateTokenBuilder.createToken()
413419
};
414420
params = extend_(params, this.params_);
415421
return buildUrl_(this.authorizationBaseUrl_, params);
@@ -1015,7 +1021,7 @@ function validate_(params) {
10151021
Object.keys(params).forEach(function(name) {
10161022
var value = params[name];
10171023
if (!value) {
1018-
throw Utilities.formatString('%s is required.', name);
1024+
throw new Error(name + ' is required.');
10191025
}
10201026
});
10211027
}

docs/Service_.html

+52-2
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ <h5>Returns:</h5>
574574

575575

576576

577-
<h4 class="name" id="getAuthorizationUrl"><span class="type-signature"></span>getAuthorizationUrl<span class="signature">()</span><span class="type-signature"> &rarr; {string}</span></h4>
577+
<h4 class="name" id="getAuthorizationUrl"><span class="type-signature"></span>getAuthorizationUrl<span class="signature">(optAdditionalParameters)</span><span class="type-signature"> &rarr; {string}</span></h4>
578578

579579

580580

@@ -596,6 +596,56 @@ <h4 class="name" id="getAuthorizationUrl"><span class="type-signature"></span>ge
596596

597597

598598

599+
<h5>Parameters:</h5>
600+
601+
602+
<table class="params">
603+
<thead>
604+
<tr>
605+
606+
<th>Name</th>
607+
608+
609+
<th>Type</th>
610+
611+
612+
613+
614+
615+
<th class="last">Description</th>
616+
</tr>
617+
</thead>
618+
619+
<tbody>
620+
621+
622+
<tr>
623+
624+
<td class="name"><code>optAdditionalParameters</code></td>
625+
626+
627+
<td class="type">
628+
629+
630+
<span class="param-type">Object</span>
631+
632+
633+
634+
</td>
635+
636+
637+
638+
639+
640+
<td class="description last">Additional parameters that should be
641+
stored in the state token and made available in the callback function.</td>
642+
</tr>
643+
644+
645+
</tbody>
646+
</table>
647+
648+
599649

600650

601651

@@ -4618,7 +4668,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Service_.
46184668
<br class="clear">
46194669

46204670
<footer>
4621-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Thu Sep 27 2018 14:12:43 GMT-0700 (PDT)
4671+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Wed Nov 14 2018 09:13:44 GMT-0500 (EST)
46224672
</footer>
46234673

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

docs/Storage_.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Service_.
763763
<br class="clear">
764764

765765
<footer>
766-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Thu Sep 27 2018 14:12:43 GMT-0700 (PDT)
766+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Wed Nov 14 2018 09:13:44 GMT-0500 (EST)
767767
</footer>
768768

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

docs/global.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1348,7 +1348,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Service_.
13481348
<br class="clear">
13491349

13501350
<footer>
1351-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Thu Sep 27 2018 14:12:43 GMT-0700 (PDT)
1351+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Wed Nov 14 2018 09:13:44 GMT-0500 (EST)
13521352
</footer>
13531353

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

docs/index.html

+23-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ <h3> </h3>
4545
<section>
4646
<article><h1>OAuth2 for Apps Script <a href="https://travis-ci.org/gsuitedevs/apps-script-oauth2"><img src="https://travis-ci.org/gsuitedevs/apps-script-oauth2.svg?branch=master" alt="Build Status"></a></h1><p>OAuth2 for Apps Script is a library for Google Apps Script that provides the
4747
ability to create and authorize OAuth2 tokens as well as refresh them when they
48-
expire. This library uses Apps Script's new
48+
expire. This library uses Apps Script's
4949
<a href="https://developers.google.com/apps-script/reference/script/state-token-builder">StateTokenBuilder</a>
5050
and <code>/usercallback</code> endpoint to handle the redirects.</p>
5151
<h2>Setup</h2><p>This library is already published as an Apps Script, making it easy to include
@@ -257,7 +257,27 @@ <h4>Storing token-related data</h4><p>Some OAuth providers return IDs and other
257257
}
258258
});</code></pre><p>Note that calling <code>Service.reset()</code> will remove all custom values from storage,
259259
in addition to the token.</p>
260-
<h4>Using service accounts</h4><p>This library supports the service account authorization flow, also known as the
260+
<h4>Passing additional parameters to the callback function</h4><p>There are occasionally cases where you need to preserve some data through the
261+
OAuth flow, so that it is available in your callback function. Although you
262+
could use the token storage mechanism discussed above for that purpose, writing
263+
to the PropertiesService is expensive and not neccessary in the case where the
264+
user doesn't start or fails to complete the OAuth flow.</p>
265+
<p>As an alternative you can store small amounts of data in the OAuth2 <code>state</code>
266+
token, which is a standard mechanism for this purpose. To do so, pass an
267+
optional hash of parameter names and values to the <code>getAuthorizationUrl()</code>
268+
method:</p>
269+
<pre class="prettyprint source lang-js"><code>var authorizationUrl = getService().getAuthorizationUrl({
270+
// Pass the additional parameter &quot;lang&quot; with the value &quot;fr&quot;.
271+
lang: 'fr'
272+
});</code></pre><p>These values will be stored along-side Apps Script's internal information in the
273+
encypted <code>state</code> token, which is passed in the authorization URL and passed back
274+
to the redirect URI. The <code>state</code> token is automatically decrypted in the
275+
callback function and you can access your parameters using the same
276+
<code>request.parameter</code> field used in web apps:</p>
277+
<pre class="prettyprint source lang-js"><code>function authCallback(request) {
278+
var lang = request.parameter.lang;
279+
// ...
280+
}</code></pre><h4>Using service accounts</h4><p>This library supports the service account authorization flow, also known as the
261281
<a href="https://tools.ietf.org/html/draft-ietf-oauth-jwt-bearer-12">JSON Web Token (JWT) Profile</a>.
262282
This is a two-legged OAuth flow that doesn't require a user to visit a URL and
263283
authorize access.</p>
@@ -669,7 +689,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Service_.
669689
<br class="clear">
670690

671691
<footer>
672-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Thu Sep 27 2018 14:12:43 GMT-0700 (PDT)
692+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Wed Nov 14 2018 09:13:44 GMT-0500 (EST)
673693
</footer>
674694

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

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "apps-script-oauth2",
3-
"version": "1.30.0",
3+
"version": "1.31.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)