From b06c42535268f8b5709a2a423dd5ab78ccf5d34d Mon Sep 17 00:00:00 2001 From: Dessa Simpson Date: Thu, 10 Sep 2020 22:42:37 -0700 Subject: [PATCH] Add streamer authentication Closes #17 --- db/15-streamer.sql | 6 ++++++ db/testdata.sql | 7 +++++-- src/app.ts | 30 +++++++++++++++++++++++++----- src/config.ts | 18 +++++++++--------- 4 files changed, 45 insertions(+), 16 deletions(-) create mode 100644 db/15-streamer.sql diff --git a/db/15-streamer.sql b/db/15-streamer.sql new file mode 100644 index 0000000..53068c8 --- /dev/null +++ b/db/15-streamer.sql @@ -0,0 +1,6 @@ +CREATE TABLE streamer ( + userid int NOT NULL, + tokenPair json, + PRIMARY KEY (userid), + FOREIGN KEY (userid) REFERENCES users(userid) +) diff --git a/db/testdata.sql b/db/testdata.sql index d670fb3..5edbbc9 100644 --- a/db/testdata.sql +++ b/db/testdata.sql @@ -1,8 +1,11 @@ -INSERT INTO users (userId,displayName,isFollower,isSubscriber) VALUES +INSERT INTO users (userid,displayName,isFollower,isSubscriber) VALUES (001,'TestUser',false,false), (002,'TestFollower',true,false), (003,'TestSubscriber',true,true), - (004,'TestSubNonFollower',false,true); + (004,'TestSubNonFollower',false,true), + (229815572,'virtualdxs',true,true); + +INSERT INTO streamer (userid) VALUES (229815572); CALL add_request('https://www.youtube.com/watch?v=dQw4w9WgXcQ',001); CALL add_request('https://www.youtube.com/watch?v=C5oeWHngDS4',002); diff --git a/src/app.ts b/src/app.ts index c9c88db..464b0d0 100644 --- a/src/app.ts +++ b/src/app.ts @@ -4,6 +4,7 @@ import * as twitch from "./twitch"; import { URLSearchParams } from "url"; import express from "express"; import session from "express-session"; +import pg from "pg"; import pgSessionStore from "connect-pg-simple"; import fetch, { Response as FetchResponse } from "node-fetch"; import db from "./db"; @@ -13,9 +14,6 @@ import errorHandler from "./errors"; // logging out the user. Should be called before checking whether a user is // logged in. async function validateApiToken(session: Express.Session) { - if (session.tokenpair) { - console.log(session.tokenpair) - } if (session.tokenpair && ! (await twitch.isApiTokenValid(session.tokenpair))) { session.destroy(()=>{}); } @@ -206,7 +204,6 @@ app.get("/callback", async (request, response) => { if (typeof tokenResponse == 'undefined') throw new Error('tokenResponse is undefined'); request.session.tokenpair = { access_token: tokenResponse.access_token, refresh_token: tokenResponse.refresh_token }; request.session.user = (await twitch.apiRequest(request.session.tokenpair,"/users")).data[0]; - const updateUserQuery = { name: "updateUser", text: "INSERT INTO users (userid,displayName,imageUrl) VALUES ($1,$2,$3)\ @@ -214,6 +211,22 @@ app.get("/callback", async (request, response) => { } var query = Object.assign(updateUserQuery,{ values: [request.session.user.id,request.session.user.display_name,request.session.user.profile_image_url] }); db.query(query); + if (typeof (tokenResponse as any).scope != 'undefined') { // Scopes requested - update streamer info + const getStreamerIdQuery = { + name: "getStreamerId", + text: "SELECT userid FROM streamer" + } + var streamerid = await db.query(getStreamerIdQuery).then((result: pg.QueryResult) => result.rows[0]['userid']); + if (request.session.user.id == streamerid) { + const updateStreamerQuery = { + name: "updateStreamer", + text: "INSERT INTO streamer (userid,tokenPair) VALUES ($1,$2)\ + ON CONFLICT (userid) DO UPDATE SET tokenPair = $2" + } + var query = Object.assign(updateStreamerQuery,{ values: [request.session.user.id,JSON.stringify(request.session.tokenpair)] }); + db.query(query); + } + } response.redirect(307, '/'); }); @@ -235,8 +248,15 @@ app.get("/", async (request, response) => { } }); +// Streamer Panel +//app.get("/streamer/", async (request, response) => { +// +//}); + +app.get("/streamer/auth", async ({}, response) => response.redirect(307, `https://id.twitch.tv/oauth2/authorize?client_id=${config.twitchClientId}&redirect_uri=${config.urlPrefix}/callback&response_type=code&scope=channel:read:subscriptions moderation:read`)); + // Logout -app.get ("/logout", async (request, response) => request.session!.destroy(() => response.redirect(307, '/'))); +app.get("/logout", async (request, response) => request.session!.destroy(() => response.redirect(307, '/'))); app.listen(config.port, () => { console.log(`Listening on port ${config.port}`); diff --git a/src/config.ts b/src/config.ts index 378e078..d19dd36 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,29 +1,29 @@ -if (!process.env.PORT) { +if (typeof process.env.PORT == undefined) { console.log("Missing environment variable PORT"); process.exit(1); } export const port: number = parseInt(process.env.PORT as string, 10); -if (!process.env.URL_PREFIX) { +if (typeof process.env.URL_PREFIX == undefined) { console.log("Missing environment variable URL_PREFIX"); process.exit(1); } -export const urlPrefix: string = process.env.URL_PREFIX; +export const urlPrefix: string = process.env.URL_PREFIX!; -if (!process.env.TWITCH_CLIENTID) { +if (typeof process.env.TWITCH_CLIENTID == undefined) { console.log("Missing environment variable TWITCH_CLIENTID"); process.exit(1); } -export const twitchClientId: string = process.env.TWITCH_CLIENTID; +export const twitchClientId: string = process.env.TWITCH_CLIENTID!; -if (!process.env.TWITCH_SECRET) { +if (typeof process.env.TWITCH_SECRET == undefined) { console.log("Missing environment variable TWITCH_SECRET"); process.exit(1); } -export const twitchSecret: string = process.env.TWITCH_SECRET; +export const twitchSecret: string = process.env.TWITCH_SECRET!; -if (!process.env.SESSION_SECRET) { +if (typeof process.env.SESSION_SECRET == undefined) { console.log("Missing environment variable SESSION_SECRET"); process.exit(1); } -export const sessionSecret: string = process.env.SESSION_SECRET; +export const sessionSecret: string = process.env.SESSION_SECRET!;