use new google oauth

This commit is contained in:
srgooglo 2023-11-21 21:07:26 +01:00
parent 426cae3b23
commit 33023b1338
2 changed files with 37 additions and 14 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "rs-bundler", "name": "rs-bundler",
"version": "0.6.0", "version": "0.7.0",
"description": "RageStudio Bundler Utility GUI", "description": "RageStudio Bundler Utility GUI",
"main": "./out/main/index.js", "main": "./out/main/index.js",
"author": "RageStudio", "author": "RageStudio",
@ -22,7 +22,7 @@
"dependencies": { "dependencies": {
"@electron-toolkit/preload": "^2.0.0", "@electron-toolkit/preload": "^2.0.0",
"@electron-toolkit/utils": "^2.0.0", "@electron-toolkit/utils": "^2.0.0",
"@google-cloud/local-auth": "^2.1.0", "@getstation/electron-google-oauth2": "^14.0.0",
"@imjs/electron-differential-updater": "^5.1.7", "@imjs/electron-differential-updater": "^5.1.7",
"@ragestudio/hermes": "^0.1.1", "@ragestudio/hermes": "^0.1.1",
"antd": "^5.10.2", "antd": "^5.10.2",

View File

@ -1,9 +1,11 @@
import fs from "node:fs" import fs from "node:fs"
import path from "node:path" import path from "node:path"
const ElectronGoogleOAuth2 = require("@getstation/electron-google-oauth2").default
import { ipcMain } from "electron" import { ipcMain } from "electron"
import progressHandler from "progress-stream" import progressHandler from "progress-stream"
import { authenticate } from "@google-cloud/local-auth"
import { google } from "googleapis" import { google } from "googleapis"
import { safeStorage } from "electron" import { safeStorage } from "electron"
@ -11,8 +13,6 @@ import { safeStorage } from "electron"
import sendToRender from "../../utils/sendToRender" import sendToRender from "../../utils/sendToRender"
export default class GoogleDriveAPI { export default class GoogleDriveAPI {
static drive_app_cred_path = path.resolve(process.cwd(), "drive.secret.json")
static async createClientAuthFromCredentials(credentials) { static async createClientAuthFromCredentials(credentials) {
return await google.auth.fromJSON(credentials) return await google.auth.fromJSON(credentials)
} }
@ -34,6 +34,11 @@ export default class GoogleDriveAPI {
static async readCredentials() { static async readCredentials() {
const encryptedValue = global.SettingsStore.get("drive_auth") const encryptedValue = global.SettingsStore.get("drive_auth")
if (!encryptedValue) {
return null
}
const decryptedValue = safeStorage.decryptString(Buffer.from(encryptedValue, "latin1")) const decryptedValue = safeStorage.decryptString(Buffer.from(encryptedValue, "latin1"))
if (!decryptedValue) { if (!decryptedValue) {
@ -44,12 +49,10 @@ export default class GoogleDriveAPI {
} }
static async saveCredentials(credentials) { static async saveCredentials(credentials) {
const app_credentials = JSON.parse(fs.readFileSync(GoogleDriveAPI.drive_app_cred_path)).installed
const payload = { const payload = {
type: "authorized_user", type: "authorized_user",
client_id: app_credentials.client_id, client_id: credentials.client_id,
client_secret: app_credentials.client_secret, client_secret: credentials.client_secret,
access_token: credentials.access_token, access_token: credentials.access_token,
refresh_token: credentials.refresh_token, refresh_token: credentials.refresh_token,
} }
@ -62,19 +65,38 @@ export default class GoogleDriveAPI {
} }
static async authorize() { static async authorize() {
const auth = await authenticate({ console.log("Authorizing Google Drive...")
scopes: ["https://www.googleapis.com/auth/drive"],
keyfilePath: GoogleDriveAPI.drive_app_cred_path,
})
await GoogleDriveAPI.saveCredentials(auth.credentials) const auth = await global._drive_oauth.openAuthWindowAndGetTokens()
await GoogleDriveAPI.saveCredentials({
...auth,
client_id: import.meta.env.MAIN_VITE_DRIVE_ID,
client_secret: import.meta.env.MAIN_VITE_DRIVE_SECRET,
})
await sendToRender("drive:authorized") await sendToRender("drive:authorized")
return auth return auth
} }
static async unauthorize() {
console.log("unauthorize Google Drive...")
global.SettingsStore.delete("drive_auth")
await sendToRender("drive:unauthorized")
}
static async init() { static async init() {
console.log("Initializing Google Drive...")
global._drive_oauth = new ElectronGoogleOAuth2(
import.meta.env.MAIN_VITE_DRIVE_ID,
import.meta.env.MAIN_VITE_DRIVE_SECRET,
["https://www.googleapis.com/auth/drive.readonly"],
)
// register ipc events // register ipc events
for (const [key, fn] of Object.entries(GoogleDriveAPI.ipcHandlers)) { for (const [key, fn] of Object.entries(GoogleDriveAPI.ipcHandlers)) {
ipcMain.handle(key, fn) ipcMain.handle(key, fn)
@ -167,5 +189,6 @@ export default class GoogleDriveAPI {
static ipcHandlers = { static ipcHandlers = {
"drive:listFiles": GoogleDriveAPI.operations.listFiles, "drive:listFiles": GoogleDriveAPI.operations.listFiles,
"drive:authorize": GoogleDriveAPI.authorize, "drive:authorize": GoogleDriveAPI.authorize,
"drive:unauthorize": GoogleDriveAPI.unauthorize,
} }
} }