|
1 |
| -# Attach Backend JWT Token manipulation Policy to APIs via REST API |
| 1 | +# Backend JWT Manipulation Policy via APK Conf |
2 | 2 |
|
3 |
| -There can be scenarios where a backend service needs to make different decisions or respond with different data, depending on the application end-user that consumes an API. This can be facilitated by APK by sending the attributes in a JWT via an HTTP header, to the backend service when the API call is being forwarded. |
| 3 | +### Before you begin |
4 | 4 |
|
5 |
| -To find more information about backend JWT generation, please refer to the [Passing End User Attributes to the Backend](https://apim.docs.wso2.com/en/latest/deploy-and-publish/deploy-on-gateway/choreo-connect/passing-enduser-attributes-to-the-backend-via-choreo-connect/#enabling-the-default-backend-jwt-generator). |
| 5 | +- [Create an API](../../../get-started/quick-start-guide.md) |
6 | 6 |
|
7 |
| -## Before you begin |
| 7 | +### Step 1 - Retrieve existing API configuration |
8 | 8 |
|
9 |
| -- [Create an API](../../../get-started/quick-start-guide.md) |
| 9 | +Here, you can use the apk-conf file which is created in [Create an API](../../../get-started/quick-start-guide.md) documentation and save this content into a file named `EmployeeService.apk-conf`. |
| 10 | + |
| 11 | +### Step 2 - Add the API Policy for Backend JWT |
| 12 | + |
| 13 | +A sample API Policy used for sending a JWT to the backend is given below. |
| 14 | +``` |
| 15 | +apiPolicies: |
| 16 | + request: |
| 17 | + - policyName: "BackendJwt" |
| 18 | + parameters: |
| 19 | + encoding: Base64 |
| 20 | + signingAlgorithm: SHA256withRSA |
| 21 | + header: X-JWT-Assertion |
| 22 | + tokenTTL: 3600 |
| 23 | + customClaims: |
| 24 | + - claim: claim1 |
| 25 | + value: value1 |
| 26 | + - claim: claim2 |
| 27 | + value: value2 |
| 28 | +``` |
| 29 | + |
| 30 | +The configurations that you need when attaching API Policies for Backend JWT manipulation are available in [Backend JWT Configurations section.](#backend-jwt-api-policy-configuration) |
| 31 | + |
| 32 | +The above policy will send the following claims to the backend under the header "X-JWT-Assertion". |
| 33 | + |
| 34 | +``` |
| 35 | +{ |
| 36 | + claim1: value1 |
| 37 | + claim2: value2 |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +The sample APK configuration content after the modification is shown below. |
| 42 | + |
| 43 | +``` |
| 44 | +name: "test-backend-jwt" |
| 45 | +basePath: "/backend-jwt" |
| 46 | +version: "1.0" |
| 47 | +type: "REST" |
| 48 | +defaultVersion: false |
| 49 | +endpointConfigurations: |
| 50 | + production: |
| 51 | + endpoint: "https://httpbin.org/anything" |
| 52 | +operations: |
| 53 | + - target: "/test" |
| 54 | + verb: "GET" |
| 55 | + secured: true |
| 56 | + scopes: [] |
| 57 | +apiPolicies: |
| 58 | + request: |
| 59 | + - policyName: "BackendJwt" |
| 60 | + parameters: |
| 61 | + encoding: Base64 |
| 62 | + signingAlgorithm: SHA256withRSA |
| 63 | + header: X-JWT-Assertion |
| 64 | + tokenTTL: 3600 |
| 65 | + customClaims: |
| 66 | + - claim: claim1 |
| 67 | + value: value1 |
| 68 | + - claim: claim2 |
| 69 | + value: value2 |
| 70 | +``` |
| 71 | +### Step 3 - Deploy the API in APK |
| 72 | + |
| 73 | +Refer to the [Deploy the API in APK](../../../get-started/quick-start-guide.md#deploy-the-api-in-apk) to deploy the API using APK configuration. |
| 74 | + |
| 75 | +### Step 4 - Generate an Acess Token |
| 76 | + |
| 77 | +Follow the [Generate Access Token](../../../develop-and-deploy-api/security/generate-access-token.md) documentation to generate an access token. |
| 78 | + |
| 79 | +### Step 5 - Invoke the API |
| 80 | + |
| 81 | +You can invoke the API using the following command. |
| 82 | + |
| 83 | +``` |
| 84 | +curl --location 'https://default.gw.wso2.com:9095/backend-jwt/1.0/test' \ |
| 85 | +--header 'Host: default.gw.wso2.com' \ |
| 86 | +--header 'Authorization: Bearer <accessToken> |
| 87 | +``` |
| 88 | + |
| 89 | +Since this guide uses the [httpbin service](https://httpbin.org/anything) which echoes the request and all of its headers, when you invoke the API, you will see a response similar to the following. The header "X-Jwt-Assertion" contains the JWT generated containing the claims. |
| 90 | + |
| 91 | +``` |
| 92 | +"headers": { |
| 93 | + "Accept": "*/*", |
| 94 | + "Accept-Encoding": "gzip, deflate, br", |
| 95 | + "Host": "httpbin.org", |
| 96 | + "X-Jwt-Assertion": "eyJraWQiOiI4.....9UcOovptvkajf6xUqUbIJfMQp9g" |
| 97 | + } |
| 98 | +``` |
| 99 | + |
| 100 | +### Backend JWT API Policy configuration |
| 101 | + |
| 102 | +The configurable fields of the above API policy have been described below. |
| 103 | + |
| 104 | +| Field | Description | |
| 105 | +| ---------------- | ----------------------------------------------------------------------------------------- | |
| 106 | +| encoding | The encoding mechanism used to encode the Backend JWT. | |
| 107 | +| signingAlgorithm | The signing algorithm used to sign the Backend JWT. | |
| 108 | +| header | The name of the HTTP header to which the Backend JWT is attached and sent to the backend. | |
| 109 | +| tokenTTL | The expiry time of the Backend JWT. | |
| 110 | +| customClaims | List of custom claims that needs to be added to the Backend JWT. | |
| 111 | +| claim | Name of the claim to send in the BackendJWT. | |
| 112 | +| value | Value of the claim to send in the BackendJWT. | |
10 | 113 |
|
11 |
| -### Backend JWT configuration |
12 |
| - |
13 |
| -<table> |
14 |
| - <tbody> |
15 |
| - <tr> |
16 |
| - <th colspan="2">Field</th> |
17 |
| - <th>Description</th> |
18 |
| - </tr> |
19 |
| - <tr> |
20 |
| - <td colspan="2" class="confluenceTd"><pre>encoding</pre></td> |
21 |
| - <td class="confluenceTd">The encoding mechanism used to encode the Backend JWT.</td> |
22 |
| - </tr> |
23 |
| - <tr> |
24 |
| - <td colspan="2" class="confluenceTd"><pre>signingAlgorithm</pre></td> |
25 |
| - <td class="confluenceTd">The signing algorithm used to sign the Backend JWT.</td> |
26 |
| - </tr> |
27 |
| - <tr> |
28 |
| - <td colspan="2" class="confluenceTd"><pre>header</pre></td> |
29 |
| - <td class="confluenceTd">The name of the HTTP header to which the Backend JWT is attached.</td> |
30 |
| - </tr> |
31 |
| - <tr> |
32 |
| - <td colspan="2" class="confluenceTd"><pre>tokenTTL</pre></td> |
33 |
| - <td class="confluenceTd">The expiry time of the Backend JWT.</td> |
34 |
| - </tr> |
35 |
| - <tr> |
36 |
| - <td colspan="2" class="confluenceTd"><pre>customClaims</pre></td> |
37 |
| - <td class="confluenceTd">List of custom claims that needs to be added to the Backend JWT.</td> |
38 |
| - </tr> |
39 |
| - </tbody> |
40 |
| -</table> |
41 |
| - |
42 |
| - |
43 |
| - Sample APK configuration content after the modification is shown below. |
44 |
| - |
45 |
| - ``` |
46 |
| - name: "test-backend-jwt" |
47 |
| - basePath: "/backend_jwt" |
48 |
| - version: "1.0.0" |
49 |
| - type: "REST" |
50 |
| - defaultVersion: false |
51 |
| - endpointConfigurations: |
52 |
| - production: |
53 |
| - endpoint: "https://httpbin.org/anything" |
54 |
| - operations: |
55 |
| - - target: "/test" |
56 |
| - verb: "GET" |
57 |
| - secured: true |
58 |
| - scopes: [] |
59 |
| - apiPolicies: |
60 |
| - request: |
61 |
| - - policyName: "BackendJwt" |
62 |
| - parameters: |
63 |
| - encoding: Base64 |
64 |
| - signingAlgorithm: SHA256withRSA |
65 |
| - header: X-JWT-Assertion |
66 |
| - tokenTTL: 3600 |
67 |
| - customClaims: |
68 |
| - - claim: claim1 |
69 |
| - value: value1 |
70 |
| - - claim: claim2 |
71 |
| - value: value2 |
72 |
| -
|
73 |
| - ``` |
|
0 commit comments