# JSON Web Token (JWT) Helper

An implementation of the Json Web Token (JWT) helper standard, offering the ability to sign and verify tokens

## Overview

The main purpose of the JSON Web Token connector is to sign and verify JSON Web Tokens.
It is implemented using the NodeJS `jsonwebtoken` library.  This [libraries docs page](https://github.com/auth0/node-jsonwebtoken) can be used for more details about the various advanced options.

## Operations List

* Sign
* Verify

## Authentication

This connector provides an authentication option that allows you to securely store any key/secret you wish to use to sign/verify the JSON Web Token.\
![The default key input value for a JSON Web Token sign/verify operation](https://tray.ai/documentation/images/connectors/helper/json-web-token-jwt/Zllfb6DKTylAWrGKB158k_Screenshot%202022-09-13%20at%2011.34.54.png)

By default the key input property will reference the key stored in the authentication, however you can manually override this with a value if you wish to source your key from elsewhere.  Please remember that if you override this key input property, it the value will not be obfuscated in the workflow execution logs.

## Example usage

Using a simple key/secret `abcdef` and the following JSON payload

```json
\{
  "sub": "1234567890",
  "name": "John Doe",
  "iat": 1516239022
\}
```

![Example JWT Sign Operation Input](https://tray.ai/documentation/images/connectors/helper/json-web-token-jwt/6BRnlWlnkQu1hvTRvJRGQt_Screenshot%202022-09-13%20at%2011.39.58.png)
You can call the `Sign` operation and will receive the following JWT response

```console
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.IWvkxodTX0kCUbSXguAwSxBlQ-AxTdq2xnweEdfUbgo
```

If you were to pass this JSON Web Token response into the `Verify` operation, you should receive the same payload back.

## Error Handling

The `Verify` operation supports [Manual Error Handling](#) to take a different path if the token is valid or invalid.  This could be used for example to receive an incoming Webhook payload, verify the payload and respond to the Webhook with different messages depending on the validity of the JWT.
![Error handling with the JWT Verify operation](https://tray.ai/documentation/images/connectors/helper/json-web-token-jwt/4Wg4v7r3mhsymtFpowMn1B_Screenshot%202022-09-13%20at%2011.42.48.png)
