# JWT Token Generation

**URL:** https://community.linx.software/community/t/jwt-token-generation/463
**Category:** How To
**Created:** [October 16, 2020, 12:00pm UTC](https://community.linx.software/community/t/jwt-token-generation/463 "2020-10-16T12:00:30Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![ronan](https://community.linx.software/community/user_avatar/community.linx.software/ronan/32/433_2.png) [@ronan](https://community.linx.software/community/u/ronan)
#### Post date: [October 16, 2020, 12:00pm UTC](https://community.linx.software/community/t/jwt-token-generation/463/1 "2020-10-16T12:00:30Z")

</div>

# Implementing Token Generation

In order for the [HTTP Bearer authentication](https://community.linx.software/t/resthost-guide-securing-your-api/462#auth-bearer) scheme to operate successfully, you need issue the [JWT Tokens](https://jwt.io/introduction/) using a secret `key` and a `payload` containing authentication details. This would typically take place in an unauthenticated operation which would issue tokens according to parameters passed in with a request.

To issue a JWT Token using Linx, use the [CreateJWT FNC](https://linx.software/docs/plugins/cryptography/reference/createjwt/) which is part of the [Cryptography PLG](https://linx.software/docs/reference/plugins/cryptography/overview/):

[CreateJWT FNC](https://linx.software/docs/plugins/cryptography/reference/createjwt/) has the following properties:

- `Secret key`: The secret key as [LST\<BYT\> TYP](https://linx.software/docs/reference/plugins/linx/content/list/) or `base64` to encode the payload with.
- `Payload`: The payload to include in the token. It must be a valid `JSON` string.
- `Algorithm`: The algorithm to use in the encoding (`HS256`, `RS356` etc.)

**Secret key:** This will be the `$.Setting` value that you added when you configured the [RESTHost svc](https://linx.software/docs/reference/plugins/rest/content/resthost/) `Auth config` with the `secret key`.

> ⚠ **Important** : Make sure the same `secret key` value is is used when verifying and generating tokens.

**Payload:** This will contain verification [`claims`](https://auth0.com/docs/tokens/concepts/jwt-claims) in a JSON format. These `claims` are properties of the token which are used to verify it. Things such as expiry time (`exp`) of the token or issuer (`iss`) can be included.

> ℹ A `unique_name` claim is **mandatory** in the `payload` which will contain a unique identifier. This `unique_name` claim will be decoded from the JWT Token during the built-in secuirty validations and then passed in to the operation via the `$.Input.Data.HttpContext.User.Name`.

Please note the terms 'Process' and 'Custom Type' have been depreciated and have been replaced with 'Function' and 'Type' respectively. More details [**here**](https://linx.software/docs/releasenotes/linx/5.20.2.0/).

 **The explanations are mostly for Linx5 users.**   
  
 Feel free to contact support@linx.software and we'll assist. 

* * *

## Create a JSON Payload

In order to create the JSON object which will contain the necessary fields of information, you can [import a Custom TYP](https://linx.software/docs/reference/designer/ui/#add-a-custom-type) by doing the following:

1. Right-click on a **project** or **folder** in the **Solution Explorer.**

2. Select **Import Custom Type**.

3. In the text editor, copy and paste the below `JSON` which contains some `claims` properties:

```json
        {
            "iss":"",
            "sub":"",
            "aud":"",
            "exp":"",
            "nbf":"",
            "iat":"",
            "jti":"",
            "unique_name":""
        }

```

1. Give the imported Custom TYP the `name` of `jwt_token` and click **Create**.

2. Next, drag-and-drop the `jwt_token` TYP from the **Solution Explorer** onto the ~~process~~ [_Function_](https://linx.software/docs/releasenotes/linx/5.20.2.0/) or operation in the **canvas** that you want to generate the tokens in.

3. In the **Properties** of the `jwt_token` TYP instance within the operation , expand the `Value` field, using the field editor.

4. In this example, the expiry time (`exp`) `claim` is going to be set for the token.

5. Next, the `unique_name` claim value is added to the token.

6. Click **Save**.

Now that the `jwt_token` TYP has been configured with the `claims` value, a JWT Token can be generated.

* * *

## Generate a JWT token

To generate a JWT token using the `secret key` and `payload`, do the following:

1. Drag a [CreateJWT FNC](https://linx.software/docs/plugins/cryptography/reference/createjwt/) from the **Plugins Panel** onto the operation or process, positioning the [CreateJWT FNC](https://linx.software/docs/plugins/cryptography/reference/createjwt/) below the `jwt_token` TYP.

2. Configure the **Properties** of the [CreateJWT FNC](https://linx.software/docs/plugins/cryptography/reference/createjwt/) like below:

The result of [CreateJWT FNC](https://linx.software/docs/plugins/cryptography/reference/createjwt/) will be a large [STR TYP](https://linx.software/docs/reference/plugins/linx/content/string/) containing a sequence of characters representing the JWT Token which can then be issued to in the operation’s response.

* * *

## Return JWT Token

Tokens can be returned as a basic type such as a [STR TYP](https://linx.software/docs/reference/plugins/linx/content/string/) or a [Custom TYP](https://linx.software/docs/reference/designer/ui/#add-a-custom-type). Usually, tokens are returned as a in the `response body` as a `JSON` object containing additional information, i.e. the expiry time of the token.

```auto
    {
      "token_type": "Bearer",
      "expires_in": 3599,
      "access_token": "eyJ0eXAiOiJKV1QiLCJhiOiJIsIng1dCI6Ik1uQ19WWmNBVGZNNXB"
    }

```

* * *

### Generic ~~process~~ [_Function_](https://linx.software/docs/releasenotes/linx/5.20.2.0/) Result

In order to return a token as the result of a ~~process~~ [_Function_](https://linx.software/docs/releasenotes/linx/5.20.2.0/) :

1. Import the above `JSON` as a [Custom TYP](https://linx.software/docs/reference/designer/ui/#add-a-custom-type) and give it the name of `token`.

2. Configure the result of the ~~process~~ [_Function_](https://linx.software/docs/releasenotes/linx/5.20.2.0/) by editing the `Output` fields in the **Properties** to have an output with the TYP of `token` TYP.

3. Add a [SetValue FNC](https://linx.software/docs/reference/plugins/linx/content/setvalue/) the the end of the ~~process~~ [_Function_](https://linx.software/docs/releasenotes/linx/5.20.2.0/) and configure the **Properties** like below:

This ~~process~~ [_Function_](https://linx.software/docs/releasenotes/linx/5.20.2.0/) can now be used interchangeable and referenced in several **operations** and **processes** in the **Solution**.

* * *

### Return token in operation response

In order to return a token as the `response body` of a [RESTHost svc](https://linx.software/docs/reference/plugins/rest/content/resthost/) operation :

Add the below object to the `API Definition`:

```auto
    Token:
      type: object
      properties:
        access_token:
          type: string
        expires_in:
          type: integer
        token_type:
          type: string

```

You will then need to reference this object as the response in the `API Definition` :

```auto
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Token'

```

To return the result of the [CreateJWT FNC](https://linx.software/docs/plugins/cryptography/reference/createjwt/) as the `response body` of the operation, add a [SetValue FNC](https://linx.software/docs/reference/plugins/linx/content/setvalue/) to the bottom of the operation and configure the **Properties** like below:

- **Target** : `$.Output.Data.Response200`
- **Source** : [expand field editor]
  - `access_token`: `CreateJWT`
  - `expires_in`: `3599`
  - `token_type`: `Bearer`

The `response body` of the operation will then contain this `token` object which contains the generated JWT Token.

> 📗[**Learn more**](https://community.linx.software/t/resthost-guide-handling-responses/461#custom-type-responses#custom-type-responses) about returning Custom TYP in responses.

* * *

---

<div class="post-metadata">

### Author: ![ronan](https://community.linx.software/community/user_avatar/community.linx.software/ronan/32/433_2.png) [@ronan](https://community.linx.software/community/u/ronan)
#### Post date: [October 28, 2020, 3:27pm UTC](https://community.linx.software/community/t/jwt-token-generation/463/2 "2020-10-28T15:27:31Z")

</div>


