# Copyright (C) 2026 Alberto Irurueta Carro (alberto@irurueta.com)
#
# 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.

# Contract of the server-rendered HTML pages of the authorization server (version 1). This file is the source of
# truth: the Spring interfaces implemented by the page controllers of auth-server/api/web are generated from it by
# openapi-generator, and OpenApiDriftIT fails when the pages the running application serves (the springdoc group
# "pages", /v3/api-docs/pages) differ from it. Every page answers text/html rendered from a Thymeleaf template.
# The endpoints Spring Security and Spring Authorization Server answer themselves are not part of it: the login form
# post (POST /login), the second-factor post (POST /login/second-factor/totp), the sign-out (POST /logout), the
# consent decision (POST /oauth2/authorize), the WebAuthn endpoints of the passkeys (/webauthn/**, POST /login/webauthn),
# the one-time token endpoints (POST /ott/generate, POST /login/ott, POST /login/second-factor/ott) and every other
# OAuth 2.1 / OpenID Connect protocol endpoint. Errors are rendered by Spring Boot's own error
# controller (/error).
openapi: 3.1.0
info:
  title: Irurueta Auth Server Pages
  description: >-
    Server-rendered HTML pages of the example OAuth 2.1 / OpenID Connect authorization server: sign-in, second
    factor, recovery codes, consent, sign-out confirmation, the passkeys of the account and the one-time token
    pages (enter a code, confirm a magic link, the development SMS inbox) the phone of the account, the identities of
    external providers linked to the account and the device verification. They are meant for a browser, never for a
    script.
  version: 1.0.0
  license:
    name: Apache License 2.0
    url: https://www.apache.org/licenses/LICENSE-2.0.txt
servers:
  - url: http://localhost:9000
    description: Local development instance (the dev profile).
tags:
  - name: Login page
    description: The sign-in page, which hosts the sign-in methods
  - name: Second factor page
    description: The page where a user who has signed in with a password presents a second factor
  - name: Recovery codes page
    description: >-
      The page that shows, once, the recovery codes generated when an authenticator app is enrolled during the sign-in
  - name: Consent page
    description: The page where the user approves the scopes a client asks for
  - name: Logout page
    description: The page that asks the user to confirm signing out
  - name: Passkeys page
    description: The account page where a signed-in user registers passkeys and sees the ones already registered
  - name: Phone page
    description: >-
      The account page where a signed-in user enrols, verifies and removes the phone number that receives one-time
      codes by SMS, with the session-protected form posts it uses
  - name: Identities page
    description: >-
      The account page where a signed-in user sees the identities of external providers linked to the account and
      links another one
  - name: Device verification page
    description: >-
      The page where a user enters the code a device shows to approve it (OAuth 2.0 Device Authorization Grant,
      RFC 8628)
  - name: One-time token page
    description: >-
      The pages of the one-time token sign-in: the one where the code received by e-mail or SMS is entered and the
      one a magic link opens
  - name: Dev SMS inbox page
    description: >-
      Development only (the dev profile): the SMS messages the logging sender recorded instead of sending them
security:
  - session: [ ]
paths:
  # tag::login-page-api[]
  /login:
    get:
      tags: [ Login page ]
      operationId: loginPage
      summary: Renders the sign-in page
      description: >-
        Renders the sign-in page with the form of every sign-in method (each LoginMethodFragment bean contributes
        one). The forms post to the security chain (POST /login), never to this operation. Public: it is where the
        browser is sent when it has no session.
      security: [ ]
      parameters:
        - name: error
          in: query
          required: false
          description: >-
            Present (its value is ignored, usually empty) when the previous sign-in failed; the page then shows the
            message of that failure, which never reveals whether the account exists.
          schema:
            type: string
        - name: logout
          in: query
          required: false
          description: >-
            Present (its value is ignored, usually empty) when the user has just signed out; the page then says so.
          schema:
            type: string
      responses:
        '200':
          description: The sign-in page.
          content:
            text/html:
              schema:
                type: string
  # end::login-page-api[]
  # tag::mfa-second-factor-page-api[]
  /login/second-factor:
    get:
      tags: [ Second factor page ]
      operationId: secondFactorPage
      summary: Renders the second-factor page
      description: >-
        Renders the page a user who has signed in with a password is sent to when the request needs a second factor
        (each SecondFactorFragment bean contributes the form of one second factor, with the data it needs for the
        signed-in user). A user without a confirmed authenticator app gets its enrolment form instead: a new pending
        secret as a QR code and an otpauth URI, and the field of the first code. A user with one gets the forms of its
        code and of a recovery code, with a warning when three or fewer recovery codes are left. The forms post to the
        security chain (for the TOTP code, a recovery code or the enrolment code, POST /login/second-factor/totp),
        never to this operation; once the factor is verified the browser resumes the request that was interrupted
        (after an enrolment, it first goes to the recovery codes page). A request without a signed-in session is
        redirected to the sign-in page.
      parameters:
        - name: error
          in: query
          required: false
          description: >-
            Present (its value is ignored, usually empty) when the previous attempt failed; the page then shows the
            message of that failure, which never reveals whether the code was close.
          schema:
            type: string
      responses:
        '200':
          description: The second-factor page.
          content:
            text/html:
              schema:
                type: string
  # end::mfa-second-factor-page-api[]
  # tag::totp-recovery-codes-page-api[]
  /login/second-factor/recovery-codes:
    get:
      tags: [ Recovery codes page ]
      operationId: recoveryCodesPage
      summary: Renders the recovery codes of a new authenticator app, once
      description: >-
        Renders the recovery codes generated when the signed-in user enrolled an authenticator app on the
        second-factor page, which redirects here after the enrolment code is accepted. The codes are kept in the
        session only until this page is rendered, so a second request shows none. Its Continue link resumes the request
        the sign-in interrupted (typically /oauth2/authorize), or goes to the root when there is none. A request
        without a signed-in session is redirected to the sign-in page.
      responses:
        '200':
          description: The recovery codes page.
          content:
            text/html:
              schema:
                type: string
  # end::totp-recovery-codes-page-api[]
  # tag::consent-page-api[]
  /oauth2/consent:
    get:
      tags: [ Consent page ]
      operationId: consentPage
      summary: Renders the consent page
      description: >-
        Renders the page the authorization endpoint redirects to when a client needs the user's approval: the client,
        the requested scopes (as checkboxes, checked by default) and the scopes granted earlier. The decision is
        posted to the authorization endpoint (POST /oauth2/authorize), or to the device verification endpoint for a device,
        never to this operation.
      parameters:
        - name: client_id
          in: query
          required: true
          description: Identifier of the registered client that asks for access.
          schema:
            type: string
            minLength: 1
        - name: scope
          in: query
          required: true
          description: Requested scopes, space delimited as in the authorization request.
          schema:
            type: string
        - name: state
          in: query
          required: true
          description: State the authorization endpoint generated for the consent step, posted back with the decision.
          schema:
            type: string
            minLength: 1
        - name: user_code
          in: query
          required: false
          description: >-
            Present when the consent is asked for a device (OAuth 2.0 Device Authorization Grant): the user code of the
            request. The decision is then posted to the device verification endpoint (POST /oauth2/device_verification)
            instead, with this code.
          schema:
            type: string
            maxLength: 32
      responses:
        '200':
          description: The consent page.
          content:
            text/html:
              schema:
                type: string
        '400':
          description: >-
            The client is unknown or a parameter is missing; the error page is rendered, without any detail of the
            failure.
          content:
            text/html:
              schema:
                type: string
  # end::consent-page-api[]
  # tag::logout-page-api[]
  /logout:
    get:
      tags: [ Logout page ]
      operationId: logoutPage
      summary: Renders the sign-out confirmation page
      description: >-
        Renders the page that asks the signed-in user to confirm signing out. A GET never signs the user out (any page
        could trigger it); the confirmation form posts to the security chain (POST /logout), with the CSRF token.
      responses:
        '200':
          description: The sign-out confirmation page.
          content:
            text/html:
              schema:
                type: string
  # end::logout-page-api[]
  # tag::passkeys-page-api[]
  /account/passkeys:
    get:
      tags: [ Passkeys page ]
      operationId: passkeysPage
      summary: Renders the passkeys account page
      description: >-
        Renders the page where the signed-in user names and registers a new passkey (the page script asks the
        WebAuthn endpoints of the security chain, POST /webauthn/register/options and POST /webauthn/register, never
        this operation) and sees the passkeys already registered, read only: renaming and deleting them is the
        bearer-protected REST API (/api/account/passkeys). Like the consent page it needs a session that satisfies the
        multi-factor policy of the account: a session that only presented the password of an account that still has to
        complete its second factor is sent to the second-factor page, so it can never register a passkey of its own.
        A request without a signed-in session is redirected to the sign-in page.
      responses:
        '200':
          description: The passkeys page.
          content:
            text/html:
              schema:
                type: string
  # end::passkeys-page-api[]
  # tag::phone-page-api[]
  /account/phone:
    get:
      tags: [ Phone page ]
      operationId: phonePage
      summary: Renders the phone account page
      description: >-
        Renders the page where the signed-in user enrols, verifies and removes the phone number that receives
        one-time codes by SMS. It shows the verified phone, masked so that only its last digits are visible, or the form
        to enter a number; once a number was submitted it shows the form that asks for the code received by SMS. The
        forms post to POST /account/phone, POST /account/phone/verify and POST /account/phone/remove, with the CSRF
        token, and each answers by redirecting back here: the bearer-protected REST API (/api/account/phone) is for
        other clients, a browser page has a session, not a token. Like the passkeys page it needs a session that
        satisfies the multi-factor policy of the account. A request without a signed-in session is redirected to the
        sign-in page.
      parameters:
        - name: status
          in: query
          required: false
          description: >-
            Outcome of the previous form post, so the page can tell it: pending (a code was sent, if the number can
            receive one), verified, removed, invalid-phone, country-not-allowed, invalid-code, rate-limited or
            step-up-required. Any other value is ignored.
          schema:
            type: string
            maxLength: 40
      responses:
        '200':
          description: The phone page.
          content:
            text/html:
              schema:
                type: string
    post:
      tags: [ Phone page ]
      operationId: startPhoneEnrolmentPage
      summary: Starts the enrolment of a phone number from the page
      description: >-
        Sends a verification code by SMS to the number (the same operation as PUT /api/account/phone, for the
        session of a browser) and redirects to the page with status pending. The answer is the same whether or not the
        number already belongs to another account, so the page never reveals which numbers are registered. A number that
        is not valid or belongs to a country SMS messages are not sent to redirects with the status invalid-phone or
        country-not-allowed, and too many requests with rate-limited. While the account has a confirmed second factor
        (the new number would replace the verified one), the session must show a recent multi-factor sign-in, or the
        page is shown with the status step-up-required and nothing is sent.
      requestBody:
        content:
          application/x-www-form-urlencoded:
            schema:
              type: object
              required: [ phone ]
              properties:
                phone:
                  type: string
                  minLength: 1
                  maxLength: 32
                  description: The phone number, in international format (+34 600 111 222) or in the national one of the default region.
      responses:
        '303':
          description: Redirects to the phone page, with the outcome in the status parameter.
          headers:
            Location:
              description: The phone page.
              schema:
                type: string
  /account/phone/verify:
    post:
      tags: [ Phone page ]
      operationId: verifyPhonePage
      summary: Confirms the phone number from the page
      description: >-
        Confirms the number with the code received by SMS (the same operation as POST /api/account/phone/verify, for the
        session of a browser) and redirects to the page with status verified. A wrong or expired code redirects with
        status invalid-code and counts as an attempt; too many requests redirect with rate-limited.
      requestBody:
        content:
          application/x-www-form-urlencoded:
            schema:
              type: object
              required: [ code ]
              properties:
                code:
                  type: string
                  minLength: 1
                  maxLength: 16
                  description: The numeric code received by SMS.
      responses:
        '303':
          description: Redirects to the phone page, with the outcome in the status parameter.
          headers:
            Location:
              description: The phone page.
              schema:
                type: string
  /account/phone/remove:
    post:
      tags: [ Phone page ]
      operationId: removePhonePage
      summary: Removes the phone number from the page
      description: >-
        Removes the verified phone and any pending verification (the same operation as DELETE /api/account/phone, for the
        session of a browser) and redirects to the page with status removed. While the account has a confirmed second
        factor the session must show a recent multi-factor sign-in, as the bearer token must for the REST API;
        otherwise nothing is removed and the redirect carries status step-up-required, which asks the user to sign in
        again.
      responses:
        '303':
          description: Redirects to the phone page, with the outcome in the status parameter.
          headers:
            Location:
              description: The phone page.
              schema:
                type: string
  # end::phone-page-api[]
  # tag::identities-page-api[]
  /account/identities:
    get:
      tags: [ Identities page ]
      operationId: identitiesPage
      summary: Renders the linked identities account page
      description: >-
        Renders the page where the signed-in user sees the identities of external providers (Google, Microsoft, Apple,
        GitHub) linked to the account and links another one: one button per configured provider, each a form that
        posts to POST /account/identities/link with the CSRF token. Unlinking is done through the bearer-protected
        REST API (DELETE /api/account/identities/{id}), which requires a recent multi-factor sign-in. Like the
        passkeys page it needs a session that satisfies the multi-factor policy of the account. A request without a
        signed-in session is redirected to the sign-in page.
      parameters:
        - name: status
          in: query
          required: false
          description: >-
            Outcome of the previous link attempt, so the page can tell it: linked (the identity is now linked),
            already-linked (the identity belongs to another account, nothing changed) or unknown-provider (the
            provider is not configured). Any other value is ignored.
          schema:
            type: string
            maxLength: 40
      responses:
        '200':
          description: The linked identities page.
          content:
            text/html:
              schema:
                type: string
  /account/identities/link:
    post:
      tags: [ Identities page ]
      operationId: linkIdentityPage
      summary: Starts linking an identity of an external provider
      description: >-
        Records, in the session, that the next sign-in with the provider is a link to the signed-in account (the
        marker is short-lived and used once) and redirects to the provider's sign-in (/oauth2/authorization/{provider},
        answered by Spring Security). When the provider sends the user back, the identity is linked to the signed-in
        account instead of signing anybody in: the session keeps its user, and the browser returns to this page with
        status linked, or already-linked when the identity belongs to another account. A provider that is not
        configured redirects to the page with status unknown-provider.
      requestBody:
        content:
          application/x-www-form-urlencoded:
            schema:
              type: object
              required: [ provider ]
              properties:
                provider:
                  type: string
                  maxLength: 20
                  description: Registration id of the provider (google, microsoft, apple or github).
      responses:
        '303':
          description: >-
            Redirects to the provider's sign-in, or to the identities page with status unknown-provider.
          headers:
            Location:
              description: The provider's sign-in, or the identities page.
              schema:
                type: string
  # end::identities-page-api[]
  # tag::device-verification-page-api[]
  /oauth2/device_verification:
    get:
      tags: [ Device verification page ]
      operationId: deviceVerificationPage
      summary: Renders the device verification page
      description: >-
        Renders the page the verification URI of the device authorization response points to: the form where the user
        types the code the device displays (RFC 8628). The form posts the code to the authorization server's own device
        verification endpoint (POST /oauth2/device_verification, owned by the framework, with the CSRF token), never to
        this operation. A request that already carries the code (verification_uri_complete) is answered by that
        endpoint directly, which also applies the multi-factor policy of the account and the limit on the attempts, and
        sends the browser on to the consent page (GET /oauth2/consent) or back here with a confirmation or an error. A
        request without a signed-in session is redirected to the sign-in page and resumed afterwards.
      parameters:
        - name: user_code
          in: query
          required: false
          description: A user code to prefill in the form. Any value that is not a plausible code is ignored.
          schema:
            type: string
            maxLength: 32
        - name: error
          in: query
          required: false
          description: >-
            Present (its value is ignored, usually empty) when the previous attempt failed; the page then shows a
            message that never says why (an unknown, expired or already used code look the same).
          schema:
            type: string
        - name: success
          in: query
          required: false
          description: >-
            Present (its value is ignored, usually empty) when the user has just approved the device; the page then
            says that the device may continue.
          schema:
            type: string
      responses:
        '200':
          description: The device verification page.
          content:
            text/html:
              schema:
                type: string
  # end::device-verification-page-api[]
  # tag::ott-pages-api[]
  /login/ott:
    get:
      tags: [ One-time token page ]
      operationId: ottCodePage
      summary: Renders the page where the one-time code is entered
      description: >-
        Where the browser lands after a one-time token was requested on POST /ott/generate, for a sign-in or as a second
        factor, whatever the identifier was: the answer never depends on whether the account exists. It tells the user
        to check the inbox (the e-mail or the SMS) and, when a numeric code was requested, shows the form to type it.
        The form posts to the security chain, never to this operation: POST /login/ott for a sign-in and
        POST /login/second-factor/ott for a second factor (the CSRF token travels in the form). A code only works in
        the browser that requested it. Public: a sign-in starts without a session; the second-factor variant is the
        one a signed-in user sees.
      security: [ ]
      parameters:
        - name: error
          in: query
          required: false
          description: >-
            Present (its value is ignored, usually empty) when the previous attempt failed; the page then shows the
            message of that failure, which never says whether the code was close or the account exists.
          schema:
            type: string
      responses:
        '200':
          description: The page.
          content:
            text/html:
              schema:
                type: string
  /login/ott/confirm:
    get:
      tags: [ One-time token page ]
      operationId: ottConfirmPage
      summary: Renders the page a magic link opens
      description: >-
        Renders a page with a single button whose form posts the token of the link to POST /login/ott. A GET never
        consumes the token, so a mail scanner or a link preview that opens the link does not use it up: only the
        user's click does. The answer is never cached and sends no referrer. The token is not checked here: it is
        verified when it is posted. Public.
      security: [ ]
      parameters:
        - name: token
          in: query
          required: true
          description: The token of the magic link, as the e-mail carried it.
          schema:
            type: string
            minLength: 1
            maxLength: 100
            pattern: '^[A-Za-z0-9-]+$'
      responses:
        '200':
          description: The confirmation page.
          content:
            text/html:
              schema:
                type: string
  # end::ott-pages-api[]
  # tag::sms-dev-inbox-page-api[]
  /dev/sms-inbox:
    get:
      tags: [ Dev SMS inbox page ]
      operationId: devSmsInboxPage
      summary: Renders the development SMS inbox
      description: >-
        Development only: it exists when the dev profile is active, and nowhere else (OpenApiDriftIT leaves it out of the
        comparison because the application it starts does not run that profile). Lists the messages the logging
        sender recorded instead of sending them, newest last, so a developer or an end-to-end test can read the code
        of a sign-in or of a phone enrolment. Public in that profile; the inbox is bounded and lives in memory.
      security: [ ]
      responses:
        '200':
          description: The recorded messages.
          content:
            text/html:
              schema:
                type: string
  # end::sms-dev-inbox-page-api[]
components:
  securitySchemes:
    session:
      type: apiKey
      in: cookie
      name: JSESSIONID
      description: >-
        Session cookie of the authorization server, set when the user signs in on the sign-in page. A request without
        a signed-in session is redirected to the sign-in page.
