API の CORS ヘッダーが正しく設定されていません

Hi,

I am having an issue with embedding discourse in our intranet site.

The API documentation states the discourse request requires the headers “API-Key” and “Api-Username” to authenticate and gain access to the feed. However the Pre-flight check says that “User-API-Key”, “User-Api-Client-Id” are the allowed values.

When called not through a browser this work as expected. But when calling through the browser the server is claiming it requires “User-API-Key”, “User-Api-Client-Id”.

I checked basic connection worked with PostMan this behaves as per the discourse docs

If we pass the Headers from the Docs the browser blocks the request due to Pre-flight check and gets a Access-Control-Allow-Headers CORS error.

If we pass the headers the server will accept we get a “not authorised error” because the application expects differently named values.

I have tried adding headers to the docker config but it doesn’t seem to apply. The CORS enabled and origin of ‘*’ is in the config.

Can anyone advise?

Thanks,
Jessica

「いいね!」 1

Just wondering if there was any more info on the above? Is this a bug or something I’m doing wrong?

Thanks,
Jessica

「いいね!」 1

We have two different API authentication systems, which can be confusing.

These are for the ‘admin API’, which is described on docs.discourse.org. This is not designed to be used from javascript clients.

These are from the “User API” specification, which can be used from a javascript client (and therefore supports CORS). There are more details about this here: User API keys specification

「いいね!」 5

@david SSO を使用しており、アプリからログアウトした際に Discourse からもユーザーをログアウトさせたいと考えています。現在は /users/by-external/${id}.json を通じてユーザー ID を取得するために「管理 API」を使用していますが、CORS エラーが発生しています。このログアウト処理のために全ユーザー向けに「ユーザー API」を有効にしたくありません。どのような解決策を提案されますか?

管理APIリクエストを送信しているのは何ですか?クライアントアプリケーション内のJavaScriptですか?

JavaScriptクライアントに管理APIを含めるべきではありません。それでは、クライアントを利用する誰もがあなたのサイトに管理者アクセスを得てしまうことになります。

「いいね!」 3

はい、私のアプリでは JavaScript を使用しています。理解しました。では、代替案は何でしょうか?特定のユーザー向けの「ユーザー API」を追加し、それを介してすべてのユーザーに対して呼び出しを行うことは可能でしょうか?

ユーザー API を使用する場合は、ユーザーごとに処理する必要があります。キーの共有は避けてください。

ただし、最も一般的な対応は、アプリのサーバー側で処理を行うことです。サーバーは、CORS の問題なく、セキュリティ上の懸念も少ない(安全に実装されている限り)管理者 API キーを使用してリクエストを送信できます。

「いいね!」 2

@david さん、ありがとうございます。それが参考になりましたので、そのように対応します。再度お礼申し上げます。

「いいね!」 2

こんにちは @david さん、バックエンドの解決策を実装しようと試みましたが、https://example.com/users/by-external/{EXTERNAL_USER_ID}.json?api_key={DISCOURSE_API_KEY}&api_username=system を呼び出すと、サインインページの HTML が返ってきます(おそらくそこへリダイレクトされているのだと思います)。ログインが必要 が有効になっている状態でこれを無効にすると、正しい JSON 応答が得られます。この設定は有効のままにしたいのですが、何が起きているか見当がつくでしょうか?

API キーとユーザー名にはヘッダーを使用する必要があります。詳細は http://docs.discourse.org/ をご確認ください。

「いいね!」 5

問題解決しました。ありがとうございます。:+1:

「いいね!」 3