curved-florist-98287
03/12/2024, 9:29 PMmutators:
noop:
enabled: true
header:
enabled: true
config:
headers:
X-User-IAM-ID: "{{ print .Subject }}"
X-User-IAM-Public-Metadata: "{{ print .Extra.identity.metadata_public }}"
but this example doesn't handle the case if the user has no "metadata_public" and it's value is "nil", how can I handle this case?chilly-king-10285
03/12/2024, 9:29 PMchilly-king-10285
03/12/2024, 9:29 PMnil
value for metadata_public
in the configuration of the header
mutator. The configuration provided in the extracts doesn't include conditional logic or error handling within the template itself. However, you could potentially handle this at the application level where the headers are being used. If the X-User-IAM-Public-Metadata
header is nil
or not present, you could have your application logic handle this case appropriately. For more advanced handling, you might want to consider using the hydrator
mutator mentioned in the extracts. This mutator allows for fetching additional data from external APIs, which can be then used by other mutators. You could potentially use this to fetch and handle user metadata in a more sophisticated way. For more detailed information, you might want to refer to the Ory Oathkeeper documentation: - [Mutators](https://www.ory.sh/docs/oathkeeper/pipeline/mutator) - [Hydrator Mutator](https://www.ory.sh/docs/oathkeeper/pipeline/mutator#hydrator) - [Header Mutator](https://www.ory.sh/docs/oathkeeper/pipeline/mutator#header)curved-florist-98287
03/12/2024, 9:36 PMchilly-king-10285
03/12/2024, 9:36 PMchilly-king-10285
03/12/2024, 9:36 PMhydrator
mutator works by making an upstream HTTP call to an API specified in the Per-Rule Configuration. The request is a POST request and it contains a JSON representation of the AuthenticationSession struct in the body. The response from the API should be a similar JSON object, but with extra
or header
fields modified. Here is a general example of how the hydrator
mutator could be used: yaml mutators: hydrator: enabled: true config: api: url: <http://my-api/my-endpoint>
In this example, the hydrator
mutator would make a POST request to <http://my-api/my-endpoint>
with the AuthenticationSession as the request body. The API at <http://my-api/my-endpoint>
would need to be set up to handle this request, fetch the necessary information from Kratos, and return a response with the extra
or header
fields modified as necessary. For more detailed information and examples, you would need to refer to the [Ory Oathkeeper documentation](https://www.ory.sh/docs/oathkeeper/pipeline/mutator#hydrator).curved-florist-98287
03/12/2024, 9:53 PMmutators:
noop:
enabled: true
header:
enabled: true
config:
headers:
X-User-IAM-ID: "{{ print .Subject }}"
X-User-IAM-Branch-Id: "{{ if .Extra.identity.metadata_public }} {{ if .Extra.identity.metadata_public.branch_id }} {{ print .Extra.identity.metadata_public.branch_id }} {{ end }} {{ end }}"
chilly-king-10285
03/12/2024, 9:53 PMchilly-king-10285
03/12/2024, 9:53 PMmetadata_public
and branch_id
exist before printing the value. This is a great way to handle potential nil
values and avoid errors. Here's the code you shared for reference: yaml mutators: noop: enabled: true header: enabled: true config: headers: X-User-IAM-ID: "{{ print .Subject }}" X-User-IAM-Branch-Id: "{{ if .Extra.identity.metadata_public }} {{ if .Extra.identity.metadata_public.branch_id }} {{ print .Extra.identity.metadata_public.branch_id }} {{ end }} {{ end }}"
In this configuration, the X-User-IAM-Branch-Id
header will only be set if .Extra.identity.metadata_public
and .Extra.identity.metadata_public.branch_id
exist, preventing potential issues with nil
values. This is a great example of how flexible the Ory Oathkeeper mutators can be when combined with the power of Go's text/template package.