Skip to content
This repository was archived by the owner on Nov 10, 2025. It is now read-only.

Commit 0fdf519

Browse files
committed
feat: refresh and revoke routes
1 parent 1c9f0b5 commit 0fdf519

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

apps/worker/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"@types/lodash": "^4.14.180",
3636
"@types/node": "^18.16.0",
3737
"@types/validator": "^13.7.2",
38-
"discord-api-types": "^0.37.0",
38+
"discord-api-types": "^0.37.63",
3939
"dotenv": "^16.0.0",
4040
"ts-devscript": "^3.0.6",
4141
"typescript": "^5.0.3"

apps/worker/src/index.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import "source-map-support/register";
22

3+
import { Routes, RouteBases } from "discord-api-types/v10";
34
import * as Sentry from "@sentry/node";
45
import { Integrations, Transaction } from "@sentry/tracing";
56
import { BaseRedisCache } from "apollo-server-cache-redis";
@@ -160,6 +161,79 @@ async function run() {
160161
app.get("/app/update", appUpdate);
161162
app.get("/app/update/*", appUpdate);
162163
app.get("/presences.zip", zippedPresences);
164+
app.post<{
165+
Querystring: {
166+
token?: string;
167+
};
168+
}>(
169+
"/oauth2/revoke",
170+
171+
async (req, reply) => {
172+
if (
173+
typeof req.query !== "object" ||
174+
!("token" in (req.query ?? {})) ||
175+
!req.query.token
176+
)
177+
return reply.status(400).send("Invalid request");
178+
179+
const params = new URLSearchParams();
180+
params.append("token", req.query.token!);
181+
params.append("token_type_hint", "access_token");
182+
params.append("client_id", process.env.DISCORD_CLIENT_ID!);
183+
params.append("client_secret", process.env.DISCORD_CLIENT_SECRET!);
184+
185+
try {
186+
const result = await fetch(
187+
RouteBases.api + Routes.oauth2TokenRevocation(),
188+
{
189+
method: "POST",
190+
body: params
191+
}
192+
);
193+
194+
if (!result.ok) return reply.status(500).send("Internal Server Error");
195+
196+
return reply.status(202).send();
197+
} catch (e) {
198+
console.log(e);
199+
return reply.status(500).send("Internal Server Error");
200+
}
201+
}
202+
);
203+
app.post<{
204+
Querystring: {
205+
refresh_token?: string;
206+
};
207+
}>("/oauth2/refreshtoken", async (req, reply) => {
208+
if (
209+
typeof req.query !== "object" ||
210+
!("refresh_token" in req.query) ||
211+
!req.query.refresh_token
212+
)
213+
return reply.status(400).send("Invalid request");
214+
215+
const params = new URLSearchParams();
216+
params.append("grant_type", "refresh_token");
217+
params.append("refresh_token", req.query.refresh_token);
218+
params.append("client_id", process.env.DISCORD_CLIENT_ID!);
219+
params.append("client_secret", process.env.DISCORD_CLIENT_SECRET!);
220+
params.append("redirect_uri", "https://api.premid.app/oauth2/callback");
221+
222+
try {
223+
const result = await fetch(
224+
RouteBases.api + Routes.oauth2TokenExchange(),
225+
{
226+
method: "POST",
227+
body: params
228+
}
229+
);
230+
231+
return reply.status(result.status).send(await result.json());
232+
} catch (e) {
233+
console.log(e);
234+
return reply.status(500).send("Internal Server Error");
235+
}
236+
});
163237

164238
app
165239
.listen(process.env.PORT || 3001, process.env.HOST || "0.0.0.0")

pnpm-lock.yaml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)