Skip to content

Commit

Permalink
unsafeParseJWT function
Browse files Browse the repository at this point in the history
  • Loading branch information
Pinta365 committed Apr 28, 2024
1 parent 478c42d commit f51260b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ const data = await validateJWT(jwt, cryptoKey);
const data = await validateJWT(jwt, false);
```

- **`unsafeParseJWT(jwt: string): Promise<JWTPayload>`**

```javascript
// "unsafely" parse a JWT without cryptokey, will return the payload.
const unsafeData = unsafeParseJWT(jwt);
```

**Helper Functions**

- **`generateKey(keyStr: string, optionsOrAlgorithm?: SupportedKeyAlgorithms | Options): Promise<CryptoKey>`**
Expand Down
2 changes: 1 addition & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cross/jwt",
"version": "0.4.2",
"version": "0.4.3",
"exports": "./mod.ts",

"tasks": {
Expand Down
2 changes: 1 addition & 1 deletion mod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// mod.ts
export { signJWT } from "./src/sign.ts";
export { validateJWT } from "./src/validate.ts";
export { unsafeParseJWT, validateJWT } from "./src/validate.ts";
export { exportPEMKey, generateKey, generateKeyPair, importPEMKey } from "./src/cryptokeys.ts";
export type {
GenerateKeyOptions,
Expand Down
13 changes: 13 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,16 @@ export class JWTRequiredOptionMissingError extends JWTFormatError {
this.name = "JWTRequiredOptionMissingError";
}
}

/**
* Represents a parsing error that occurred during JWT parsing.
*/
export class JWTParseError extends Error {
/**
* @param {string} message - A descriptive message about the parsing failure.
*/
constructor(message: string) {
super(message);
this.name = "JWTParseError";
}
}
18 changes: 18 additions & 0 deletions src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
JWTExpiredError,
JWTFormatError,
JWTNotYetValidError,
JWTParseError,
JWTRequiredClaimMissingError,
JWTUnsupportedAlgorithmError,
JWTValidationError,
Expand Down Expand Up @@ -237,3 +238,20 @@ export async function verify(key: CryptoKey, data: string, signature: string, op
throw new Error("Unsupported algorithm");
}
}

/**
* Validates and parses a JWT, verifying it with the given key.
*
* @param {string} jwt - The encoded JWT string.
* @returns {Promise<JWTPayload>} A promise resolving to the decoded JWT payload.
* @throws {JWTParseError} If the jwt string is not parsable.
*/
export function unsafeParseJWT(jwt: string): Promise<JWTPayload> {
try {
const jwtParts = validateParts(jwt);
const payload = JSON.parse(textDecode(decodeBase64Url(jwtParts[1])));
return payload;
} catch (error) {
throw new JWTParseError(error);
}
}

0 comments on commit f51260b

Please sign in to comment.