Add OAuth 2.0 token to Request template tag (#715)

This commit is contained in:
Gregory Schier
2018-01-17 13:36:06 +07:00
parent e196c75d17
commit dbe5fdd736
3 changed files with 10 additions and 3 deletions

View File

@@ -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)) {

View File

@@ -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);

View File

@@ -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;