Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

microsfot sync failed #10381

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';

import { EnvironmentModule } from 'src/engine/core-modules/environment/environment.module';
import { MicrosoftAPIRefreshAccessTokenService } from 'src/modules/connected-account/refresh-access-token-manager/drivers/microsoft/services/microsoft-api-refresh-access-token.service';

@Module({
imports: [EnvironmentModule],
providers: [MicrosoftAPIRefreshAccessTokenService],
exports: [MicrosoftAPIRefreshAccessTokenService],
})
export class MicrosoftAPIRefreshAccessTokenModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Injectable } from '@nestjs/common';

import axios from 'axios';

import { EnvironmentService } from 'src/engine/core-modules/environment/environment.service';

@Injectable()
export class MicrosoftAPIRefreshAccessTokenService {
constructor(private readonly environmentService: EnvironmentService) {}

async refreshAccessToken(refreshToken: string): Promise<string> {
const response = await axios.post(
'https://login.microsoftonline.com/common/oauth2/v2.0/token',
new URLSearchParams({
client_id: this.environmentService.get('AUTH_MICROSOFT_CLIENT_ID'),
client_secret: this.environmentService.get(
'AUTH_MICROSOFT_CLIENT_SECRET',
),
refresh_token: refreshToken,
grant_type: 'refresh_token',
}),
);

return response.data.access_token;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { Module } from '@nestjs/common';

import { GoogleAPIRefreshAccessTokenModule } from 'src/modules/connected-account/refresh-access-token-manager/drivers/google/google-api-refresh-access-token.module';
import { MicrosoftAPIRefreshAccessTokenModule } from 'src/modules/connected-account/refresh-access-token-manager/drivers/microsoft/microsoft-api-refresh-access-token.module';
import { RefreshAccessTokenService } from 'src/modules/connected-account/refresh-access-token-manager/services/refresh-access-token.service';

@Module({
imports: [GoogleAPIRefreshAccessTokenModule],
imports: [
GoogleAPIRefreshAccessTokenModule,
MicrosoftAPIRefreshAccessTokenModule,
],
providers: [RefreshAccessTokenService],
exports: [RefreshAccessTokenService],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common';

import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
import { GoogleAPIRefreshAccessTokenService } from 'src/modules/connected-account/refresh-access-token-manager/drivers/google/services/google-api-refresh-access-token.service';
import { MicrosoftAPIRefreshAccessTokenService } from 'src/modules/connected-account/refresh-access-token-manager/drivers/microsoft/services/microsoft-api-refresh-access-token.service';
import {
RefreshAccessTokenException,
RefreshAccessTokenExceptionCode,
Expand All @@ -12,6 +13,7 @@ import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/s
export class RefreshAccessTokenService {
constructor(
private readonly googleAPIRefreshAccessTokenService: GoogleAPIRefreshAccessTokenService,
private readonly microsoftAPIRefreshAccessTokenService: MicrosoftAPIRefreshAccessTokenService,
private readonly twentyORMManager: TwentyORMManager,
) {}

Expand All @@ -31,7 +33,6 @@ export class RefreshAccessTokenService {

switch (connectedAccount.provider) {
case 'microsoft':
return '';
case 'google': {
try {
accessToken = await this.refreshAccessToken(
Expand Down Expand Up @@ -73,6 +74,10 @@ export class RefreshAccessTokenService {
return this.googleAPIRefreshAccessTokenService.refreshAccessToken(
refreshToken,
);
case 'microsoft':
return this.microsoftAPIRefreshAccessTokenService.refreshAccessToken(
refreshToken,
);
default:
throw new RefreshAccessTokenException(
`Provider ${connectedAccount.provider} is not supported`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import { Process } from 'src/engine/core-modules/message-queue/decorators/proces
import { Processor } from 'src/engine/core-modules/message-queue/decorators/processor.decorator';
import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
import { RefreshAccessTokenExceptionCode } from 'src/modules/connected-account/refresh-access-token-manager/exceptions/refresh-access-token.exception';
import { RefreshAccessTokenService } from 'src/modules/connected-account/refresh-access-token-manager/services/refresh-access-token.service';
import { isThrottled } from 'src/modules/connected-account/utils/is-throttled';
import {
MessageChannelSyncStage,
MessageChannelWorkspaceEntity,
} from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity';
import { MessageImportDriverExceptionCode } from 'src/modules/messaging/message-import-manager/drivers/exceptions/message-import-driver.exception';
import { MessageImportExceptionCode } from 'src/modules/messaging/message-import-manager/exceptions/message-import.exception';
import { MessagingFullMessageListFetchService } from 'src/modules/messaging/message-import-manager/services/messaging-full-message-list-fetch.service';
import { MessagingPartialMessageListFetchService } from 'src/modules/messaging/message-import-manager/services/messaging-partial-message-list-fetch.service';
import { MessagingTelemetryService } from 'src/modules/messaging/monitoring/services/messaging-telemetry.service';
Expand All @@ -30,6 +34,7 @@ export class MessagingMessageListFetchJob {
private readonly messagingPartialMessageListFetchService: MessagingPartialMessageListFetchService,
private readonly messagingTelemetryService: MessagingTelemetryService,
private readonly twentyORMManager: TwentyORMManager,
private readonly refreshAccessTokenService: RefreshAccessTokenService,
) {}

@Process(MessagingMessageListFetchJob.name)
Expand Down Expand Up @@ -73,6 +78,37 @@ export class MessagingMessageListFetchJob {
return;
}

try {
messageChannel.connectedAccount.accessToken =
await this.refreshAccessTokenService.refreshAndSaveAccessToken(
messageChannel.connectedAccount,
workspaceId,
);
} catch (error) {
switch (error.code) {
case RefreshAccessTokenExceptionCode.REFRESH_ACCESS_TOKEN_FAILED:
case RefreshAccessTokenExceptionCode.REFRESH_TOKEN_NOT_FOUND:
await this.messagingTelemetryService.track({
eventName: `refresh_token.error.insufficient_permissions`,
workspaceId,
connectedAccountId: messageChannel.connectedAccountId,
messageChannelId: messageChannel.id,
message: `${error.code}: ${error.reason ?? ''}`,
});
throw {
code: MessageImportDriverExceptionCode.INSUFFICIENT_PERMISSIONS,
message: error.message,
};
case RefreshAccessTokenExceptionCode.PROVIDER_NOT_SUPPORTED:
throw {
code: MessageImportExceptionCode.PROVIDER_NOT_SUPPORTED,
message: error.message,
};
default:
throw error;
}
}

switch (messageChannel.syncStage) {
case MessageChannelSyncStage.PARTIAL_MESSAGE_LIST_FETCH_PENDING:
this.logger.log(
Expand Down
Loading