Updating Your APIs to Generate and Require JWTs

In order for Thriftly to pass JWTs to your APIs’ users, you must update the services you use as Thriftly APIs to contain:

  • A struct to be used as your service’s JSON Web Token. The Thriftly Server will pass users this token (when they successfully call the Login function explained below) and then require the token (using the Auth function explained below) when users perform subsequent API calls.

  • The Login attribute and function. When a user completes a call to this function (e.g. by providing their username and password), Thriftly passes the user a JWT that is embedded in their subsequent API calls.

  • The Auth attribute and function. The Auth function looks for and validates the JWTs created by your Login function. When a user calls another function within your service (for example, to update or delete a customer’s record from your database), Auth confirms the user’s call contains a valid JWT before allowing them to access other functions.

Below, we’ll provide basic examples of how to update .NET/C#, Java, DataFlex, and Delphi services to include a JWT struct, the Login attribute and function, and the Auth attribute and function. Select the appropriate link from the list below to begin integrating JWT authorization into your service:

Adding JWT Authorization Within a .NET/C# Service

To update a .NET/C# service to include JWT-based authorization:

  1. Open your service within Microsoft Visual Studio.

Opening a service from the Visual Studio Solution Explorer sidebar
  1. Update your service to include a new JWT struct, as shown below. This struct defines the claims (unique, user-identifying information) that your JWT will include. Claims often contain information about a user’s session, including a username or ID and a login expiration. Thriftly does not require you to pass any specific claims within your users’ JWTs, though we recommend adding at least a login expiration. If you’d like more information on what claims you can add to your JWTs and how to use them, click here:

The JWT struct within a .NET/C# service
  1. Update the class contained within your service to include a new Login function, as shown below. Note that you can modify this Login function to suit your organization’s login needs and conventions. For example, instead of validating a user’s email, you could validate a user’s username and password. Additionally, you must modify the JWT your login function returns to define the claims you implemented in step 2 above:

A Login function within a .NET/C# service
  1. Update your class to include a new Auth function, as shown below:

An Auth function within a .NET/C# service

When you’ve finished, the start of your service (after the using Thriftly.Server directive) should look similar to the code below:

Full JWT implementation within a .NET/C# service

At this point, you must finalize your JWT configuration within Thriftly Developer and test your implementation using our API testing interface. To do so, jump to the Finalizing and Testing Your JWT Configuration section.

Adding JWT Authorization Within a Java Service

To update a Java service to include JWT-based authorization:

  1. Open your service within Eclipse Java.

Opening a service from the Package Explorer sidebar
  1. Update your service to import the Thriftly AuthAttribute and UnsecuredAttribute and include a new JWT struct, as shown below. Your JWT struct defines the claims (unique, user-identifying information) that your JWT will include. Claims often contain information about a user’s session, including a username or ID and a login expiration. Thriftly does not require you to pass any specific claims within your users’ JWTs, though we recommend adding at least a login expiration. If you’d like more information on what claims you can add to your JWTs and how to use them, click here:

The JWT struct within a Java service
  1. Update the original class contained within your service to include a new Login function, as shown below. Note that you can modify this Login function to suit your organization’s login needs and conventions. For example, instead of validating a user’s username and password, you could simply validate a user’s email. Additionally, you must modify the JWT your login function returns to define the claims you implemented in step 2 above:

A Login function within a Java service
  1. Update your class to include a new Auth function, as shown below:

An Auth function within a Java service

When you’ve finished, the start of your service should look similar to the code below:

Full JWT implementation within a Java service

At this point, you must finalize your JWT configuration within Thriftly Developer and test your implementation using our API testing interface. To do so, jump to the Finalizing and Testing Your JWT Configuration section.

Adding JWT Authorization Within a DataFlex Service

To update a DataFlex service to include JWT-based authorization:

  1. Open your service within DataFlex Studio.

Opening a service from the DataFlex Studio Code Explorer
  1. Update your service to include a new JWT struct, as shown below. This struct defines the claims (unique, user-identifying information) that your JWT will include. Claims often contain information about a user’s session, including a username or ID and a login expiration. Thriftly does not require you to pass any specific claims within your users’ JWTs, though we recommend adding at least a login expiration. If you’d like more information on what claims you can add to your JWTs and how to use them, click here:

The JWT struct within a DataFlex service
  1. Update your service object to include a new Login function, as shown below. Note that you can modify this Login function to suit your organization’s login needs and conventions. For example, instead of validating a user’s username and password, you could simply validate a user’s email. Additionally, you must modify the JWT your login function returns to define the claims you implemented in step 2 above:

A Login function within a DataFlex service
  1. Update your class to include a new Auth function, as shown below:

An Auth function within a DataFlex service

When you’ve finished, the start of your service should look similar to the code below:

Full JWT implementation within a DataFlex service

At this point, you must finalize your JWT configuration within Thriftly Developer and test your implementation using our API testing interface. To do so, jump to the Finalizing and Testing Your JWT Configuration section.

Adding JWT Authorization Within a Delphi Service

To update a Delphi service to include JWT-based authorization:

  1. Open your service within Delphi RAD Studio.

Opening a service from the Delphi RAD Studio Project Manager
  1. Update your service to include the uses DateUtils and System.SysUtils directives and a new JWT struct, as shown below. Your JWT struct defines the claims (unique, user-identifying information) that your JWT will include. Claims often contain information about a user’s session, including a username or ID and a login expiration. Thriftly does not require you to pass any specific claims within your users’ JWTs, though we recommend adding at least a login expiration. If you’d like more information on what claims you can add to your JWTs and how to use them, click here:

The JWT struct within a Delphi service
  1. Update the class contained within your service to include a new Login and Auth function, as shown below. Note that the UnsecuredAttribute positioned above the Login function is necessary to allow users to log in to your service and receive a JWT.

The Login and Auth functions created within a Delphi service
  1. Update your service’s implementation to include both the Login and Auth functions, as shown below. Note that you can modify the Login function to suit your organization’s login needs and conventions. For example, instead of validating a user’s username and password, you could simply validate a user’s email. Additionally, you must modify the JWT your login function returns to define the claims you implemented in step 2 above:

The Login and Auth functions implemented within a Delphi service

When you’ve finished, your service should look similar to the code below:

Full JWT implementation within a Delphi service

At this point, you must finalize your JWT configuration within Thriftly Developer and test your implementation using our API testing interface. To do so, jump to the Finalizing and Testing Your JWT Configuration section.