Filter user data with PEX

Learn more about PEX query and how to use it for requesting data from Affinidi Vault.

Requesting data from Affinidi Vault uses the Presentation Exchange (PEX) protocol to define the data requirements of the website for the user to share in the form of a Verifiable Presentation (VP). For the application to trust the data that the user shares you need to define the different set of constraints through input_descriptors of the presentation definition that must be followed and satisfied by the user.

See the different usage of Presentation Definition below to help you understand and define the correct PEX query to request data from Affinidi Vault.

Requesting emails from a specific domain

In this scenario, you aim to accept emails or permit logins only from that particular domain.


{
  "id": "vp_token_with_email_vc",
  "input_descriptors": [
    {
      "id": "email_vc_data",
      "name": "Email VC data",
      "purpose": "Check if data contains necessary fields",
      "constraints": {
        "fields": [
          {
            "path": [
              "$.type"
            ],
            "purpose": "Check if VC type is correct",
            "filter": {
              "type": "array",
              "contains": {
                "type": "string",
                "pattern": "^Email$"
              }
            }
          },
          {
            "path": [
              "$.credentialSubject.email"
            ],
            "purpose": "Email address from specific domain",
            "filter": {
              "type": "string",
              "pattern": "^[^@\\s]+@affinidi\\.com$"
            }
          }
        ]
      }
    }
  ]
}

In the above example, we check that the credential is an Email VC and that its email address belongs to a specific domain, affinidi.com. The pattern is anchored with ^ and $, and the . is escaped, so an address such as name@affinidi.com.example.org doesn’t match. This PEX query enables Affinidi Login flow to restrict logins to particular domains like corporate emails.

Request data from specific VC type

In this scenario, you aim to request data generated from a specific VC type.


{
    "id": "vp_token_with_specific_type",
    "input_descriptors": [
      {
        "id": "email_vc",
        "name": "Email VC",
        "purpose": "we can get vc of email schema",
        "group": ["A"],
        "constraints": {
          "fields": [
            {
              "path": [
                "$.type"
              ],
              "purpose": "Check if VC type is correct",
              "filter": {
                "type": "array",
                "contains": {
                  "type": "string",
                  "pattern": "^Email$"
                }
              }
            }
          ]
        }
      }
    ]
}

In the above example, we check whether the VC is issued using a specific type. Specifically, the schema should be of type Email. This PEX query is helpful in scenarios where your application expects a clear data structure to prevent parsing failures caused by unrecognised data formats. The default Presentation Definition used by Affinidi Login makes the same check without ^ and $, so it also matches types that contain Email.

Request data from a specific issuer

In this scenario, you aim to ensure that the data retrieved from the user’s Affinidi Vault originates from a particular issuer. You can meet this requirement by implementing checks based on the specific DID of the issuer within the PEX query.


{
  "id": "vp_token_with_specific_issuer",
  "input_descriptors": [
    {
      "id": "vc_issuer",
      "name": "VC Issuer",
      "purpose": "Check if VC Issuer is Trusted",
      "constraints": {
        "fields": [
          {
            "path": [
              "$.issuer"
            ],
            "purpose": "issuer",
            "filter": {
              "type": "string",
              "pattern": "^did:key:zQ3shtMGCU89kb2RMknNZcYGUcHW8P6Cq3CoQyvoDs7Qqh33N$"
            }
          }
        ]
      }
    }
  ]
}

In the above example, we are checking the issuer field in the VC for a specific DID value; in this case, we are checking if Affinidi issues the VC.

Request user profile from a specific country

In this scenario, you aim to accept user profiles from specific demographics (e.g. Country).


{
  "id": "vp_token_with_profile_vc",
  "input_descriptors": [
    {
      "id": "user_profile_vc",
      "name": "User profile",
      "purpose": "Check if data contains necessary fields and data",
      "constraints": {
        "fields": [
          {
            "path": [
              "$.type"
            ],
            "purpose": "Check if VC type is correct",
            "filter": {
              "type": "array",
              "contains": {
                "type": "string",
                "pattern": "^UserProfile$"
              }
            }
          },
          {
            "path": [
              "$.credentialSubject.address.country"
            ],
            "purpose": "User profile from specific country",
            "filter": {
              "type": "string",
              "pattern": "^Singapore$"
            }
          }
        ]
      }
    }
  ]
}

In the example above, we request the UserProfile VC, which Affinidi Vault generates from the user’s profile, and check that the address object’s country field is Singapore. Countries are stored as country names. This PEX query is useful when your application restricts access to specific resources based on the user’s region.