1
0
Fork 0
mirror of synced 2024-10-01 17:47:46 +13:00

Store tokens in cache and amend redirect

This commit is contained in:
Adria Navarro 2023-05-31 10:32:11 +02:00
parent c89708cda3
commit 3a6a3eb8a5
4 changed files with 16 additions and 37 deletions

View file

@ -1,10 +1,10 @@
import * as google from "../sso/google" import * as google from "../sso/google"
import { Cookie } from "../../../constants" import { Cookie } from "../../../constants"
import { clearCookie, getCookie } from "../../../utils" import { clearCookie, getCookie } from "../../../utils"
import { doWithDB } from "../../../db"
import * as configs from "../../../configs" import * as configs from "../../../configs"
import { BBContext, Database, SSOProfile } from "@budibase/types" import { BBContext, SSOProfile } from "@budibase/types"
import { ssoSaveUserNoOp } from "../sso/sso" import { ssoSaveUserNoOp } from "../sso/sso"
import { cache, utils } from "../../../"
const GoogleStrategy = require("passport-google-oauth").OAuth2Strategy const GoogleStrategy = require("passport-google-oauth").OAuth2Strategy
type Passport = { type Passport = {
@ -36,8 +36,8 @@ export async function preAuth(
ssoSaveUserNoOp ssoSaveUserNoOp
) )
if (!ctx.query.appId || !ctx.query.datasourceId) { if (!ctx.query.appId) {
ctx.throw(400, "appId and datasourceId query params not present.") ctx.throw(400, "appId query param not present.")
} }
return passport.authenticate(strategy, { return passport.authenticate(strategy, {
@ -69,7 +69,7 @@ export async function postAuth(
( (
accessToken: string, accessToken: string,
refreshToken: string, refreshToken: string,
profile: SSOProfile, _profile: SSOProfile,
done: Function done: Function
) => { ) => {
clearCookie(ctx, Cookie.DatasourceAuth) clearCookie(ctx, Cookie.DatasourceAuth)
@ -79,23 +79,16 @@ export async function postAuth(
{ successRedirect: "/", failureRedirect: "/error" }, { successRedirect: "/", failureRedirect: "/error" },
async (err: any, tokens: string[]) => { async (err: any, tokens: string[]) => {
const baseUrl = `/builder/app/${authStateCookie.appId}/data` const baseUrl = `/builder/app/${authStateCookie.appId}/data`
// update the DB for the datasource with all the user info
await doWithDB(authStateCookie.appId, async (db: Database) => { const id = utils.newid()
let datasource await cache.store(
try { `datasource:creation:${authStateCookie.appId}:google:${id}`,
datasource = await db.get(authStateCookie.datasourceId) {
} catch (err: any) { tokens,
if (err.status === 404) {
ctx.redirect(baseUrl)
}
} }
if (!datasource.config) { )
datasource.config = {}
} ctx.redirect(`${baseUrl}/new?type=google&action=continue&id=${id}`)
datasource.config.auth = { type: "google", ...tokens }
await db.put(datasource)
ctx.redirect(`${baseUrl}/datasource/${authStateCookie.datasourceId}`)
})
} }
)(ctx, next) )(ctx, next)
} }

View file

@ -3,8 +3,6 @@
import { store } from "builderStore" import { store } from "builderStore"
import { auth } from "stores/portal" import { auth } from "stores/portal"
export let preAuthStep
export let datasource
export let disabled export let disabled
export let samePage export let samePage
@ -15,18 +13,8 @@
class:disabled class:disabled
{disabled} {disabled}
on:click={async () => { on:click={async () => {
let ds = datasource
let appId = $store.appId let appId = $store.appId
if (!ds) { const url = `/api/global/auth/${tenantId}/datasource/google?appId=${appId}`
const resp = await preAuthStep()
if (resp.datasource && resp.appId) {
ds = resp.datasource
appId = resp.appId
} else {
ds = resp
}
}
const url = `/api/global/auth/${tenantId}/datasource/google?datasourceId=${ds._id}&appId=${appId}`
if (samePage) { if (samePage) {
window.location = url window.location = url
} else { } else {

View file

@ -3,7 +3,6 @@
import { IntegrationNames } from "constants/backend" import { IntegrationNames } from "constants/backend"
import cloneDeep from "lodash/cloneDeepWith" import cloneDeep from "lodash/cloneDeepWith"
import GoogleButton from "../_components/GoogleButton.svelte" import GoogleButton from "../_components/GoogleButton.svelte"
import { saveDatasource as save } from "builderStore/datasource"
import { organisation } from "stores/portal" import { organisation } from "stores/portal"
import { onMount } from "svelte" import { onMount } from "svelte"
@ -33,7 +32,7 @@
]} integration.</Body ]} integration.</Body
> >
</Layout> </Layout>
<GoogleButton preAuthStep={() => save(datasource, true)} samePage /> <GoogleButton samePage />
{:else if isGoogleConfigured === false} {:else if isGoogleConfigured === false}
<Body size="S" <Body size="S"
>Google authentication is not enabled, please complete Google SSO >Google authentication is not enabled, please complete Google SSO

View file

@ -140,7 +140,6 @@ export const datasourcePreAuth = async (ctx: any, next: any) => {
{ {
provider, provider,
appId: ctx.query.appId, appId: ctx.query.appId,
datasourceId: ctx.query.datasourceId,
}, },
Cookie.DatasourceAuth Cookie.DatasourceAuth
) )