1
0
Fork 0
mirror of synced 2024-09-30 00:57:16 +13:00

e2e secure microsoft auth

This commit is contained in:
Martin McKeaveney 2023-09-23 00:10:12 +01:00
parent 7db7159b4c
commit b979b29313
9 changed files with 55 additions and 8 deletions

View file

@ -190,6 +190,10 @@ export const createPlatformUserView = async () => {
if (doc.tenantId) { if (doc.tenantId) {
emit(doc._id.toLowerCase(), doc._id) emit(doc._id.toLowerCase(), doc._id)
} }
if (doc.ssoId) {
emit(doc.ssoId, doc._id)
}
}` }`
await createPlatformView(viewJs, ViewName.PLATFORM_USERS_LOWERCASE) await createPlatformView(viewJs, ViewName.PLATFORM_USERS_LOWERCASE)
} }

View file

@ -4,7 +4,7 @@ import env from "../environment"
import { import {
PlatformUser, PlatformUser,
PlatformUserByEmail, PlatformUserByEmail,
PlatformUserById, PlatformUserById, PlatformUserBySsoId,
User, User,
} from "@budibase/types" } from "@budibase/types"
@ -45,6 +45,20 @@ function newUserEmailDoc(
} }
} }
function newUserSsoIdDoc(
ssoId: string,
email: string,
userId: string,
tenantId: string,
): PlatformUserBySsoId {
return {
_id: ssoId,
userId,
email,
tenantId,
}
}
/** /**
* Add a new user id or email doc if it doesn't exist. * Add a new user id or email doc if it doesn't exist.
*/ */
@ -64,11 +78,17 @@ async function addUserDoc(emailOrId: string, newDocFn: () => PlatformUser) {
} }
} }
export async function addUser(tenantId: string, userId: string, email: string) { export async function addUser(tenantId: string, userId: string, email: string, ssoId?: string) {
await Promise.all([ const promises = [
addUserDoc(userId, () => newUserIdDoc(userId, tenantId)), addUserDoc(userId, () => newUserIdDoc(userId, tenantId)),
addUserDoc(email, () => newUserEmailDoc(userId, email, tenantId)), addUserDoc(email, () => newUserEmailDoc(userId, email, tenantId)),
]) ]
if (ssoId) {
promises.push(addUserDoc(ssoId, () => newUserSsoIdDoc(ssoId, email, userId, tenantId)))
}
await Promise.all(promises)
} }
// DELETE // DELETE

View file

@ -278,7 +278,7 @@ export class UserDB {
builtUser._rev = response.rev builtUser._rev = response.rev
await eventHelpers.handleSaveEvents(builtUser, dbUser) await eventHelpers.handleSaveEvents(builtUser, dbUser)
await platform.users.addUser(tenantId, builtUser._id!, builtUser.email) await platform.users.addUser(tenantId, builtUser._id!, builtUser.email, builtUser.ssoId)
await cache.user.invalidateUser(response.id) await cache.user.invalidateUser(response.id)
await Promise.all(groupPromises) await Promise.all(groupPromises)

View file

@ -1,4 +1,4 @@
import { Account } from "../../documents" import { Account, AccountSSOProvider } from "../../documents"
import { Hosting } from "../../sdk" import { Hosting } from "../../sdk"
export interface CreateAccountRequest { export interface CreateAccountRequest {
@ -11,6 +11,8 @@ export interface CreateAccountRequest {
tenantName?: string tenantName?: string
name?: string name?: string
password: string password: string
provider?: AccountSSOProvider
thirdPartyProfile: object
} }
export interface SearchAccountsRequest { export interface SearchAccountsRequest {

View file

@ -20,6 +20,11 @@ export interface CreatePassswordAccount extends CreateAccount {
password: string password: string
} }
export interface CreateVerifiableSSOAccount extends CreateAccount {
provider?: AccountSSOProvider
thirdPartyProfile?: any
}
export const isCreatePasswordAccount = ( export const isCreatePasswordAccount = (
account: CreateAccount account: CreateAccount
): account is CreatePassswordAccount => account.authType === AuthType.PASSWORD ): account is CreatePassswordAccount => account.authType === AuthType.PASSWORD
@ -50,6 +55,8 @@ export interface Account extends CreateAccount {
licenseKeyActivatedAt?: number licenseKeyActivatedAt?: number
licenseRequestedAt?: number licenseRequestedAt?: number
licenseOverrides?: LicenseOverrides licenseOverrides?: LicenseOverrides
provider?: AccountSSOProvider
providerType?: AccountSSOProviderType
quotaUsage?: QuotaUsage quotaUsage?: QuotaUsage
offlineLicenseToken?: string offlineLicenseToken?: string
} }

View file

@ -55,6 +55,7 @@ export interface User extends Document {
userGroups?: string[] userGroups?: string[]
onboardedAt?: string onboardedAt?: string
scimInfo?: { isSync: true } & Record<string, any> scimInfo?: { isSync: true } & Record<string, any>
ssoId?: string
} }
export enum UserStatus { export enum UserStatus {

View file

@ -15,4 +15,13 @@ export interface PlatformUserById extends Document {
tenantId: string tenantId: string
} }
export type PlatformUser = PlatformUserByEmail | PlatformUserById /**
* doc id is a unique SSO provider ID for the user
*/
export interface PlatformUserBySsoId extends Document {
tenantId: string
userId: string
email: string
}
export type PlatformUser = PlatformUserByEmail | PlatformUserById | PlatformUserBySsoId

View file

@ -95,7 +95,8 @@ const parseBooleanParam = (param: any) => {
export const adminUser = async ( export const adminUser = async (
ctx: Ctx<CreateAdminUserRequest, CreateAdminUserResponse> ctx: Ctx<CreateAdminUserRequest, CreateAdminUserResponse>
) => { ) => {
const { email, password, tenantId } = ctx.request.body // @ts-ignore
const { email, password, tenantId, ssoId } = ctx.request.body
if (await platform.tenants.exists(tenantId)) { if (await platform.tenants.exists(tenantId)) {
ctx.throw(403, "Organisation already exists.") ctx.throw(403, "Organisation already exists.")
@ -136,6 +137,8 @@ export const adminUser = async (
global: true, global: true,
}, },
tenantId, tenantId,
// @ts-ignore
ssoId,
} }
try { try {
// always bust checklist beforehand, if an error occurs but can proceed, don't get // always bust checklist beforehand, if an error occurs but can proceed, don't get

View file

@ -14,6 +14,7 @@ function buildAdminInitValidation() {
email: Joi.string().required(), email: Joi.string().required(),
password: Joi.string(), password: Joi.string(),
tenantId: Joi.string().required(), tenantId: Joi.string().required(),
ssoId: Joi.string(),
}) })
.required() .required()
.unknown(false) .unknown(false)