Files
calendar/packages/app-store/_utils/oauth/parseRefreshTokenResponse.ts
T
Joe Au-YeungandGitHub 7a790aaabe refactor: Moving credential syncing to API (#13963)
* Implement credential sync secret

* Create auth middleware for credential syncing

* Create GET endpoint

* Update swagger comment

* Add verifyCredentialSyncEnabled middleware

* Fix middleware input

* Fix APP_CREDENTIAL_SHARING_ENABLED to return boolean

* Add check for admin API key

* Add GET method

* Middleware require credential sync secret

* Return only the credentialId

* Update default CALCOM_CREDENTIAL_SYNC_HEADER_NAME value

* Add post endpoint

* Return credential object for get endpoint

* Require a userId in the param

* POST add keys to body

* Add PATCH endpoint

* Add DELETE endpoint

* Fix minimumTokenResponseSchema

* Add encrypted key

* Remove prototype

* Add swagger doc comments

* Fix parsing and error handling

* Add create selectedCalendar and destinationCalendar params
2024-03-15 16:03:49 +00:00

47 lines
1.5 KiB
TypeScript

import { z } from "zod";
import { APP_CREDENTIAL_SHARING_ENABLED } from "@calcom/lib/constants";
export const minimumTokenResponseSchema = z
.object({
access_token: z.string(),
})
.passthrough()
.superRefine((tokenObject, ctx) => {
if (!Object.values(tokenObject).some((value) => typeof value === "number")) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message:
"Missing a field that defines a token expiry date. Check the specific app package to see how token expiry is defined.",
});
}
});
export type ParseRefreshTokenResponse<S extends z.ZodTypeAny> =
| z.infer<S>
| z.infer<typeof minimumTokenResponseSchema>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const parseRefreshTokenResponse = (response: any, schema: z.ZodTypeAny) => {
let refreshTokenResponse;
const credentialSyncingEnabled =
APP_CREDENTIAL_SHARING_ENABLED && process.env.CALCOM_CREDENTIAL_SYNC_ENDPOINT;
if (APP_CREDENTIAL_SHARING_ENABLED && process.env.CALCOM_CREDENTIAL_SYNC_ENDPOINT) {
refreshTokenResponse = minimumTokenResponseSchema.safeParse(response);
} else {
refreshTokenResponse = schema.safeParse(response);
}
if (!refreshTokenResponse.success) {
throw new Error("Invalid refreshed tokens were returned");
}
if (!refreshTokenResponse.data.refresh_token && credentialSyncingEnabled) {
refreshTokenResponse.data.refresh_token = "refresh_token";
}
return refreshTokenResponse.data;
};
export default parseRefreshTokenResponse;