Skip to main content

OAuth2 Scopes and Presentation Definition Mapping

Scope design

When designing a system that uses OAuth2, you have to decide how scopes map to resources that the client will attempt to access. "Resource access" is typically a specific REST-style HTTP operation on a specific URL, e.g. POST /products/staplers/1. Things to consider when designing scopes are discussed in this section.

Broad v.s. narrow scopes

Broad scopes are generally high level e.g., a scope that gives access to a certain use case or larger group of resources. Narrow scopes are often low-level e.g., a scope that gives read access to a specific resource, limited set of resources or operations. Examples scopes for an employee that's authorized to buy supplies for their employer:

  • Very broad: buyer
  • Broad: buyer office (office supplies only)
  • Broad: buyer lt-1000 (orders less than 1000 euros)
  • Narrower: buyer office:staplers (staplers only)
  • Very narrow: buyer office:staplers:red lt-10 (red staplers only, less than 10 euros)

How scopes are mapped to operations on resources influences:

  • How often clients need to request a new access token, if the previous token does not give access to a required resource.
  • When access to a specific resource is authorized: when the access token is issued, or when it's used.

Broad, high-level Scopes

High-level, broad scopes typically give access to an entire use case, service, or group of resources. Checks that are executed before an access token is issued are limited to the Verifiable Credentials the client can present.

  • Identification and authentication (user/client identity)
  • General user access to the functionality (e.g. is admin, can buy supplies, etc.)

A real-life example of a broad scope is the Nuts eOverdracht use case, which specifies the following scopes:

  • eOverdracht-sender which gives access to the receiver's services required by a care organization that wants to transfer a patient to another organization.
  • eOverdracht-receiver which gives access to the sender's services to the transfer receiver.

However, when a resource is accessed, the system needs to verify that the scope gives access to the specific resource operation.

This type of scope is supported by the Nuts node.

Narrow, low-level Scopes

Narrow, low-level scopes typically give access to specific operations on specific resources, e.g., reading a specific patient's medical summary.

This type of scope is not supported by the Nuts node, because:

  • narrow scopes often contain resource identifiers, which requires wildcards/regexes in policy mapping (more on that below), which is currently not supported by the Nuts node.
  • this leads to more access tokens, since each access token has a more limited use. If user authentication involves manual input (e.g., presenting a credential using a mobile wallet), user experience will deteriorate.

Another consideration is that using low-level scopes, moves most authorization decisions to the access token issuance. This is viable and supported by the Nuts node, but complicated: it requires the vendor to implement a REST API that understands Presentation Definitions.

Policies: Mapping Scope to Authentication Subject

Due to the ongoing development of personal authentication methods and associated protocols, the Nuts node currently only supports the OAuth2 vp_token grant for production. User authentication via OpenID4VP is experimental and not intended for production. It's still required to pass user claims within the token request if a data exchange contains PII (Personally Identifiable Information) and/or medical data (e.g., Social Security Number or EHR records)

Mapping document

This section contains an example of a presentation definition mapping document as it could be specified by a use case. The Presentation Definition is described more in detail in AuthN using Verifiable Credentials.

{
  "zorgtoepassing": {
    "organization": {
      "format": {
        "ldp_vc": {
          "proof_type": [
            "JsonWebSignature2020"
          ]
        },
        "ldp_vp": {
          "proof_type": [
            "JsonWebSignature2020"
          ]
        },
        "jwt_vc": {
          "alg": [
            "ES256"
          ]
        },
        "jwt_vp": {
          "alg": [
            "ES256"
          ]
        }
      },
      "id": "pd_any_care_organization",
      "name": "Care organization",
      "purpose": "Finding a care organization for authorizing access to medical metadata",
      "input_descriptors": [
        {
          "id": "id_nuts_care_organization_cred",
          "constraints": {
            "fields": [
              {
                "path": [
                  "$.type"
                ],
                "filter": {
                  "type": "string",
                  "const": "NutsOrganizationCredential"
                }
              },
              {
                "id": "organization_name",
                "path": [
                  "$.credentialSubject.organization.name",
                  "$.credentialSubject[0].organization.name"
                ],
                "filter": {
                  "type": "string"
                }
              },
              {
                "id": "organization_city",
                "path": [
                  "$.credentialSubject.organization.city",
                  "$.credentialSubject[0].organization.city"
                ],
                "filter": {
                  "type": "string"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

Policy file structure

A policy file is a JSON object. Each key is an OAuth2 scope; its value says which credentials the requester must present for that scope:

    organization: a presentation definition for the credentials of the care organization (the requester's organization wallet). user: a presentation definition for the credentials of the user. service_provider (in development): a presentation definition for the credentials of the service provider that requests access on behalf of the care organization. See "Service provider credentials" below.

    A scope needs at least one of these.

    The Nuts node loads the policy files from a directory; see Access Token Policy in the Nuts node documentation.

    Claims in the token introspection response

    When the data holder introspects an access token, the response contains the values from the presented credentials. Each constraint field with an id becomes a claim, named by that id.

    Example: this field in the presentation definition

    {
      "id": "fullName",
      "path": ["$.credentialSubject.fullName"],
      "filter": {
        "type": "string"
      }
    }
    

    gives this introspection response (some fields left out):

    {
      "iss": "did:web:example.com",
      "active": true,
      "scope": "example_scope",
      "fullName": "John Doe"
    }
    

    When choosing field IDs:

      Give every field used for logging or authorization decisions its own ID. Use a registered claim name where one exists, see IANA JWT claims. Don't use the names of claims the introspection response already has: iss, sub, exp, iat, active, client_id, scope.

      Returning part of a value

      To return only part of a text value, use a pattern filter with one capture group. The claim then holds what the capture group matched.

      Example: the credential has "role": "Admin level 4". This field returns "admin_level": "4":

      {
        "id": "admin_level",
        "path": ["$.credentialSubject.role"],
        "filter": {
          "type": "string",
          "pattern": "Admin level ([0-9])"
        }
      }
      

      Only one capture group is allowed; more give an error.

      Service provider credentials (in development)

      Status: in development. This feature is under active development and may change without notice. It must be enabled in the Nuts node, see Two-VP flow.

      Normally the requester presents one Verifiable Presentation (VP), from the care organization's wallet. A use case can also require a second VP from the service provider (e.g. the vendor) that requests access on behalf of the care organization. The token request then uses the RFC 7523 jwt-bearer grant:

        VP1, from the care organization's wallet, matches the organization presentation definition. It is sent as assertion. VP2, from the service provider's wallet, matches the service_provider presentation definition. It is sent as client_assertion.

        Binding the two presentations

        A constraint field with the same id in both presentation definitions links them: the value found in VP1 must also be the value in VP2.

        Example: VP2 must contain a ServiceProviderDelegationCredential issued by the same DID that issued the care organization's HealthcareProviderCredential in VP1:

        {
          "example_delegated_scope": {
            "organization": {
              "id": "org_pd",
              "input_descriptors": [{
                "id": "hcp_credential",
                "constraints": {
                  "fields": [
                    { "path": ["$.type"], "filter": { "type": "string", "const": "HealthcareProviderCredential" } },
                    { "id": "delegating_hcp", "path": ["$.issuer"] }
                  ]
                }
              }]
            },
            "service_provider": {
              "id": "sp_pd",
              "input_descriptors": [{
                "id": "delegation_credential",
                "constraints": {
                  "fields": [
                    { "path": ["$.type"], "filter": { "type": "string", "const": "ServiceProviderDelegationCredential" } },
                    { "id": "delegating_hcp", "path": ["$.issuer"] }
                  ]
                }
              }]
            }
          }
        }
        

        If the service provider's wallet has no such credential, the token request fails with HTTP 412 (Precondition Failed).

        Choosing between credentials: credential_selection

        When several credentials in a wallet match one input descriptor, the access token request can say which one to use, with credential_selection: a map from a field id to the value that field must have, e.g. {"patient_id": "123"}.

        In the two-VP flow, the node also adds the values it found in VP1 to this map before it builds VP2. Values the caller passed always win.