================================================================================
PROCEDURE: RENEWING THE ESA MAAP OFFLINE TOKEN (90-DAY VALIDITY)
================================================================================

PURPOSE
-------
The ESA MAAP offline token (a.k.a. "Long Lasting Token" or LLT) is valid for
90 days. Once it expires, the script
end2end/test_recup_shortlived_token_using_the_offline_token.py fails with an
HTTP 400 or 401 error when trying to exchange the offline token for a
short-lived access token. This procedure explains how to obtain a fresh
offline token and update your local configuration.

PREREQUISITES
-------------
- An active ESA MAAP account with access initialized.
- An ESA EO Sign-In account (https://eoiam-idp.eo.esa.int/).
- A web browser.

NOTE ON IDENTITY PROVIDERS
--------------------------
The ESA MAAP token page offers two login options: "Login with ESA Account"
and "Login with NASA Earthdata Login (EDL)".

In practice, the "Login with NASA Earthdata Login (EDL)" path may fail with:

    We are sorry...
    The ID token issued by the identity provider does not match the
    configured essential claim. Please contact your administrator.

If this happens, use the "Login with ESA Account" option instead.
That path is confirmed to work with the ESA MAAP portal.

================================================================================
DIAGNOSTIC: CONFIRMING THE TOKEN IS EXPIRED
================================================================================
Run:

    python end2end/test_recup_shortlived_token_using_the_offline_token.py \
        credentials_esa_maap_biomass.txt

Expected output when the offline token has expired:

    ============================================================
    STEP 1: Checking credentials file
    ============================================================
    ✅ Credentials file exists.
    ✅ All required keys are present.

    ============================================================
    STEP 2: Requesting short-lived access token
    ============================================================
    ❌ Failed to retrieve access token.
       Error type: HTTPError
       Error message: 400 Client Error: Bad Request for url:
       https://iam.maap.eo.esa.int/realms/esa-maap/protocol/openid-connect/token

This confirms the offline token is no longer valid and must be renewed.

================================================================================
STEP 1: OPEN THE TOKEN PAGE
================================================================================
Navigate to:

    https://portal.maap.eo.esa.int/ini/services/auth/token/#

Note: the URL may include a trailing "#". This is normal.

================================================================================
STEP 2: AUTHENTICATE WITH THE ESA ACCOUNT
================================================================================
- Choose "Login with ESA Account" (NOT "Login with NASA Earthdata Login").
- You will be redirected to the ESA EO Sign-In portal.
- Enter your ESA EO Sign-In credentials and authorize access.
- After successful authorization, you will be redirected back to the ESA MAAP
  token page.

If the NASA EDL path is attempted and fails with the error message
"The ID token issued by the identity provider does not match the configured
essential claim", retry with "Login with ESA Account".

================================================================================
STEP 3: COPY THE OFFLINE TOKEN
================================================================================
- The page displays both a short-lived access token and an offline token.
- COPY THE OFFLINE TOKEN (not the short-lived access token).
- The offline token is a long string (typically 800+ characters).

================================================================================
STEP 4: UPDATE YOUR LOCAL CREDENTIALS FILE
================================================================================
The credentials file for this project is:

    credentials_esa_maap_biomass.txt

It should contain exactly three lines:

    OFFLINE_TOKEN=<paste your new offline token here>
    CLIENT_ID=offline-token
    CLIENT_SECRET=p1eL7uonXs6MDxtGbgKdPVRAmnGxHpVE

WARNING: Do NOT add spaces around the "=" sign.
WARNING: Do NOT commit this file to any version control system.

================================================================================
STEP 5: VERIFY THE NEW TOKEN
================================================================================
Run the diagnostic script again:

    python end2end/test_recup_shortlived_token_using_the_offline_token.py \
        credentials_esa_maap_biomass.txt

Expected output when the offline token is valid:

    ============================================================
    STEP 1: Checking credentials file
    ============================================================
    ✅ Credentials file exists.
    ✅ All required keys are present.

    ============================================================
    STEP 2: Requesting short-lived access token
    ============================================================
    ✅ Successfully retrieved access token.
       Token (first 40 chars): eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSld...
       Token length: 1234 characters

    ============================================================
    ✅ ALL CHECKS PASSED
    ============================================================

================================================================================
STEP 6 (OPTIONAL): SECURE THE FILE
================================================================================
Restrict permissions on the credentials file:

    chmod 600 credentials_esa_maap_biomass.txt

This ensures only your user account can read or modify it.

================================================================================
USEFUL LINKS
================================================================================
- ESA MAAP Portal:               https://portal.maap.eo.esa.int/
- ESA MAAP Token Page:           https://portal.maap.eo.esa.int/ini/services/auth/token/#
- ESA EO Sign-In Portal:         https://eoiam-idp.eo.esa.int/
- ESA MAAP Documentation:        https://docs.maap-project.org/

================================================================================
TROUBLESHOOTING
================================================================================
Symptom: HTTP 400 Bad Request when calling get_token()
Cause:   Offline token expired OR malformed request.
Fix:     Renew the offline token (see STEP 1) and update the credentials file.

Symptom: HTTP 401 Unauthorized when calling get_token()
Cause:   Offline token revoked, or account not authorized.
Fix:     Verify your account status on the ESA MAAP portal.

Symptom: NASA EDL login fails with
         "The ID token issued by the identity provider does not match the
         configured essential claim"
Cause:   Mismatch between the NASA EDL profile and the ESA MAAP account.
Fix:     Use "Login with ESA Account" instead on the token page.

Symptom: DNS error "cannot resolve iam.maap.eo.esa.int"
Cause:   Network restriction.
Fix:     Run from a network that can reach ESA MAAP, or use the MAAP ADE.

Symptom: The new offline token is rejected with HTTP 400 immediately
Cause:   The token was copied with truncation, or contains extra whitespace.
Fix:     Re-copy the token as a single line without trailing spaces.

================================================================================
END OF PROCEDURE
================================================================================
