Skip to content

Commit 2642e5f

Browse files
committed
Repair and improve the JWT Signer with RS256
1 parent 236d959 commit 2642e5f

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

src/cryptography/drivers/node/AsymmetricJWTSigner.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,12 @@ export class AsymmetricJWTSigner extends JWTSigner {
5555
}
5656

5757
let claimBody = claimComponents[1]
58-
59-
let decodedSignature = new Buffer(decode(claimComponents[2]))
58+
let claimSignature = claimComponents[2]
6059

6160
// Delegate validation of the signature to the signer
6261
await this.signer.verify(
6362
new Buffer(`${claimHeader}.${claimBody}`),
64-
decodedSignature
63+
new Buffer(claimSignature)
6564
)
6665

6766
// If no error occurred then the token is valid. Parse the claim and return

src/cryptography/drivers/node/RSASHA256Signer.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ export class RSASHA256Signer extends AsymmetricSigner {
1313
}
1414

1515
public async verify(plaintext: Buffer, signature: Buffer): Promise<Buffer> {
16-
let isValid = await RS256.verify(plaintext, signature, this.publicKey)
16+
let isValid = await RS256.verify(
17+
plaintext,
18+
signature.toString(),
19+
this.publicKey
20+
)
1721

1822
if (!isValid) {
1923
throw new InvalidSignatureError()

tests/cryptography/drivers/node/AsymmetricJWTSigner.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,22 @@ describe("AsymmetricJWTSigner", () => {
9999
admin: true,
100100
})
101101
})
102+
103+
it("Should throw if the JWT is invalid", async () => {
104+
try {
105+
await jwtSigner.verify(
106+
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9" +
107+
"." +
108+
"eyJuYW1lIjoiSGVsbG8gV29ybGQiLCJhZG1pbiI6dHJ1ZX0" +
109+
"." +
110+
"abcdefg"
111+
)
112+
expect(true).to.equal(false)
113+
} catch (e) {
114+
expect(e.message).to.equal(
115+
"The signature provided was not valid."
116+
)
117+
}
118+
})
102119
})
103120
})

0 commit comments

Comments
 (0)