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 will 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. Select New OpenAPI. Click Next.

    Select OpenAPI

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

    Basic information screen

  6. Click Next.
  7. Make no changes on the Secure screen. Click Next.

    Secure API

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

    New API summary

  9. In the side bar of the Design page, select Paths to display the Paths panel.

    Paths page

  10. Click Add.
  11. In the Path name field, enter /gen.
  12. In the Operations section, click Add.
  13. Select GET and click Add.

    Path info

  14. Click Save.
  15. Click /gen in the list of available paths.

    Path list

  16. Click GET in the list of Operations.
  17. Scroll down. In the Parameters section, click Add.
    1. Select REQUIRED.
    2. Enter iss-claim in the NAME field.
    3. Select header in the LOCATED IN field.
    4. Select string in the TYPE field.
    5. Enter Enter https://myidp.ibm.com to match in the DESCRIPTION field.

    Path Parameter settings

  18. Click Add to add a second parameter.
    1. Select REQUIRED.
    2. Enter aud-claim in the NAME field.
    3. Select header in the LOCATED IN field.
    4. Select string in the TYPE field.
    5. Enter Enter ClientID1 to match in the DESCRIPTION field.

    Path Parameter settings

  19. In the Response section, change the description of the pre-supplied 200 status code to 200 OK.

    Path Response settings

  20. Click Save.
  21. Click Assemble.

    Path list

  22. Hover the mouse over the existing Proxy or Invoke action and click the trash can icon to delete it.

    Proxy action

  23. Drag the Set Variable action onto the processing flow line. A configuration panel automatically opens.

    Set Variable action added

  24. Click + Action field.
  25. Enter hs256-key in the Set field.
  26. Select string in the Type field.
  27. Enter a JWK in the Value field. Here 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

  28. Close the property panel. Click Save.
  29. Drag the Generate JWT action onto the processing flow line after the set-variable icon. A configuration panel automatically opens.

    JWT Generate action added

  30. Enter request.headers.iss-claim in the Issuer Claim field.
  31. Enter request.headers.aud-claim in the Audience Claim field.

    JWT Generate properties

  32. Enter hs256-key in the Sign JWK variable name field.
  33. Select HS256 in the Cryptogrpahic Algorithm field.

    JWT Generate properties

  34. Close the property panel. Click Save.
  35. Drag the GatewayScript action onto the processing flow line after the Generate JWT icon. A configuration panel automatically opens.
  36. Enter the following code:
    var apim = require('apim');
    apim.setvariable('message.body',apim.getvariable('generated.jwt'));

    GatewayScript action added

  37. 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 the Test icon The Test icon.

    Assemble page

  2. Click Activate API.

    Test setup

  3. Select the get /gen Operation.
  4. Enter https://myidp.ibm.com in the iss-claim field.
  5. Enter ClientID1 in the aud-claim field.

    Set param

  6. Click Invoke. You may encounter a yellow error box with a URL embedded in it. Click this URL to override a browser certificate error.

    Test invoke

  7. Click Invoke again. The response contains the generated JWT.

    Repeat invoke

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.