feat: Domain-wide delegation for Google Calendar and Google Meet (#16622)
* Add endpoints for testing the flow * Add MVP * new route * Fixes * Fixes * Remove enable toggle support from domainWideDelegation * Fixes * Revert "Remove enable toggle support from domainWideDelegation" This reverts commit c29e729206cd1fa063f9c9ce0cf148ef1577d60b. * Revert yarn.lock * More fixes * Fix new workspace platform add * refactor: improvements * refactor: bug fixes and improvements * fix: type errors * fix: conflicts * chore: update test * fix: logic * chore: improvements * fix: toglle * fix: bugs * fix: type err * chore: check number type * fix: after conflicts * chore: fix type err * fix: type errors and tests * fix: tets * test * chore: remove unused * fix: google meet url on booking page and secondary calendar * fix: add property * fix: type err * fix: re assingment bug * fix: use getAllCredentials * chore: fix import * fix: installed count * fix: pass event type * fix: import * fix: [Stacked PR] Review fixes (#17958) * Review fixes * fix: destination calendar bug --------- Co-authored-by: Udit Takkar <[email protected]> * refactor: use repository * chore: remove duplicate * fix: More review fixes for domain wide delegation (#17969) * Reuse buildCredentialPayloadForCalendar * fixes --------- Co-authored-by: Udit Takkar <[email protected]> * fix ts error * Fix getSchedule not using dwd credentials (#17995) * fix: Remove direct DWD table access from google-calendar and remove delegatedToId column from Credential (#18015) * Remove fn rename to reduce number of files changed * chore: check feature globally * test: add unit tests * chore: move function * test: add booking test * wip * Remove domain-wide-delegation team feature flag * Make sure duplicate calendars are not shown due to DWD. Show DWD when there is conflict * Fix tests and their ts errors * Fix more tests * Move findUsersForAvailabilityCheck to separate file as it has AppStore dependency causing problem with Routing Forms. Build crashes * fix: Multiple calendar connections from Google not showing up in apps/installed * DestinationCalendar must have either credentialId or domainWideDeelgationId. Also handle the case when DWD is disabled and there was a non-dwd credential that could be used * Disable deletetion of DWD as it is destructive and prefer disabling instead * Show DWD credential calendars at the top * Fixed tests * Calendar Cache DWD support * Self-Review: Verify email required and add more selectedCalendar tests * Self Review: shorten names * Self ReviewL Dead code removal * Revert "Calendar Cache DWD support" This reverts commit 009f236470fa21eba5986117d4f6e4d4c5e38c34. * Some misc fixes * fixes * Performance improvememt in slots loading and booking * More cases for handling dwd credentials * simplify the logic. Ensure that EventManager and the modules below it in the flow have CredentialForCalendarService available * Fix tests * Fix installed/conferencing not showing gogole meet * Shorten name * Fix ApI v2 tests * Add some more tests * add getSchedule tests * Improve tests * Make Google Meet default when DWD for google is enabled * Enable feature flagging for DWD * chore: bump libraries * Encrypt serviceaccount private key * Fix ts errors * bump platform libraries * org add dwd * org add dwd * bump platform libraries * fix selected calendars * fix remove selected dwd calendar * remove oauthclient id aliad in authedCalendar * remove oauthclient id aliad in authedCalendar * refactor: OrganizationsDwdController * chore: export toggleDwdEnabled from platform-libraries * feat: v2 update (enable / disable) dwd * refactor: SelectedCalendarsController check if user belongs to dwd org & for not dwd use previous logic * wip: DestinationCalendar send domainWideDelegationCredentialId from frontend to api * try fix set destination calendar api v2 * fixup! try fix set destination calendar api v2 * setting google meet as default location working for dwd * bump platform libraries version * allow office 365 workspace slug * allow office 365 workspace slug * chore: v2 create dwd MS and Google service keys input * fix: CreateDwdInput serviceAccountKey * fix: CredentialForCalendarService type * fix: check workspace slug * feat: update serviceAccessKey of DWD * testing * fixup! testing * fix: getAuthUtl bug * fix: unit tests * fix: type err and unit test * fix: e2e test * fix: credentials bug and failing unit tests * Update CalendarService.ts * chore: refactor office365 calendar service and add testDomainWideDelegationSetup * fix: office365Calendar use correct clientId/Secret for DWD * fix: office365Calendar dwd no need refresh token * test: add unit test for outlook dwd and setup * feat: added dwd support for office365 calendar * feat: added dwd support for office365 video * test: finish test for outlook dwd * fix: create dialog bug * chore: remove console logs * fix: refreshToken bug * bump version libraries * refactor: fetch dwd credentials only in findQualifiedHosts * fixup! refactor: fetch dwd credentials only in findQualifiedHosts * fix: type err * fix: type err * fix: getUserDisplayName * fix: unit test * chore: bump platform lib --------- Co-authored-by: Syed Ali Shahbaz <[email protected]> Co-authored-by: Udit Takkar <[email protected]> Co-authored-by: Udit Takkar <[email protected]> Co-authored-by: Morgan Vernay <[email protected]> Co-authored-by: supalarry <[email protected]> Co-authored-by: Somay Chauhan <[email protected]> Co-authored-by: Morgan <[email protected]>
This commit is contained in:
173 files changed
+12277
-1137
No files matched your search
+20
-8
@@ -109,10 +109,6 @@ const getTabs = (orgBranding: OrganizationBranding | null) => {
|
||||
name: "admin_api",
|
||||
href: "https://cal.com/docs/enterprise-features/api/api-reference/bookings#admin-access",
|
||||
},
|
||||
// {
|
||||
// name: "domain_wide_delegation",
|
||||
// href: "/settings/organizations/domain-wide-delegation",
|
||||
// },
|
||||
],
|
||||
},
|
||||
{
|
||||
@@ -166,9 +162,16 @@ const getTabs = (orgBranding: OrganizationBranding | null) => {
|
||||
// The following keys are assigned to admin only
|
||||
const adminRequiredKeys = ["admin"];
|
||||
const organizationRequiredKeys = ["organization"];
|
||||
const organizationAdminKeys = ["privacy", "billing", "OAuth Clients", "SSO", "directory_sync"];
|
||||
const organizationAdminKeys = [
|
||||
"privacy",
|
||||
"billing",
|
||||
"OAuth Clients",
|
||||
"SSO",
|
||||
"directory_sync",
|
||||
"domain_wide_delegation",
|
||||
];
|
||||
|
||||
const useTabs = () => {
|
||||
const useTabs = ({ isDwdEnabled }: { isDwdEnabled: boolean }) => {
|
||||
const session = useSession();
|
||||
const { data: user } = trpc.viewer.me.useQuery({ includePasswordAdded: true });
|
||||
const orgBranding = useOrgBranding();
|
||||
@@ -199,6 +202,14 @@ const useTabs = () => {
|
||||
});
|
||||
}
|
||||
|
||||
// Add domain-wide-delegation menu item only if feature flag is enabled
|
||||
if (isDwdEnabled) {
|
||||
newArray.push({
|
||||
name: "domain_wide_delegation",
|
||||
href: "/settings/organizations/domain-wide-delegation",
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
...tab,
|
||||
children: newArray,
|
||||
@@ -232,7 +243,7 @@ const useTabs = () => {
|
||||
if (isAdmin) return true;
|
||||
return !adminRequiredKeys.includes(tab.name);
|
||||
});
|
||||
}, [isAdmin, orgBranding, isOrgAdminOrOwner, user]);
|
||||
}, [isAdmin, orgBranding, isOrgAdminOrOwner, user, isDwdEnabled]);
|
||||
|
||||
return processTabsMemod;
|
||||
};
|
||||
@@ -418,7 +429,6 @@ const SettingsSidebarContainer = ({
|
||||
}: SettingsSidebarContainerProps) => {
|
||||
const searchParams = useCompatSearchParams();
|
||||
const { t } = useLocale();
|
||||
const tabsWithPermissions = useTabs();
|
||||
const [otherTeamMenuState, setOtherTeamMenuState] = useState<
|
||||
{
|
||||
teamId: number | undefined;
|
||||
@@ -430,6 +440,8 @@ const SettingsSidebarContainer = ({
|
||||
enabled: !!session.data?.user?.org && !currentOrgProp,
|
||||
});
|
||||
|
||||
const tabsWithPermissions = useTabs({ isDwdEnabled: !!_currentOrg?.features?.domainWideDelegation });
|
||||
|
||||
const { data: _otherTeams } = trpc.viewer.organizations.listOtherTeams.useQuery(undefined, {
|
||||
enabled: !!session.data?.user?.org && !otherTeamsProp,
|
||||
});
|
||||
|
||||
+112
@@ -0,0 +1,112 @@
|
||||
## Version 1.0
|
||||
### Release Plan
|
||||
1. Read the document(domain-wide-delegation.md) and acknowledge it.
|
||||
3. Deploy:
|
||||
1. Follow "Setting up Domain-Wide Delegation for Google Calendar API" in domain-wide-delegation.md to create Service Account and create a workspace.
|
||||
2. Merge PR(without Calendar Cache Support) and then deploy.
|
||||
4. Enable for i.cal.com:
|
||||
1. Disable Calendar Cache for i.cal.com
|
||||
2. Enable DWD for i.cal.com first and then test there
|
||||
3. Wait for 1-2 days and keep monitoring the errors in Sentry and Axiom.
|
||||
5. Merge & Deploy the Calendar Cache support(with DWD) PR
|
||||
1. Enable Calendar Cache back for i.cal.com
|
||||
2. Observe the errors in Sentry and Axiom.
|
||||
6. Enable for a big customer:
|
||||
1. Wait for a week and keep monitoring the errors in Sentry and Axiom.
|
||||
7. Use delegatedCredentialsFirst instead of delegatedCredentialsLast in EventManager.ts
|
||||
- Observe errors in Sentry and Axiom.
|
||||
|
||||
## Manual Testing
|
||||
- V2/V1 APIs - There could be problem if DWD credentials aren't supported there. Because as organization enables DWD and new users won't need to connect their calendars in the old way. e.g. getBusyCalendarTimes seem to be used there in V2.
|
||||
- [x] Request reshedule - cancellation of meeting in calendar verification
|
||||
- [x] Isolation of DWD credentials for different organizations
|
||||
- [x] Using same domain name for different organization isn't allowed. It is restricted during creation of DWD.
|
||||
- [x] Location Change of a booking to Google Meet(from Cal Video)
|
||||
- [x] Google Meet/Google Calendar
|
||||
- [x] Shows up installed on apps/google-meet and apps/instaled/conferencing
|
||||
- [x] Can't remove the app
|
||||
- [x] Seated Event
|
||||
- [x] Seat cancellation removes calendar invite
|
||||
- [x] Onboarding
|
||||
- [x] When DWD is not enabled, the flow works.
|
||||
- [x] When DWD is enabled, Google Calendar is pre-installed and Destination Calendar and Selected Calendar are configurable. On next step, Google Meet is pre-installed and shown at the top and could be set as default.
|
||||
- [x] User Level Selected Calendar and Destination Calendars are honored - verified through booking the user.
|
||||
- [ ] Event Type Selected Calendar and Destination Calendar
|
||||
- [ ] Dynamic Group Booking
|
||||
- [x] Owner must have verified email to enable DWD
|
||||
- [x] RR Team Event
|
||||
- [x] Booking
|
||||
- Unavailable slot isn't available for booking. Unavailable user isn't used.
|
||||
- [x] Reroute
|
||||
- [x] Reassign
|
||||
- [x] Troubleshooter
|
||||
- [x] Shows busy times from Calendar
|
||||
- [ ] Calendar Cache[TO BE TESTED LATER]
|
||||
- [ ] Event Type Selected Calendar caching test
|
||||
- [ ] User Selected Calendar caching test
|
||||
|
||||
### Important
|
||||
- **Performance**
|
||||
- There could be 100s of users in an organization with already connected calendars. Enabling DWD adds a duplicate credential(in-memory) for each of them.
|
||||
- Because a credential isn't aware of which externalIds it has access to(without connecting with Google Calendar API itself), we can't identify which credential is for which SelectedCalendar and thus we can't de-duplicate them. This has an impact on fetching the availability and thus for a team event with x participants, we could have 2x requests to Google Calendar API. Because for big value of x, user might be using routing already with team members and thus the actual value of x might be much lower. So, we could be fine. Also, we would have Calendar Cache enabled there already to reduce the requests.
|
||||
- Bugs
|
||||
- [x] For RR event, if I am able to see the slots for a blocked day in calendar(by temporarily unselecting the calendar), and then we enable the calendar back, the blocked day can be booked.
|
||||
- [x] Not using Goole Meet with Default conferencing app.
|
||||
- [x] Duplicate Calendar Events in Google Calendar when choosing non-primary calendar as destination. No idea why this is happening.
|
||||
- The issue was in getAttendees in Google CalendarService not using externalCalendarId.
|
||||
- [x] Restrict toggling based on email verified
|
||||
- [x] Duplicate Calendar connections in 'apps/installed' if a user already had connected calendar and DWD is enabled.
|
||||
- [x] Calendar Cache has credentialId column which isn't applicable for DWD(Solution: Added userId there)
|
||||
- [x] Troubleshooter
|
||||
- [x] Shows busy times from Claendar
|
||||
- [x] If a user has connected a calendar, and then DWD is enabled.
|
||||
- Tested various scenarios for it
|
||||
- [x] Inviting a new user.
|
||||
- Verified that Google Calendar is shown pre-installed.
|
||||
- How about Google Meet(which depends on Google Calendar) - Correctly shows up as installed.
|
||||
- TODO:
|
||||
- [ ] Add experimental/Beta flag in DWD
|
||||
- [ ] Consideration of multiple domains email in a single team event. We might need to consider all the member's domains in the team event when fetching availabilituy.
|
||||
- [x] Performance
|
||||
- [x] Available Slots and booking flow shouldn't slow down. Right now the querying logic is not optimized. We query per team member, we should do one query - This is fixed and took quite a refactor but it is cleaner code now.
|
||||
- [ ] Tag all DWD related logs and errors with "DWD:"
|
||||
- [x] Troubleshooter
|
||||
- [x] Google CalendarService unit tests to verify that if DWD credential is provided it uses impersonation to access API otherwise it uses regular user credential API.
|
||||
- [x] setDestinationCalendar.handler.ts tests to verify that when DWD is enabled it still correctly sets the destination calendar.
|
||||
- [x] getConnectedDestinationCalendars tests.
|
||||
- [x] Creating DWD shouldn't immediately enable it. Enabling has separate check to confirm if it is actually configured in google workspace
|
||||
- [x] Added check to avoid adding same domain for a workspace platform in another organization if it is already enabled in some other organization
|
||||
- [x] Don't show dwd in menu for non-org-admin users - It errors with something_went_wrong right now
|
||||
- [x] Don't allow disabled platform to be selected in the UI for creation.
|
||||
- We have disabled coming the disabled platform to be coming into the list that effectively disables edit of existing dwd and creation of new dwd for that platform.
|
||||
- [x] Where should we show the user the client ID to enable domain wide delegation?
|
||||
- [x] It must be shown to the organization owner/admin only
|
||||
- [x] There could be multiple checkboxes per domain to enable domain wide delegation for a domain
|
||||
- [x] Which domain to allow
|
||||
- Any domain can be added by a user
|
||||
- [x] Support multiple domains in DomainWideDelegation schema for an organization
|
||||
- [x] Use the domain as well to identify if the domain wide delegation is enabled
|
||||
- [x] Before enabling Domain-wide delegation, there should be a check to ensure that the clientID has been added to the Workspace Platform
|
||||
- [x] We should allow setting default conferencing app during onboarding
|
||||
|
||||
### Follow-up release
|
||||
- [ ] Confirmation for DwD toggling off
|
||||
- [ ] Confirmation for DwD deletion - Deletion isn't there at the moment.
|
||||
- [ ] Make Google Meet "show up" as default conferencing app when DWD for Google is enabled.
|
||||
- [ ] Profile pic from Google with DWD might not be working - Fix it.
|
||||
|
||||
### Security
|
||||
- [x] We don't let any one user see the added service account key from UI.
|
||||
- [ ] We intend to implement Workload Identity Federation in the future.
|
||||
|
||||
### Documentation
|
||||
- After enabling domain-wide delegation, the credential is shown pre-installed and the connection can't be removed(or the app can't be uninstalled by user)
|
||||
- Steps
|
||||
- App admin will first create a Workspace Platform and then organization owner/admin can enable domain-wide delegation for a domain
|
||||
- As soon as domain-wide delegation is created, it would start taking preference over the personal credentials of the organization members and it would be used for that.
|
||||
|
||||
Version-2.0
|
||||
- Workload Identity Federation to ensure that the service account key is never stored in DB.
|
||||
|
||||
|
||||
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
## Setting up Domain-Wide Delegation for Google Calendar API
|
||||
|
||||
Step 1: Create a Google Cloud Project
|
||||
|
||||
Before you can create a service account, you'll need to set up a Google Cloud project.
|
||||
|
||||
1. Create a Google Cloud Project:
|
||||
- Go to the Google Cloud Console
|
||||
- Select Create Project
|
||||
- Give your project a name and select your billing account (if applicable)
|
||||
- Click Create
|
||||
2. Enable the Google Calendar API:
|
||||
1. Go to the Google Cloud Console
|
||||
2. Select API & Services → Library
|
||||
3. Search for "Google Calendar API"
|
||||
4. Click Enable
|
||||
|
||||
Step 2: Create a Service Account
|
||||
|
||||
A service account is needed to act on behalf of users
|
||||
|
||||
1. Navigate to the Service Accounts page:
|
||||
- In the Google Cloud Console, go to IAM & Admin → Service Accounts
|
||||
2. Create a New Service Account:
|
||||
- Click on Create Service Account
|
||||
- Give your service account a name and description
|
||||
- Click Create and Continue
|
||||
|
||||
Step 3: To Be taken by Cal.com instance admin:
|
||||
- Create a Workspace Platform with slug="google". Slug has to be exactly this. This is how we know we need to use Google Calendar and Google Meet.
|
||||
|
||||
Last Step (To Be Taken By Cal.com organization Owner/Admin): Assign Specific API Permissions via OAuth Scopes:
|
||||
- Create DWD with workspace platform "google"
|
||||
- User must be a member of the Google Workspace to be able to enable DWD as there is a validation if the user's calendar can be accessed through the service account
|
||||
- Get the Client ID from there
|
||||
- Go to your Google Admin Console (admin-google-com)
|
||||
- Navigate to Security → Access and Data Controls -> API controls -> Manage Domain-Wide Delegation
|
||||
- Here, you'll authorize the Client ID(Unique ID) to access the Google Calendar API
|
||||
- Add the necessary API scopes for Google Calendar(Full access to Google Calendar)
|
||||
https://www.googleapis.com/auth/calendar
|
||||
|
||||
|
||||
## Onboarding Improvement
|
||||
- Just adding a member to the organization would do the following:
|
||||
- Member to receive events in their calendar, even if they don't login to their account and complete the onboarding process.
|
||||
- The booking location would be Google Meet, even if the user hasn't set it as default(Though Cal Video would show up as default, but we still use Google Meet in this case. We will fix it later.)
|
||||
- It would still not use their calendar for conflict checking, but user can complete the onboarding(just select one calendar there for conflict checking)
|
||||
- Onboarding process: Google Calendar is pre-installed for any new member of the organization(assuming the user has an email of the DWD domain) and Destination Calendar and Selected Calendar are configurable. On next step, Google Meet is pre-installed and shown at the top and could be set as default.
|
||||
|
||||
## Restrictions after enabling DWD
|
||||
- Enabling DWD for a particular workspace in Cal.com(only google supported at the moment) disables the user from disconnecting that credential.
|
||||
|
||||
## Who can create DWD and enable it?
|
||||
- Only the owner/admin of the organization can create DWD
|
||||
- Only the owner/admin of the organization can enable the created DWD. Following additional requirements are there:
|
||||
- The client ID must be added to the Google Admin Console
|
||||
- The user's email must be a member of the Google Workspace
|
||||
- The user's email must be verified.
|
||||
|
||||
## Disabling DWD
|
||||
- Disabling DWD maintains the user's preferences in terms of SelectedCalendars and DestinationCalendar.
|
||||
|
||||
## Developer Notes
|
||||
### Terminology
|
||||
- DWD Credential: A DWD service account key along with user's email becomes the DWD Credential which is an alternative to regular Credential in DB.
|
||||
- DWD: Domain Wide Delegation
|
||||
- non-dwd credential: Regular credentials that are stored in Credentials table
|
||||
|
||||
### How DWD works
|
||||
- We use the Cal.com user's email to impersonate that user using DWD Credential(which is just a service account key at the moment)
|
||||
- That gives us read/write permission to get availability of the user and create new events in their calendar.
|
||||
|
||||
### What is a DWD Credential?
|
||||
- A DWD service account key along with user's email becomes the DWD Credential which is an alternative to regular Credential in DB.
|
||||
- DWD doesn't completely replace the regular credentials. DWD Credential gives access to the cal.com user's email in Google Calendar. So, if the user needs to connect to some other email's calendar, we need to use the regular credentials.
|
||||
|
||||
### Important Points
|
||||
- No Credential table entry is created when enabling DWD. The workspace platform's related apps will be considered as "installed" for the users with email matching dwd domain. An in-memory credential like object is created for this purpose. It allows avoiding creation of thousands of records for all the members of the organization when dwd is enabled.
|
||||
- DWD Credential is applicable to Users only.
|
||||
- For team, we don't use dwd credential as you can impersonate a user and not team through Dwd credential. Currently supported apps(Google Calendar and Google Meet) don't support team installation, so we could simply allow enabling DWD without any issues.
|
||||
- Disabling a workspace platform stops it from being used for any new organizations and also disables any DWD using the workspace platform from being edited.
|
||||
- It still all existing DWDs to keep on working
|
||||
- Adding any number of DWDs for a particular workspace always gives the same Client ID as DWD uses the workspace's default Service Account.
|
||||
- We should disable DWD and not delete it when we want to stop using it temporarily. Deleting DWD also removes all the seletedCalendar entries connected to it.
|
||||
|
||||
### How apps/installed loads the credentials
|
||||
1. Identify the logged in user's email
|
||||
2. Identify the domainWideDelegations for that email's domain
|
||||
3. Build in-memory credentials for the domainWideDelegations and use them along with the actual credentials(that user might have connected) of the user
|
||||
4. We don't show the non-dwd connected calendar(if there is a corresponding dwd connected calendar). Though we use the non-dwd credentials to identify the selected calendars, for the dwd connected calendar.
|
||||
|
||||
### Impact on existing users booking flow
|
||||
- There should be no impact on availability on enabling DWD because we keep on using the existing credentials along with new DWD credential.
|
||||
- When booking the event, we sort the credentials with DWD credentials last, so there should be no impact on creating calendar events.
|
||||
- NOTE: We will followup with sorting the credentials with DWD credentials first in a followup PR(They are preferred because they don't expire)
|
||||
|
||||
### Impact on APIs - [ To Verify ]
|
||||
- We don't support DWD through APIs yet. So, all existing APIs would continue to work with non-dwd credentials only.
|
||||
|
||||
### Notes when testing locally
|
||||
- You need to enable the feature through feature flag.
|
||||
- You could use Acme org and login as owner1-acme@example.com
|
||||
- Make sure to change the email of the user above to your workspace owner's email(other member's email might also work). This is necessary otherwise you won't be able to enable DWD for the organization.
|
||||
- Note: After changing the email, you would have to logout and login again as required by NextAuth
|
||||
@@ -17,6 +17,7 @@ const selectedCalendarSelectSchema = z.object({
|
||||
integration: z.string(),
|
||||
externalId: z.string(),
|
||||
credentialId: z.coerce.number(),
|
||||
domainWideDelegationCredentialId: z.string().nullish().default(null),
|
||||
eventTypeId: z.coerce.number().nullish(),
|
||||
});
|
||||
|
||||
@@ -62,14 +63,17 @@ async function getHandler() {
|
||||
|
||||
async function postHandler(req: NextRequest) {
|
||||
const user = await authMiddleware();
|
||||
|
||||
const body = await req.json();
|
||||
const { integration, externalId, credentialId, eventTypeId } = selectedCalendarSelectSchema.parse(body);
|
||||
const { integration, externalId, credentialId, eventTypeId, domainWideDelegationCredentialId } =
|
||||
selectedCalendarSelectSchema.parse(body);
|
||||
|
||||
await SelectedCalendarRepository.upsert({
|
||||
userId: user.id,
|
||||
integration,
|
||||
externalId,
|
||||
credentialId,
|
||||
domainWideDelegationCredentialId,
|
||||
eventTypeId: eventTypeId ?? null,
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user