Skip to content

Commit

Permalink
fix(sheets): πŸ”’οΈ Check token id before updating creds
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Jul 2, 2022
1 parent 994ae61 commit 9cddc75
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
14 changes: 10 additions & 4 deletions apps/builder/libs/google-sheets.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Credentials as CredentialsFromDb } from 'db'
import { OAuth2Client, Credentials } from 'google-auth-library'
import { GoogleSheetsCredentialsData } from 'models'
import { decrypt, encrypt } from 'utils'
import { decrypt, encrypt, isDefined } from 'utils'
import prisma from './prisma'

export const oauth2Client = new OAuth2Client(
Expand Down Expand Up @@ -33,9 +33,15 @@ export const getAuthenticatedGoogleClient = async (
const updateTokens =
(credentialsId: string, existingCredentials: GoogleSheetsCredentialsData) =>
async (credentials: Credentials) => {
const newCredentials = {
refresh_token: existingCredentials.refresh_token,
...credentials,
if (
isDefined(existingCredentials.id_token) &&
credentials.id_token !== existingCredentials.id_token
)
return
const newCredentials: GoogleSheetsCredentialsData = {
...existingCredentials,
expiry_date: credentials.expiry_date,
access_token: credentials.access_token,
}
const { encryptedData, iv } = encrypt(newCredentials)
await prisma.credentials.update({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return res
.status(400)
.send({ message: "User didn't accepted required scopes" })
// console.log(tokens)
const { encryptedData, iv } = encrypt(tokens)
const credentials = {
name: email,
Expand Down
14 changes: 10 additions & 4 deletions apps/viewer/libs/google-sheets.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Credentials as CredentialsFromDb } from 'db'
import { OAuth2Client, Credentials } from 'google-auth-library'
import { GoogleSheetsCredentialsData } from 'models'
import { decrypt, encrypt } from 'utils'
import { decrypt, encrypt, isDefined } from 'utils'
import prisma from './prisma'

export const getAuthenticatedGoogleClient = async (
Expand Down Expand Up @@ -29,9 +29,15 @@ export const getAuthenticatedGoogleClient = async (
const updateTokens =
(credentialsId: string, existingCredentials: GoogleSheetsCredentialsData) =>
async (credentials: Credentials) => {
const newCredentials = {
refresh_token: existingCredentials.refresh_token,
...credentials,
if (
isDefined(existingCredentials.id_token) &&
credentials.id_token !== existingCredentials.id_token
)
return
const newCredentials: GoogleSheetsCredentialsData = {
...existingCredentials,
expiry_date: credentials.expiry_date,
access_token: credentials.access_token,
}
const { encryptedData, iv } = encrypt(newCredentials)
await prisma.credentials.update({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { badRequest, initMiddleware, methodNotAllowed } from 'utils'
import { badRequest, initMiddleware, methodNotAllowed, hasValue } from 'utils'
import { GoogleSpreadsheet } from 'google-spreadsheet'
import { getAuthenticatedGoogleClient } from 'libs/google-sheets'
import { Cell } from 'models'
Expand All @@ -15,7 +15,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const spreadsheetId = req.query.spreadsheetId as string
const sheetId = req.query.sheetId as string
const credentialsId = req.query.credentialsId as string | undefined
if (!credentialsId) return badRequest(res)
if (!hasValue(credentialsId)) return badRequest(res)
const referenceCell = {
column: req.query['referenceCell[column]'],
value: req.query['referenceCell[value]'],
Expand Down Expand Up @@ -63,7 +63,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
credentialsId?: string
values: { [key: string]: string }
}
if (!credentialsId) return badRequest(res)
if (!hasValue(credentialsId)) return badRequest(res)
const doc = new GoogleSpreadsheet(spreadsheetId)
const auth = await getAuthenticatedGoogleClient(credentialsId)
if (!auth)
Expand All @@ -81,16 +81,16 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
}
}
if (req.method === 'PATCH') {
const spreadsheetId = req.query.spreadsheetId.toString()
const sheetId = req.query.sheetId.toString()
const spreadsheetId = req.query.spreadsheetId as string
const sheetId = req.query.sheetId as string
const { credentialsId, values, referenceCell } = (
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
) as {
credentialsId?: string
referenceCell: Cell
values: { [key: string]: string }
}
if (!credentialsId) return badRequest(res)
if (!hasValue(credentialsId)) return badRequest(res)
const doc = new GoogleSpreadsheet(spreadsheetId)
const auth = await getAuthenticatedGoogleClient(credentialsId)
if (!auth)
Expand Down
9 changes: 9 additions & 0 deletions packages/utils/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,12 @@ export const env = (key = ''): string | undefined => {
? undefined
: (process.env['NEXT_PUBLIC_' + key] as string)
}

export const hasValue = (
value: string | undefined | null
): value is NonNullable<string> =>
value !== undefined &&
value !== null &&
value !== '' &&
value !== 'undefined' &&
value !== 'null'

4 comments on commit 9cddc75

@vercel
Copy link

@vercel vercel bot commented on 9cddc75 Jul 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 9cddc75 Jul 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

builder-v2-typebot-io.vercel.app
app.typebot.io
builder-v2-git-main-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 9cddc75 Jul 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

viewer-v2-alpha – ./apps/viewer

apr.nigerias.io
ar.nigerias.io
sat.cr8.ai
aso.nigerias.io
bot.aws.bj
an.nigerias.io
apo.nigerias.io
am.nigerias.io
vhpage.cr8.ai
bt.id8rs.com
chat.sureb4.com
eventhub.com.au
games.klujo.com
sakuranembro.it
bot.upfunnel.art
bot.piccinato.co
clo.closeer.work
faqs.nigerias.io
stan.vselise.com
feedback.ofx.one
voicehelp.cr8.ai
app.chatforms.net
bot.agfunnel.tech
ov1.wpwakanda.com
bot.maitempah.com
gentleman-shop.fr
sell.applepie.pro
ov2.wpwakanda.com
ov3.wpwakanda.com
admin.applepie.pro
this-is-a-test.com
goalsettingbot.com
zap.techadviser.in
carsalesenquiry.com
forms.webisharp.com
bot.eventhub.com.au
order.maitempah.com
bot.cotemeuplano.com
typebot.stillio.com
bot.ansuraniphone.my
chat.hayurihijab.com
get.freebotoffer.xyz
abutton.wpwakanda.com
bbutton.wpwakanda.com
cdd.searchcube.com.sg
bot.meuesocial.com.br
bot.incusservices.com
apply.ansuraniphone.my
chat.missarkansas.org
sbutton.wpwakanda.com
bbutton.wpwwakanda.com
felipewelington.com.br
c23111azqw.nigerias.io
form.searchcube.com.sg
bot.upgradesolutions.eu
gcase.barrettamario.it
kodawariab736.skeep.it
info.clickasuransi.com
83242573.actualizar.xyz
view.onlinebotdemo.xyz
mainmenu.diddancing.com
subfooter.wpwakanda.com
91181264.your-access.one
form.sergiolimajr.com.br
type.opaulovieira.com.br
hunterbot.saleshunter.ai
aibot.angrybranding.co.uk
bot.cabinrentalagency.com
boyfriend-breakup.riku.ai
type.dericsoncalari.com.br
presente.empresarias.com.mx
designguide.techyscouts.com
piazzatorre.barrettamario.it
onboarding.libertydreamcare.ie
agendamento.sergiolimajr.com.br
type.talitasouzamarques.com.br
bookings.littlepartymonkeys.com
preagendamento.sergiolimajr.com.br
studiotecnicoimmobiliaremerelli.it
bot.comercializadoraomicron.com
personal-trainer.barrettamario.it
viewer-v2-alpha-typebot-io.vercel.app
viewer-v2-alpha-git-main-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 9cddc75 Jul 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./apps/docs

docs-git-main-typebot-io.vercel.app
docs-typebot-io.vercel.app
docs.typebot.io

Please sign in to comment.