From 0b7057dd1df02ee1d6edeaf87296ef24e6e8b69d Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 7 Dec 2022 17:33:26 +0000 Subject: [PATCH] Some fixes based on test failure. --- .../src/middleware/passport/google.ts | 6 +++--- .../backend-core/src/middleware/passport/oidc.ts | 6 +++--- .../src/middleware/passport/third-party-common.ts | 15 +++++++++++++-- packages/worker/src/sdk/users/users.ts | 2 +- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/packages/backend-core/src/middleware/passport/google.ts b/packages/backend-core/src/middleware/passport/google.ts index deba849233..dd3dc8b86d 100644 --- a/packages/backend-core/src/middleware/passport/google.ts +++ b/packages/backend-core/src/middleware/passport/google.ts @@ -1,9 +1,9 @@ import { ssoCallbackUrl } from "./utils" -import { authenticateThirdParty } from "./third-party-common" +import { authenticateThirdParty, SaveUserFunction } from "./third-party-common" import { ConfigType, GoogleConfig, Database, SSOProfile } from "@budibase/types" const GoogleStrategy = require("passport-google-oauth").OAuth2Strategy -export function buildVerifyFn(saveUserFn?: Function) { +export function buildVerifyFn(saveUserFn?: SaveUserFunction) { return ( accessToken: string, refreshToken: string, @@ -39,7 +39,7 @@ export function buildVerifyFn(saveUserFn?: Function) { export async function strategyFactory( config: GoogleConfig["config"], callbackUrl: string, - saveUserFn?: Function + saveUserFn?: SaveUserFunction ) { try { const { clientID, clientSecret } = config diff --git a/packages/backend-core/src/middleware/passport/oidc.ts b/packages/backend-core/src/middleware/passport/oidc.ts index 27c3c647b7..40bc22ec0c 100644 --- a/packages/backend-core/src/middleware/passport/oidc.ts +++ b/packages/backend-core/src/middleware/passport/oidc.ts @@ -1,5 +1,5 @@ import fetch from "node-fetch" -import { authenticateThirdParty } from "./third-party-common" +import { authenticateThirdParty, SaveUserFunction } from "./third-party-common" import { ssoCallbackUrl } from "./utils" import { Config, @@ -17,7 +17,7 @@ type JwtClaims = { email: string } -export function buildVerifyFn(saveUserFn?: Function) { +export function buildVerifyFn(saveUserFn?: SaveUserFunction) { /** * @param {*} issuer The identity provider base URL * @param {*} sub The user ID @@ -106,7 +106,7 @@ function validEmail(value: string) { */ export async function strategyFactory( config: OIDCConfiguration, - saveUserFn?: Function + saveUserFn?: SaveUserFunction ) { try { const verify = buildVerifyFn(saveUserFn) diff --git a/packages/backend-core/src/middleware/passport/third-party-common.ts b/packages/backend-core/src/middleware/passport/third-party-common.ts index 8798ce5298..451cdf6cc6 100644 --- a/packages/backend-core/src/middleware/passport/third-party-common.ts +++ b/packages/backend-core/src/middleware/passport/third-party-common.ts @@ -9,6 +9,17 @@ import fetch from "node-fetch" import { ThirdPartyUser } from "@budibase/types" const jwt = require("jsonwebtoken") +type SaveUserOpts = { + requirePassword?: boolean + hashPassword?: boolean + currentUserId?: string +} + +export type SaveUserFunction = ( + user: ThirdPartyUser, + opts: SaveUserOpts +) => Promise + /** * Common authentication logic for third parties. e.g. OAuth, OIDC. */ @@ -16,7 +27,7 @@ export async function authenticateThirdParty( thirdPartyUser: ThirdPartyUser, requireLocalAccount: boolean = true, done: Function, - saveUserFn?: Function + saveUserFn?: SaveUserFunction ) { if (!saveUserFn) { throw new Error("Save user function must be provided") @@ -81,7 +92,7 @@ export async function authenticateThirdParty( // create or sync the user try { - await saveUserFn(dbUser, false, false) + await saveUserFn(dbUser, { hashPassword: false, requirePassword: false }) } catch (err: any) { return authError(done, err) } diff --git a/packages/worker/src/sdk/users/users.ts b/packages/worker/src/sdk/users/users.ts index 132aef4e69..9577046e7f 100644 --- a/packages/worker/src/sdk/users/users.ts +++ b/packages/worker/src/sdk/users/users.ts @@ -103,7 +103,7 @@ export const getUser = async (userId: string) => { return user } -interface SaveUserOpts { +export interface SaveUserOpts { hashPassword?: boolean requirePassword?: boolean currentUserId?: string