feat: OAuth 2.0 support for atoms (#27158)
* fix: useOAuthClient support OAuth 2.0 * fix: cannot read properties of undefined (reading NEXT_PUBLIC_IS_E2E) * fix: allow OAuth 2.0 token to connect gcal or ms calendar * fix: allow OAuth 2.0 token to save gcal or ms calendar credentials * refactor: dont set oauth id header for OAuth 2.0 * fix: calendar events not showing and emails not sent * feat: CalOAuth2Provider * chore: make OAuth 2.0 work in examples app * chore: refresh OAuth 2.0 tokens * docs: running examples app with oauth 2.0 * fix: remove sensitive console.log statements that leak secrets Remove logging of: - OAuth authorization codes (oauth2-user.ts) - Token-bearing exchange responses (oauth2-user.ts) - /me response data containing PII (oauth2-user.ts) - OAuth2 refresh response with tokens (refresh.ts) - Response payload with access tokens (_app.tsx) Addresses Cubic AI review feedback for issues with confidence >= 9/10 Co-Authored-By: unknown <> * docs: update readme * fix: implemente cubic feedback * fix: seed script import * fix: seed script pkce * fix: correct typos and SQLite capitalization in OAuth2 README (#27176) Co-authored-by: cubic-dev-ai[bot] <1082092+cubic-dev-ai[bot]@users.noreply.github.com> * refactor: dont return name in public oauth endpoint * docs: CalOAuthProvider * chore: add NEXT_PUBLIC_IS_E2E constant to test * docs: fix duplicated 'or' in Cal OAuth Provider documentation (#27177) Co-authored-by: cubic-dev-ai[bot] <1082092+cubic-dev-ai[bot]@users.noreply.github.com> * revert: is e2e constant * fix: typecheck * refactor: example app users select * update readme * chore: update oauth atoms readme * refactor: enable booking managed event types with user.username instead of profile.username * fix: EventTypeSettings when viewing round robin * test: add e2e tests for atoms-oauth2 controller Co-Authored-By: [email protected] <[email protected]> * fix: correct error message path in atoms-oauth2 e2e test Co-Authored-By: [email protected] <[email protected]> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> Co-authored-by: cubic-dev-ai[bot] <1082092+cubic-dev-ai[bot]@users.noreply.github.com> Co-authored-by: Rajiv Sahal <[email protected]>
This commit is contained in:
27 files changed
+663
-65
No files matched your search
@@ -35,7 +35,7 @@ import type {
|
||||
} from "./languages";
|
||||
import { EN } from "./languages";
|
||||
|
||||
export type CalProviderProps = {
|
||||
export type BaseCalProviderProps = {
|
||||
children?: ReactNode;
|
||||
clientId: string;
|
||||
accessToken?: string;
|
||||
@@ -48,6 +48,7 @@ export type CalProviderProps = {
|
||||
version?: API_VERSIONS_ENUM;
|
||||
organizationId?: number;
|
||||
isEmbed?: boolean;
|
||||
isOAuth2?: boolean;
|
||||
} & i18nProps;
|
||||
|
||||
export function BaseCalProvider({
|
||||
@@ -64,7 +65,8 @@ export function BaseCalProvider({
|
||||
onTokenRefreshSuccess,
|
||||
onTokenRefreshError,
|
||||
isEmbed,
|
||||
}: CalProviderProps) {
|
||||
isOAuth2
|
||||
}: BaseCalProviderProps) {
|
||||
const [error, setError] = useState<string>("");
|
||||
const [stateOrgId, setOrganizationId] = useState<number>(0);
|
||||
|
||||
@@ -89,6 +91,7 @@ export function BaseCalProvider({
|
||||
|
||||
const { isInit } = useOAuthClient({
|
||||
isEmbed,
|
||||
isOAuth2,
|
||||
clientId,
|
||||
apiUrl: options.apiUrl,
|
||||
refreshUrl: options.refreshUrl,
|
||||
@@ -167,6 +170,7 @@ export function BaseCalProvider({
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
return isInit ? (
|
||||
<AtomsContext.Provider
|
||||
value={{
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
"use client";
|
||||
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
import { useEffect } from "react";
|
||||
|
||||
import { VERSION_2024_06_14 } from "@calcom/platform-constants";
|
||||
|
||||
import http from "../lib/http";
|
||||
import type { BaseCalProviderProps } from "./BaseCalProvider";
|
||||
import { BaseCalProvider } from "./BaseCalProvider";
|
||||
import type { translationKeys } from "./languages";
|
||||
|
||||
const queryClient = new QueryClient();
|
||||
|
||||
type CalOAuthProviderProps = Omit<BaseCalProviderProps, "isOAuth2">;
|
||||
|
||||
/**
|
||||
* Renders a CalProvider component.
|
||||
*
|
||||
* @component
|
||||
* @param {string} props.clientId - The platform oauth client ID.
|
||||
* @param {string} props.accessToken - The access token of your managed user. - Optional
|
||||
* @param {object} props.options - The options object.
|
||||
* @param {string} [options.apiUrl] - The API URL. https://api.cal.com/v2
|
||||
* @param {string} [options.refreshUrl] - The url point to your refresh endpoint. - Optional, required if accessToken is provided.
|
||||
* @param {boolean} [autoUpdateTimezone=true] - Whether to automatically update the timezone. - Optional
|
||||
* @param {function} props.onTimezoneChange - The callback function for timezone change. - Optional
|
||||
* @param {ReactNode} props.children - The child components. - Optional
|
||||
* @returns {JSX.Element} The rendered CalProvider component.
|
||||
*/
|
||||
export function CalOAuthProvider({
|
||||
clientId,
|
||||
accessToken,
|
||||
options,
|
||||
children,
|
||||
autoUpdateTimezone = true,
|
||||
labels,
|
||||
language = "en",
|
||||
onTimezoneChange,
|
||||
version = VERSION_2024_06_14,
|
||||
organizationId,
|
||||
isEmbed = false,
|
||||
}: CalOAuthProviderProps) {
|
||||
useEffect(() => {
|
||||
http.setVersionHeader(version);
|
||||
}, [version]);
|
||||
|
||||
useEffect(() => {
|
||||
if (accessToken) {
|
||||
queryClient.resetQueries();
|
||||
}
|
||||
}, [accessToken]);
|
||||
|
||||
useEffect(() => {
|
||||
http.setPlatformEmbedHeader(isEmbed);
|
||||
}, [isEmbed]);
|
||||
|
||||
return (
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<BaseCalProvider
|
||||
isEmbed={isEmbed}
|
||||
isOAuth2={true}
|
||||
autoUpdateTimezone={autoUpdateTimezone}
|
||||
onTimezoneChange={onTimezoneChange}
|
||||
clientId={clientId}
|
||||
accessToken={accessToken}
|
||||
options={options}
|
||||
version={version}
|
||||
labels={labels as Record<translationKeys, string>}
|
||||
language={language}
|
||||
organizationId={organizationId}>
|
||||
{children}
|
||||
</BaseCalProvider>
|
||||
</QueryClientProvider>
|
||||
);
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
import { isAxiosError } from "axios";
|
||||
import { useEffect } from "react";
|
||||
import http from "../lib/http";
|
||||
import type { CalProviderProps } from "./BaseCalProvider";
|
||||
import type { BaseCalProviderProps } from "./BaseCalProvider";
|
||||
import { BaseCalProvider } from "./BaseCalProvider";
|
||||
import type { translationKeys } from "./languages";
|
||||
|
||||
@@ -38,6 +38,8 @@ const queryClient: QueryClient = new QueryClient({
|
||||
},
|
||||
});
|
||||
|
||||
type CalProviderProps = Omit<BaseCalProviderProps, "isOAuth2">;
|
||||
|
||||
/**
|
||||
* Renders a CalProvider component.
|
||||
*
|
||||
@@ -89,6 +91,7 @@ export function CalProvider({
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<BaseCalProvider
|
||||
isEmbed={isEmbed}
|
||||
isOAuth2={false}
|
||||
autoUpdateTimezone={autoUpdateTimezone}
|
||||
onTimezoneChange={onTimezoneChange}
|
||||
onTokenRefreshStart={onTokenRefreshStart}
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
export { CalProvider } from "./CalProvider";
|
||||
export { CalOAuthProvider } from "./CalOAuthProvider";
|
||||
@@ -7,13 +7,19 @@ import type { ApiResponse } from "@calcom/platform-types";
|
||||
|
||||
import http from "../lib/http";
|
||||
|
||||
interface OAuthClientData {
|
||||
clientId: string;
|
||||
organizationId: number | null;
|
||||
}
|
||||
|
||||
export interface useOAuthClientProps {
|
||||
isEmbed?: boolean;
|
||||
clientId: string;
|
||||
apiUrl?: string;
|
||||
refreshUrl?: string;
|
||||
onError: (error: string) => void;
|
||||
onSuccess: (data: { client: string; organizationId: number; name: string }) => void;
|
||||
onSuccess: (data: OAuthClientData) => void;
|
||||
isOAuth2?: boolean;
|
||||
}
|
||||
export const useOAuthClient = ({
|
||||
isEmbed,
|
||||
@@ -22,9 +28,11 @@ export const useOAuthClient = ({
|
||||
refreshUrl,
|
||||
onError,
|
||||
onSuccess,
|
||||
isOAuth2 = false,
|
||||
}: useOAuthClientProps) => {
|
||||
const prevClientId = usePrevious(clientId);
|
||||
const [isInit, setIsInit] = useState<boolean>(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (apiUrl) {
|
||||
http.setUrl(apiUrl);
|
||||
@@ -38,13 +46,16 @@ export const useOAuthClient = ({
|
||||
useEffect(() => {
|
||||
if (!isEmbed && clientId && http.getUrl() && prevClientId !== clientId) {
|
||||
try {
|
||||
const fetchUrl = isOAuth2 ? `/atoms/auth/oauth2/clients/${clientId}` : `/provider/${clientId}`;
|
||||
http
|
||||
.get<ApiResponse<{ client: string; organizationId: number; name: string }>>(`/provider/${clientId}`)
|
||||
.get<ApiResponse<OAuthClientData>>(fetchUrl)
|
||||
.then((response) => {
|
||||
if (response.data.status === SUCCESS_STATUS) {
|
||||
onSuccess(response.data.data);
|
||||
}
|
||||
http.setClientIdHeader(clientId);
|
||||
if (!isOAuth2) {
|
||||
http.setClientIdHeader(clientId);
|
||||
}
|
||||
})
|
||||
.catch((err: AxiosError) => {
|
||||
if (err.response?.status === 401) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
export { CalProvider } from "./cal-provider";
|
||||
export { CalProvider, CalOAuthProvider } from "./cal-provider";
|
||||
export { GcalConnect } from "./connect/google/GcalConnect";
|
||||
export { AvailabilitySettingsPlatformWrapper as AvailabilitySettings } from "./availability";
|
||||
export type { AvailabilitySettingsPlatformWrapperProps as AvailabilitySettingsProps } from "./availability/wrappers/AvailabilitySettingsPlatformWrapper";
|
||||
|
||||
Reference in new issue
Block a user