Browse Source

web: start work on auth

ghorsington 3 years ago
parent
commit
856df858df
6 changed files with 53 additions and 9 deletions
  1. 3 1
      .env.template
  2. 1 1
      bot/src/rpc.ts
  3. 1 1
      docker-compose.dev.yml
  4. 37 3
      web/src/routes/auth.ts
  5. 10 2
      web/src/routes/login.ts
  6. 1 1
      web/src/rpc.ts

+ 3 - 1
.env.template

@@ -18,4 +18,6 @@ GMAIL_NAME=
 GMAIL_PASSWORD=
 ERRORS_ADDR=
 
-NOCTBOT_ADDR=
+NOCTBOT_ADDR=
+RPC_PORT=
+WEB_AUTH_URI=

+ 1 - 1
bot/src/rpc.ts

@@ -5,7 +5,7 @@ import { ModuleRpcProtocolServer } from "rpc_ts/lib/protocol/server";
 import { NoctBotService } from "@shared/rpc/backend";
 import { logger } from "./logging";
 
-const PORT = +(process.env.WEB_PORT ?? "8181");
+const PORT = +(process.env.RPC_PORT ?? "8181");
 
 
 const app = express();

+ 1 - 1
docker-compose.dev.yml

@@ -3,7 +3,7 @@ version: '3.7'
 services:
   noctbot:
     ports:
-      - 8181:8181
+      - "${RPC_PORT}:${RPC_PORT}"
 
   db:
     ports:

+ 37 - 3
web/src/routes/auth.ts

@@ -1,8 +1,42 @@
 import { Request as ExpressRequest, Response as ExpressResponse } from "express";
-import { rpcClient } from "src/rpc";
+import got from "got";
+
+const TOKEN_API = "https://discord.com/api/oauth2/token";
+
+interface CodeResponse {
+    code?: string;
+}
+
+interface AccessTokenResponse {
+    // eslint-disable-next-line camelcase
+    access_token: string;
+    // eslint-disable-next-line camelcase
+    token_type: string;
+    // eslint-disable-next-line camelcase
+    expires_in: number;
+    // eslint-disable-next-line camelcase
+    refresh_token: string;
+    // eslint-disable-next-line camelcase
+    scope: string;
+}
 
 export const get = async (req: ExpressRequest, res: ExpressResponse): Promise<void> => {
-    const result = await rpcClient.getPing({ ping: "PING" });
-    console.log(`Res: ${result.text}, url: ${req.url}`);
+    const data = req.query as CodeResponse;
+    if (!data.code) {
+        res.redirect("/");
+        return;
+    }
+    const result = await got<AccessTokenResponse>(TOKEN_API, {
+        method: "post",
+        form: {
+            client_id: process.env.BOT_CLIENT_ID,
+            client_secret: process.env.BOT_CLIENT_SECRET,
+            grant_type: "authorization_code",
+            code: data.code,
+            scope: "identify",
+            redirect_uri: process.env.WEB_AUTH_URI,
+        },
+    });
+    console.log(result.body);
     res.redirect("/");
 };

+ 10 - 2
web/src/routes/login.ts

@@ -1,6 +1,14 @@
 import { Request as ExpressRequest, Response as ExpressResponse } from "express";
+import { stringify } from "querystring";
+
+const AUTHORIZE_URL = "https://discord.com/api/oauth2/authorize";
 
 export const get = async (req: ExpressRequest, res: ExpressResponse): Promise<void> => {
-    console.log(`referrer: ${req.headers.referer}`);
-    res.redirect("/");
+    const params = stringify({
+        client_id: process.env.BOT_CLIENT_ID,
+        redirect_url: process.env.WEB_AUTH_URI,
+        response_type: "code",
+        scope: "identify",
+    });
+    res.redirect(`${AUTHORIZE_URL}?${params}`);
 };

+ 1 - 1
web/src/rpc.ts

@@ -3,6 +3,6 @@ import { NoctBotService } from "@shared/rpc/backend";
 import { NodeHttpTransport } from "@improbable-eng/grpc-web-node-http-transport";
 
 export const rpcClient = ModuleRpcProtocolClient.getRpcClient(NoctBotService, {
-    remoteAddress: `http://${process.env.NOCTBOT_ADDR}:${process.env.WEB_PORT}`,
+    remoteAddress: `http://${process.env.NOCTBOT_ADDR}:${process.env.RPC_PORT}`,
     getGrpcWebTransport: NodeHttpTransport(),
 });