Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/knokbak/cf-jwt
Browse files Browse the repository at this point in the history
  • Loading branch information
knokbak committed Jul 22, 2022
2 parents a5eef3b + f0194ae commit 3e9bc32
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"typescript": "^4.7.4"
},
"dependencies": {
"base64url": "^3.0.1",
"crypto-js": "^4.1.1"
}
}
19 changes: 13 additions & 6 deletions src/JWT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import { Algorithm } from './types';
import { algo as CryptoJS, enc as CryptoJSEncoders } from 'crypto-js';
import Base64URL from 'base64url';

export default class JWT {
public static signSync(
Expand Down Expand Up @@ -296,14 +297,16 @@ export default class JWT {
const computedSignature = hmac
.finalize()
.toString(CryptoJSEncoders.Base64);
const str = Buffer.from(computedSignature, 'base64').toString(
/*const str = Buffer.from(computedSignature, 'base64').toString(
'base64url'
);
);*/
const str = Base64URL.fromBase64(computedSignature);
return signature === str;
}

private static decodeSegment(segment: string): any {
const str = Buffer.from(segment, 'base64url').toString('utf8');
//const str = Buffer.from(segment, 'base64url').toString('utf8');
const str = Base64URL.decode(segment);
return JSON.parse(str);
}

Expand All @@ -316,10 +319,12 @@ export default class JWT {
const hmac = CryptoJS.HMAC.create(algo, secret);
hmac.update(data);
const finalized = hmac.finalize();
return Buffer.from(
/*const out = Buffer.from(
finalized.toString(CryptoJSEncoders.Base64),
'base64'
).toString('base64url');
).toString('base64url');*/
const out = Base64URL.fromBase64(finalized.toString(CryptoJSEncoders.Base64));
return out;
}

private static determineAlgorithm(algorithm: Algorithm): any {
Expand All @@ -337,7 +342,9 @@ export default class JWT {

private static encodeSegment(segment: any): string {
const str = JSON.stringify(segment);
return Buffer.from(str, 'utf8').toString('base64url');
//const out = Buffer.from(str, 'utf8').toString('base64url');
const out = Base64URL.encode(str);
return out;
}

private static buildPayload(data: any, options: any): any {
Expand Down

0 comments on commit 3e9bc32

Please sign in to comment.