Skip to content

Migrate MicrosoftAPIRefreshAccessTokenService to @azure/msal-node#16954

Merged
charlesBochet merged 4 commits intomainfrom
fix-ms-oauth2-token-refres
Jan 8, 2026
Merged

Migrate MicrosoftAPIRefreshAccessTokenService to @azure/msal-node#16954
charlesBochet merged 4 commits intomainfrom
fix-ms-oauth2-token-refres

Conversation

@neo773
Copy link
Copy Markdown
Member

@neo773 neo773 commented Jan 6, 2026

No description provided.

Copy link
Copy Markdown
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.

@socket-security
Copy link
Copy Markdown

socket-security bot commented Jan 6, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 4 files

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="packages/twenty-server/src/modules/connected-account/refresh-tokens-manager/drivers/microsoft/services/microsoft-api-refresh-tokens.service.ts">

<violation number="1" location="packages/twenty-server/src/modules/connected-account/refresh-tokens-manager/drivers/microsoft/services/microsoft-api-refresh-tokens.service.ts:5">
P1: Using `import type` for `TwentyConfigService` may break NestJS dependency injection. Type-only imports are erased at compile time, which can prevent TypeScript from emitting the design-time type metadata that NestJS needs for constructor injection. Use a regular import to match the pattern in the Google service.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

import { ConfidentialClientApplication } from '@azure/msal-node';

import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';
import type { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Jan 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: Using import type for TwentyConfigService may break NestJS dependency injection. Type-only imports are erased at compile time, which can prevent TypeScript from emitting the design-time type metadata that NestJS needs for constructor injection. Use a regular import to match the pattern in the Google service.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/twenty-server/src/modules/connected-account/refresh-tokens-manager/drivers/microsoft/services/microsoft-api-refresh-tokens.service.ts, line 5:

<comment>Using `import type` for `TwentyConfigService` may break NestJS dependency injection. Type-only imports are erased at compile time, which can prevent TypeScript from emitting the design-time type metadata that NestJS needs for constructor injection. Use a regular import to match the pattern in the Google service.</comment>

<file context>
@@ -1,67 +1,53 @@
+import { ConfidentialClientApplication } from &#39;@azure/msal-node&#39;;
 
-import { TwentyConfigService } from &#39;src/engine/core-modules/twenty-config/twenty-config.service&#39;;
+import type { TwentyConfigService } from &#39;src/engine/core-modules/twenty-config/twenty-config.service&#39;;
 import {
   ConnectedAccountRefreshAccessTokenException,
</file context>
Suggested change
import type { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';
import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';

✅ Addressed in d4ecb69

* Extracts the refresh token from the MSAL token cache.
* @see https://github.com/duolingo/metasearch/blob/3d782bba8c0068461acb442d89e7d555df5d0025/src/oauth.microsoft.ts#L42-L44
*/
private extractMicrosoftRefreshTokenFromCache(): string {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is not ideal, it should do for now as the serialized token cache is an complex object, and it's not backwards compatible with our current approach which only stores the raw refresh_token, this would involve creating migration strategies and the other alternative, our old approach (raw HTTP request) wasn't that good either

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Jan 6, 2026

🚀 Preview Environment Ready!

Your preview environment is available at: http://bore.pub:37734

This environment will automatically shut down when the PR is closed or after 5 hours.

const response = await msalClient.acquireTokenByRefreshToken({
refreshToken,
scopes: ['https://graph.microsoft.com/.default'],
forceCache: true,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we force cache?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

During my testing it didn't store the token in TokenCache if this was off

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@neo773
This does not explain the why here:
Per documentation: Force MSAL to cache a refresh token flow response when there is no account in the cache. Used for migration scenarios.

Why do we need it?

},
);
const responseData = response.data as MicrosoftRefreshTokenResponse;
const response = await msalClient.acquireTokenByRefreshToken({
Copy link
Copy Markdown
Member

@FelixMalfait FelixMalfait Jan 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is risky maybe give a bit more details in your PR description, thanks!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did add the info here
#16954 (comment)

TLDR;
Microsoft wants you to offload the token lifecycle to their new SDK with a cache storage layer for persisting data.
This data is a proprietary JSON blob and not compatible with our existing schema as it involves heavy changes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, I don't understand the rational behind this PR, what are we migrating? why? could you add more details?

Copy link
Copy Markdown
Member

@FelixMalfait FelixMalfait left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but please add details before merging to make sure everything is well considered thanks!

@charlesBochet
Copy link
Copy Markdown
Member

Codewise it works but I don't get this change, please add more details why we need this

@charlesBochet charlesBochet added this pull request to the merge queue Jan 8, 2026
Merged via the queue into main with commit a5eae50 Jan 8, 2026
76 checks passed
@charlesBochet charlesBochet deleted the fix-ms-oauth2-token-refres branch January 8, 2026 17:31
@twenty-eng-sync
Copy link
Copy Markdown

Hey @neo773! After you've done the QA of your Pull Request, you can mark it as done here. Thank you!

1 similar comment
@twenty-eng-sync
Copy link
Copy Markdown

Hey @neo773! After you've done the QA of your Pull Request, you can mark it as done here. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants