# Copyright 2021 - 2021 PeopleWare n.v.
#
# Licensed 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.

post:
  summary: Get access and refresh token
  description: >-
    The token endpoint is used by the client to obtain an access token by presenting its authorization grant or refresh
    token. The token endpoint is used with every relevant authorization grant.


    This spec only supports the _authorization code grant with PKCE_, _client credentials grant_, and refreshing an
    access token.


    The _implicit grant_ is deprecated. The _resource owner password credentials grant_ **must not** be used in
    production, but is necessary for automated tests.
  operationId: tokenGet
  parameters:
    - $ref: 'request/authorization.yaml'
  requestBody:
    required: true
    description: >-
      - _authorization code with PKCE_:
        The authorization code grant type is used to obtain both access tokens and refresh tokens and is optimized
        for confidential clients. Since this is a redirection-based flow, the client must be capable of interacting
        with the resource owner's user-agent (typically a web browser) and capable of receiving incoming requests
        (via redirection) from the authorization server.

        See [Section 4.1 of RFC 6749](https://tools.ietf.org/html/rfc6749#section-4.1).

        This spec only supports the authorization code grant with PKCE (Proof Key for Code Exchange).

        See [RFC 7636](https://tools.ietf.org/html/rfc7636).

      - _client credentials_:
        The client can request an access token using only its client credentials […] (in the `authorization` header)
        when the client is requesting access to the protected resources under its control, or those of another
        resource owner that have been previously arranged with the authorization server […].

        See [Section 4.4 of RFC 6749](https://tools.ietf.org/html/rfc6749#section-4.4).

      - _refresh token_:
        If the authorization server issued a refresh token to the client, the client makes a refresh request to the
        token endpoint.

        If the orignal refresh token was obtained using _client credentials_, the client credentials need to be
        added in the `authorization header`.

        See [Section 6 of RFC 6749](https://tools.ietf.org/html/rfc6749#section-6).

      - _resource owner password credentials_:
        The resource owner password credentials grant type is only mentioned in this spec to support automated tests.
        This grant **must not** be used in production.

        Since this access token request utilizes the resource owner's password, the authorization server **must**
        protect the endpoint against brute force attacks (e.g., using rate-limitation or generating alerts).

        See [Section 4.3 of RFC 6749](https://tools.ietf.org/html/rfc6749#section-4.3).
    content:
      application/x-www-form-urlencoded:
        schema:
          $ref: 'request/RequestBody.yaml#/schema'
  responses:
    '200':
      $ref: 'response/200.yaml'
    '400':
      $ref: 'response/400.yaml'
    '401':
      $ref: 'response/401.yaml'
    '429':
      $ref: 'response/429.yaml'
