|
1 | 1 | import "source-map-support/register"; |
2 | 2 |
|
| 3 | +import { Routes, RouteBases } from "discord-api-types/v10"; |
3 | 4 | import * as Sentry from "@sentry/node"; |
4 | 5 | import { Integrations, Transaction } from "@sentry/tracing"; |
5 | 6 | import { BaseRedisCache } from "apollo-server-cache-redis"; |
@@ -160,6 +161,79 @@ async function run() { |
160 | 161 | app.get("/app/update", appUpdate); |
161 | 162 | app.get("/app/update/*", appUpdate); |
162 | 163 | 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 | + }); |
163 | 237 |
|
164 | 238 | app |
165 | 239 | .listen(process.env.PORT || 3001, process.env.HOST || "0.0.0.0") |
|
0 commit comments