Skip to content

Commit 2395e88

Browse files
Don't set the authorization headers on webhook/interactions related routes
germanoeich/nirn-proxy#23
1 parent 3483493 commit 2395e88

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

src/main/java/net/dv8tion/jda/api/requests/Route.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,27 @@ public static class Templates {
356356
public static final Route GET_GUILD_TEMPLATES = new Route(GET, "guilds/{guild_id}/templates");
357357
}
358358

359+
// Looks very nasty because JDA still requires Java 8 support
360+
private static final Set<Route> ROUTES_WITHOUT_TOKEN = new HashSet<>(
361+
Arrays.asList(
362+
Webhooks.GET_TOKEN_WEBHOOK,
363+
Webhooks.DELETE_TOKEN_WEBHOOK,
364+
Webhooks.MODIFY_TOKEN_WEBHOOK,
365+
Webhooks.EXECUTE_WEBHOOK,
366+
Webhooks.EXECUTE_WEBHOOK_FETCH,
367+
Webhooks.EXECUTE_WEBHOOK_EDIT,
368+
Webhooks.EXECUTE_WEBHOOK_DELETE,
369+
Webhooks.EXECUTE_WEBHOOK_SLACK,
370+
Webhooks.EXECUTE_WEBHOOK_GITHUB,
371+
372+
Interactions.CALLBACK,
373+
Interactions.CREATE_FOLLOWUP,
374+
Interactions.EDIT_FOLLOWUP,
375+
Interactions.DELETE_FOLLOWUP,
376+
Interactions.GET_MESSAGE
377+
)
378+
);
379+
359380
/**
360381
* Create a route template for the given HTTP method.
361382
*
@@ -626,6 +647,15 @@ public boolean isInteractionBucket() {
626647
return isInteraction;
627648
}
628649

650+
/**
651+
* Whether this route should include the bot token in the authorization header.
652+
*
653+
* @return True, if this route should include the bot token in the header
654+
*/
655+
public boolean includeTokenInHeader() {
656+
return !ROUTES_WITHOUT_TOKEN.contains(this);
657+
}
658+
629659
/**
630660
* The {@link Method} of this route template.
631661
* <br>Multiple routes with different HTTP methods can share a rate-limit.

src/main/java/net/dv8tion/jda/internal/requests/Requester.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,13 @@ private void applyBody(Request<?> apiRequest, okhttp3.Request.Builder builder) {
295295
private void applyHeaders(Request<?> apiRequest, okhttp3.Request.Builder builder) {
296296
builder.header("user-agent", userAgent)
297297
.header("accept-encoding", "gzip")
298-
.header("authorization", authConfig.getToken())
299298
.header("x-ratelimit-precision", "millisecond"); // still sending this in case of regressions
300299

300+
// Only add bot token for routes that do use token-based authentication
301+
if (apiRequest.getRoute().getBaseRoute().includeTokenInHeader()) {
302+
builder.header("authorization", authConfig.getToken());
303+
}
304+
301305
// Apply custom headers like X-Audit-Log-Reason
302306
// If customHeaders is null this does nothing
303307
if (apiRequest.getHeaders() != null) {

0 commit comments

Comments
 (0)