|
| 1 | +# Pulling field arguments from JWT claims |
| 2 | + |
| 3 | +This snippet shows how JWT claims can be used for field arguments. |
| 4 | + |
| 5 | +This examples demonstrates a number of StepZen capabilities: |
| 6 | + |
| 7 | +- Authorization using JWT |
| 8 | +- Field access rules |
| 9 | +- Field visibility patterns |
| 10 | +- `@value` directive with access to JWT claims |
| 11 | + - https://www.ibm.com/docs/en/api-connect/ace/saas?topic=directives-directive-value |
| 12 | +- Reshaping |
| 13 | + |
| 14 | +## Restricting access through JWT claims. |
| 15 | + |
| 16 | +This example shows how a JWT claim may be used as a field argument, in this case the JWT subject claim |
| 17 | +is used as a customer's identifier. Thus the customer can only view their own information even though |
| 18 | +the backend database includes all customers. |
| 19 | + |
| 20 | +The field `Query.customer` provides an identifier lookup to all customers. This field is restricted |
| 21 | +from being executed by field visibility and access rules. |
| 22 | + |
| 23 | +Instead a field `Query.me` is exposed with no field arguments that invokes `Query.customer` |
| 24 | +with the customer identifier pulled from the `sub` claim in the request's JWT. |
| 25 | + |
| 26 | +This is implemented using `@sequence` with the first step an intermediate field (`Query._myid`) that is annotated with `@value` using ECMAScript. |
| 27 | +This script has access to field arguments of its annotated field (in this case none) and JWT claims through `$jwt`. |
| 28 | +Thus it returns the `sub` claim which is then automatically mapped as a scalar value to the sole argument of |
| 29 | +the next step in the sequence (`Query.customer(id:)`). |
| 30 | + |
| 31 | +An alternate version of `Query._myid` exists `Query._my_id_jsonata` showing that scripts can be implemented in JSONata. |
| 32 | +The default language is ECMAScript. |
| 33 | + |
| 34 | +Note these concepts could be combined with field access through ABAC rules (see `protection/simpleABACSample`) |
| 35 | +so that `Query.customer` could be exposed, but only customer service reps could call it |
| 36 | +with any customer identifier. |
| 37 | + |
| 38 | +## Try it out! |
| 39 | + |
| 40 | +Deploy the schema from `protection/jwt-claims` relative to the repository's root directory: |
| 41 | + |
| 42 | +``` |
| 43 | +stepzen deploy |
| 44 | +``` |
| 45 | + |
| 46 | +Run the [sample operations](operations.graphql): |
| 47 | + |
| 48 | +JWT with `sub: 5`. |
| 49 | + |
| 50 | +JWT: https://jwt.io/#debugger-io?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1In0.LE_mbGsS2FbxF41r4wOYKhWdBoYhnIk0-6d6U7ibF-A |
| 51 | + |
| 52 | +Secret Key: development-only |
| 53 | + |
| 54 | +``` |
| 55 | +stepzen request -f operations.graphql --operation-name=Customer --header "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1In0.LE_mbGsS2FbxF41r4wOYKhWdBoYhnIk0-6d6U7ibF-A" |
| 56 | +``` |
| 57 | + |
| 58 | +JWT with `sub: 9`. |
| 59 | + |
| 60 | +JWT: https://jwt.io/#debugger-io?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1In0.LE_mbGsS2FbxF41r4wOYKhWdBoYhnIk0-6d6U7ibF-A |
| 61 | + |
| 62 | +Secret Key: development-only |
| 63 | + |
| 64 | +``` |
| 65 | +stepzen request -f operations.graphql --operation-name=Customer --header "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI5In0.liX79YjQ1EIqdVSLqvKoVJxoj63OkBANwZLsZcdLzDM" |
| 66 | +``` |
0 commit comments