|
| 1 | +import { NodeCG } from "nodecg/types/server"; |
| 2 | +import { Result, emptySuccess, success, error, ServiceBundle } from "nodecg-io-core"; |
| 3 | +import { google, sheets_v4 } from "googleapis"; |
| 4 | +import type { Credentials } from "google-auth-library/build/src/auth/credentials"; |
| 5 | +import type { OAuth2Client } from "google-auth-library/build/src/auth/oauth2client"; |
| 6 | +import * as express from "express"; |
| 7 | +import opn = require("open"); |
| 8 | + |
| 9 | +interface GSheetsServiceConfig { |
| 10 | + clientID: string; |
| 11 | + clientSecret: string; |
| 12 | + refreshToken?: string; |
| 13 | +} |
| 14 | + |
| 15 | +export type GSheetsServiceClient = sheets_v4.Sheets; |
| 16 | + |
| 17 | +module.exports = (nodecg: NodeCG) => { |
| 18 | + new GSheetsService(nodecg, "gsheets", __dirname, "../gsheets-schema.json").register(); |
| 19 | +}; |
| 20 | + |
| 21 | +class GSheetsService extends ServiceBundle<GSheetsServiceConfig, GSheetsServiceClient> { |
| 22 | + async validateConfig(_config: GSheetsServiceConfig): Promise<Result<void>> { |
| 23 | + return emptySuccess(); |
| 24 | + } |
| 25 | + |
| 26 | + async createClient(config: GSheetsServiceConfig): Promise<Result<GSheetsServiceClient>> { |
| 27 | + const auth = new google.auth.OAuth2({ |
| 28 | + clientId: config.clientID, |
| 29 | + clientSecret: config.clientSecret, |
| 30 | + redirectUri: "http://localhost:9090/nodecg-io-gsheets/oauth2callback", |
| 31 | + }); |
| 32 | + if (config.refreshToken) { |
| 33 | + this.nodecg.log.info("Re-using saved refresh token."); |
| 34 | + auth.setCredentials({ |
| 35 | + refresh_token: config.refreshToken, |
| 36 | + }); |
| 37 | + } else { |
| 38 | + this.nodecg.log.info("No refresh token found. Starting auth flow to get one..."); |
| 39 | + auth.setCredentials(await this.initialAuth(auth)); |
| 40 | + if (auth.credentials.refresh_token) { |
| 41 | + config.refreshToken = auth.credentials.refresh_token; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + // Save refresh tokens so they can be used next time to get a access token again |
| 46 | + auth.on("tokens", (tokens) => { |
| 47 | + if (tokens.refresh_token) { |
| 48 | + config.refreshToken = tokens.refresh_token; |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + const client = new sheets_v4.Sheets({ auth }); |
| 53 | + return success(client); |
| 54 | + } |
| 55 | + |
| 56 | + private initialAuth(auth: OAuth2Client): Promise<Credentials> { |
| 57 | + const authUrl = auth.generateAuthUrl({ |
| 58 | + access_type: "offline", |
| 59 | + scope: "https://www.googleapis.com/auth/spreadsheets", |
| 60 | + prompt: "consent", |
| 61 | + }); |
| 62 | + |
| 63 | + return new Promise((resolve, reject) => { |
| 64 | + const router: express.Router = express.Router(); |
| 65 | + |
| 66 | + router.get("/nodecg-io-gsheets/oauth2callback", async (req, res) => { |
| 67 | + try { |
| 68 | + const params = req.query; |
| 69 | + res.end("<script>open(location, '_self').close();</script>"); |
| 70 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 71 | + const { tokens } = await auth.getToken(params.code!.toString()); |
| 72 | + resolve(tokens); |
| 73 | + } catch (e) { |
| 74 | + reject(error(e)); |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + this.nodecg.mount(router); |
| 79 | + opn(authUrl, { wait: false }).then((cp) => cp.unref()); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + stopClient(_client: GSheetsServiceClient): void { |
| 84 | + // Cannot stop client |
| 85 | + } |
| 86 | +} |
0 commit comments