Space Cloud allows you to transform the JWT claims of a request easily using Go templates.
This feature is available in event triggers and remote services only.
Many times, your services might be expecting token claims with a different structure as compared to what your clients or Space Cloud would be providing. Or you might want to set or completely override the token claims in some cases. In either case, you want a way to describe the desired token claims.
For example, you might have an existing email service that sends a welcome email to users on signup. And, now you want to use it with Space Cloud’s event triggers. However, let’s say that your email service requires the following claims in the request:
{
"role": "admin"
}
Now instead of changing your email service or making a wrapper service, you can simply specify a transformation in Space Cloud to provide the above claims in a request.
Space Cloud allows you to template out the desired claims using Go templates. You must specify the template either as a JSON or a YAML string.
Space Cloud executes the specified go template to generate the templated output at runtime. It then parses the templated output depending on the template format (JSON/YAML) and uses that parsed output as the claims.
Go templates are extremely powerful. You can use flow control blocks (if else
, loops, etc.), variables, functions and much more in Go templates.
If you are new to Go templates, read this article on using go templates.
The templates you specify will have access to certain pre-defined variables. For example, the variable auth
contains the existing claims.
For example, let’s say that the claims provided by a client while calling a remote service contains the user’s id in the id
field of the claims, while you want to provide it to your service as the userId
field in the claims. Here’s how you can do it using the auth
variable:
{
"userId": "{{.auth.id}}"
}
Check out the complete list of available variables.
Space Cloud also comes packed with certain helper functions which you can use in templates.
Head over to the Eventing
section of the Mission Control:
Click the Edit
button beside the required event trigger to open the event trigger configuration modal.
Open the Advanced
section and scroll below to Apply Tramsformations
section:
After checking the checkbox, you can specify the template for JWT claims as well as the template output format (JSON/YAML).
Head over to the GraphQL API
section in Mission Control:
Click the View
button beside the required service to view the endpoints of that service:
Open the Advanced
section of the endpoint configuration and scroll below to the Apply transformations
section:
After checking the checkbox, you can specify the template for JWT claims as well as the template output format (JSON/YAML).