* move delegation credential repository to features * mv credential repository to features * update imports * mv * fix * fix * fix * fix * fix * update imports * update imports * update eslint rule * fix * fix * mv getConnectedDestinationCalendars * fix import errors * mv getCalendarsEvents * remove getUsersCredentials * wip * revert eslint rule change for now * fix type checks * fix * format * cleanup * fix * fix * fix * fix * fix tests * migrate getUserAvailability * migrate * fix tests * fix type checks * fix * fix * migrate crmManager * update imports * migrate raqbUtils to appstore * migrate getLuckyUser to features * migrate findTeamMembersMatchingAttributeLogic to appstore * update imports * fix * fix * fix test * fix unit tests * fix * fix * add eslint config
55 lines
2.5 KiB
TypeScript
55 lines
2.5 KiB
TypeScript
import { isInMemoryDelegationCredential } from "@calcom/lib/delegationCredential";
|
|
import logger from "@calcom/lib/logger";
|
|
import { CredentialRepository } from "@calcom/lib/server/repository/credential";
|
|
import type { CredentialForCalendarService } from "@calcom/types/Credential";
|
|
|
|
import { getTokenObjectFromCredential } from "./getTokenObjectFromCredential";
|
|
|
|
const log = logger.getSubLogger({
|
|
prefix: ["getCurrentTokenObject"],
|
|
});
|
|
|
|
function buildDummyTokenObjectForDelegationUserCredential() {
|
|
return {
|
|
access_token: "TOKEN_PLACEHOLDER_FOR_DELEGATION_CREDENTIAL",
|
|
};
|
|
}
|
|
|
|
/**
|
|
* OAuthManager helper to get the current token object. It decides to use the Credential that is passed or retrieve from db.
|
|
*/
|
|
export async function getCurrentTokenObject(
|
|
credential: Pick<CredentialForCalendarService, "key" | "id" | "delegatedToId" | "userId">
|
|
) {
|
|
let inDbCredential;
|
|
// CalendarService currently receives an in-memory delegation credential which is incapable to persist access token generated for a user.
|
|
// So, in this case we read Delegation User Credential from db separately if available. updateTokenObject will create new Delegation User Credential in db if not available.
|
|
if (credential.delegatedToId && isInMemoryDelegationCredential({ credentialId: credential.id })) {
|
|
if (!credential.userId) {
|
|
log.error("DelegationCredential: No user id found for delegation credential");
|
|
} else {
|
|
log.debug("Getting current token object for delegation credential");
|
|
const delegationUserCredentialInDb =
|
|
await CredentialRepository.findUniqueByUserIdAndDelegationCredentialId({
|
|
userId: credential.userId,
|
|
delegationCredentialId: credential.delegatedToId,
|
|
});
|
|
inDbCredential = delegationUserCredentialInDb;
|
|
if (!inDbCredential) {
|
|
log.error("getCurrentTokenObject: No delegation user credential found in db");
|
|
// We return a dummy token object. OAuthManager requires a token object that must have access_token.
|
|
// OAuthManager will help fetching new token object and then that would be stored in DB.
|
|
return buildDummyTokenObjectForDelegationUserCredential();
|
|
}
|
|
}
|
|
} else {
|
|
log.debug("Getting current token object for non delegation credential");
|
|
inDbCredential = credential;
|
|
}
|
|
if (!inDbCredential) {
|
|
throw new Error("getCurrentTokenObject: No delegation user credential found in db");
|
|
}
|
|
const currentTokenObject = getTokenObjectFromCredential(inDbCredential);
|
|
return currentTokenObject;
|
|
}
|