Skip to content

Commit e9b8163

Browse files
authored
Merge pull request #1957 from RandomByte/jwt-sign/add-header-option
Add 'header' ingredient to JWT Sign operation
2 parents 20390ae + 71c8c8a commit e9b8163

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

src/core/operations/JWTSign.mjs

+8-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ class JWTSign extends Operation {
3636
name: "Signing algorithm",
3737
type: "option",
3838
value: JWT_ALGORITHMS
39+
},
40+
{
41+
name: "Header",
42+
type: "text",
43+
value: "{}"
3944
}
4045
];
4146
}
@@ -46,11 +51,12 @@ class JWTSign extends Operation {
4651
* @returns {string}
4752
*/
4853
run(input, args) {
49-
const [key, algorithm] = args;
54+
const [key, algorithm, header] = args;
5055

5156
try {
5257
return jwt.sign(input, key, {
53-
algorithm: algorithm === "None" ? "none" : algorithm
58+
algorithm: algorithm === "None" ? "none" : algorithm,
59+
header: JSON.parse(header || "{}")
5460
});
5561
} catch (err) {
5662
throw new OperationError(`Error: Have you entered the key correctly? The key should be either the secret for HMAC algorithms or the PEM-encoded private key for RSA and ECDSA.

tests/operations/tests/JWTSign.mjs

+20-9
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,18 @@ TestRegister.addTests([
4444
recipeConfig: [
4545
{
4646
op: "JWT Sign",
47-
args: [hsKey, "HS256"],
47+
args: [hsKey, "HS256", "{}"],
48+
}
49+
],
50+
},
51+
{
52+
name: "JWT Sign: HS256 with custom header",
53+
input: inputObject,
54+
expectedOutput: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImN1c3RvbS5rZXkifQ.eyJTdHJpbmciOiJTb21lU3RyaW5nIiwiTnVtYmVyIjo0MiwiaWF0IjoxfQ.kXln8btJburfRlND8IDZAQ8NZGFFZhvHyooHa6N9za8",
55+
recipeConfig: [
56+
{
57+
op: "JWT Sign",
58+
args: [hsKey, "HS256", `{"kid":"custom.key"}`],
4859
}
4960
],
5061
},
@@ -55,7 +66,7 @@ TestRegister.addTests([
5566
recipeConfig: [
5667
{
5768
op: "JWT Sign",
58-
args: [hsKey, "HS384"],
69+
args: [hsKey, "HS384", "{}"],
5970
}
6071
],
6172
},
@@ -66,7 +77,7 @@ TestRegister.addTests([
6677
recipeConfig: [
6778
{
6879
op: "JWT Sign",
69-
args: [hsKey, "HS512"],
80+
args: [hsKey, "HS512", "{}"],
7081
}
7182
],
7283
},
@@ -77,7 +88,7 @@ TestRegister.addTests([
7788
recipeConfig: [
7889
{
7990
op: "JWT Sign",
80-
args: [esKey, "ES256"],
91+
args: [esKey, "ES256", "{}"],
8192
},
8293
{
8394
op: "JWT Decode",
@@ -92,7 +103,7 @@ TestRegister.addTests([
92103
recipeConfig: [
93104
{
94105
op: "JWT Sign",
95-
args: [esKey, "ES384"],
106+
args: [esKey, "ES384", "{}"],
96107
},
97108
{
98109
op: "JWT Decode",
@@ -107,7 +118,7 @@ TestRegister.addTests([
107118
recipeConfig: [
108119
{
109120
op: "JWT Sign",
110-
args: [esKey, "ES512"],
121+
args: [esKey, "ES512", "{}"],
111122
},
112123
{
113124
op: "JWT Decode",
@@ -122,7 +133,7 @@ TestRegister.addTests([
122133
recipeConfig: [
123134
{
124135
op: "JWT Sign",
125-
args: [rsKey, "RS256"],
136+
args: [rsKey, "RS256", "{}"],
126137
},
127138
{
128139
op: "JWT Decode",
@@ -137,7 +148,7 @@ TestRegister.addTests([
137148
recipeConfig: [
138149
{
139150
op: "JWT Sign",
140-
args: [rsKey, "RS384"],
151+
args: [rsKey, "RS384", "{}"],
141152
},
142153
{
143154
op: "JWT Decode",
@@ -152,7 +163,7 @@ TestRegister.addTests([
152163
recipeConfig: [
153164
{
154165
op: "JWT Sign",
155-
args: [esKey, "RS512"],
166+
args: [esKey, "RS512", "{}"],
156167
},
157168
{
158169
op: "JWT Decode",

0 commit comments

Comments
 (0)