This would be a good answer for me except that I cannot get header based auth to work so I am attempting to do it with forms and am also getting CSRF. Any solution? I am trying to create a new topic using the API.
We will need more information to be able to help. Can you share a code snippet showing how you are trying to use the header based authentication?
The problem I’ve been having stems mostly from whenever I try to make a call with header based auth, the accepted headers are always “User-Api-Key” and “User-Client-Id” instead of “Api-Key” and “Api-Username” so I get CORS errors. I do not believe I need user based keys. I am just trying to do a simple authenticated request to create a new topic. If I try to use the “User-Api-Key” I am getting a 403 errors.
This is sample code (Angular Typescript), but should be similar to other setups:
createNewTopic(postBody: any) {
let url = "myserver.com/posts.json";
let CATEGORY_ARTICLES = 6;
let topicContent = `
<p>Some content
</p>
<p>Did that work?</p>
`;
let post = {
"title": "Tdk Test 1",
"raw": topicContent,
"category": CATEGORY_ARTICLES,
"target_usernames": "tdekoekkoek",
"archetype": "",
"created_at": new Date()
}
let headers = new HttpHeaders()
.set("Accept", "application/json")
.set("User-Api-Key", DISCOURSE_API_KEY)
.set("User-Key", "system")
.set("Content-Type", "application/x-www-form-urlencoded")
let options = {headers: headers};
return this.http.post(url, post, options);
}
user-api-key and user-client-id are a completely different method of authentication. Regular API keys won’t work with those headers. What happens when you use the correct header names? (api-key and api-username)
I get a CORS error because the server is expecting User-Api-Key. I examine the expected headers in the OPTIONS response and that is what it says. There is a similar topic elsewhere on this forum where a user was experiencing this when calling from a javascript app.
Oh, so you are trying to make this request from a client-side javascript application? That is not permitted for numerous reasons. For example, if you include an admin API key in the source code of your javascript app, any user could gain full admin access to your forum. Here is a previous topic on the subject.
Yes that is what I am trying to do. That should be more clearly documented. That has been my whole strategy since getting started with Discourse. I appreciate your help. Been battling with this for days and reading the documentation. Will have to rethink my whole approach as I am currently building a serverless application though I do have access to a node.js server.
This isn’t a restriction specific to Discourse - in general it is a bad idea to include admin credentials in the source code of a website.
If you can make the Discourse API call from your node.js server, that would probably be the best solution. If you need your application to be purely client-side, then requesting user-specific api keys is an option, although their setup is a lot more complex: User API keys specification
Frankly accessing APIs from client applications with CORS enabled is extremely common and standard. I can’t believe there is not a more secure way of doing this. An entire industry revolves around hosted APIs that are accessed from different domains. As for accessing with user api, I’ve seen how to create those, but am unclear about what the actual credentials are. I am always getting 403 permission denied when using that method.
أعتقد أنني في وضع مماثل.
قليلًا عن حالة الاستخدام:
أستخدم Discourse كواجهة خلفية (backend) وأبني الواجهة الأمامية (frontend) باستخدام VueJS. أعزم على استخدام واجهة برمجة التطبيقات (REST API) فقط. لكل مستخدم في منصتنا، قمت بإنشاء مفتاح API خاص بالمستخدم، وأستخدم كلا الرأسين (Api-Key و Api-Username) للمصادقة.
كل مستخدم يقوم بالمصادقة بشكل آمن مع خوادمنا، ثم يتلقى رمز Discourse الخاص به للمصادقة. بعد ذلك نرغب في استخدام هذا الرمز (في واجهة VueJS الأمامية) لإرسال طلبات API (نشر مواضيع، منشورات، تحرير، إلخ).
لا شيء مشفر بشكل ثابت؛ جميع الرموز تُستلم بعد المصادقة الصحيحة.
المشكلة:
أحصل على رسالة خطأ CORS رغم أنني قمت بتفعيلها في ملف app.yml وضبطتها في قسم الأمان ضمن إعدادات المسؤول.
الخطأ: Request header field api-key is not allowed by Access-Control-Allow-Headers
هل أواجه رسالة خطأ CORS لنفس السبب؟ لماذا تُحجب طلبات CORS عند استخدام رأسّي “Api-Key” و “Api-Username”؟ يجب أن يكون هناك سوء فهم مني لحالة الاستخدام.
تعديل:
لدي نظرية حول سوء فهمي. عندما أنشئ مفتاح API لمستخدم من نقطة نهاية “إنشاء مفتاح API لمستخدم” في واجهة برمجة التطبيقات REST، هل يمتلك هذا المفتاح صلاحيات إدارية؟ أم أنه مجرد مفتاح مستخدم للنشر والتعليق؟ إذا كان الأمر كذلك، فأنا أفهم القيود.
يرجى تصحيحي إذا لزم الأمر.
@david، ألا يمكن لـ Discourse ببساطة أن يقوم الخادم بتوقيع سر الجلسة باستخدام HMAC، ويمكن استخدام سر الجلسة هذا بدوره لتوقيع الأشياء في الجلسة المعطاة؟ إذا تمت سرقته، فمن المحتمل ألا يتم سرقة ملف تعريف ارتباط الجلسة أو CSRF nonce أيضًا. إنه يشبه إلى حد ما سلسلة الشهادات - لا يتعين عليك وضع المفتاح الجذر في المتصفح.
هذه لواجهة برمجة تطبيقات المسؤول فقط. الطريقة التي فهمتها هي أنه بالنسبة لعملاء JavaScript، تحتاج إلى، كما ذكر أعلاه، التحقق من User API keys specification .