From dbe5fdd73693c976bddb5cc701e3dbbade0d6bb0 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Wed, 17 Jan 2018 13:36:06 +0700 Subject: [PATCH] Add OAuth 2.0 token to Request template tag (#715) --- packages/insomnia-app/app/network/o-auth-2/misc.js | 1 - .../insomnia-app/app/templating/base-extension.js | 1 + plugins/insomnia-plugin-request/index.js | 11 +++++++++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/insomnia-app/app/network/o-auth-2/misc.js b/packages/insomnia-app/app/network/o-auth-2/misc.js index d579c9284a..51c28199e5 100644 --- a/packages/insomnia-app/app/network/o-auth-2/misc.js +++ b/packages/insomnia-app/app/network/o-auth-2/misc.js @@ -33,7 +33,6 @@ export function authorizeUserInWindow ( ) { return new Promise((resolve, reject) => { let finalUrl = null; - let hasError = false; function _parseUrl (currentUrl) { if (currentUrl.match(urlSuccessRegex)) { diff --git a/packages/insomnia-app/app/templating/base-extension.js b/packages/insomnia-app/app/templating/base-extension.js index 8a6ee358d9..2434ab43dc 100644 --- a/packages/insomnia-app/app/templating/base-extension.js +++ b/packages/insomnia-app/app/templating/base-extension.js @@ -79,6 +79,7 @@ export default class BaseExtension { models: { request: {getById: models.request.getById}, workspace: {getById: models.workspace.getById}, + oAuth2Token: {getByRequestId: models.oAuth2Token.getByParentId}, cookieJar: { getOrCreateForWorkspace: workspace => { return models.cookieJar.getOrCreateForParentId(workspace._id); diff --git a/plugins/insomnia-plugin-request/index.js b/plugins/insomnia-plugin-request/index.js index 80020c1c11..3a28cbfc4f 100644 --- a/plugins/insomnia-plugin-request/index.js +++ b/plugins/insomnia-plugin-request/index.js @@ -12,12 +12,13 @@ module.exports.templateTags = [{ options: [ {displayName: 'URL', value: 'url', description: 'fully qualified URL'}, {displayName: 'Cookie', value: 'cookie', description: 'cookie value by name'}, - {displayName: 'Header', value: 'header', description: 'header value by name'} + {displayName: 'Header', value: 'header', description: 'header value by name'}, + {displayName: 'OAuth 2.0 Token', value: 'oauth2', description: 'access token'} ] }, { type: 'string', - hide: args => ['url'].includes(args[0].value), + hide: args => ['url', 'oauth2'].includes(args[0].value), displayName: args => { switch (args[0].value) { case 'cookie': @@ -82,6 +83,12 @@ module.exports.templateTags = [{ const namesStr = names.map(n => `"${n}"`).join(',\n\t'); throw new Error(`No header with name "${name}".\nChoices are [\n\t${namesStr}\n]`); + case 'oauth2': + const token = await context.util.models.oAuth2Token.getByRequestId(request._id); + if (!token) { + throw new Error('No OAuth 2.0 tokens found for request'); + } + return token.accessToken; } return null;