Skip to content

Commit

Permalink
renamed boolean to 'isThrowing'
Browse files Browse the repository at this point in the history
  • Loading branch information
timonson committed Mar 28, 2020
1 parent e3e28f9 commit 456e00a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ In [cases](https://www.rfc-editor.org/rfc/rfc7515.html#appendix-F) where you
only need the signing and verification feature of the JWS, you can omit the
_payload_.

#### validateJwt(jwt: string, key: string, hasErrorsEnabled: boolean = true, critHandlers?: Handlers): Promise<JwtObject | null>
#### validateJwt(jwt: string, key: string, isThrowing: boolean = true, critHandlers?: Handlers): Promise<JwtObject | null>

The function `validateJwt` returns a _promise_ which - if the JWT is valid -
resolves to a JWT representation as JavaScript object:
`{header, payload, signature}`.
`{ header, payload, signature }`. If the Jwt is invalid the promise resolves to
`null` or an `Error` is thrown - depending how you set the boolean `isThrowing`.

#### setExpiration(exp: number | Date): number

Expand Down Expand Up @@ -115,7 +116,7 @@ for await (const req of serve("0.0.0.0:8000")) {
req.respond({ body: encode(jwt + "\n") })
} else {
const requestBody = decode(await Deno.readAll(req.body))
;(await validateJwt(requestBody, key, false))
await validateJwt(requestBody, key, false)
? req.respond({ body: encode("Valid JWT\n") })
: req.respond({ body: encode("Invalid JWT\n"), status: 401 })
}
Expand Down
4 changes: 2 additions & 2 deletions validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function parseAndDecode(jwt: string): JwtObject {
async function validateJwt(
jwt: string,
key: string,
hasErrorsEnabled = true,
isThrowing = true,
critHandlers?: Handlers
): Promise<JwtObject | null> {
try {
Expand All @@ -95,7 +95,7 @@ async function validateJwt(
else throw Error("signatures don't match")
} catch (err) {
err.message = `Invalid JWT: ${err.message}`
if (hasErrorsEnabled) throw err
if (isThrowing) throw err
else return null
}
}
Expand Down

0 comments on commit 456e00a

Please sign in to comment.