Tutorial: Generate a JSON Web Token (JWT)

This tutorial shows you how to define and implement a REST API definition that generates a JSON Web Token (JWT).

About this tutorial

In this tutorial, you complete the following lessons:
  1. Generate a JWT
  2. Testing the REST API
Note: The Sandbox catalog must be configured to use either a DataPower® Gateway (v5 compatible) or a DataPower API Gateway or both. See Creating and configuring catalogs.

Generate a JWT

Create a REST API to generate and return a JSON Web Token (JWT).

To add and define this REST API, complete the following steps:
  1. Log in to API Manager.
  2. In the Welcome page, click the Develop APIs and Products tile.

    API Manager screen

  3. Click Add > API .

    Add API screen

  4. Ensure that OpenAPI 3.0 is selected.
  5. Select New OpenAPI and click Next.

    Select OpenAPI

  6. Enter the appropriate information to create a REST API definition.
    1. In the Title field, enter JWT.
    2. The Name and Base Path fields auto-populate with the terms jwt and /jwt respectively.
    3. The Version field auto-populates with 1.0.0.

    Basic information screen

  7. Click Next.
  8. Under the Secure section, click Next.

    Secure API

  9. You see the progress as the new API gets created. When it is done, you see a Summary. Click Edit API.

    New API summary

  10. In the side bar of the Design page, select Paths to display the Paths panel.
  11. Click Add +.

    Paths page

  12. In the Path field, enter /gen.
  13. Click Add.

    Add a path info

  14. Scroll down. In the Parameters section, click Add +.

    Add parameters

    1. Enter iss-claim in the Parameter name field.
    2. Select the header from the Located In list.
    3. Enter https://myidp.ibm.com to match in the Description field.
    4. Select the Required checkbox.
    5. Click Add.

      Path Parameter settings

    6. Scroll down. In the Schema section, click Create

      Add schema

    7. Select string in the Type list.
    8. Click Add.

      Add string

  15. After you create the parameter, click Save.
  16. To add a second parameter. Complete the following steps:
    1. In the side bar of the Design page, select Parameters under the newly created /gen path.

      Parameter side bar

    2. Click Add +.

      Parameter page

    3. Enter aud-claim in the Parameter name field.
    4. Select header from the Located In list.
    5. Enter Enter ClientID1 to match in the Description field.
    6. Select the Required checkbox.
    7. Click Add.

      Path Parameter settings

    8. Scroll down. In the Schema section, click Create.

      Add schema

    9. Select string from the Type list.
    10. Click Add.

      Add string

  17. After you create the second parameter, click Save.
  18. Click Components > Responses.
  19. Click Add.

    Responses page

  20. The Response name field auto-populates with 200.
  21. Enter 200 OK in the Description field.

    Add schema

  22. Click Add.
  23. After you create the response, click Save.
  24. Select the Gateway tab.
  25. Hover the mouse over the existing policy node and click the trash can icon to delete it.

    Gateway page

  26. From Actions, click Set Variable to add the action onto the processing flow line. A configuration panel automatically opens.
  27. Click Add action.

    Set Variable action added

  28. Enter hs256-key in the Set field.
  29. Select string from the Type list.
  30. Enter a JWK in the Value field. The following is an example. { "alg": "HS256", "kty": "oct", "use": "sig", "k": "o5yErLaE-dbgVpSw65Rq57OA9dHyaF66Q_Et5azPa-XUjbyP0w9iRWhR4kru09aFfQLXeIODIN4uhjElYKXt8n76jt0Pjkd2pqk4t9abRF6tnL19GV4pflfL6uvVKkP4weOh39tqHt4TmkBgF2P-gFhgssZpjwq6l82fz3dUhQ2nkzoLA_CnyDGLZLd7SZ1yv73uzfE2Ot813zmig8KTMEMWVcWSDvy61F06vs_6LURcq_IEEevUiubBxG5S2akNnWigfpbhWYjMI5M22FOCpdcDBt4L7K1-yHt95Siz0QUb0MNlT_X8F76wH7_A37GpKKJGqeaiNWmHkgWdE8QWDQ", "kid": "hs256-key" }

    Setvar properties

  31. Close the property panel. Click Save.
  32. From Actions, click Generate JWT to add the action onto the processing flow line after the set-variable icon. A configuration panel automatically opens.
    Note: If the Build assembly flow panel is not visible, click + near the set-variable to show the panel.

    JWT Generate action added

  33. Enter request.headers.iss-claim in the Issuer Claim field.
  34. Enter request.headers.aud-claim in the Audience Claim field.
  35. Enter hs256-key in the Sign JWK variable name field.
  36. Select HS256 from the Cryptogrpahic Algorithm list.

    JWT Generate properties

  37. Close the property panel. Click Save.
  38. From Actions, click GatewayScript to add the action onto the processing flow line after the Generate JWT icon. A configuration panel automatically opens.
  39. Enter the following code:
    var apim = require('apim');
    apim.setvariable('message.body',apim.getvariable('generated.jwt'));

    GatewayScript action added

    Note: You might see the following warning when you add the GatewayScript action: This gatewayscript policy should not use the apim module which is only for migrating old APIs. You can ignore this warning and proceed.
  40. Close the property panel. Click Save.

Testing the REST API

Note: Due to Cross-Origin Resource Sharing (CORS) restrictions, the assembly test tool cannot be used with the Chrome or Safari browsers on the macOS Catalina platform.
To test the REST API, complete the following steps:
  1. Click Test.
    Note: You cannot test the REST API if you have not configured any gateways for the catalog and an error message is displayed under the Test tab.Test
  2. Click Target configuration and set Auto-publish to On.
  3. Click Save preferences. The API status is shown as Online in the Test tab.
  4. Enter https://myidp.ibm.com in the Value field corresponding to the iss-claim parameter.
  5. Enter ClientID1 in the Value field corresponding to the aud-claim parameter.
  6. Click Send.

    Test page and send button

  7. The response contains the generated JWT.

    Test result

Manage your API definition

Now, that your new API works correctly, you can manage this API. To see your immediate options, take the following steps.

  1. Click the Develop icon Develop icon on the navigation bar.

    Navigation bar

  2. Click the Options icon The manage icon alongside the JWT API.

    Manage API

  3. Select Download.

What you did in this tutorial

In this tutorial, you completed the following activities:
  • Created a new API definition that generates a JSON Web Token (JWT).
  • Tested the new API.