From 16af8bf0081d2cdbcdc8339c888a0cecd27c05ff Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Fri, 17 Jan 2025 14:38:17 -0800 Subject: [PATCH] JWT plugin --- package-lock.json | 8 ++++---- package.json | 2 +- plugins/auth-basic/src/index.ts | 11 +++-------- plugins/auth-bearer/src/index.ts | 11 +++-------- plugins/auth-jwt/src/index.ts | 13 ++++++------- plugins/template-function-response/src/index.ts | 10 +++++----- 6 files changed, 22 insertions(+), 33 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6390025..910150b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "plugins/*" ], "dependencies": { - "@yaakapp/api": "^0.2.27" + "@yaakapp/api": "^0.2.29" }, "devDependencies": { "@types/node": "^22.7.4", @@ -1013,9 +1013,9 @@ } }, "node_modules/@yaakapp/api": { - "version": "0.2.27", - "resolved": "https://registry.npmjs.org/@yaakapp/api/-/api-0.2.27.tgz", - "integrity": "sha512-OkgABeXDxlg3Vx3HbpkIFvAaMTxfqcLQx4X7Tm5/24eZOzbp/n2dtRXRBUcd4w7hI/NjQUetGxjPBTxlJDsQxQ==", + "version": "0.2.29", + "resolved": "https://registry.npmjs.org/@yaakapp/api/-/api-0.2.29.tgz", + "integrity": "sha512-RuyWRUGhYIQjFmaZTYuAekSkidl0b9rWcTV1l+Hs5Pw+LUv8+euedAvNodEJgxAgy42btUns7QUmdM5YNvgoug==", "dependencies": { "@types/node": "^22.5.4" } diff --git a/package.json b/package.json index a998942..9e4eced 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,6 @@ "workspaces-run": "^1.0.2" }, "dependencies": { - "@yaakapp/api": "^0.2.27" + "@yaakapp/api": "^0.2.29" } } diff --git a/plugins/auth-basic/src/index.ts b/plugins/auth-basic/src/index.ts index 7661cd4..342b3cf 100644 --- a/plugins/auth-basic/src/index.ts +++ b/plugins/auth-basic/src/index.ts @@ -17,15 +17,10 @@ export const plugin: PluginDefinition = { optional: true, password: true, }], - async onApply(_ctx: any, args: any): Promise { + async onApply(_ctx, args) { const { username, password } = args.config; - return { - url: args.url, - headers: [{ - name: 'Authorization', - value: 'Basic ' + Buffer.from(`${username}:${password}`).toString('base64'), - }], - }; + const value = 'Basic ' + Buffer.from(`${username}:${password}`).toString('base64'); + return { setHeaders: [{ name: 'Authorization', value }] }; }, }, }; diff --git a/plugins/auth-bearer/src/index.ts b/plugins/auth-bearer/src/index.ts index 5b6783d..a08a5dd 100644 --- a/plugins/auth-bearer/src/index.ts +++ b/plugins/auth-bearer/src/index.ts @@ -12,15 +12,10 @@ export const plugin: PluginDefinition = { optional: true, password: true, }], - async onApply(_ctx: any, args: any): Promise { + async onApply(_ctx, args) { const { token } = args.config; - return { - url: args.url, - headers: [{ - name: 'Authorization', - value: `Bearer ${token}`.trim(), - }], - }; + const value = `Bearer ${token}`.trim(); + return { setHeaders: [{ name: 'Authorization', value }] }; }, }, }; diff --git a/plugins/auth-jwt/src/index.ts b/plugins/auth-jwt/src/index.ts index ecad48e..b5e2309 100644 --- a/plugins/auth-jwt/src/index.ts +++ b/plugins/auth-jwt/src/index.ts @@ -14,7 +14,8 @@ const algorithms = [ 'ES256', 'ES384', 'ES512', -]; + 'none', +] as const; const defaultAlgorithm = algorithms[0]; @@ -29,7 +30,7 @@ export const plugin: PluginDefinition = { name: 'algorithm', label: 'Algorithm', defaultValue: defaultAlgorithm, - options: algorithms.map(value => ({ name: value, value })), + options: algorithms.map(value => ({ name: value === 'none' ? 'None' : value, value })), }, { type: 'text', @@ -53,11 +54,9 @@ export const plugin: PluginDefinition = { async onApply(_ctx, args) { const { algorithm, secret: _secret, secretBase64, payload } = args.config; const secret = secretBase64 ? Buffer.from(`${_secret}`, 'base64') : `${_secret}`; - const token = jwt.sign(`${payload}`, secret, { algorithm: algorithm as any }); - return { - url: args.url, - headers: [{ name: 'Authorization', value: `Bearer ${token}` }], - }; + const token = secret ? jwt.sign(`${payload}`, secret, { algorithm: algorithm as any }) : jwt.sign(`${payload}`, null); + const value = `Bearer ${token}`; + return { setHeaders: [{ name: 'Authorization', value }] }; } , }, diff --git a/plugins/template-function-response/src/index.ts b/plugins/template-function-response/src/index.ts index 90bb652..15a67a0 100644 --- a/plugins/template-function-response/src/index.ts +++ b/plugins/template-function-response/src/index.ts @@ -2,27 +2,27 @@ import { DOMParser } from '@xmldom/xmldom'; import { CallTemplateFunctionArgs, Context, + FormInput, HttpResponse, PluginDefinition, RenderPurpose, - TemplateFunctionArg, } from '@yaakapp/api'; import { JSONPath } from 'jsonpath-plus'; import { readFileSync } from 'node:fs'; import xpath from 'xpath'; -const behaviorArg: TemplateFunctionArg = { +const behaviorArg: FormInput = { type: 'select', name: 'behavior', label: 'Sending Behavior', defaultValue: 'smart', options: [ - { label: 'When no responses', value: 'smart' }, - { label: 'Always', value: 'always' }, + { name: 'When no responses', value: 'smart' }, + { name: 'Always', value: 'always' }, ], }; -const requestArg: TemplateFunctionArg = +const requestArg: FormInput = { type: 'http_request', name: 'request',