1
0
Fork 0
mirror of synced 2024-07-07 15:25:52 +12:00

Some fixes based on test failure.

This commit is contained in:
mike12345567 2022-12-07 17:33:26 +00:00
parent 85dd6f2880
commit 20984e8072
4 changed files with 20 additions and 9 deletions

View file

@ -1,9 +1,9 @@
import { ssoCallbackUrl } from "./utils" 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" import { ConfigType, GoogleConfig, Database, SSOProfile } from "@budibase/types"
const GoogleStrategy = require("passport-google-oauth").OAuth2Strategy const GoogleStrategy = require("passport-google-oauth").OAuth2Strategy
export function buildVerifyFn(saveUserFn?: Function) { export function buildVerifyFn(saveUserFn?: SaveUserFunction) {
return ( return (
accessToken: string, accessToken: string,
refreshToken: string, refreshToken: string,
@ -39,7 +39,7 @@ export function buildVerifyFn(saveUserFn?: Function) {
export async function strategyFactory( export async function strategyFactory(
config: GoogleConfig["config"], config: GoogleConfig["config"],
callbackUrl: string, callbackUrl: string,
saveUserFn?: Function saveUserFn?: SaveUserFunction
) { ) {
try { try {
const { clientID, clientSecret } = config const { clientID, clientSecret } = config

View file

@ -1,5 +1,5 @@
import fetch from "node-fetch" import fetch from "node-fetch"
import { authenticateThirdParty } from "./third-party-common" import { authenticateThirdParty, SaveUserFunction } from "./third-party-common"
import { ssoCallbackUrl } from "./utils" import { ssoCallbackUrl } from "./utils"
import { import {
Config, Config,
@ -17,7 +17,7 @@ type JwtClaims = {
email: string email: string
} }
export function buildVerifyFn(saveUserFn?: Function) { export function buildVerifyFn(saveUserFn?: SaveUserFunction) {
/** /**
* @param {*} issuer The identity provider base URL * @param {*} issuer The identity provider base URL
* @param {*} sub The user ID * @param {*} sub The user ID
@ -106,7 +106,7 @@ function validEmail(value: string) {
*/ */
export async function strategyFactory( export async function strategyFactory(
config: OIDCConfiguration, config: OIDCConfiguration,
saveUserFn?: Function saveUserFn?: SaveUserFunction
) { ) {
try { try {
const verify = buildVerifyFn(saveUserFn) const verify = buildVerifyFn(saveUserFn)

View file

@ -9,6 +9,17 @@ import fetch from "node-fetch"
import { ThirdPartyUser } from "@budibase/types" import { ThirdPartyUser } from "@budibase/types"
const jwt = require("jsonwebtoken") const jwt = require("jsonwebtoken")
type SaveUserOpts = {
requirePassword?: boolean
hashPassword?: boolean
currentUserId?: string
}
export type SaveUserFunction = (
user: ThirdPartyUser,
opts: SaveUserOpts
) => Promise<any>
/** /**
* Common authentication logic for third parties. e.g. OAuth, OIDC. * Common authentication logic for third parties. e.g. OAuth, OIDC.
*/ */
@ -16,7 +27,7 @@ export async function authenticateThirdParty(
thirdPartyUser: ThirdPartyUser, thirdPartyUser: ThirdPartyUser,
requireLocalAccount: boolean = true, requireLocalAccount: boolean = true,
done: Function, done: Function,
saveUserFn?: Function saveUserFn?: SaveUserFunction
) { ) {
if (!saveUserFn) { if (!saveUserFn) {
throw new Error("Save user function must be provided") throw new Error("Save user function must be provided")
@ -81,7 +92,7 @@ export async function authenticateThirdParty(
// create or sync the user // create or sync the user
try { try {
await saveUserFn(dbUser, false, false) await saveUserFn(dbUser, { hashPassword: false, requirePassword: false })
} catch (err: any) { } catch (err: any) {
return authError(done, err) return authError(done, err)
} }

View file

@ -103,7 +103,7 @@ export const getUser = async (userId: string) => {
return user return user
} }
interface SaveUserOpts { export interface SaveUserOpts {
hashPassword?: boolean hashPassword?: boolean
requirePassword?: boolean requirePassword?: boolean
currentUserId?: string currentUserId?: string