Erreur CORS Access-Control-Allow-Headers avec l'API après la mise à jour de Discourse

J’ai mis à jour Discourse hier et je reçois toutes les callbacks de l’API avec une erreur. J’ai constaté que le problème venait des en-têtes d’authentification (1), mais en essayant quelques appels, j’obtiens cette erreur :

L'accès à XMLHttpRequest à 'https://mydomain.com/notifications.json?username=admin' depuis l'origine 'https://mydomain.com' a été bloqué par la politique CORS : le champ d'en-tête de requête api-username n'est pas autorisé par Access-Control-Allow-Headers dans la réponse de préflight.

J’utilise Vue et j’ai ajouté api-key et api-username aux en-têtes comme ceci :

Vue.http
        .get(
             "https://discourse.mydomain.com/notifications.json?username="+input.username,
          {
            headers: {
             "Api-Key":"xxxxxxxxxxxxxxxxx",
             "Api-Username": "system",
              "content-type": "application/json",
              accept: "application/json"
            }
          }
        )

(1) Discourse REST API Documentation

You may need to have CORS setup in your site settings?

Yes, I have added the domain where I call the API in discourse settings, but that error about field api-username, I dont know when I have to resolve.

You are putting your system API key in client side code? :scream:
This means that anyone can grab it from your Javascript code and use it to completely own your forum.

Any Ajax HTTP requests to Discourse should be leveraging an existing session, or not be needing a session at all.

3 « J'aime »

Not just a testing , I am using the username

      Vue.http
    .get(
      "https://discourse.mydomain.com/notifications.json?username="+input.username,
      {
        headers: {
       "Api-Key":"xxxxxxxxxxxxxxxxxxxxxx",
        "Api-Username": input.username    
        }
      }
    )

But I getting the same error with de Cors and the field api-username

1 « J'aime »

Let me repeat what I said:

That means: no authentication headers. At all.

1 « J'aime »

You really should not be using these API credentials for CORS requests which is why that header field is not allowed.

However we do allow the user-api headers in CORS:

3 « J'aime »

I have read the User API keys specification

In my case I have SSO with a frontend app in javascript, Could I consuming the API without using the authorization UI for every user? I would like a way that a could use de api-username … is that possible?

Regards

Pourriez-vous résoudre cela ?
Merci !

Au final, je suis revenu à la version précédente de Discourse pour éviter de devoir modifier toute l’intégration de l’API.