|
1 | 1 | import { CallTemplateFunctionArgs, Context, PluginDefinition } from '@yaakapp/api'; |
2 | | -import { createHash } from 'node:crypto'; |
| 2 | +import { createHash, createHmac } from 'node:crypto'; |
3 | 3 |
|
4 | | -const algorithms = ['md5', 'sha1', 'sha256', 'sha512']; |
| 4 | +const algorithms = ['md5', 'sha1', 'sha256', 'sha512'] as const; |
| 5 | +const encodings = ['base64', 'hex'] as const; |
5 | 6 |
|
6 | | -export const plugin: PluginDefinition = { |
7 | | - templateFunctions: algorithms.map(algorithm => ({ |
8 | | - name: `hash.${algorithm}`, |
9 | | - description: 'Hash a value to its hexidecimal representation', |
10 | | - args: [ |
11 | | - { |
12 | | - name: 'input', |
13 | | - label: 'Input', |
14 | | - placeholder: 'input text', |
15 | | - type: 'text', |
16 | | - }, |
17 | | - ], |
18 | | - async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> { |
19 | | - if (!args.values.input) return ''; |
20 | | - return createHash(algorithm) |
21 | | - .update(args.values.input, 'utf-8') |
22 | | - .digest('hex'); |
| 7 | +type TemplateFunctionPlugin = NonNullable<PluginDefinition['templateFunctions']>[number]; |
| 8 | + |
| 9 | +const hashFunctions: TemplateFunctionPlugin[] = algorithms.map(algorithm => ({ |
| 10 | + name: `hash.${algorithm}`, |
| 11 | + description: 'Hash a value to its hexidecimal representation', |
| 12 | + args: [ |
| 13 | + { |
| 14 | + type: 'text', |
| 15 | + name: 'input', |
| 16 | + label: 'Input', |
| 17 | + placeholder: 'input text', |
| 18 | + multiLine: true, |
| 19 | + }, |
| 20 | + { |
| 21 | + type: 'select', |
| 22 | + name: 'encoding', |
| 23 | + label: 'Encoding', |
| 24 | + defaultValue: 'base64', |
| 25 | + options: encodings.map(encoding => ({ |
| 26 | + label: capitalize(encoding), |
| 27 | + value: encoding, |
| 28 | + })), |
| 29 | + }, |
| 30 | + ], |
| 31 | + async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> { |
| 32 | + const input = String(args.values.input); |
| 33 | + const encoding = String(args.values.encoding) as typeof encodings[number]; |
| 34 | + |
| 35 | + return createHash(algorithm) |
| 36 | + .update(input, 'utf-8') |
| 37 | + .digest(encoding); |
| 38 | + }, |
| 39 | +})); |
| 40 | + |
| 41 | +const hmacFunctions: TemplateFunctionPlugin[] = algorithms.map(algorithm => ({ |
| 42 | + name: `hmac.${algorithm}`, |
| 43 | + description: 'Compute the HMAC of a value', |
| 44 | + args: [ |
| 45 | + { |
| 46 | + type: 'text', |
| 47 | + name: 'input', |
| 48 | + label: 'Input', |
| 49 | + placeholder: 'input text', |
| 50 | + multiLine: true, |
23 | 51 | }, |
24 | | - })), |
| 52 | + { |
| 53 | + type: 'text', |
| 54 | + name: 'key', |
| 55 | + label: 'Key', |
| 56 | + password: true, |
| 57 | + }, |
| 58 | + { |
| 59 | + type: 'select', |
| 60 | + name: 'encoding', |
| 61 | + label: 'Encoding', |
| 62 | + defaultValue: 'base64', |
| 63 | + options: encodings.map(encoding => ({ |
| 64 | + value: encoding, |
| 65 | + label: capitalize(encoding), |
| 66 | + })), |
| 67 | + }, |
| 68 | + ], |
| 69 | + async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> { |
| 70 | + const input = String(args.values.input); |
| 71 | + const key = String(args.values.key); |
| 72 | + const encoding = String(args.values.encoding) as typeof encodings[number]; |
| 73 | + |
| 74 | + return createHmac(algorithm, key, {}) |
| 75 | + .update(input) |
| 76 | + .digest(encoding); |
| 77 | + }, |
| 78 | +})); |
| 79 | + |
| 80 | +export const plugin: PluginDefinition = { |
| 81 | + templateFunctions: [...hashFunctions, ...hmacFunctions], |
25 | 82 | }; |
| 83 | + |
| 84 | +function capitalize(str: string): string { |
| 85 | + return str.charAt(0).toUpperCase() + str.slice(1); |
| 86 | +} |
0 commit comments