From 1d0f8fb850690fdad4ac49a1f405dd54ab679330 Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Sun, 12 Aug 2018 11:18:25 -0700 Subject: [PATCH 1/2] fix: add better error handling around sample client and redirect URIs --- samples/sampleclient.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/samples/sampleclient.js b/samples/sampleclient.js index 94cd5483284..63ac0a0baf6 100644 --- a/samples/sampleclient.js +++ b/samples/sampleclient.js @@ -33,15 +33,35 @@ if (fs.existsSync(keyPath)) { keys = keyFile.installed || keyFile.web; } +const invalidRedirectUri = `The provided keyfile does not define a valid +redirect URI. There must be at least one redirect URI defined, and this sample +assumes it redirects to 'http://localhost:3000/oauth2callback'. Please edit +your keyfile, and add a 'redirect_uris' section. For example: + +"redirect_uris": [ + "http://localhost:3000/oauth2callback" +] +`; + class SampleClient { constructor (options) { this._options = options || { scopes: [] }; + // validate the redirectUri. This is a frequent cause of confusion. + if (!keys.redirect_uris || keys.redirect_uris.length === 0) { + throw new Error(invalidRedirectUri); + } + const redirectUri = keys.redirect_uris[keys.redirect_uris.length - 1]; + const parts = url.parse(redirectUri, false); + if (redirectUri.length > 0 && parts.port !== '3000' || parts.hostname !== 'localhost' || parts.path !== '/oauth2callback') { + throw new Error(invalidRedirectUri); + } + // create an oAuth client to authorize the API call this.oAuth2Client = new google.auth.OAuth2( keys.client_id, keys.client_secret, - keys.redirect_uris[0] + redirectUri ); } From a36c38dc2c318b1010c74f9accb793403474246f Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Tue, 28 Aug 2018 12:48:04 -0700 Subject: [PATCH 2/2] fix lint error --- samples/sampleclient.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/sampleclient.js b/samples/sampleclient.js index 63ac0a0baf6..74170addafc 100644 --- a/samples/sampleclient.js +++ b/samples/sampleclient.js @@ -33,9 +33,9 @@ if (fs.existsSync(keyPath)) { keys = keyFile.installed || keyFile.web; } -const invalidRedirectUri = `The provided keyfile does not define a valid +const invalidRedirectUri = `The provided keyfile does not define a valid redirect URI. There must be at least one redirect URI defined, and this sample -assumes it redirects to 'http://localhost:3000/oauth2callback'. Please edit +assumes it redirects to 'http://localhost:3000/oauth2callback'. Please edit your keyfile, and add a 'redirect_uris' section. For example: "redirect_uris": [ @@ -53,7 +53,7 @@ class SampleClient { } const redirectUri = keys.redirect_uris[keys.redirect_uris.length - 1]; const parts = url.parse(redirectUri, false); - if (redirectUri.length > 0 && parts.port !== '3000' || parts.hostname !== 'localhost' || parts.path !== '/oauth2callback') { + if (redirectUri.length === 0 || parts.port !== '3000' || parts.hostname !== 'localhost' || parts.path !== '/oauth2callback') { throw new Error(invalidRedirectUri); }