<?xml version="1.0" encoding="utf-8"?>

<?xml-model href="rfc7991bis.rnc"?>

<!DOCTYPE rfc [
  <!ENTITY nbsp "&#160;">
<!ENTITY zwsp "&#8203;">
<!ENTITY nbhy "&#8209;">
<!ENTITY wj "&#8288;">
]>

<rfc xmlns:xi="http://www.w3.org/2001/XInclude" category="info" docName="draft-ietf-scim-cursor-pagination-00" ipr="trust200902" obsoletes="" updates="7643,7644" submissionType="IETF" xml:lang="en" version="3">

  <front>

    <title abbrev="SCIM Cursor Pagination">Cursor-based Pagination of SCIM Resources</title>

    <author fullname="Matt Peterson" initials="M" surname="Peterson">
      <organization>One Identity</organization>
      <address>
        <email>matt.peterson@oneidentity.com</email>
      </address>
    </author>

    <author fullname="Danny Zollner" initials="D" surname="Zollner">
      <organization>Microsoft</organization>
      <address>
        <email>danny.zollner@microsoft.com</email>
      </address>
    </author>

    <date year="2023" month="2" day="16" />

    <!-- Meta-data Declarations -->

    <area>General</area>

    <workgroup>SCIM</workgroup>

    <keyword>SCIM</keyword>
    <keyword>pagination</keyword>
    <keyword>cursor</keyword>

    <abstract>
      <t> This document defines additional SCIM query parameters and result attributes to allow use
        of cursor-based pagination in SCIM implementations that are implemented with existing
        code bases, databases, or APIs where cursor-based pagination is already well-established.
      </t>
    </abstract>
  </front>

  <middle>
    <section title="Introduction">
      <t> The two common patterns for result pagination in HTTP-based protocols are index-based
        pagination and cursor-based pagination. Rather than attempt to compare and contrast the
        advantages and disadvantages of competing pagination patterns, this document simply
        recognizes that SCIM service providers are commonly implemented as an interoperability
        layer on top of already existing application codebases, databases, and/or APIs that already
        have a well-established pagination pattern. </t>

      <t> Translating from an underlying cursor-based pagination pattern to the index-based
        pagination defined in <xref target="RFC7644" sectionFormat="of" section="3.4.2.4" />
        ultimately requires the SCIM service provider to fully iterate the underlying cursor, store
        the results, and then serve indexed pages from the stored results. This task of "pagination
        translation" dramatically increases complexity and memory requirements for implementing a
        SCIM Service Provider, and may be an impediment to SCIM adoption for some applications and
        identity systems.
      </t>

      <t> This document defines a simple addition to the SCIM protocol that allows SCIM service
        providers to reuse underlying cursors without expensive translation. Support for
        cursor-based pagination in SCIM encourages broader cross-application identity management
        interoperability by encouraging SCIM service provider implementations for applications and
        identity systems where cursor-based pagination is already well-established.</t>

      <section title="Notational Conventions">
        <t>
          The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD
          NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as
          described in
          <xref target="RFC2119" />
          .
        </t>
      </section>
    </section>

    <section title="Query Parameters and Response Attributes" anchor="query_params">
      <t>The following table describes the URL pagination parameters requests for using cursor-based
        pagination: </t>

      <table>
        <thead>
          <tr>
            <th>Parameter</th>
            <th>Description</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td align="left">cursor</td>
            <td align="left"> The string value of the nextCursor attribute from a previous result
              page. The cursor value MUST be empty or omitted for the first request of a
              cursor-paginated query. </td>
          </tr>
          <tr>
            <td align="left">count</td>
            <td align="left"> A positive integer. Specifies the desired maximum number of query
              results per page, e.g., count=10. When specified, the service provider MUST NOT return
              more results than specified, although it MAY return fewer results. If count is not
              specified in the query, the maximum number of results is set by the service provider.
            </td>
          </tr>
        </tbody>
      </table>

      <t> The following table describes cursor-based pagination attributes returned in a paged query
        response: </t>

      <table>
        <thead>
          <tr>
            <th>Element</th>
            <th>Description</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td align="left">nextCursor</td>
            <td align="left"> A cursor value string that MAY be used in a subsequent request to
              obtain the next page of results. Service providers supporting cursor-based pagination
              MUST include nextCursor in all paged query responses except when returning the last
              page. nextCursor is omitted from a response only to indicate that there are no more
              result pages. </td>
          </tr>
          <tr>
            <td align="left">previousCursor</td>
            <td align="left"> A cursor value string that MAY be used in a subsequent request to
              obtain the previous page of results. Use of previousCursor is OPTIONAL. Service
              Providers that are unable to support a previousCursor MAY omit previousCursor when
              sending paged query responses. </td>
          </tr>
        </tbody>
      </table>

      <t>The SCIM client MUST consider cursor to be opaque and make no assumptions about 
        cursor values. When the client wants to retrieve another result page for a query, it 
        should query the same Service Provider endpoint with all query parameters and values
        being identical to the initial query with the exception of the cursor value which 
        should be set to a nextCursor (or previousCursor) value that was returned by
        Service Provider in a previous response.</t>

      <t>For example, to retrieve the first 10 Users with username starting with "J", 
        use an empty cursor and set the count to 10: </t>

      <sourcecode>
        <![CDATA[
  GET /Users?filter=username%20sw%20J&cursor&count=10
  Host: example.com
  Accept: application/scim+json
  Authorization: Bearer U8YJcYYRMjbGeepD
]]>
      </sourcecode>

      <t> The response to the query above returns metadata regarding pagination similar to the
        following example (actual resources removed for brevity): </t>

      <sourcecode>
        <![CDATA[
  {
    "totalResults":100,
    "itemsPerPage":10,
    "nextCursor":"VZUTiyhEQJ94IR",
    "schemas":["urn:ietf:params:scim:api:messages:2.0:ListResponse"],
    "Rsesources":[{
       ...
     }]
  }
]]>
      </sourcecode>

      <t> Given the example above, to request the next page or results, use the same query 
        parameters and values except set the cursor to the value of nextCursor ("VZUTiyhEQJ94IR"): </t>

      <sourcecode>
        <![CDATA[
  GET /Users?filter=username%20sw%20J&cursor=VZUTiyhEQJ94IR&count=10
  Host: example.com
  Accept: application/scim+json
  Authorization: Bearer U8YJcYYRMjbGeepD

  {
    "totalResults":100,
    "itemsPerPage":10,
    "previousCursor: "ze7L30kMiiLX6x"
    "nextCursor":"YkU3OF86Pz0rGv",
    "schemas":["urn:ietf:params:scim:api:messages:2.0:ListResponse"],
    "Rsesources":[{
       ...
     }]
  }

]]>
      </sourcecode>

      <t> In the example above, the response includes the OPTIONAL previousCursor indicating
      that the Service Provider supports forward and reverse traversal of result pages.</t>

      <t> As described in
        <xref target="RFC7644" sectionFormat="of" section="3.4.1" /> Service Providers
        SHOULD return an accurate value for totalResults which is the total number of resources for all
        pages.  Service Providers implementing cursor pagination that are unable to estimate totalResults
        MAY return a response with totalResults set to zero (0). </t>

      <section title="Pagination errors">
        <t> If a Service Provider encounters an invalid pagination query parameters (invalid cursor
          value, count value, etc), or other error condition, the Service Provider SHOULD return 
          the appropriate HTTP response status code and detailed JSON error response as defined in
          <xref target="RFC7644" sectionFormat="of" section="3.12" />. Most pagination error conditions
          would generate HTTP response with status code 400. Since many pagination error conditions
          are not user recoverable, error messages SHOULD focus on communicating error details to the
          SCIM client developer.</t>

        <t> For example, cursor pagination implementations SHOULD anticipate the 
        following error conditions: </t>

        <table>
          <thead>
            <tr>
              <th>Status</th>
              <th>Message</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td align="left">400 (Bad Request)</td>
              <td align="left"> Cursor value is invalid. Cursor value should be empty to request the first page and
                set to the nextCursor or previousCursor value for subsequent queries. </td>
            </tr>
            <tr>
              <td align="left">400 (Bad Request)</td>
              <td align="left"> Cursor has expired.  Do not wait longer than cursorTimeout (600 sec) to request additional pages. </td>
            </tr>
            <tr>
              <td align="left">400 (Bad Request)</td>
              <td align="left">Count value is invalid.  Count value must be between 1 - and maximumPageSize (500)</td>
            </tr>
          </tbody>
        </table>
      </section>

      <section title="Sorting">
        <t>
          If sorting is implemented as described
          <xref target="RFC7644" sectionFormat="of" section="3.4.2.3" />
          ,
          then cursor-paged results SHOULD be sorted.
        </t>
      </section>

      <section title="Cursors as the Only Pagination Method">

        <t> A SCIM Service Provider MAY require cursor-based pagination to retrieve all results
          for a query by including a "nextCursor" value in the response even when the 
          query does not include the "cursor" parameter. </t>

        <t> For example: </t>

        <sourcecode>
          <![CDATA[
   GET /Users
   Host: example.com
   Accept: application/scim+json
]]>
        </sourcecode>

        <t> The SCIM Service Provider may respond to the above query with a page containing
          defaultPageSize results and a "nextCursor" value as shown in the below example
          (Resources omitted for brevity): </t>

        <sourcecode>
          <![CDATA[
  {
    "totalResults":5000,
    "itemsPerPage":100,
    "nextCursor":"HPq72Pax3JUaNa",
    "schemas":["urn:ietf:params:scim:api:messages:2.0:ListResponse"],
    "Resources":[{
       ...
     }]
  }
]]>
        </sourcecode>

      </section>

    </section>

    <section title="Querying Resources Using HTTP POST">
      <t> <xref target="RFC7644" sectionFormat="of" section="3.4.2.4" /> defines how clients MAY
        execute the HTTP POST verb combined with the "/.search" path extension to issue execute
        queries without passing parameters on the URL. When using "/.search", the client would pass
        the parameters defined in <xref target="query_params" />
      </t>

      <sourcecode>
        <![CDATA[
  POST /User.search
  Host: example.com
  Accept: application/scim+json
  Authorization: Bearer U8YJcYYRMjbGeepD
  {
    "schemas": [
      "urn:ietf:params:scim:api:messages:2.0:SearchRequest"],
    "attributes": ["displayName", "userName"],
    "filter":
       "displayName sw \"smith\"",
    "cursor": "",
    "count": 10
  }
]]>
      </sourcecode>

      <t> Which would return a result containing a "nextCursor" value which may be used by the client
        in a subsequent call to return the next page of resources </t>

      <sourcecode>
        <![CDATA[
  {
    "totalResults":100,
    "itemsPerPage":10,
    "nextCursor":"VZUTiyhEQJ94IR",
    "schemas":["urn:ietf:params:scim:api:messages:2.0:ListResponse"],
    "Resources":[{
       ...
     }]
  }
]]>
      </sourcecode>

    </section>

    <section title="Service Provider Configuration">

      <t> The /ServiceProviderConfig resource defined in
        <xref target="RFC7644" sectionFormat="of" section="4" />
        facilitates discovery of SCIM service provider features. A SCIM Service
        provider implementing cursor-based pagination SHOULD include the following additional
        attribute in JSON document returned by the /ServiceProviderConfig endpoint: </t>

      <dl>
        <dt>pagination</dt>
        <dd> A complex type that indicates pagination configuration options. OPTIONAL. </dd>
        <dt />
        <dd>
          <dl>
            <dt>cursor</dt>
            <dd> A Boolean value specifying support of cursor-based paginations. REQUIRED. </dd>
          </dl>
          <dl>
            <dt>index</dt>
            <dd> A Boolean value specifying support of index-based pagination. REQUIRED. </dd>
          </dl>
          <dl>
            <dt>defaultPageSize</dt>
            <dd> Non-negative integer value specifying the default number of results 
              returned in a page when a count is not specified in the query. OPTIONAL. </dd>
          </dl>
          <dl>
            <dt>maxPageSize</dt>
            <dd> Non-negative integer specifying the maximum number of results returned
              in a page regardless of what is specified for the count in a query. OPTIONAL. </dd>
          </dl>
          <dl>
            <dt>cursorTimeout</dt>
            <dd> Non-negative integer specifying the maximum number seconds that a cursor
              is valid between page requests.  Clients waiting too long between cursor pagination 
              requests may receive an invalid cursor error response. OPTIONAL. </dd>
          </dl>
        </dd>
      </dl>

      <t> Before using cursor-based pagination, a SCIM client MAY fetch the Service Provider
        Configuration document from the SCIM service provider and verify that cursor-based
        pagination is supported. </t>

      <t> For example: </t>

      <sourcecode>
        <![CDATA[
   GET /ServiceProviderConfig
   Host: example.com
   Accept: application/scim+json
]]>
      </sourcecode>

      <t> A service provider supporting both cursor-based pagination and index-based pagination
        would return a document similar to the following (full ServiceProviderConfig schema
        defined in <xref target="RFC7643" sectionFormat="of" section="5" /> has been omitted for
        brevity): </t>

      <sourcecode>
        <![CDATA[
  {
    "schemas": [
      "urn:ietf:params:scim:schemas:core:2.0:ServiceProviderConfig"],
      
      ... 

    "pagination": {
       "cursor": true,
       "index": true
    },

    ...
      
   }
]]>
      </sourcecode>

    </section>

    <section title="Security Considerations">
      <t> Service Provider implementors SHOULD ensure that misuse of pagination
        by a SCIM client does not deplete Service Provider resources or prevent valid
        requests from other clients being handled.  Defenses for a SCIM Service Provider 
        are similar those used to protect other Web API services -- including the use of
        a "Web API gateway" layer, to provide authentication, rate limiting, IP allow/block
        lists, logging and monitoring, response caching, etc.  </t>

      <t> For example, an obvious protection against abuse is for the Service Provider to 
        require client authentication in order to retrieve large result sets and enforce 
        an overriding totalResults limit for non-authenticated clients. Another example,
        would be for a Service Provider that implements cursor pagination to restrict number
        of cursors that can be allocated by a client or enforce cursor lifetime.  
      </t>
    </section>

  </middle>

  <back>

    <references title="Normative References">
      <xi:include href="https://www.rfc-editor.org/refs/bibxml/reference.RFC.2119.xml" />
      <xi:include href="https://www.rfc-editor.org/refs/bibxml/reference.RFC.7643.xml" />
      <xi:include href="https://www.rfc-editor.org/refs/bibxml/reference.RFC.7644.xml" />
    </references>

  </back>

</rfc>