check keys of client secrets

This commit is contained in:
arc-alex
2023-11-26 17:11:55 +01:00
parent 7b34c8f3b1
commit 6880f459b6
2 changed files with 33 additions and 23 deletions
@@ -455,8 +455,8 @@ export async function uploadGoogleSheetClientFile(req, res) {
try { try {
const client = JSON.parse(fs.readFileSync(req.file.path as string, 'utf-8')); const client = JSON.parse(fs.readFileSync(req.file.path as string, 'utf-8'));
const auth = await Sheet.saveClientSecrets(client); await Sheet.saveClientSecrets(client);
res.status(200).send(auth); res.status(200).send('OK');
} catch (error) { } catch (error) {
res.status(500).send({ message: error.toString() }); res.status(500).send({ message: error.toString() });
} }
+31 -21
View File
@@ -38,22 +38,25 @@ class sheet {
if (!sheet.client) { if (!sheet.client) {
return ret; return ret;
} }
ret.auth = await this.refreshToken(); try {
if (ret.auth) { ret.auth = await this.refreshToken();
const settings = DataProvider.getGoogleSheet(); if (ret.auth) {
const x = await this.exist(settings.id, settings.worksheet); const settings = DataProvider.getGoogleSheet();
if (x === true) { const x = await this.exist(settings.id, settings.worksheet);
ret.id = true; if (x === true) {
this.sheetId = settings.id; ret.id = true;
} else if (x !== false) { this.sheetId = settings.id;
ret.id = true; } else if (x !== false) {
ret.worksheet = true; ret.id = true;
this.sheetId = settings.id; ret.worksheet = true;
this.worksheetId = x.worksheetId; this.sheetId = settings.id;
this.range = x.range; this.worksheetId = x.worksheetId;
this.range = x.range;
}
} }
} catch (err) {
logger.error(LogOrigin.Server, `Google Sheet: Faild to refresh token ${err}`);
} }
logger.info(LogOrigin.Server, `Sheet State: ${JSON.stringify(ret)}`);
return ret; return ret;
} }
@@ -199,17 +202,24 @@ class sheet {
/** /**
* saves Object to appdata path as client_secret.json * saves Object to appdata path as client_secret.json
* @param {} secrets * @param {Object} secrets
*/ */
public async saveClientSecrets(secrets): Promise<GoogleSheetState> { public async saveClientSecrets(secrets: Object) {
ensureDirectory(this.sheetsFolder);
logger.info(LogOrigin.Server, 'Sheets: got new client_secret');
//TODO: test that this is actualy a client file?
//invalidate previus auths
sheet.client = null; sheet.client = null;
sheet.authUrl = null; sheet.authUrl = null;
ensureDirectory(this.sheetsFolder);
if (
'client_id' in secrets ||
!('project_id' in secrets) ||
!('auth_uri' in secrets) ||
!('token_uri' in secrets) ||
!('auth_provider_x509_cert_url' in secrets) ||
!('client_secret' in secrets) ||
!('redirect_uris' in secrets)
) {
throw new Error('Sheet slient secret is missing some keys');
}
await writeFile(this.client_secret, JSON.stringify(secrets), 'utf-8'); await writeFile(this.client_secret, JSON.stringify(secrets), 'utf-8');
return;
} }
/** /**