mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-10-29 16:07:14 +00:00
feat(dashboard): scoped API keys with rate limits and quotas
Bearer keys (dlk_...) are checked per request against their owner's current account (ban, lock, demotion, sign out everywhere stop or narrow them at once) and their scope: permission routes need the permission in scope, read-only keys only read, level-only routes need an all-permission key, and session-only paths (sign-in, password, 2FA, key management) are never reachable with a key. Per-key rate limit and daily quota with 429 and X-RateLimit/X-Quota/Retry-After headers; usage is written in batches every minute. WebSocket subscriptions honour the scope too. Routes to list, make, rotate and revoke keys; staff with api_keys_manage can see and revoke others' keys under the rank rules. POST /api/auth/token now makes an all-permission key. Audit entries for create/rotate/revoke/denied, and actions done with a key are attributed to "user (key name)". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -259,7 +259,23 @@ namespace RouteUtils {
|
||||
void Route(eHTTPMethod method, const std::string& path, int16_t minGmLevel, const std::string& description, Handler handler);
|
||||
void Route(eHTTPMethod method, const std::string& path, const Perm& permission, const std::string& description, Handler handler);
|
||||
|
||||
// Whether the signed-in user has a permission
|
||||
// Routes registered while one of these lives only read, even POSTs (DataTables and lookups send their query as a
|
||||
// body), so read-only API keys may use them
|
||||
struct ReadRoutes {
|
||||
ReadRoutes();
|
||||
~ReadRoutes();
|
||||
ReadRoutes(const ReadRoutes&) = delete;
|
||||
ReadRoutes& operator=(const ReadRoutes&) = delete;
|
||||
};
|
||||
|
||||
// Route for a POST that only reads (see ReadRoutes)
|
||||
template<typename Access>
|
||||
void ReadRoute(eHTTPMethod method, const std::string& path, const Access& access, const std::string& description, Handler handler) {
|
||||
ReadRoutes reads;
|
||||
Route(method, path, access, description, std::move(handler));
|
||||
}
|
||||
|
||||
// Whether the signed-in user has a permission (and, for a request made with an API key, the key's scope has it)
|
||||
bool Can(const HTTPContext& context, const std::string& permission);
|
||||
|
||||
// A character typed by a person: a number is its ID, anything else its name. nullopt: no such character.
|
||||
|
||||
Reference in New Issue
Block a user