#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

# CODE_COPIED_TO_POLARIS

# From Apache Iceberg Version: 1.7.1

---
paths:
  # The /v1/oauth/tokens endpoint is copied from iceberg-rest-catalog-open-api.yaml,
  # ensuring that its deprecation and removal schedule is independent of the Iceberg release cycle.
  /v1/oauth/tokens:

    post:
      tags:
        - OAuth2 API
      summary: Get a token using an OAuth2 flow (DEPRECATED for REMOVAL)
      deprecated: true
      operationId: getToken
      description:
        The `oauth/tokens` endpoint is **DEPRECATED for REMOVAL**. It is _not_ recommended to
        implement this endpoint, unless you are fully aware of the potential security implications.

        All clients are encouraged to explicitly set the configuration property `oauth2-server-uri`
        to the correct OAuth endpoint.

        Deprecated since Iceberg (Java) 1.6.0. The endpoint and related types will be removed from
        this spec in Iceberg (Java) 2.0.

        See [Security improvements in the Iceberg REST specification](https://github.com/apache/iceberg/issues/10537)


        Exchange credentials for a token using the OAuth2 client credentials flow or token exchange.


        This endpoint is used for three purposes -

        1. To exchange client credentials (client ID and secret) for an access token
        This uses the client credentials flow.

        2. To exchange a client token and an identity token for a more specific access token
        This uses the token exchange flow.

        3. To exchange an access token for one with the same claims and a refreshed expiration period
        This uses the token exchange flow.


        For example, a catalog client may be configured with client credentials from the OAuth2
        Authorization flow. This client would exchange its client ID and secret for an access token
        using the client credentials request with this endpoint (1). Subsequent requests would then
        use that access token.


        Some clients may also handle sessions that have additional user context. These clients would
        use the token exchange flow to exchange a user token (the "subject" token) from the session
        for a more specific access token for that user, using the catalog's access token as the
        "actor" token (2). The user ID token is the "subject" token and can be any token type
        allowed by the OAuth2 token exchange flow, including a unsecured JWT token with a sub claim.
        This request should use the catalog's bearer token in the "Authorization" header.


        Clients may also use the token exchange flow to refresh a token that is about to expire by
        sending a token exchange request (3). The request's "subject" token should be the expiring
        token. This request should use the subject token in the "Authorization" header.
      parameters:
        - name: Authorization
          in: header
          schema:
            type: string
          required: false
      requestBody:
        required: true
        content:
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/OAuthTokenRequest'
      responses:
        200:
          $ref: '#/components/responses/OAuthTokenResponse'
        400:
          $ref: '#/components/responses/OAuthErrorResponse'
        401:
          $ref: '#/components/responses/OAuthErrorResponse'
        5XX:
          $ref: '#/components/responses/OAuthErrorResponse'
      security:
        - BearerAuth: []

components:
  schemas:

    TokenType:
      type: string
      enum:
        - urn:ietf:params:oauth:token-type:access_token
        - urn:ietf:params:oauth:token-type:refresh_token
        - urn:ietf:params:oauth:token-type:id_token
        - urn:ietf:params:oauth:token-type:saml1
        - urn:ietf:params:oauth:token-type:saml2
        - urn:ietf:params:oauth:token-type:jwt
      description:
        Token type identifier, from RFC 8693 Section 3


        See https://datatracker.ietf.org/doc/html/rfc8693#section-3

    OAuthClientCredentialsRequest:
      deprecated: true
      description:
        The `oauth/tokens` endpoint and related schemas are **DEPRECATED for REMOVAL** from this
        spec, see description of the endpoint.


        OAuth2 client credentials request


        See https://datatracker.ietf.org/doc/html/rfc6749#section-4.4
      type: object
      required:
        - grant_type
        - client_id
        - client_secret
      properties:
        grant_type:
          type: string
          enum:
            - client_credentials
        scope:
          type: string
        client_id:
          type: string
          description:
            Client ID


            This can be sent in the request body, but OAuth2 recommends sending it in
            a Basic Authorization header.
        client_secret:
          type: string
          description:
            Client secret


            This can be sent in the request body, but OAuth2 recommends sending it in
            a Basic Authorization header.

    OAuthTokenExchangeRequest:
      deprecated: true
      description:
        The `oauth/tokens` endpoint and related schemas are **DEPRECATED for REMOVAL** from this
        spec, see description of the endpoint.


        OAuth2 token exchange request


        See https://datatracker.ietf.org/doc/html/rfc8693
      type: object
      required:
        - grant_type
        - subject_token
        - subject_token_type
      properties:
        grant_type:
          type: string
          enum:
            - urn:ietf:params:oauth:grant-type:token-exchange
        scope:
          type: string
        requested_token_type:
          $ref: '#/components/schemas/TokenType'
        subject_token:
          type: string
          description: Subject token for token exchange request
        subject_token_type:
          $ref: '#/components/schemas/TokenType'
        actor_token:
          type: string
          description: Actor token for token exchange request
        actor_token_type:
          $ref: '#/components/schemas/TokenType'

    OAuthTokenRequest:
      deprecated: true
      description:
        The `oauth/tokens` endpoint and related schemas are **DEPRECATED for REMOVAL** from this
        spec, see description of the endpoint.
      anyOf:
        - $ref: '#/components/schemas/OAuthClientCredentialsRequest'
        - $ref: '#/components/schemas/OAuthTokenExchangeRequest'
    OAuthError:
      deprecated: true
      description:
        The `oauth/tokens` endpoint and related schemas are **DEPRECATED for REMOVAL** from this
        spec, see description of the endpoint.
      type: object
      required:
        - error
      properties:
        error:
          type: string
          enum:
            - invalid_request
            - invalid_client
            - invalid_grant
            - unauthorized_client
            - unsupported_grant_type
            - invalid_scope
        error_description:
          type: string
        error_uri:
          type: string

    OAuthTokenResponse:
      deprecated: true
      description:
        The `oauth/tokens` endpoint and related schemas are **DEPRECATED for REMOVAL** from this
        spec, see description of the endpoint.
      type: object
      required:
        - access_token
        - token_type
      properties:
        access_token:
          type: string
          description:
            The access token, for client credentials or token exchange
        token_type:
          type: string
          enum:
            - bearer
            - mac
            - N_A
          description:
            Access token type for client credentials or token exchange


            See https://datatracker.ietf.org/doc/html/rfc6749#section-7.1
        expires_in:
          type: integer
          description:
            Lifetime of the access token in seconds for client credentials or token exchange
        issued_token_type:
          $ref: '#/components/schemas/TokenType'
        refresh_token:
          type: string
          description: Refresh token for client credentials or token exchange
        scope:
          type: string
          description: Authorization scope for client credentials or token exchange

  responses:

    OAuthTokenResponse:
      description: OAuth2 token response for client credentials or token exchange
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/OAuthTokenResponse'

    OAuthErrorResponse:
      description: OAuth2 error response
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/OAuthError'
